91 lines
3.6 KiB
JavaScript
91 lines
3.6 KiB
JavaScript
import {Vector} from './Vector.js'
|
|
import {NotAType, Returns, Undefined} from '#core/Type.js'
|
|
import {Any, Optional, match} from '#core/TypePatterns.js'
|
|
import {BooleanT} from '#boolean/BooleanT.js'
|
|
|
|
export const deepEqual = [
|
|
match([Vector, Any], Returns(BooleanT, () => false)),
|
|
match([Any, Vector], Returns(BooleanT, () => false)),
|
|
match([Vector, Vector], (math, [V, W]) => {
|
|
if (V.Component === NotAType || W.Component === NotAType) {
|
|
return Returns(BooleanT, (v, w) => v === w
|
|
|| (v.length === w.length
|
|
&& v.every((e, i) => math.deepEqual(e, w[i]))))
|
|
}
|
|
const compDeep = math.deepEqual.resolve([V.Component, W.Component])
|
|
return Returns(BooleanT, (v,w) => v === w
|
|
|| (v.length === w.length
|
|
&& v.every((e, i) => compDeep(e, w[i]))))
|
|
})
|
|
]
|
|
|
|
export const indistinguishable = [
|
|
match([Vector, Any, Optional([Any, Any])], (math, [V, E, T]) => {
|
|
const VComp = V.Component
|
|
if (T.length === 0) { // no tolerances
|
|
const same = math.indistinguishable.resolve([VComp, E])
|
|
return Returns(
|
|
Vector(same.returns), (v, e) => v.map(f => same(f, e)))
|
|
}
|
|
const [[RT, AT]] = T
|
|
const same = math.indistinguishable.resolve([VComp, E, RT, AT])
|
|
return Returns(
|
|
Vector(same.returns),
|
|
(v, e, [[rT, aT]]) => v.map(f => same(f, e, rT, aT)))
|
|
}),
|
|
match([Any, Vector, Optional([Any, Any])], (math, [E, V, T]) => {
|
|
// reimplement to get other order in same so as not to assume
|
|
// same is symmetric, even though it probably is
|
|
const VComp = V.Component
|
|
if (T.length === 0) { // no tolerances
|
|
const same = math.indistinguishable.resolve([E, VComp])
|
|
return Returns(
|
|
Vector(same.returns), (e, v) => v.map(f => same(e, f)))
|
|
}
|
|
const [[RT, AT]] = T
|
|
const same = math.indistinguishable.resolve([E, VComp, RT, AT])
|
|
return Returns(
|
|
Vector(same.returns),
|
|
(e, v, [[rT, aT]]) => v.map(f => same(e, f, rT, aT)))
|
|
}),
|
|
match([Vector, Vector, Optional([Any, Any])], (math, [V, W, T]) => {
|
|
const VComp = V.Component
|
|
const WComp = W.Component
|
|
let same
|
|
let sameNoV
|
|
let sameNoW
|
|
if (T.length === 0) { // no tolerances
|
|
same = math.indistinguishable.resolve([VComp, WComp])
|
|
sameNoV = math.indistinguishable.resolve([Undefined, WComp])
|
|
sameNoW = math.indistinguishable.resolve([VComp, Undefined])
|
|
} else {
|
|
const [[RT, AT]] = T
|
|
same = math.indistinguishable.resolve([VComp, WComp, RT, AT])
|
|
sameNoV = math.indistinguishable.resolve([Undefined, WComp, RT, AT])
|
|
sameNoW = math.indistinguishable.resolve([VComp, Undefined, RT, AT])
|
|
}
|
|
return Returns(
|
|
Vector(same.returns),
|
|
(v, w, [tol = [0, 0]]) => {
|
|
const [rT, aT] = tol
|
|
const vInc = Number(v.length > 1)
|
|
const wInc = Number(w.length >= v.length || w.length > 1)
|
|
const retval = []
|
|
let vIx = 0
|
|
let wIx = 0
|
|
let remainder = vIx < v.length || wIx < w.length
|
|
while ((vInc && vIx < v.length)
|
|
|| (wInc && wIx < w.length)
|
|
) {
|
|
if (vIx >= v.length) {
|
|
retval.push(sameNoV(undefined, w[wIx], rT, aT))
|
|
} else if (wIx >= w.length) {
|
|
retval.push(sameNoW(v[vIx], undefined, rT, aT))
|
|
} else retval.push(same(v[vIx], w[wIx], rT, aT))
|
|
vIx += vInc
|
|
wIx += wInc
|
|
}
|
|
return retval
|
|
})
|
|
})
|
|
]
|