-
Notifications
You must be signed in to change notification settings - Fork 0
/
two-fields.js
410 lines (404 loc) · 12.2 KB
/
two-fields.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
export default function constructorFactory(
l = Symbol('left'),
r = Symbol('right')
) {
let cached = { added: false, resized: false, node: null }
function result(added = false, resized = false, node = null) {
cached.added = added
cached.resized = resized
cached.node = node
return cached
// return { added, resized, node }
}
function addNode(curr, node, comp) {
let cmp = comp(curr, node)
if (cmp > 0) {
let left = curr[l]
if (typeof left === 'number') { // c c
node[r] = 0 // 1 n 0
node[l] = 1 // 1
curr[l] = node
return result(true, true)
} else {
let right = left[r]
if (typeof right === 'number') {
if (right === 0) {
let a = addNode(left, node, comp)
if (a.resized) {
a.node = smallRotate(curr, left, right)
a.resized = false
}
return a
} else { // right === 2 c c
node[r] = left // l2 n l1
node[l] = 1 // 1 1 1
curr[l] = node
left[r] = 1
return result(true, false)
}
} else { // c
let a = addNode(left, node, comp) // l r
if (a.resized) {
let diff = right[r]
if (diff === 2) {
right[r] = 1
a.resized = false
} else if (diff === 1) {
right[r] = 0
} else {
a.node = rotate(curr, left, right, diff)
a.resized = false
}
} else if (a.node !== null) {
curr[l] = a.node
a.node = null
}
return a
}
}
} else if (cmp < 0) {
let left = curr[l]
if (typeof left === 'number') { // c c
node[r] = 2 // 1 n2
node[l] = 1 // 1
curr[l] = node
return result(true, true, null)
} else {
let right = left[r]
if (typeof right === 'number') {
if (right === 0) { // c c
node[r] = 1 // l 0 l n1
node[l] = 1 // 1 1 1
left[r] = node
return result(true, false)
} else { // right === 2
let a = addNode(left, node, comp)
if (a.resized) {
a.node = smallRotate(curr, left, right)
a.resized = false
}
return a
}
} else { // c
let a = addNode(right, node, comp) // l r
if (a.resized) {
let diff = right[r]
if (diff === 0) {
right[r] = 1
a.resized = false
} else if (diff === 1) {
right[r] = 2
} else {
a.node = rotate(curr, left, right, diff)
a.resized = false
}
} else if (a.node !== null) {
left[r] = a.node
a.node = null
}
return a
}
}
}
return result()
}
function smallRotate(curr, left, right) {
let lleft = left[l]
let ldiff = lleft[r]
if (right === 0) {
if (ldiff === 0) {
left[r] = curr[r] // c l
lleft[r] = curr // l 0 ll c1
curr[r] = 1 // ll 0
curr[l] = 1
return left
} else {
lleft[r] = curr[r] // c ll
lleft[l] = left // l 0 l c1
left[r] = curr // ll2
left[l] = 1
curr[r] = 1
curr[l] = 1
return lleft
}
} else {
if (ldiff === 0) {
lleft[r] = curr[r] // c ll
lleft[l] = curr // l2 c l1
curr[l] = 1 // ll 0
curr[r] = left
left[r] = 1
left[l] = 1
return lleft
} else {
left[r] = curr[r] // c l
left[l] = curr // l2 c ll1
curr[r] = lleft // ll2
curr[l] = 1
lleft[r] = 1
return left
}
}
}
function rotate(curr, left, right, diff) {
if (diff === 0) {
// 0- 1
// 2 |x| ? ?
// |x| ? |x| |x? |x? |x|
// |x? |x?
let lleft = left[l]
let lright = lleft[r]
let ldiff = lright[r]
if (ldiff === 2) {
let lrleft = lright[l]
let lrright = lrleft[r]
let lrdiff
if (typeof lrright == 'number') {
lrdiff = lrright // c lr
if (lrdiff === 0) { // l r0 l c1
lleft[r] = lrleft // ll lr2 ll lrl1 r2
curr[l] = right // lrl 0
} else { // lrdiff === 2
curr[l] = lrleft // c lr
lrleft[r] = right // l r0 l c1
lrleft = lleft // ll lr2 ll 0 lrl r1
} // lrl2
} else {
lrdiff = lrright[r] // c lr
lleft[r] = lrleft // l r0 l c1
curr[l] = lrright // ll lr2 ll lrl? lrr r?
lrright[r] = right // lrl lrr?
}
lright[l] = left
lright[r] = curr[r]
left[r] = curr
curr[r] = 1
if (lrdiff === 0) {
lrleft[r] = 1
right[r] = 2
} else if (lrdiff === 1) {
lrleft[r] = 1
right[r] = 1
} else {
lrleft[r] = 0
right[r] = 1
}
return lright
} else {
// 0- 2 0- 1
// 1 |x| |x| 0 0 |x| |x| 1
// |x| |x| |1| |x| |x| |x| |x| |1| |x| |x|
// |1| |1| |1| |1|
left[l] = lleft
left[r] = curr[r]
lleft[r] = curr
curr[l] = lright
lright[r] = right // c l
if (ldiff === 0) { // l r0 ll c1
curr[r] = 1 // ll lr0 lr r1
right[r] = 1
} else { // ldiff === 1
curr[r] = 2 // c l
right[r] = 0 // l r0 ll c2
} // lr lr1 lr r0
return left
}
} else { // diff === 2
let rleft = right[l]
let rright = rleft[r]
let rdiff = rright[r]
if (rdiff === 0) {
// 2+ 1
// |x| 0 ? ?
// ? |x| |x| |x? |x? |x|
// |x? |x?
let rlleft = rleft[l]
let rlright = rlleft[r]
let rldiff
if (typeof rlright === 'number') {
rldiff = rlright // c rl
if (rldiff === 0) { // l r2 c r1
left[r] = rlleft // rl rr0 l rll1 rr2
right[l] = rright // rll 0
} else { // rldiff === 2
right[l] = rlleft // c rl
rlleft[r] = rright // l r2 c r1
rlleft = left // rl rr0 l 0 rll rr1
} // rll2
} else {
rldiff = rlright[r] // c rl
left[r] = rlleft // l r2 c r1
right[l] = rlright // rl rr0 l rll? rlr rr?
rlright[r] = rright // rll rlr?
}
rleft[l] = curr
rleft[r] = curr[r]
curr[r] = right
right[r] = 1
if (rldiff === 0) {
rlleft[r] = 1
rright[r] = 2
} else if (rldiff === 1) {
rlleft[r] = 1
rright[r] = 1
} else {
rlleft[r] = 0
rright[r] = 1
}
return rleft
} else {
// 2+ 0 2+ 1
// |x| 1 2 |x| |x| 2 1 |x|
// |x| |x| |x| |x| |1| |x| |x| |x| |x| |1|
// |1| |1| |1| |1|
right[l] = curr
right[r] = curr[r]
curr[r] = rright
left[r] = rleft // c r
if (rdiff === 1) { // l r2 c rr0
rright[r] = 0 // rl rr1 l rl2
rleft[r] = 2
} else { // c r
rright[r] = 1 // l r2 c rr1
rleft[r] = 1 // rl rr2 l rl1
}
return right
}
}
}
function walkTree(node, action, depth) {
let left = node[l]
if (typeof left === 'number') {
action(node, left, depth)
} else {
let right = left[r]
if (typeof right === 'number') {
if (right === 0) {
walkTree(left, action, depth + 1)
action(node, right, depth)
} else {
action(node, right, depth)
walkTree(left, action, depth + 1)
}
} else {
walkTree(left, action, depth + 1)
action(node, right[r], depth)
walkTree(right, action, depth + 1)
}
}
}
return class IntrusiveIndex {
constructor(comp) {
this.comp = comp
this.root = null
}
add(node) {
let { root, comp } = this
if (root === null) {
this.root = node
node[r] = 0
node[l] = 1
return true
} else {
let a = addNode(root, node, comp)
if (a.node !== null) {
this.root = a.node
a.node = null
}
return a.added
}
}
get(value) {
let { root, comp } = this
if (root == null) return
while (true) {
let cmp = comp(root, value)
if (cmp < 0) {
let left = root[l]
if (typeof left === 'number') break
let right = left[r]
if (typeof right === 'number') {
if (right === 2) root = left
else break
} else {
root = right
}
} else if (cmp > 0) {
let left = root[l]
if (typeof left === 'number') break
let right = left[r]
if (typeof right === 'number') {
if (right === 0) root = left
else break
} else {
root = left
}
} else if (cmp === 0) {
return root
} else break
}
}
walk(action) {
let { root } = this
if (root === null) return
walkTree(root, action, 0)
}
static get l() {
return l
}
static get r() {
return r
}
}
}
function print(index) {
let nodes = []
index.walk((node, diff, depth) => {
let sign =
diff === 1 ? ' =' :
diff === 0 ? ' <' :
diff === 2 ? ' >' : ' ?'
nodes.push(' '.repeat(depth) + node.value + sign)
})
nodes.reverse()
for (let node of nodes) {
console.log(node)
}
}
let IIA = constructorFactory('left', 'right')
for (let i = 0; i < 10; i++) {
let index = new IIA((a, b) => a.value - b.value)
let values = Array(1000).fill(0)
.map((_, i) => i)
.sort((a, b) => Math.random() - 0.5)
for (let value of values) {
index.add({ ['foo_' + i]: true, value })
}
let sum = 0
for (let value of values) {
sum += index.get({ value }).value
}
}
let index = new IIA((a, b) => a.value - b.value)
let values = Array(1000000).fill(0)
.map((_, i) => i)
.sort((a, b) => Math.random() - 0.5)
function Row(value) {
this.value = value
this[IIA.l] = null
this[IIA.r] = null
}
let start = Date.now()
for (let i = 0; i < values.length; i++) {
index.add(new Row(values[i]))
}
console.log(Date.now() - start)
start = Date.now()
let sum = 0
for (let i = 0; i < values.length; i++) {
let value = values[i]
sum += index.get({ value }).value
}
console.log(Date.now() - start, sum)
// console.log(JSON.stringify(index.root, null, 2))
// print(index)