11import { printTree } from 'tree-dump/lib/printTree' ;
22import { printBinary } from 'tree-dump/lib/printBinary' ;
33import { first , insertLeft , insertRight , last , next , prev , remove } from 'sonic-forest/lib/util' ;
4+ import { first2 , insert2 , next2 , remove2 } from 'sonic-forest/lib/util2' ;
45import { splay } from 'sonic-forest/lib/splay/util' ;
56import { Anchor } from '../rga/constants' ;
67import { Point } from '../rga/Point' ;
@@ -19,6 +20,9 @@ import type {Printable} from 'tree-dump/lib/types';
1920import type { MutableSlice , Slice } from '../slice/types' ;
2021import type { Slices } from '../slice/Slices' ;
2122import type { OverlayPair , OverlayTuple } from './types' ;
23+ import type { Comparator } from 'sonic-forest/lib/types' ;
24+
25+ const spatialComparator : Comparator < OverlayPoint > = ( a : OverlayPoint , b : OverlayPoint ) => a . cmpSpatial ( b ) ;
2226
2327/**
2428 * Overlay is a tree structure that represents all the intersections of slices
@@ -29,6 +33,7 @@ import type {OverlayPair, OverlayTuple} from './types';
2933 */
3034export class Overlay < T = string > implements Printable , Stateful {
3135 public root : OverlayPoint < T > | undefined = undefined ;
36+ public root2 : MarkerOverlayPoint < T > | undefined = undefined ;
3237
3338 /** A virtual absolute start point, used when the absolute start is missing. */
3439 public readonly START : OverlayPoint < T > ;
@@ -171,20 +176,17 @@ export class Overlay<T = string> implements Printable, Stateful {
171176 return new UndefEndIter ( this . points0 ( after ) ) ;
172177 }
173178
174- public markers0 ( ) : UndefIterator < MarkerOverlayPoint < T > > {
175- let curr = this . first ( ) ;
179+ public markers0 ( after : undefined | MarkerOverlayPoint < T > ) : UndefIterator < MarkerOverlayPoint < T > > {
180+ let curr = after ? next2 ( after ) : first2 ( this . root2 ) ;
176181 return ( ) => {
177- while ( curr ) {
178- const ret = curr ;
179- if ( curr ) curr = next ( curr ) ;
180- if ( ret instanceof MarkerOverlayPoint ) return ret ;
181- }
182- return ;
182+ const ret = curr ;
183+ if ( curr ) curr = next2 ( curr ) ;
184+ return ret ;
183185 } ;
184186 }
185187
186188 public markers ( ) : IterableIterator < MarkerOverlayPoint < T > > {
187- return new UndefEndIter ( this . markers0 ( ) ) ;
189+ return new UndefEndIter ( this . markers0 ( undefined ) ) ;
188190 }
189191
190192 public pairs0 ( after : undefined | OverlayPoint < T > ) : UndefIterator < OverlayPair < T > > {
@@ -404,6 +406,7 @@ export class Overlay<T = string> implements Printable, Stateful {
404406 }
405407
406408 private insMarker ( slice : MarkerSlice < T > ) : [ start : OverlayPoint < T > , end : OverlayPoint < T > ] {
409+ // TODO: When marker is at rel start, and cursor too, the overlay point should have reference to the cursor.
407410 const point = this . mPoint ( slice , Anchor . Before ) ;
408411 const pivot = this . insPoint ( point ) ;
409412 if ( ! pivot ) {
@@ -445,6 +448,10 @@ export class Overlay<T = string> implements Printable, Stateful {
445448 * @returns Returns the existing point if it was already in the tree.
446449 */
447450 private insPoint ( point : OverlayPoint < T > ) : OverlayPoint < T > | undefined {
451+ if ( point instanceof MarkerOverlayPoint ) {
452+ this . root2 = insert2 ( this . root2 , point , spatialComparator ) ;
453+ // if (this.root2 !== point) this.root2 = splay2(this.root2!, point, 10);
454+ }
448455 let pivot = this . getOrNextLower ( point ) ;
449456 if ( ! pivot ) pivot = first ( this . root ) ;
450457 if ( ! pivot ) {
@@ -461,6 +468,8 @@ export class Overlay<T = string> implements Printable, Stateful {
461468 }
462469
463470 private delPoint ( point : OverlayPoint < T > ) : void {
471+ if ( point instanceof MarkerOverlayPoint )
472+ this . root2 = remove2 ( this . root2 , point ) ;
464473 this . root = remove ( this . root , point ) ;
465474 }
466475
@@ -519,9 +528,21 @@ export class Overlay<T = string> implements Printable, Stateful {
519528 ] )
520529 ) ;
521530 } ;
531+ const printMarkerPoint = ( tab : string , point : MarkerOverlayPoint < T > ) : string => {
532+ return (
533+ point . toString ( tab ) +
534+ printBinary ( tab , [
535+ ! point . l2 ? null : ( tab ) => printMarkerPoint ( tab , point . l2 ! ) ,
536+ ! point . r2 ? null : ( tab ) => printMarkerPoint ( tab , point . r2 ! ) ,
537+ ] )
538+ ) ;
539+ } ;
522540 return (
523541 `${ this . constructor . name } #${ this . hash . toString ( 36 ) } ` +
524- printTree ( tab , [ ! this . root ? null : ( tab ) => printPoint ( tab , this . root ! ) ] )
542+ printTree ( tab , [
543+ ! this . root ? null : ( tab ) => printPoint ( tab , this . root ! ) ,
544+ ! this . root2 ? null : ( tab ) => printMarkerPoint ( tab , this . root2 ! ) ,
545+ ] )
525546 ) ;
526547 }
527548}
0 commit comments