@nerd-bible/wordgard 0.5.3-beta3 → 0.5.3-beta5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/set.js ADDED
@@ -0,0 +1,814 @@
1
+ import { ChangeSet } from 'wordgard/doc';
2
+
3
+ function findAbove(array, start, n) {
4
+ let from = start, to = array.length;
5
+ for (;;) {
6
+ if (from == to)
7
+ return from;
8
+ let mid = (from + to) >> 1;
9
+ if (array[mid] > n)
10
+ to = mid;
11
+ else
12
+ from = mid + 1;
13
+ }
14
+ }
15
+ function heapBubble(heap, index, cmp) {
16
+ for (let cur = heap[index];;) {
17
+ let childIndex = (index << 1) + 1;
18
+ if (childIndex >= heap.length)
19
+ break;
20
+ let child = heap[childIndex];
21
+ if (childIndex + 1 < heap.length && cmp(child, heap[childIndex + 1]) >= 0) {
22
+ child = heap[childIndex + 1];
23
+ childIndex++;
24
+ }
25
+ if (cmp(cur, child) < 0)
26
+ break;
27
+ heap[childIndex] = cur;
28
+ heap[index] = child;
29
+ index = childIndex;
30
+ }
31
+ }
32
+ function heapSink(heap, index, cmp) {
33
+ let elt = heap[index];
34
+ while (index > 0) {
35
+ let parent = (index - 1) >> 1;
36
+ if (cmp(heap[parent], elt) < 0)
37
+ break;
38
+ heap[index] = heap[parent];
39
+ heap[parent] = elt;
40
+ index = parent;
41
+ }
42
+ }
43
+ function heapPop(heap, cmp) {
44
+ let last = heap.pop();
45
+ if (heap.length) {
46
+ heap[0] = last;
47
+ heapBubble(heap, 0, cmp);
48
+ }
49
+ }
50
+ function addReplacements(changes, replace) {
51
+ let add = [], at = 0, len = changes.newLength;
52
+ for (let repl of replace) {
53
+ if (repl.from < at)
54
+ throw new Error("Replacing ranges must be ordered and non-overlapping");
55
+ if (repl.to > len)
56
+ throw new Error("Replacing range out of bounds");
57
+ if (repl.from > at)
58
+ add.push(repl.from - at, -1);
59
+ add.push(repl.to - repl.from, repl.to - repl.from);
60
+ at = repl.to;
61
+ }
62
+ if (len > at)
63
+ add.push(len - at, -1);
64
+ return ChangeSet.composeSections(changes.sections, add);
65
+ }
66
+
67
+ let Chunk$1 = class Chunk {
68
+ start;
69
+ from;
70
+ to;
71
+ value;
72
+ constructor(start, from, to, value) {
73
+ this.start = start;
74
+ this.from = from;
75
+ this.to = to;
76
+ this.value = value;
77
+ }
78
+ get end() {
79
+ return this.start + this.to[this.to.length - 1];
80
+ }
81
+ move(start) {
82
+ return start == this.start ? this : new Chunk(start, this.from, this.to, this.value);
83
+ }
84
+ };
85
+ let SetBuilder$1 = class SetBuilder {
86
+ chunks = [];
87
+ spilled = null;
88
+ lastTo = -1;
89
+ lastFrom = -1;
90
+ get next() {
91
+ return this.spilled || (this.spilled = new SetBuilder());
92
+ }
93
+ addChunk(chunk) {
94
+ if (chunk.start < this.lastTo) {
95
+ this.next.addChunk(chunk);
96
+ }
97
+ else {
98
+ this.chunks.push(chunk);
99
+ this.lastTo = chunk.end;
100
+ }
101
+ }
102
+ add(source, pre) {
103
+ if (typeof source != "function") {
104
+ let array = source;
105
+ source = add => { for (let [from, to, value] of array)
106
+ add(from, to, value); };
107
+ }
108
+ source((from, to, value) => {
109
+ if (pre)
110
+ pre(from, to);
111
+ this.addRange(from, to, value);
112
+ });
113
+ }
114
+ addRange(from, to, value) {
115
+ if (from >= to)
116
+ throw new Error("Ranges cannot be empty");
117
+ (this.lastTo <= from ? this : this.next).addInner(from, to, value);
118
+ }
119
+ addInner(from, to, value) {
120
+ let chunk;
121
+ if (this.chunks.length) {
122
+ chunk = this.chunks[this.chunks.length - 1];
123
+ if (chunk.value.length >= 512)
124
+ chunk = undefined;
125
+ }
126
+ if (!chunk) {
127
+ this.chunks.push(chunk = new Chunk$1(from, [], [], []));
128
+ }
129
+ chunk.from.push(from - chunk.start);
130
+ chunk.to.push(to - chunk.start);
131
+ chunk.value.push(value);
132
+ this.lastFrom = from;
133
+ this.lastTo = to;
134
+ }
135
+ finish(plus = null) {
136
+ let next = this.spilled ? this.spilled.finish(plus) : plus;
137
+ if (!this.chunks.length)
138
+ return next || RangeSet.empty;
139
+ return RangeSet.new(this.chunks, next);
140
+ }
141
+ };
142
+ class RangeSet {
143
+ chunks;
144
+ next;
145
+ constructor(
146
+ chunks,
147
+ next) {
148
+ this.chunks = chunks;
149
+ this.next = next;
150
+ }
151
+ static new(chunks, next) {
152
+ return new RangeSet(chunks, next);
153
+ }
154
+ static create(source) {
155
+ let build = new SetBuilder$1();
156
+ build.add(source, from => {
157
+ if (from < build.lastFrom)
158
+ throw new Error("Ranges must be added in order");
159
+ });
160
+ return build.finish();
161
+ }
162
+ get length() {
163
+ return Math.max(this.chunks.length ? this.chunks[this.chunks.length - 1].end : 0, this.next ? this.next.length : 0);
164
+ }
165
+ get empty() {
166
+ return this == RangeSet.empty;
167
+ }
168
+ cursor(from = 0) {
169
+ if (!this.next)
170
+ return new LayerCursor(this.chunks, from);
171
+ let cursors = [];
172
+ for (let layer = this; layer; layer = layer.next)
173
+ cursors.push(new LayerCursor(layer.chunks, from));
174
+ return new HeapCursor$1(cursors);
175
+ }
176
+ static cursor(sets, from = 0) {
177
+ let cursors = [];
178
+ for (let set of sets)
179
+ if (!set.empty) {
180
+ for (let layer = set; layer; layer = layer.next)
181
+ cursors.push(new LayerCursor(layer.chunks, from));
182
+ }
183
+ return cursors.length == 0 ? RangeSet.empty.cursor() : cursors.length == 1 ? cursors[0] : new HeapCursor$1(cursors);
184
+ }
185
+ map(map, replace = []) {
186
+ let { sections } = map;
187
+ if (replace.length)
188
+ sections = addReplacements(map, replace);
189
+ else if (map.empty)
190
+ return this;
191
+ return this.mapInner(sections, map, replace);
192
+ }
193
+ mapInner(sections, map, replace) {
194
+ let cursor = new LayerCursor(this.chunks, 0);
195
+ let replI = 0, posA = 0, posB = 0;
196
+ let build = new SetBuilder$1();
197
+ for (let i = 0; i < sections.length;) {
198
+ let len = sections[i++], ins = sections[i++];
199
+ if (ins < 0) {
200
+ while (i < sections.length && sections[i + 1] < 0) {
201
+ len += sections[i];
202
+ i += 2;
203
+ }
204
+ let upto = i == sections.length ? 1e9 : posA + len, off = posB - posA;
205
+ while (cursor.cur) {
206
+ let chunk = cursor.cur;
207
+ if (cursor.i == 0 && chunk.end < upto) {
208
+ build.addChunk(chunk.move(chunk.start + off));
209
+ cursor.next(true);
210
+ }
211
+ else if (cursor.to < upto) {
212
+ build.addRange(cursor.from + off, cursor.to + off, cursor.value);
213
+ cursor.next();
214
+ }
215
+ else {
216
+ break;
217
+ }
218
+ }
219
+ posB += len;
220
+ }
221
+ else {
222
+ let replStartI = replI, endB = posB + ins;
223
+ copyMappedUpto$1(cursor, posA, map, build, replace, replStartI);
224
+ while (replI < replace.length && replace[replI].from < endB) {
225
+ let repl = replace[replI++];
226
+ if (repl.add)
227
+ build.add(repl.add);
228
+ }
229
+ if (len)
230
+ cursor.goto(posA + len - 1);
231
+ copyMappedUpto$1(cursor, posA + len, map, build, replace, replStartI);
232
+ posB = endB;
233
+ }
234
+ posA += len;
235
+ }
236
+ return build.finish(this.next && this.next.mapInner(sections, map, replace));
237
+ }
238
+ modify(spec) {
239
+ let { replace, add, filter } = spec;
240
+ let result = this;
241
+ if (replace && replace.length) {
242
+ result = result.map(ChangeSet.empty(Math.max(result.length, replace[replace.length - 1].to)), replace);
243
+ }
244
+ return add || filter ? result.modifyInner(add, filter) : result;
245
+ }
246
+ modifyInner(add, filter) {
247
+ let build = new SetBuilder$1();
248
+ let cursor = new LayerCursor(this.chunks, 0);
249
+ let advance = (pos) => {
250
+ for (;;) {
251
+ let { cur } = cursor;
252
+ if (!cur)
253
+ return;
254
+ if (cursor.i == 0 && cur.end <= pos && !filter) {
255
+ build.addChunk(cur);
256
+ cursor.next(true);
257
+ }
258
+ else if (cursor.from > pos) {
259
+ break;
260
+ }
261
+ else {
262
+ if (!filter || filter(cursor.from, cursor.to, cursor.value))
263
+ build.addRange(cursor.from, cursor.to, cursor.value);
264
+ cursor.next();
265
+ }
266
+ }
267
+ };
268
+ if (add)
269
+ build.add(add, advance);
270
+ advance(1e9);
271
+ return build.finish(this.next && this.next.modifyInner(add, filter));
272
+ }
273
+ compareRange(fromA, b, fromB, len, change) {
274
+ if (this == b)
275
+ return;
276
+ let curA = new LayerCursor(this.chunks, fromA), curB = new LayerCursor(b.chunks, fromB), off = fromB - fromA;
277
+ let end = fromB + len, startA = -1, endA = -1, startB = -1, endB = -1;
278
+ for (;;) {
279
+ if (startA >= endA)
280
+ [startA, endA] = curA.value ? [Math.max(fromB, curA.from + off), Math.min(end, curA.to + off)] : [1e9, 1e9];
281
+ if (startB >= endB)
282
+ [startB, endB] = curB.value ? [Math.max(fromB, curB.from), Math.min(end, curB.to)] : [1e9, 1e9];
283
+ let start = Math.min(startA, startB), upto;
284
+ if (start >= end)
285
+ break;
286
+ if (startA < startB) {
287
+ change(startA, upto = Math.min(endA, startB));
288
+ startA = upto;
289
+ }
290
+ else if (startB < startA) {
291
+ change(startB, upto = Math.min(endB, startA));
292
+ startB = upto;
293
+ }
294
+ else if (curA.cur.value == curB.cur.value) {
295
+ curA.next(true);
296
+ curB.next(true);
297
+ startA = startB = 1e9;
298
+ }
299
+ else {
300
+ upto = Math.min(endA, endB);
301
+ if (!curA.value.eq(curB.value))
302
+ change(startA, upto);
303
+ startA = startB = upto;
304
+ }
305
+ if (startA >= endA)
306
+ curA.next();
307
+ if (startB >= endB)
308
+ curB.next();
309
+ }
310
+ if (this.next || b.next)
311
+ (this.next || RangeSet.empty).compareRange(fromA, b.next || RangeSet.empty, fromB, len, change);
312
+ }
313
+ static empty = /*@__PURE__*/(() => new RangeSet([], null))();
314
+ }
315
+ function copyMappedUpto$1(cursor, upto, map, build, replace, replI) {
316
+ while (cursor.value && cursor.from <= upto) {
317
+ let value = cursor.value;
318
+ let from = map.mapPos(cursor.from, value.inclusiveStart ? -1 : 1);
319
+ let to = map.mapPos(cursor.to, value.inclusiveEnd ? 1 : -1);
320
+ let filtered = from >= to;
321
+ for (let i = replI; !filtered && i < replace.length && replace[i].from < to; i++) {
322
+ if (replace[i].to > from)
323
+ filtered = true;
324
+ }
325
+ if (!filtered)
326
+ build.addRange(from, to, value);
327
+ cursor.next();
328
+ }
329
+ }
330
+ class LayerCursor {
331
+ chunks;
332
+ chunkI = 0;
333
+ i = 0;
334
+ from = 0;
335
+ constructor(chunks, start) {
336
+ this.chunks = chunks;
337
+ this.goto(start);
338
+ }
339
+ goto(pos) {
340
+ if (pos < this.from)
341
+ this.chunkI = this.i = 0;
342
+ else if (pos < this.to)
343
+ return;
344
+ let { chunks } = this;
345
+ while (this.chunkI < chunks.length && chunks[this.chunkI].end <= pos) {
346
+ this.chunkI++;
347
+ this.i = 0;
348
+ }
349
+ if (this.chunkI == chunks.length) {
350
+ this.i = 0;
351
+ this.from = this.to = 1e9;
352
+ this.cur = this.value = null;
353
+ }
354
+ else {
355
+ let chunk = this.cur = chunks[this.chunkI];
356
+ let i = this.i = findAbove(chunk.to, this.i, pos - chunk.start);
357
+ this.from = chunk.from[i] + chunk.start;
358
+ this.to = chunk.to[i] + chunk.start;
359
+ this.value = chunk.value[i];
360
+ }
361
+ }
362
+ next(chunk) {
363
+ let { cur } = this;
364
+ if (!cur)
365
+ return;
366
+ if (!chunk && this.i < cur.value.length - 1) {
367
+ this.i++;
368
+ }
369
+ else {
370
+ this.chunkI++;
371
+ this.i = 0;
372
+ if (this.chunkI == this.chunks.length) {
373
+ this.from = this.to = 1e9;
374
+ this.value = this.cur = null;
375
+ return;
376
+ }
377
+ else {
378
+ cur = this.cur = this.chunks[this.chunkI];
379
+ }
380
+ }
381
+ this.from = cur.from[this.i] + cur.start;
382
+ this.to = cur.to[this.i] + cur.start;
383
+ this.value = cur.value[this.i];
384
+ }
385
+ }
386
+ let cmpCursor$1 = (a, b) => {
387
+ return (a.from - b.from) || (a.value.inclusiveStart ? (b.value.inclusiveStart ? 0 : 1) : -1) || (a.to - b.to);
388
+ };
389
+ let HeapCursor$1 = class HeapCursor {
390
+ cursors;
391
+ heap = [];
392
+ constructor(cursors) {
393
+ this.cursors = cursors;
394
+ for (let cur of cursors)
395
+ if (cur.value) {
396
+ this.heap.push(cur);
397
+ heapSink(this.heap, this.heap.length - 1, cmpCursor$1);
398
+ }
399
+ this.fill();
400
+ }
401
+ fill() {
402
+ if (this.heap.length) {
403
+ ({ from: this.from, to: this.to, value: this.value } = this.heap[0]);
404
+ }
405
+ else {
406
+ this.from = this.to = 1e9;
407
+ this.value = null;
408
+ }
409
+ }
410
+ goto(pos) {
411
+ this.heap = [];
412
+ for (let cur of this.cursors) {
413
+ cur.goto(pos);
414
+ if (cur.value) {
415
+ this.heap.push(cur);
416
+ heapSink(this.heap, this.heap.length - 1, cmpCursor$1);
417
+ }
418
+ }
419
+ }
420
+ next() {
421
+ if (this.heap.length) {
422
+ this.heap[0].next();
423
+ if (this.heap[0].value)
424
+ heapBubble(this.heap, 0, cmpCursor$1);
425
+ else
426
+ heapPop(this.heap, cmpCursor$1);
427
+ this.fill();
428
+ }
429
+ }
430
+ };
431
+
432
+ class Chunk {
433
+ start;
434
+ pos;
435
+ value;
436
+ constructor(start, pos, value) {
437
+ this.start = start;
438
+ this.pos = pos;
439
+ this.value = value;
440
+ }
441
+ get end() {
442
+ return this.start + this.pos[this.pos.length - 1];
443
+ }
444
+ get startSide() {
445
+ return this.value[0].side;
446
+ }
447
+ get endSide() {
448
+ return this.value[this.value.length - 1].side;
449
+ }
450
+ move(start) {
451
+ return start == this.start ? this : new Chunk(start, this.pos, this.value);
452
+ }
453
+ }
454
+ class SetBuilder {
455
+ chunks = [];
456
+ lastPos = -1;
457
+ lastSide = -1;
458
+ addChunk(chunk) {
459
+ this.chunks.push(chunk);
460
+ this.lastPos = chunk.end;
461
+ this.lastSide = chunk.endSide;
462
+ }
463
+ add(source, pre) {
464
+ if (typeof source != "function") {
465
+ let array = source;
466
+ source = add => { for (let [pos, value] of array)
467
+ add(pos, value); };
468
+ }
469
+ source((pos, value) => {
470
+ if (pre)
471
+ pre(pos, value);
472
+ this.addPoint(pos, value);
473
+ });
474
+ }
475
+ addPoint(pos, value) {
476
+ let chunk;
477
+ if (this.chunks.length) {
478
+ chunk = this.chunks[this.chunks.length - 1];
479
+ if (chunk.value.length >= 512)
480
+ chunk = undefined;
481
+ }
482
+ if (!chunk) {
483
+ this.chunks.push(chunk = new Chunk(pos, [], []));
484
+ }
485
+ chunk.pos.push(pos - chunk.start);
486
+ chunk.value.push(value);
487
+ if ((pos - this.lastPos || value.side || this.lastSide) >= 0) {
488
+ this.lastPos = pos;
489
+ this.lastSide = value.side;
490
+ }
491
+ else {
492
+ for (let i = chunk.value.length - 1, chunkI = this.chunks.length - 1;;) {
493
+ let nextI = i - 1, nextChunk = chunk;
494
+ if (nextI < 0) {
495
+ if (!chunkI)
496
+ break;
497
+ nextChunk = this.chunks[--chunkI];
498
+ nextI = nextChunk.value.length - 1;
499
+ }
500
+ if ((pos - nextChunk.pos[nextI] || value.side - nextChunk.value[nextI].side) >= 0)
501
+ break;
502
+ chunk.pos[i] = nextChunk.pos[nextI];
503
+ chunk.value[i] = nextChunk.value[nextI];
504
+ nextChunk.pos[nextI] = pos;
505
+ nextChunk.value[nextI] = value;
506
+ i = nextI;
507
+ chunk = nextChunk;
508
+ }
509
+ }
510
+ }
511
+ finish() {
512
+ return this.chunks.length ? PointSet.new(this.chunks) : PointSet.empty;
513
+ }
514
+ }
515
+ class PointSet {
516
+ chunks;
517
+ constructor(
518
+ chunks) {
519
+ this.chunks = chunks;
520
+ }
521
+ static new(chunks) { return new PointSet(chunks); }
522
+ static create(source) {
523
+ let build = new SetBuilder();
524
+ build.add(source, (from, value) => {
525
+ if ((from - build.lastPos || value.side - build.lastSide) < 0)
526
+ throw new Error("Points must be added in order");
527
+ });
528
+ return build.finish();
529
+ }
530
+ get length() {
531
+ return this.chunks.length ? this.chunks[this.chunks.length - 1].end : 0;
532
+ }
533
+ get empty() {
534
+ return this == PointSet.empty;
535
+ }
536
+ cursor(from = 0, side = -1e9) {
537
+ return new PointCursor(this, from, side);
538
+ }
539
+ static cursor(sets, from = 0, side = -1e9) {
540
+ let cursors = [];
541
+ for (let set of sets)
542
+ if (!set.empty)
543
+ cursors.push(set.cursor(from, side));
544
+ return cursors.length == 0 ? PointSet.empty.cursor() : cursors.length == 1 ? cursors[0] : new HeapCursor(cursors);
545
+ }
546
+ at(pos) {
547
+ for (let chunk of this.chunks) {
548
+ if (chunk.end > pos)
549
+ break;
550
+ if (chunk.start > pos)
551
+ continue;
552
+ let index = findAbove(chunk.pos, 0, pos - 1);
553
+ if (index < chunk.pos.length && chunk.pos[index] == pos)
554
+ return chunk.value[index];
555
+ }
556
+ return undefined;
557
+ }
558
+ map(map, replace = []) {
559
+ let { sections } = map;
560
+ if (replace.length)
561
+ sections = addReplacements(map, replace);
562
+ else if (map.empty)
563
+ return this;
564
+ return this.mapInner(sections, map, replace);
565
+ }
566
+ mapInner(sections, map, replace) {
567
+ let cursor = new PointCursor(this, 0, -1e9);
568
+ let replI = 0, posA = 0, posB = 0;
569
+ let build = new SetBuilder();
570
+ for (let i = 0; i < sections.length;) {
571
+ let len = sections[i++], ins = sections[i++];
572
+ if (ins < 0) {
573
+ while (i < sections.length && sections[i + 1] < 0) {
574
+ len += sections[i];
575
+ i += 2;
576
+ }
577
+ let upto = i == sections.length ? 1e9 : posA + len, off = posB - posA;
578
+ while (cursor.cur) {
579
+ let chunk = cursor.cur;
580
+ if (cursor.i == 0 && chunk.end < upto) {
581
+ build.addChunk(chunk.move(chunk.start + off));
582
+ cursor.next(true);
583
+ }
584
+ else if (cursor.pos < upto) {
585
+ build.addPoint(cursor.pos + off, cursor.value);
586
+ cursor.next();
587
+ }
588
+ else {
589
+ break;
590
+ }
591
+ }
592
+ posB += len;
593
+ }
594
+ else {
595
+ let replStartI = replI, endB = posB + ins;
596
+ copyMappedUpto(cursor, posA, map, build, replace, replStartI);
597
+ while (replI < replace.length && replace[replI].from < endB) {
598
+ let repl = replace[replI++];
599
+ if (repl.add)
600
+ build.add(repl.add);
601
+ }
602
+ if (len)
603
+ cursor.goto(posA + len, -1e9);
604
+ copyMappedUpto(cursor, posA + len, map, build, replace, replStartI);
605
+ posB = endB;
606
+ }
607
+ posA += len;
608
+ }
609
+ return build.finish();
610
+ }
611
+ modify(spec) {
612
+ let { replace, add, filter } = spec;
613
+ let result = this;
614
+ if (replace && replace.length) {
615
+ result = result.map(ChangeSet.empty(Math.max(result.length, replace[replace.length - 1].to)), replace);
616
+ }
617
+ return add || filter ? result.modifyInner(add, filter) : result;
618
+ }
619
+ modifyInner(add, filter) {
620
+ let build = new SetBuilder();
621
+ let cursor = new PointCursor(this, 0, -1e9);
622
+ let advance = (pos) => {
623
+ for (;;) {
624
+ let { cur } = cursor;
625
+ if (!cur)
626
+ return;
627
+ if (cursor.i == 0 && cur.end <= pos && !filter) {
628
+ build.addChunk(cur);
629
+ cursor.next(true);
630
+ }
631
+ else if (cursor.pos >= pos) {
632
+ break;
633
+ }
634
+ else {
635
+ if (!filter || filter(cursor.pos, cursor.value))
636
+ build.addPoint(cursor.pos, cursor.value);
637
+ cursor.next();
638
+ }
639
+ }
640
+ };
641
+ if (add)
642
+ build.add(add, advance);
643
+ advance(1e9);
644
+ return build.finish();
645
+ }
646
+ compareRange(fromA, b, fromB, len, change) {
647
+ if (this == b)
648
+ return;
649
+ let curA = new PointCursor(this, fromA, -1e9), curB = new PointCursor(b, fromB, -1e9);
650
+ let off = fromB - fromA, endB = fromB + len;
651
+ for (;;) {
652
+ let nextA = curA.value ? curA.pos + off : 1e9, nextB = curB.value ? curB.pos : 1e9;
653
+ if (Math.min(nextA, nextB) > endB)
654
+ break;
655
+ let cmp = nextA - nextB || curA.side - curB.side;
656
+ if (cmp == 0 && curA.cur.value == curB.cur.value) { curA.next(true);
657
+ curB.next(true);
658
+ }
659
+ else if (cmp == 0 && curA.value.eq(curB.value)) {
660
+ curA.next();
661
+ curB.next();
662
+ }
663
+ else if (cmp < 0) {
664
+ change(nextA, curA.value);
665
+ curA.next();
666
+ }
667
+ else {
668
+ change(nextB, curB.value);
669
+ curB.next();
670
+ }
671
+ }
672
+ }
673
+ static empty = /*@__PURE__*/(() => new PointSet([]))();
674
+ }
675
+ function copyMappedUpto(cursor, upto, map, build, replace, replI) {
676
+ while (cursor.value && cursor.pos <= upto) {
677
+ let value = cursor.value;
678
+ let pos = map.mapPos(cursor.pos, value.side < 0 ? -1 : 1, value.trackMode);
679
+ if (pos != null) {
680
+ let filtered = false;
681
+ for (let i = replI; !filtered && i < replace.length && replace[i].from <= pos; i++) {
682
+ if (replace[i].to >= pos)
683
+ filtered = true;
684
+ }
685
+ if (!filtered)
686
+ build.addPoint(pos, value);
687
+ }
688
+ cursor.next();
689
+ }
690
+ }
691
+ class PointCursor {
692
+ set;
693
+ chunkI = 0;
694
+ i = 0;
695
+ pos = -1;
696
+ constructor(set, start, side) {
697
+ this.set = set;
698
+ this.goto(start, side);
699
+ }
700
+ get side() { return this.value ? this.value.side : 1e9; }
701
+ goto(pos, side) {
702
+ let diff = pos - this.pos || side - this.side;
703
+ if (diff < 0) {
704
+ this.chunkI = this.i = 0;
705
+ }
706
+ else if (diff == 0 && this.cur) {
707
+ for (;; this.i--) {
708
+ if (!this.i) {
709
+ this.chunkI = 0;
710
+ break;
711
+ }
712
+ if ((pos - this.cur.pos[this.i - 1] || side - this.cur.value[this.i - 1].side) < 0)
713
+ break;
714
+ }
715
+ }
716
+ for (let first = true;;) {
717
+ if (this.chunkI == this.set.chunks.length) {
718
+ this.pos = 1e9;
719
+ this.cur = this.value = null;
720
+ break;
721
+ }
722
+ let chunk = this.set.chunks[this.chunkI];
723
+ if (chunk.end < pos || this.i == chunk.value.length) {
724
+ this.chunkI++;
725
+ this.i = 0;
726
+ }
727
+ else if (first) {
728
+ this.i = findAbove(chunk.pos, this.i, pos - chunk.start - 1);
729
+ first = false;
730
+ }
731
+ else if ((pos - (chunk.start + chunk.pos[this.i]) || side - chunk.value[this.i].side) > 0) {
732
+ this.i++;
733
+ }
734
+ else {
735
+ this.cur = chunk;
736
+ this.pos = chunk.pos[this.i] + chunk.start;
737
+ this.value = chunk.value[this.i];
738
+ break;
739
+ }
740
+ }
741
+ }
742
+ next(chunk) {
743
+ let { cur } = this;
744
+ if (!cur)
745
+ return;
746
+ if (!chunk && this.i < cur.value.length - 1) {
747
+ this.i++;
748
+ }
749
+ else {
750
+ this.chunkI++;
751
+ this.i = 0;
752
+ if (this.chunkI == this.set.chunks.length) {
753
+ this.value = this.cur = null;
754
+ return;
755
+ }
756
+ else {
757
+ cur = this.cur = this.set.chunks[this.chunkI];
758
+ }
759
+ }
760
+ this.pos = cur.pos[this.i] + cur.start;
761
+ this.value = cur.value[this.i];
762
+ }
763
+ }
764
+ function cmpCursor(a, b) {
765
+ return a.pos - b.pos || a.value.side - b.value.side;
766
+ }
767
+ class HeapCursor {
768
+ cursors;
769
+ heap = [];
770
+ constructor(cursors) {
771
+ this.cursors = cursors;
772
+ for (let cur of cursors)
773
+ if (cur.value) {
774
+ this.heap.push(cur);
775
+ heapSink(this.heap, this.heap.length - 1, cmpCursor);
776
+ }
777
+ this.fill();
778
+ }
779
+ get side() {
780
+ return this.value ? this.value.side : 1e9;
781
+ }
782
+ fill() {
783
+ if (this.heap.length) {
784
+ ({ pos: this.pos, value: this.value } = this.heap[0]);
785
+ }
786
+ else {
787
+ this.pos = 1e9;
788
+ this.value = null;
789
+ }
790
+ }
791
+ goto(pos, side) {
792
+ this.heap = [];
793
+ for (let cur of this.cursors) {
794
+ cur.goto(pos, side);
795
+ if (cur.value) {
796
+ this.heap.push(cur);
797
+ heapSink(this.heap, this.heap.length - 1, cmpCursor);
798
+ }
799
+ }
800
+ }
801
+ next() {
802
+ if (this.heap.length) {
803
+ this.heap[0].next();
804
+ if (this.heap[0].value)
805
+ heapBubble(this.heap, 0, cmpCursor);
806
+ else
807
+ heapPop(this.heap, cmpCursor);
808
+ this.fill();
809
+ }
810
+ }
811
+ get set() { return this.heap.length ? this.heap[0].set : PointSet.empty; }
812
+ }
813
+
814
+ export { PointSet, RangeSet, findAbove };