.
This commit is contained in:
461
out.js
461
out.js
@@ -2,7 +2,7 @@
|
||||
// - copy method
|
||||
// - remove range method
|
||||
|
||||
class DLLNode {
|
||||
DLLNode = class DLLNode {
|
||||
constructor(val, skip = this, prev = this, next = this) {
|
||||
this.val = val
|
||||
this.skip = skip
|
||||
@@ -29,7 +29,7 @@ class DLLNode {
|
||||
}
|
||||
}
|
||||
|
||||
class DLL {
|
||||
DLL = class DLL {
|
||||
constructor(a = []) {
|
||||
this.h = new DLLNode()
|
||||
this.length = 0;
|
||||
@@ -170,7 +170,7 @@ class DLL {
|
||||
toString() { return this.toArray().toString() }
|
||||
}
|
||||
|
||||
class Point {
|
||||
Pt = Point = class Point {
|
||||
constructor(x, y) {
|
||||
this.x = x
|
||||
this.y = y
|
||||
@@ -232,14 +232,12 @@ class Point {
|
||||
toString() { return this.x + "," + this.y }
|
||||
}
|
||||
|
||||
Pt = Point
|
||||
|
||||
function P(...args) {
|
||||
P = function P(...args) {
|
||||
return new Point(...args)
|
||||
}
|
||||
|
||||
|
||||
class Grid {
|
||||
Grid = class Grid {
|
||||
constructor(w, h, fill = 0) {
|
||||
this.width = w
|
||||
this.height = h
|
||||
@@ -428,7 +426,7 @@ class Grid {
|
||||
print(sep = "\t", ...pts) { console.log(this.toString(sep, pts)) }
|
||||
}
|
||||
|
||||
class BinHeap {
|
||||
BinHeap = class BinHeap {
|
||||
constructor(cond = (p, c) => p < c) {
|
||||
this.cond = cond
|
||||
this.data = []
|
||||
@@ -488,14 +486,14 @@ class BinHeap {
|
||||
}
|
||||
}
|
||||
|
||||
class Cxn {
|
||||
Cxn = class Cxn {
|
||||
constructor(dest, weight = 1) {
|
||||
this.dest = dest
|
||||
this.weight = weight
|
||||
}
|
||||
}
|
||||
|
||||
class SearchData {
|
||||
SearchData = class SearchData {
|
||||
constructor(id, dist = Infinity, last = undefined, custom = {}) {
|
||||
this.id = id
|
||||
this.dist = dist
|
||||
@@ -527,7 +525,7 @@ class SearchData {
|
||||
}
|
||||
}
|
||||
|
||||
class Node {
|
||||
Node = class Node {
|
||||
static GLOBAL_ID = 0
|
||||
|
||||
constructor(val) {
|
||||
@@ -620,117 +618,7 @@ class Node {
|
||||
}
|
||||
}
|
||||
|
||||
let warned = false
|
||||
|
||||
Object.defineProperties(Object.prototype, {
|
||||
copyDeep: {
|
||||
value: function() {
|
||||
return JSON.parse(JSON.stringify(this))
|
||||
}
|
||||
},
|
||||
num: {
|
||||
value: function() {
|
||||
if (this.map) {
|
||||
return this.map((e) => +e)
|
||||
} else if (this.mapMut) {
|
||||
return this.mapMut((e) => +e)
|
||||
} else {
|
||||
console.error("Object.prototype.num: No suitable map method found")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperties(Array.prototype, {
|
||||
dll: {
|
||||
value: function() {
|
||||
return new DLL(this)
|
||||
}
|
||||
},
|
||||
copy: {
|
||||
value: function() {
|
||||
return this.slice()
|
||||
}
|
||||
},
|
||||
copyDeep: {
|
||||
value: function() {
|
||||
return JSON.parse(JSON.stringify(this))
|
||||
}
|
||||
},
|
||||
sum: {
|
||||
value: function(val) {
|
||||
return this.reduce((a, b) => a + b, val)
|
||||
}
|
||||
},
|
||||
flatDeep: {
|
||||
value: function() {
|
||||
return this.flat(Infinity)
|
||||
}
|
||||
},
|
||||
transpose: {
|
||||
value: function() {
|
||||
return Array(this[0].length).fill().map((_, i) => this.map(e => e[i]))
|
||||
}
|
||||
},
|
||||
findLast: {
|
||||
value: function(func) {
|
||||
for (let i = this.length - 1; i >= 0; i--) {
|
||||
if (func(this[i], i, this)) {
|
||||
return this[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
findLastIndex: {
|
||||
value: function(func) {
|
||||
for (let i = this.length - 1; i >= 0; i--) {
|
||||
if (func(this[i], i, this)) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
},
|
||||
interleave: {
|
||||
value: function(that) {
|
||||
return [this, that].transpose().flat()
|
||||
}
|
||||
},
|
||||
rotate: {
|
||||
value: function(n) {
|
||||
let k = (this.length + n) % this.length
|
||||
return [...this.slice(k), ...this.slice(0, k)]
|
||||
}
|
||||
},
|
||||
sub: {
|
||||
value: function(that) {
|
||||
return this.filter(e => !that.includes(e))
|
||||
}
|
||||
},
|
||||
int: {
|
||||
value: function(that) {
|
||||
return this.filter(e => that.includes(e))
|
||||
}
|
||||
},
|
||||
uniq: {
|
||||
value: function() {
|
||||
return this.filter((e, i) => this.indexOf(e) == i)
|
||||
}
|
||||
},
|
||||
pushUniq: {
|
||||
value: function(...vals) {
|
||||
if (!warned) {
|
||||
console.warn("You should probably use a Set")
|
||||
warned = true
|
||||
}
|
||||
|
||||
return this.push(...vals.uniq().sub(this))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
class PointArray extends Array {
|
||||
PtArray = PointArray = class PointArray extends Array {
|
||||
static convert(arr) {
|
||||
if (!(arr instanceof PointArray)) {
|
||||
arr.__proto__ = PointArray.prototype
|
||||
@@ -740,57 +628,287 @@ class PointArray extends Array {
|
||||
}
|
||||
}
|
||||
|
||||
PtArray = PointArray
|
||||
let warned = false
|
||||
|
||||
Object.defineProperty(Array.prototype, "pt", {
|
||||
get: function() {
|
||||
return PointArray.convert(this)
|
||||
}
|
||||
})
|
||||
load = function load() {
|
||||
Object.defineProperties(Object.prototype, {
|
||||
copyDeep: {
|
||||
value: function() {
|
||||
return JSON.parse(JSON.stringify(this))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
num: {
|
||||
value: function() {
|
||||
if (this.map) {
|
||||
return this.map((e) => +e)
|
||||
} else if (this.mapMut) {
|
||||
return this.mapMut((e) => +e)
|
||||
} else {
|
||||
console.error("Object.prototype.num: No suitable map method found")
|
||||
}
|
||||
},
|
||||
configurable: true
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperties(PointArray.prototype, {
|
||||
sort: {
|
||||
value: function(func = (a, b) => a.readingOrderCompare(b)) {
|
||||
return Array.prototype.sort.apply(this, func)
|
||||
}
|
||||
},
|
||||
includes: {
|
||||
value: function(pt) {
|
||||
return pt.isIn(this)
|
||||
}
|
||||
},
|
||||
indexOf: {
|
||||
value: function(pt) {
|
||||
return pt.indexIn(this)
|
||||
}
|
||||
},
|
||||
lastIndexOf: {
|
||||
value: function(pt) {
|
||||
return pt.lastIndexIn(this)
|
||||
}
|
||||
},
|
||||
sub: {
|
||||
value: function(that) {
|
||||
return this.filter(e => !that.pt.includes(e))
|
||||
}
|
||||
},
|
||||
int: {
|
||||
value: function(that) {
|
||||
return this.filter(e => that.pt.includes(e))
|
||||
}
|
||||
},
|
||||
uniq: {
|
||||
value: function() {
|
||||
return this.filter((e, i) => this.pt.indexOf(e) == i)
|
||||
}
|
||||
},
|
||||
pushUniq: {
|
||||
value: function(...vals) {
|
||||
return this.push(...vals.pt.uniq().pt.sub(this))
|
||||
}
|
||||
}
|
||||
})
|
||||
Object.defineProperties(Array.prototype, {
|
||||
pt: {
|
||||
get: function() {
|
||||
return PointArray.convert(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
last: {
|
||||
get: function() {
|
||||
return this[this.length - 1]
|
||||
},
|
||||
set: function(val) {
|
||||
this[this.length - 1] = val
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
dll: {
|
||||
value: function() {
|
||||
return new DLL(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
copy: {
|
||||
value: function() {
|
||||
return this.slice()
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
copyDeep: {
|
||||
value: function() {
|
||||
return JSON.parse(JSON.stringify(this))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
sum: {
|
||||
value: function(val) {
|
||||
return this.reduce((a, b) => a + b, val)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
flatDeep: {
|
||||
value: function() {
|
||||
return this.flat(Infinity)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
transpose: {
|
||||
value: function() {
|
||||
return Array(this[0].length).fill().map((_, i) => this.map(e => e[i]))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
findLast: {
|
||||
value: function(func) {
|
||||
for (let i = this.length - 1; i >= 0; i--) {
|
||||
if (func(this[i], i, this)) {
|
||||
return this[i]
|
||||
}
|
||||
}
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
findLastIndex: {
|
||||
value: function(func) {
|
||||
for (let i = this.length - 1; i >= 0; i--) {
|
||||
if (func(this[i], i, this)) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
interleave: {
|
||||
value: function(that) {
|
||||
return [this, that].transpose().flat()
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
rotate: {
|
||||
value: function(n) {
|
||||
let k = (this.length + n) % this.length
|
||||
return [...this.slice(k), ...this.slice(0, k)]
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
sub: {
|
||||
value: function(that) {
|
||||
return this.filter(e => !that.includes(e))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
int: {
|
||||
value: function(that) {
|
||||
return this.filter(e => that.includes(e))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
uniq: {
|
||||
value: function() {
|
||||
return this.filter((e, i) => this.indexOf(e) == i)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
pushUniq: {
|
||||
value: function(...vals) {
|
||||
if (!warned) {
|
||||
console.warn("You should probably use a Set")
|
||||
warned = true
|
||||
}
|
||||
|
||||
return this.push(...vals.uniq().sub(this))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
count: {
|
||||
value: function(fn) {
|
||||
return this.filter(typeof fn == "function" ? fn : (e) => e == fn).length
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
minIndex: {
|
||||
value: function(fn = (e) => e, tiebreak) {
|
||||
let min = Infinity
|
||||
let idx
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
let val = fn(this[i])
|
||||
|
||||
if (min > val ||
|
||||
(min == val && tiebreak && tiebreak(min, val, idx, i) > 0)) {
|
||||
min = val
|
||||
idx = i
|
||||
}
|
||||
}
|
||||
|
||||
return idx
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
min: {
|
||||
value: function(fn, tiebreak) {
|
||||
return this[this.minIndex(fn, tiebreak)]
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
maxIndex: {
|
||||
value: function(fn = (e) => e, tiebreak) {
|
||||
let max = -Infinity
|
||||
let idx
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
let val = fn(this[i])
|
||||
|
||||
if (max < val ||
|
||||
(max == val && tiebreak && tiebreak(max, val, idx, i) > 0)) {
|
||||
max = val
|
||||
idx = i
|
||||
}
|
||||
}
|
||||
|
||||
return idx
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
max: {
|
||||
value: function(fn, tiebreak) {
|
||||
return this[this.maxIndex(fn, tiebreak)]
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
mean: {
|
||||
value: function() {
|
||||
return this.sum() / this.length
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
freqs: {
|
||||
value: function() {
|
||||
return this.uniq().map((e) => [e, this.count(e)])
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
mode: {
|
||||
value: function(tiebreak) {
|
||||
return this.freqs().max((e) => e[1], (a, b, ai, bi) => tiebreak(a[0], b[0]))[0]
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
antimode: {
|
||||
value: function(tiebreak) {
|
||||
return this.freqs().min((e) => e[1], (a, b, ai, bi) => tiebreak(a[0], b[0]))[0]
|
||||
},
|
||||
configurable: true
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperties(PointArray.prototype, {
|
||||
sort: {
|
||||
value: function(func = (a, b) => a.readingOrderCompare(b)) {
|
||||
return Array.prototype.sort.apply(this, func)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
includes: {
|
||||
value: function(pt) {
|
||||
return pt.isIn(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
indexOf: {
|
||||
value: function(pt) {
|
||||
return pt.indexIn(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
lastIndexOf: {
|
||||
value: function(pt) {
|
||||
return pt.lastIndexIn(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
sub: {
|
||||
value: function(that) {
|
||||
return this.filter(e => !that.pt.includes(e))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
int: {
|
||||
value: function(that) {
|
||||
return this.filter(e => that.pt.includes(e))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
uniq: {
|
||||
value: function() {
|
||||
return this.filter((e, i) => this.pt.indexOf(e) == i)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
pushUniq: {
|
||||
value: function(...vals) {
|
||||
return this.push(...vals.pt.uniq().pt.sub(this))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
count: {
|
||||
value: function(fn) {
|
||||
return this.filter(typeof fn == "function" ? fn : (e) => e.equals(fn)).length
|
||||
},
|
||||
configurable: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
load()
|
||||
utils = {
|
||||
fetch: (url) => fetch(url).then(e => e.text()),
|
||||
fetchEval: (url) => utils.fetch(url).then(e => eval(e)),
|
||||
@@ -798,3 +916,6 @@ utils = {
|
||||
createGridArray: (w, h, fill = undefined) => Array(h).fill().map(() => Array(w).fill(fill))
|
||||
}
|
||||
|
||||
if (typeof window == "undefined") {
|
||||
debugger
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user