-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (157 loc) · 3.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const { distance } = require("fastest-levenshtein")
const defaultLowest = Number.MAX_SAFE_INTEGER
const hashPoint = (i, j) => `${i},${j}`
const computeDistanceMatrix = function(leftSet, rightSet) {
const ij = {}
const ji = {}
for (let i = 0; i < leftSet.length; ++i) {
for (let j = 0; j < rightSet.length; ++j) {
ij[hashPoint(i, j)] = distance(leftSet[i], rightSet[j])
ji[hashPoint(j, i)] = distance(rightSet[j], leftSet[i])
}
}
return { ij, ji }
}
const getNextExclusiveCombination = function(
{ ij, ji },
leftSet,
rightSet,
historyLeft,
historyRight,
) {
const nextCombination = [defaultLowest]
let distance
nextI: for (let i = 0; i < leftSet.length; ++i) {
for (const h of historyLeft) {
if (h === i) {
continue nextI
}
}
nextJ: for (let j = 0; j < rightSet.length; ++j) {
for (const h of historyRight) {
if (h === j) {
continue nextJ
}
}
if (
(distance = Math.min(ij[hashPoint(i, j)], ji[hashPoint(j, i)])) <
nextCombination[0]
) {
nextCombination[0] = distance
nextCombination[1] = i
nextCombination[2] = j
break
}
}
}
return nextCombination
}
const getNextPoint = function(leftSet, rightSet, history) {
for (let i = 0; i < leftSet.length; ++i) {
for (let j = 0; j < rightSet.length; ++j) {
if (!(hashPoint(i, j) in history)) {
return [i, j]
}
}
}
}
const runStartingFrom = function(matrixes, leftSet, rightSet, i, j) {
let distances = []
const historyLeft = []
const historyRight = []
const { ij, ji } = matrixes
let nextPoint = [
ij[hashPoint(i, j)] < ji[hashPoint(j, i)]
? ij[hashPoint(i, j)]
: ji[hashPoint(j, i)],
]
while (true) {
distances.push(nextPoint[0])
historyLeft.push(i)
historyRight.push(j)
nextPoint = getNextExclusiveCombination(
matrixes,
leftSet,
rightSet,
historyLeft,
historyRight,
)
if (nextPoint[0] == defaultLowest) {
break
}
i = nextPoint[1]
j = nextPoint[2]
}
return { distances, historyLeft, historyRight }
}
const runSetPair = function(leftSet, rightSet) {
if (leftSet.length > rightSet.length) {
const tmp = leftSet
leftSet = rightSet
rightSet = tmp
}
const history = {}
const matrixes = computeDistanceMatrix(leftSet, rightSet)
while (true) {
const nextPoint = getNextPoint(leftSet, rightSet, history)
if (!nextPoint) {
break
}
history[hashPoint(nextPoint[0], nextPoint[1])] = runStartingFrom(
matrixes,
leftSet,
rightSet,
nextPoint[0],
nextPoint[1],
)
}
return history
}
const compare = function(input, setsToCompareAgainst) {
const matches = []
for (let i = 0; i < setsToCompareAgainst.length; ++i) {
const set = setsToCompareAgainst[i]
const history = runSetPair(input, set)
for (const value of Object.values(history)) {
const { distances, historyRight, historyLeft } = value
const partition = distances.reduce(function(acc, v) {
return acc + v
}, 0)
let sequentialVariance = 0
for (let i = 1; i < historyLeft.length; i++) {
sequentialVariance += Math.abs(
historyRight[i] -
historyRight[i - 1] -
(historyLeft[i] - historyLeft[i - 1]),
)
}
matches.push({ i, sequentialVariance, partition, value })
}
}
matches.sort(function(a, b) {
if (a.partition < b.partition) {
return -1
} else if (b.partition < a.partition) {
return 1
}
if (a.sequentialVariance < b.sequentialVariance) {
return -1
} else if (b.sequentialVariance < a.sequentialVariance) {
return 1
}
return 0
})
const orderedResults = []
const seen = new Map()
for (const { i } of matches) {
if (seen.has(i)) {
continue
}
seen.set(i, 0)
orderedResults.push(setsToCompareAgainst[i])
}
return orderedResults
}
module.exports = {
compare,
}