-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.ts
More file actions
373 lines (345 loc) · 9.78 KB
/
Copy pathnode.ts
File metadata and controls
373 lines (345 loc) · 9.78 KB
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
/**
* @file This contains most of the data types used by the `ListDocumentModel`.
* While `index.ts` does most of the heavy lifting, this file is the source of
* most definitions used there. The files were split to make it easier for me to
* switch since I can switch using tabs in my text editor.
* @author Nathan Pennie <kb1rd@kb1rd.net>
*/
/** */
import { DBstNode } from '../bst'
import { LogootInt } from './int'
import { CompareResult } from '../utils'
import { LogootPosition } from './position'
const DocStart = 'S'
type DocStart = 'S'
const DocEnd = 'E'
type DocEnd = 'E'
type LeftAnchor = DocStart | LogootPosition
type RightAnchor = LogootPosition | DocEnd
enum NodeType {
DATA,
REMOVAL,
// SUGGESTED_REMOVAL,
DUMMY
}
class AnchorLogootNode extends DBstNode<AnchorLogootNode> {
left_anchor?: LeftAnchor
right_anchor?: RightAnchor
conflict_with: Set<AnchorLogootNode> = new Set<AnchorLogootNode>()
constructor(
public logoot_start: LogootPosition,
public length: number,
public type = NodeType.DATA,
public clk = new LogootInt(0)
) {
super()
}
get logoot_end(): LogootPosition {
return this.logoot_start.offsetLowest(this.length)
}
get ldoc_start(): number {
return this.absolute_value
}
get ldoc_length(): number {
return this.type === NodeType.DATA ? this.length : 0
}
get ldoc_end(): number {
return this.ldoc_start + this.ldoc_length
}
get true_left(): LeftAnchor {
return this.left_anchor || this.logoot_start
}
get true_right(): RightAnchor {
return this.right_anchor || this.logoot_end
}
reduceLeft(anchor: LeftAnchor): void {
// If the anchor is the start, our anchor cannot be reduced
if (anchor && anchor !== DocStart) {
if (
anchor.gt(this.logoot_start) &&
!this.logoot_start.equalsHigherLevel(anchor)
) {
return
}
if (
this.true_left === DocStart ||
anchor.gteq(this.true_left) ||
anchor.equalsHigherLevel(this.true_left)
) {
if (anchor.eq(this.logoot_start)) {
delete this.left_anchor
return
}
this.left_anchor = anchor.copy()
}
}
}
reduceRight(anchor: RightAnchor): void {
if (anchor && anchor !== DocEnd) {
if (
anchor.lt(this.logoot_end) &&
!anchor.equalsHigherLevel(this.logoot_end)
) {
return
}
if (this.true_right === DocEnd || anchor.lteq(this.true_right)) {
if (anchor.eq(this.logoot_end)) {
delete this.right_anchor
return
}
this.right_anchor = anchor.copy()
}
}
}
findLeftAnchorNode(left = this.inorder_predecessor): AnchorLogootNode {
if (!left) {
return undefined
}
const true_left = this.true_left
if (true_left === DocStart) {
return undefined
}
const it = new Set<AnchorLogootNode>(left.conflict_with).add(left).values()
let value: AnchorLogootNode
while (({ value } = it.next()) && value) {
if (value.logoot_end.eq(true_left)) {
return value
}
}
return undefined
}
findRightAnchorNode(right = this.inorder_successor): AnchorLogootNode {
if (!right) {
return undefined
}
const true_right = this.true_right
if (true_right === DocEnd) {
return undefined
}
const it = new Set<AnchorLogootNode>(right.conflict_with)
.add(right)
.values()
let value: AnchorLogootNode
while (({ value } = it.next()) && value) {
if (value.logoot_start.eq(true_right)) {
return value
}
}
return undefined
}
addConflictsFromNode(
node: AnchorLogootNode
): {
node?: AnchorLogootNode
added: boolean
} {
let nnode: AnchorLogootNode
let did_add = false
const tryAdd = (n: AnchorLogootNode, to: AnchorLogootNode = this): void => {
if (n === to) {
return
}
if (!to.conflict_with.has(n)) {
did_add = true
to.conflict_with.add(n)
}
}
if (node.logoot_start.lt(this.logoot_start)) {
// Left
if (node.true_right === 'E') {
tryAdd(node)
} else if (node.true_right.gt(this.logoot_start)) {
if (node.true_right.lt(this.logoot_end)) {
const pos = this.positionOf(node.true_right)
if (pos > 0 && pos < this.length) {
nnode = this.splitAround(pos)
}
}
tryAdd(node)
}
} else {
// Right
if (node.true_left === 'S') {
tryAdd(node)
} else if (
(node.true_left.lt(this.logoot_end) &&
!node.true_left.equalsHigherLevel(this.logoot_end)) ||
this.logoot_end.equalsHigherLevel(node.true_left)
) {
// eslint-disable-next-line
let cf_node: AnchorLogootNode = this
if (node.true_left.gt(this.logoot_start)) {
const pos = this.positionOf(node.true_left)
if (pos > 0 && pos < this.length) {
nnode = this.splitAround(pos)
cf_node = nnode
}
}
tryAdd(node, cf_node)
}
}
return { node: nnode, added: did_add }
}
/**
* Infers a node's conflicts from its neighbors. This assumes that the
* neighbors have the correct `conflict_with`. This will only add conflicts
* that the neighbors are in or create.
* @param neighbor The neighbor to infer conflicts from
* @param newNode Will be called when a node is split
*/
updateNeighborConflicts(
neighbor: AnchorLogootNode,
newNode: (n: AnchorLogootNode) => void
): boolean {
let did_add = false
const nodes: AnchorLogootNode[] = [this]
const all_scan = new Set<AnchorLogootNode>(neighbor.conflict_with)
all_scan.add(neighbor)
all_scan.forEach((snode) => {
const new_nodes: AnchorLogootNode[] = []
nodes.forEach((dnode) => {
const { node, added } = dnode.addConflictsFromNode(snode)
did_add = did_add || added
if (node) {
newNode(node)
new_nodes.push(node)
}
})
nodes.push(...new_nodes)
})
return did_add
}
preferential_cmp(other: AnchorLogootNode): CompareResult {
return this.logoot_start.cmp(other.logoot_start)
}
positionOf(pos: LogootPosition): number {
const level = this.logoot_start.length
if (
pos.length < level ||
pos.lt(this.logoot_start) ||
pos.gt(this.logoot_end)
) {
return undefined
}
const lval = pos.l(level - 1)[0].copy()
lval.sub(this.logoot_start.l(level - 1)[0])
return lval.js_int
}
splitAround(
pos: number,
next: IterableIterator<AnchorLogootNode> = this.successorIterator()
): AnchorLogootNode {
if (this.length < 2) {
throw new TypeError('This node cannot be split. It is too small.')
}
if (pos < 1 || pos >= this.length) {
throw new TypeError('The split position is not in the node.')
}
const node = new AnchorLogootNode(
this.logoot_start.offsetLowest(pos),
this.length - pos,
this.type,
this.clk.copy()
)
if (this.type === NodeType.DATA) {
node.value = this.ldoc_start + pos
} else {
node.value = this.ldoc_start
}
this.length = pos
node.conflict_with = new Set<AnchorLogootNode>(this.conflict_with)
node.right_anchor = this.right_anchor
delete this.right_anchor
delete node.left_anchor
let value: AnchorLogootNode
let done: boolean
while (
({ value, done } = next.next()) &&
!done &&
value.conflict_with.has(this)
) {
value.conflict_with.delete(this)
value.conflict_with.add(node)
}
return node
}
toString(short?: boolean): string {
const type_string = {
[NodeType.DATA]: 'D',
[NodeType.DUMMY]: 'X',
[NodeType.REMOVAL]: 'R'
}
const type = type_string[this.type] || '?'
return (
`${type} ${this.logoot_start},${this.ldoc_start} + ${this.length} ` +
`@ ${this.clk} (${this.true_left}<---->${this.true_right})` +
(this.conflict_with.size === 0 || short
? ''
: ((): string => {
let str = ' {\n'
this.conflict_with.forEach(
(n) => (str += ` ${n.toString(true)}\n`)
)
str += '}'
return str
})())
)
}
}
function sliceNodesIntoRanges(
boundaries: LogootPosition[],
nodes: AnchorLogootNode[],
onNewNode: (node: AnchorLogootNode) => void
): AnchorLogootNode[][] {
if (boundaries.some((b) => !b)) {
throw new TypeError('Boundaries must be defined')
}
const buckets = [
...Array(boundaries.length + 1)
].map((): AnchorLogootNode[] => [])
nodes = nodes.sort((a, b) => a.preferential_cmp(b))
nodes.forEach((node) => {
buckets.forEach((bucket: AnchorLogootNode[], i: number) => {
const cb = boundaries[i]
if (node && (!cb || node.logoot_start.lt(cb))) {
// Since the boundary is considered the right end of a node, the next
// bucket will include any nodes with a position on a lower level:
// [1]
// | buckets[i] |-----------| buckets[i+1] |
// [1,0] [1,1]
// ---|node|---
if (
cb &&
node.logoot_start.length > cb.length &&
node.logoot_start.copy().truncateTo(cb.length).eq(cb)
) {
return
}
bucket.push(node)
const pos = boundaries[i] && node.positionOf(boundaries[i])
if (
boundaries[i] &&
node.logoot_end.gt(boundaries[i]) &&
pos &&
pos > 0 &&
pos < node.length
) {
node = node.splitAround(node.positionOf(boundaries[i]))
onNewNode(node)
} else {
node = undefined
}
}
})
})
return buckets
}
export {
AnchorLogootNode,
sliceNodesIntoRanges,
NodeType,
DocStart,
DocEnd,
LeftAnchor,
RightAnchor
}