-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
341 lines (269 loc) · 7.41 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
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
exports.fullRoots = function (index, result) {
if (index & 1) throw new Error('You can only look up roots for depth(0) blocks')
if (!result) result = []
index /= 2
let offset = 0
let factor = 1
while (true) {
if (!index) return result
while (factor * 2 <= index) factor *= 2
result.push(offset + factor - 1)
offset = offset + 2 * factor
index -= factor
factor = 1
}
}
exports.futureRoots = function (index, result) {
if (index & 1) throw new Error('You can only look up future roots for depth(0) blocks')
if (!result) result = []
let factor = 1
// make first root
while (factor * 2 <= index) factor *= 2
// full factor of 2 - done
if (factor * 2 - 2 === index) return result
let pos = factor / 2 - 1
// while its not a full tree
while ((pos + factor / 2 - 1) !== index) {
pos += factor
// read too far, to to left child
while ((pos + factor / 2 - 1) > index) {
factor /= 2
pos -= factor / 2
}
// the "gap" is a future root
result.push(pos - factor / 2)
}
return result
}
exports.patch = function (from, to) {
if (from === 0 || from >= to) return []
const roots = exports.fullRoots(from)
const target = exports.fullRoots(to)
// first find the first root that is different
let i = 0
for (; i < target.length; i++) {
if (i >= roots.length || roots[i] !== target[i]) break
}
const patch = []
if (i < roots.length) {
// now we need to grow the newest root until it hits the diff one
let prev = roots.length - 1
const ite = exports.iterator(roots[prev--])
while (ite.index !== target[i]) {
ite.sibling()
if (prev >= 0 && ite.index === roots[prev]) {
prev--
} else {
patch.push(ite.index)
}
patch.push(ite.parent())
}
i++ // patched to next root, so inc
}
// include the rest
for (; i < target.length; i++) patch.push(target[i])
return patch
}
exports.depth = function (index) {
let depth = 0
index += 1
while (!(index & 1)) {
depth++
index = rightShift(index)
}
return depth
}
exports.sibling = function (index, depth) {
if (!depth) depth = exports.depth(index)
const offset = exports.offset(index, depth)
return exports.index(depth, offset & 1 ? offset - 1 : offset + 1)
}
exports.parent = function (index, depth) {
if (!depth) depth = exports.depth(index)
const offset = exports.offset(index, depth)
return exports.index(depth + 1, rightShift(offset))
}
exports.leftChild = function (index, depth) {
if (!(index & 1)) return -1
if (!depth) depth = exports.depth(index)
return exports.index(depth - 1, exports.offset(index, depth) * 2)
}
exports.rightChild = function (index, depth) {
if (!(index & 1)) return -1
if (!depth) depth = exports.depth(index)
return exports.index(depth - 1, 1 + (exports.offset(index, depth) * 2))
}
exports.children = function (index, depth) {
if (!(index & 1)) return null
if (!depth) depth = exports.depth(index)
const offset = exports.offset(index, depth) * 2
return [
exports.index(depth - 1, offset),
exports.index(depth - 1, offset + 1)
]
}
exports.leftSpan = function (index, depth) {
if (!(index & 1)) return index
if (!depth) depth = exports.depth(index)
return exports.offset(index, depth) * twoPow(depth + 1)
}
exports.rightSpan = function (index, depth) {
if (!(index & 1)) return index
if (!depth) depth = exports.depth(index)
return (exports.offset(index, depth) + 1) * twoPow(depth + 1) - 2
}
exports.nextLeaf = function (index) {
let factor = 1
let r = index
while ((r & 1) === 1) {
r = (r - 1) / 2
factor *= 2
}
return index + factor + 1
}
exports.count = function (index, depth) {
if (!(index & 1)) return 1
if (!depth) depth = exports.depth(index)
return twoPow(depth + 1) - 1
}
exports.countLeaves = function (index) {
return (exports.count(index) + 1) / 2
}
exports.spans = function (index, depth) {
if (!(index & 1)) return [index, index]
if (!depth) depth = exports.depth(index)
const offset = exports.offset(index, depth)
const width = twoPow(depth + 1)
return [offset * width, (offset + 1) * width - 2]
}
exports.index = function (depth, offset) {
return (1 + 2 * offset) * twoPow(depth) - 1
}
exports.offset = function (index, depth) {
if (!(index & 1)) return index / 2
if (!depth) depth = exports.depth(index)
return ((index + 1) / twoPow(depth) - 1) / 2
}
exports.iterator = function (index) {
const ite = new Iterator()
ite.seek(index || 0)
return ite
}
function twoPow (n) {
return n < 31 ? 1 << n : ((1 << 30) * (1 << (n - 30)))
}
function rightShift (n) {
return (n - (n & 1)) / 2
}
function Iterator () {
this.index = 0
this.offset = 0
this.factor = 0
}
Iterator.prototype.seek = function (index) {
this.index = index
if (this.index & 1) {
this.offset = exports.offset(index)
this.factor = twoPow(exports.depth(index) + 1)
} else {
this.offset = index / 2
this.factor = 2
}
}
Iterator.prototype.isLeft = function () {
return (this.offset & 1) === 0
}
Iterator.prototype.isRight = function () {
return (this.offset & 1) === 1
}
Iterator.prototype.contains = function (index) {
return index > this.index
? index < (this.index + this.factor / 2)
: index < this.index
? index > (this.index - this.factor / 2)
: true
}
Iterator.prototype.prev = function () {
if (!this.offset) return this.index
this.offset--
this.index -= this.factor
return this.index
}
Iterator.prototype.next = function () {
this.offset++
this.index += this.factor
return this.index
}
Iterator.prototype.count = function () {
if (!(this.index & 1)) return 1
return this.factor - 1
}
Iterator.prototype.countLeaves = function () {
return (this.count() + 1) / 2
}
Iterator.prototype.sibling = function () {
return this.isLeft() ? this.next() : this.prev()
}
Iterator.prototype.parent = function () {
if (this.offset & 1) {
this.index -= this.factor / 2
this.offset = (this.offset - 1) / 2
} else {
this.index += this.factor / 2
this.offset /= 2
}
this.factor *= 2
return this.index
}
Iterator.prototype.leftSpan = function () {
this.index = this.index - this.factor / 2 + 1
this.offset = this.index / 2
this.factor = 2
return this.index
}
Iterator.prototype.rightSpan = function () {
this.index = this.index + this.factor / 2 - 1
this.offset = this.index / 2
this.factor = 2
return this.index
}
Iterator.prototype.leftChild = function () {
if (this.factor === 2) return this.index
this.factor /= 2
this.index -= this.factor / 2
this.offset *= 2
return this.index
}
Iterator.prototype.rightChild = function () {
if (this.factor === 2) return this.index
this.factor /= 2
this.index += this.factor / 2
this.offset = 2 * this.offset + 1
return this.index
}
Iterator.prototype.nextTree = function () {
this.index = this.index + this.factor / 2 + 1
this.offset = this.index / 2
this.factor = 2
return this.index
}
Iterator.prototype.prevTree = function () {
if (!this.offset) {
this.index = 0
this.factor = 2
} else {
this.index = this.index - this.factor / 2 - 1
this.offset = this.index / 2
this.factor = 2
}
return this.index
}
Iterator.prototype.fullRoot = function (index) {
if (index <= this.index || (this.index & 1) > 0) return false
while (index > this.index + this.factor + this.factor / 2) {
this.index += this.factor / 2
this.factor *= 2
this.offset /= 2
}
return true
}