-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathposition.ts
More file actions
564 lines (529 loc) · 16.8 KB
/
position.ts
File metadata and controls
564 lines (529 loc) · 16.8 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import { LogootInt } from './int'
import { CompareResult, FatalError } from '../utils'
import { BranchKey, BranchOrder } from './branch'
import { Comparable, cmpResult } from '../compare'
/**
* A comparable position from the original Logootish algorithm, This is just an
* array of numbers with some utility functions. In Logoot, it must always be
* possible to allocate a position between any possible two positions. In this
* algorithm, a position with more `levels` (or elements in the array) comes
* first. Positions are represented in writing the same as arrays: `[1,2,3]`
* @example ```typescript
* const a = new LogootishPosition()
* console.log(a.toString()) // [0]
*
* const b = a.offsetLowest(1)
* console.log(b.toString()) // [1]
*
* console.log(new LogootishPosition(1, a, b).toString()) // [0]
* console.log(new LogootishPosition(2, a, b).toString()) // [0,0]
* ```
*/
class LogootishPosition extends Comparable<LogootishPosition> {
array: LogootInt[] = [new LogootInt(0)]
immutable = false
/**
* This constructor constructs a new position that is in the range specified
* by `start` and `end`. By using `len`, it is possible to enforce that a
* certain number of additional positions are available in the selected range.
* This guarantees that there's space for a LogootNode of length `len` at this
* position between `start` and `end`.
*
* @param len - The length of the allocation to make. The length is never
* actually stored in the Logoot position, but is used when finding space for
* the position to be created and `len` position(s) after it.
* @param start - This will cause the new position to have a value greater
* than or equal to this. This value is tricky: It must be the end of the last
* node. So if `A` is at `[1]` and an allocation *after* it is desired, then
* `[2]` would need to be passed to `start`.
* @param end - This will cause the new position to have a value less than or
* equal to this, subject to the value of `len`.
* @throws {TypeError} Will throw if `start` is greater than `end`.
*/
constructor(
len = 0,
readonly start?: LogootishPosition,
readonly end?: LogootishPosition
) {
super()
if (start && end && start.gt(end)) {
throw new TypeError('Start is greater than end')
}
if (!start && end) {
this.array = end.inverseOffsetLowest(len).array
} else if (!end && start) {
this.array = start.copy().array
} else if (start && end) {
let done = false
const itstart = start.array.values()
const itend = end.array.values()
let nstart
let nend
this.array.length = 0
while (!done) {
if (!nstart || !nstart.done) {
nstart = itstart.next()
}
if (!nend || !nend.done) {
nend = itend.next()
}
if (!nstart.done && !nend.done) {
// See if we have enough space to insert 'len' between the nodes
if (nend.value.gteq(new LogootInt(nstart.value).add(len))) {
// There's space. We're done now: At the shallowest possible level
done = true
}
// Regardless, the start ID is the new ID for this level of our node
this.array.push(new LogootInt(nstart.value))
} else if (!nstart.done) {
// So there's no end restriction, that means we can just add right on
// top of the old end (the start of the new node)
this.array.push(new LogootInt(nstart.value))
done = true
} else if (!nend.done) {
// We have an end restriction, but no start restriction, so we just
// put the new node's start behind the old end
this.array.push(new LogootInt(nend.value).sub(len))
done = true
} else {
// So both other IDs have nothing else. It must be time to make a new
// level and be done
this.array.push(new LogootInt())
done = true
}
}
}
}
static fromJSON(eventnode: LogootishPosition.JSON): LogootishPosition {
const pos = new LogootishPosition()
pos.array.length = 0
eventnode.forEach((n) => {
pos.array.push(LogootInt.fromJSON(n))
})
return pos
}
static fromInts(...ints: (LogootInt | number)[]): LogootishPosition {
const pos = new LogootishPosition()
pos.array.length = 0
ints.forEach((n) => {
pos.array.push(new LogootInt(n))
})
return pos
}
toJSON(): LogootishPosition.JSON {
return this.array.map((n) => n.toJSON())
}
/**
* @returns Internal array length
*/
get length(): number {
// A zero-length position is NOT valid
// Through some sneakiness, you COULD directly assign the array to make it
// have a length of zero. Don't do it.
return this.array.length
}
/**
* Returns the last index of the array. This is useful because before this,
* the algorithm code often contained many occurences of `length - 1`. This
* is used to cut down redundancy.
*/
get levels(): number {
// A zero-length position is NOT valid
// Through some sneakiness, you COULD directly assign the array to make it
// have a length of zero. Don't do it.
return this.length - 1
}
iterator(): IterableIterator<LogootInt> {
return this.array.values()
}
/**
* An array accessor
*/
level(n: number): LogootInt {
if (this.immutable) {
return this.array[n] && this.array[n].i
}
return this.array[n]
}
/**
* An array accessor
* @alias level
*/
l(n: number): LogootInt {
return this.level(n)
}
/**
* Returns a new position with `offset` added to the lowest level of the
* position.
*/
offsetLowest(offset: number | LogootInt): LogootishPosition {
return Object.assign(new LogootishPosition(), {
array: this.array.map((current, i, array) => {
return i < array.length - 1
? current
: new LogootInt(current).add(offset)
})
})
}
/**
* Returns a new position with `offset` subtracted from the lowest level of
* the position.
*/
inverseOffsetLowest(offset: number | LogootInt): LogootishPosition {
return Object.assign(new LogootishPosition(), {
array: this.array.map((current, i, array) => {
return i < array.length - 1
? current
: new LogootInt(current).sub(offset)
})
})
}
/**
* Duplicates this position.
*/
copy(): LogootishPosition {
return Object.assign(new LogootishPosition(), {
array: this.array.map((e) => new LogootInt(e))
})
}
/**
* Return a copy of this position, but with the number of levels specified by
* `level`. If this position has fewer levels, zeroes will be added in place.
*/
equivalentPositionAtLevel(level: number): LogootishPosition {
return Object.assign(new LogootishPosition(), {
array: new Array(level + 1).fill(0, 0, level + 1).map((el, i) => {
return new LogootInt(this.array[i])
})
})
}
private cmp_level(pos: LogootishPosition, level: number): CompareResult {
if (level >= this.length) {
if (this.length === pos.length) {
return 0
}
return 1
}
if (level >= pos.length) {
return -1
}
switch (this.level(level).cmp(pos.level(level))) {
case 1:
return 1
case -1:
return -1
case 0:
return this.cmp_level(pos, level + 1)
}
}
cmp(pos: LogootishPosition): CompareResult {
return this.cmp_level(pos, 0)
}
/**
* Return this position if it is between `min` or `max`, otherwise return
* `min` if this is less and `max` if this is greater.
* @param min - The minimum output.
* @param max - The maximum output.
* @param preserve_levels - If defined, the output number of levels will be
* equal to `preserve_levels`.
* @returns Either this position, min, or max. It is **not** copied, so if you
* want to modify it, you should copy it.
*/
clamp(
min: LogootishPosition,
max: LogootishPosition,
preserve_levels?: undefined | number
): LogootishPosition {
const clamped = this.cmp(min) < 0 ? min : this.cmp(max) > 0 ? max : this
if (preserve_levels !== undefined) {
return clamped.equivalentPositionAtLevel(preserve_levels)
} else {
return clamped.copy()
}
}
toString(): string {
let str = '['
this.array.forEach((el, i, a) => {
str += el.toString() + (i >= a.length - 1 ? '' : ',')
})
str += ']'
return str
}
}
namespace LogootishPosition {
export type JSON = LogootInt.JSON[]
export namespace JSON {
export const Schema = { type: 'array', items: LogootInt.JSON.Schema }
}
}
class BranchOrderInconsistencyError extends Error {
constructor(message: string) {
super(message)
this.name = 'BranchOrderInconsistencyError'
}
}
function positionsCompatible(s: LogootPosition, e: LogootPosition): boolean {
if (s.length > e.length) {
s = s.copy().truncateTo(e.length)
} else if (e.length > s.length) {
e = e.copy().truncateTo(s.length)
}
return s.lteq(e)
}
class LogootPosition extends Comparable<LogootPosition> {
protected lp: LogootishPosition = new LogootishPosition()
protected branch_array: BranchKey[] = []
readonly branch_order: BranchOrder
constructor(
br: BranchKey,
len = 0,
readonly start?: LogootPosition,
readonly end?: LogootPosition,
branch_order?: BranchOrder
) {
super()
if (start && end && !positionsCompatible(start, end)) {
throw new TypeError('Start is greater than end')
}
// First, set up the branch order
if (start && end && start.branch_order !== end.branch_order) {
throw new BranchOrderInconsistencyError(
'Start and end do not have the same branch order'
)
}
const existing_branch_order = start?.branch_order || end?.branch_order
if (!branch_order && (start || end)) {
branch_order = start?.branch_order || end?.branch_order
} else if (
branch_order &&
existing_branch_order &&
branch_order !== existing_branch_order
) {
throw new BranchOrderInconsistencyError(
'The provided branch order is not the same as the position branch order'
)
}
if (!branch_order) {
branch_order = new BranchOrder()
}
this.branch_order = branch_order
const tgt_i = branch_order.i(br)
let done = false
const itstart = start
? start.iterator()
: (function* (): IterableIterator<[LogootInt, BranchKey]> {
return
})()
const itend = end
? end.iterator()
: (function* (): IterableIterator<[LogootInt, BranchKey]> {
return
})()
let nstart, nend
this.branch_array.length = 0
this.lp.array.length = 0
while (!done) {
if (!nstart || !nstart.done) {
nstart = itstart.next()
}
if (!nend || !nend.done) {
nend = itend.next()
}
const bs = nstart.value ? branch_order.i(nstart.value[1]) : -Infinity
const be = nend.value ? branch_order.i(nend.value[1]) : Infinity
if (bs > tgt_i || be < tgt_i) {
// The target branch is not in the available range; Move on
if (!nstart.value) {
this.branch_array.push(nend.value[1])
this.lp.array.push(nend.value[0].copy())
} else {
this.branch_array.push(nstart.value[1])
const int = nstart.value[0].copy()
this.lp.array.push(int)
}
} else {
if (bs === be) {
// So now we have to allocate between linear numbers just like a
// classic `LogootishPositon`
this.branch_array.push(nstart.value[1])
if (nstart.value[0].copy().add(len).lteq(nend.value[0])) {
// There's enough space to cram the target data in here; We're done
done = true
}
this.lp.array.push(nstart.value[0].copy())
} else {
// Since we can allocate on our own branch, there's technically
// infinite space.
if (bs === tgt_i) {
this.branch_array.push(br)
this.lp.array.push(nstart.value[0].copy())
} else if (be === tgt_i) {
this.branch_array.push(br)
this.lp.array.push(nend.value[0].copy().sub(len))
} else {
this.branch_array.push(br)
this.lp.array.push(new LogootInt(0))
}
// ...and we're done here
done = true
}
}
}
}
static fromIntsBranches(
order: BranchOrder,
...els: [number | LogootInt, BranchKey][]
): LogootPosition {
const lp = new LogootPosition(els[0][1], 0, undefined, undefined, order)
lp.lp = LogootishPosition.fromInts(...els.map(([n]) => n))
lp.branch_array = els.map(([, b]) => b)
return lp
}
copy(): LogootPosition {
const lp = new LogootPosition(
this.branch_order.b(0),
0,
undefined,
undefined,
this.branch_order
)
lp.branch_array = this.branch_array.map((k) => k)
lp.lp = this.lp.copy()
return lp
}
selfTest(): void {
if (this.branch_array.length !== this.lp.length) {
throw new FatalError(
'LogootPosition array was corrupted. This should never happen.'
)
}
}
get length(): number {
return this.lp.length
}
/**
* Returns the last index of the array. This is useful because before this,
* the algorithm code often contained many occurences of `length - 1`. This
* is used to cut down redundancy.
*/
get levels(): number {
// A zero-length position is NOT valid
// Through some sneakiness, you COULD directly assign the array to make it
// have a length of zero. Don't do it.
return this.length - 1
}
*iterator(): IterableIterator<[LogootInt, BranchKey]> {
const n_iter = this.lp.iterator()
const b_iter = this.branch_array.values()
let n
let b
while (true) {
n = n_iter.next()
b = b_iter.next()
if (n.done && b.done) {
return
} else if (n.done !== b.done) {
throw new FatalError(
'LogootPosition array was corrupted. This should never happen.'
)
}
yield [n.value, b.value]
}
}
private cmp_level(pos: LogootPosition, level: number): CompareResult {
if (level >= this.length) {
if (this.length === pos.length) {
return 0
}
return 1
}
if (level >= pos.length) {
return -1
}
const ti = this.branch_order.i(this.branch_array[level])
const oi = this.branch_order.i(pos.branch_array[level])
switch (cmpResult(ti - oi)) {
case 1:
return 1
case -1:
return -1
case 0:
switch (this.lp.l(level).cmp(pos.lp.l(level))) {
case 1:
return 1
case -1:
return -1
case 0:
return this.cmp_level(pos, level + 1)
}
}
// TODO: Throw some kind of error
}
cmp(pos: LogootPosition): CompareResult {
return this.cmp_level(pos, 0)
}
l(l: number): [LogootInt, BranchKey] {
return [this.lp.l(l), this.branch_array[l]]
}
offsetLowest(n: number): LogootPosition {
const lp = this.copy()
lp.lp = lp.lp.offsetLowest(n)
return lp
}
inverseOffsetLowest(n: number): LogootPosition {
const lp = this.copy()
lp.lp = lp.lp.inverseOffsetLowest(n)
return lp
}
truncateTo(level: number): LogootPosition {
if (this.branch_array.length < level) {
throw new TypeError('Truncate cannot add levels')
}
if (level < 1) {
throw new TypeError('Cannot truncate to a level less than 1')
}
this.branch_array.length = level
this.lp.array.length = level
return this
}
equalsHigherLevel(to: LogootPosition): boolean {
return to.length < this.length && this.copy().truncateTo(to.length).eq(to)
}
toJSON(): LogootPosition.JSON {
const jb = (b: BranchKey): string | number => {
if (typeof b === 'symbol') {
throw new TypeError('Cannot convert Symbol to JSON')
} else {
return b
}
}
return this.branch_array.map((br, i) => [this.lp.l(i).toJSON(), jb(br)])
}
toMappedOrderJSON(order: BranchOrder): LogootPosition.MappedOrderJSON {
return this.branch_array.map((br, i) => [
this.lp.l(i).toJSON(),
order.i(br)
])
}
toString(): string {
// Corruption can cause some seriously weird errors here. If the user is
// `console.log`ging stuff, then it's probably fine to do a quick self test
this.selfTest()
const bstr = this.branch_array.map((br) => br.toString())
return `[${bstr.map((br, i) => `(${this.lp.level(i)},${br})`)}]`
}
}
namespace LogootPosition {
export type JSON = [LogootInt.JSON, number | string][]
export type MappedOrderJSON = [LogootInt.JSON, number][]
export namespace JSON {
export const Schema = {
type: 'array',
items: {
type: 'array',
items: [LogootInt.JSON.Schema, { type: ['number', 'string'] }]
}
}
}
}
export { LogootPosition, BranchOrderInconsistencyError, LogootishPosition }