|
| 1 | +export function indordseq(empty, key, size, measure) { |
| 2 | + // Probably needs a total order instead of a measure |
| 3 | + // this causes generality problems in the insert, merge, partition and delete |
| 4 | + // methods |
1 | 5 |
|
2 |
| -export function indordseq ( empty , key , size , measure ) { |
| 6 | + const IndOrdSeq = function (tree) { |
| 7 | + this.tree = tree; |
| 8 | + }; |
3 | 9 |
|
4 |
| -// probably needs a total order instead of a measure |
5 |
| -// this causes generality problems in the insert, merge, partition and delete |
6 |
| -// methods |
| 10 | + IndOrdSeq.prototype.isEmpty = function () { |
| 11 | + return this.tree.isEmpty(); |
| 12 | + }; |
7 | 13 |
|
8 |
| -const IndOrdSeq = function ( tree ) { |
9 |
| - this.tree = tree ; |
10 |
| -} ; |
| 14 | + IndOrdSeq.prototype.measure = function () { |
| 15 | + return this.tree.measure(); |
| 16 | + }; |
11 | 17 |
|
12 |
| -IndOrdSeq.prototype.isEmpty = function ( ) { |
13 |
| - return this.tree.isEmpty( ) ; |
14 |
| -} ; |
| 18 | + IndOrdSeq.prototype.min = IndOrdSeq.prototype.head = function () { |
| 19 | + return this.tree.head(); |
| 20 | + }; |
15 | 21 |
|
16 |
| -IndOrdSeq.prototype.measure = function ( ) { |
17 |
| - return this.tree.measure( ) ; |
18 |
| -} ; |
| 22 | + IndOrdSeq.prototype.tail = function () { |
| 23 | + return new IndOrdSeq(this.tree.tail()); |
| 24 | + }; |
19 | 25 |
|
20 |
| -IndOrdSeq.prototype.min = |
21 |
| -IndOrdSeq.prototype.head = function ( ) { |
22 |
| - return this.tree.head( ) ; |
23 |
| -} ; |
| 26 | + IndOrdSeq.prototype.max = IndOrdSeq.prototype.last = function () { |
| 27 | + return this.tree.last(); |
| 28 | + }; |
24 | 29 |
|
25 |
| -IndOrdSeq.prototype.tail = function ( ) { |
26 |
| - return new IndOrdSeq( this.tree.tail( ) ) ; |
27 |
| -} ; |
| 30 | + IndOrdSeq.prototype.init = function () { |
| 31 | + return new IndOrdSeq(this.tree.init()); |
| 32 | + }; |
28 | 33 |
|
29 |
| -IndOrdSeq.prototype.max = |
30 |
| -IndOrdSeq.prototype.last = function ( ) { |
31 |
| - return this.tree.last( ) ; |
32 |
| -} ; |
| 34 | + IndOrdSeq.prototype.takeUntil = function (predicate) { |
| 35 | + return new IndOrdSeq(this.tree.takeUntil(predicate)); |
| 36 | + }; |
33 | 37 |
|
34 |
| -IndOrdSeq.prototype.init = function ( ) { |
35 |
| - return new IndOrdSeq( this.tree.init( ) ) ; |
36 |
| -} ; |
| 38 | + IndOrdSeq.prototype.dropUntil = function (predicate) { |
| 39 | + return new IndOrdSeq(this.tree.dropUntil(predicate)); |
| 40 | + }; |
37 | 41 |
|
38 |
| -IndOrdSeq.prototype.takeUntil = function ( predicate ) { |
39 |
| - return new IndOrdSeq( this.tree.takeUntil( predicate ) ) ; |
40 |
| -} ; |
| 42 | + IndOrdSeq.prototype[Symbol.iterator] = function () { |
| 43 | + return this.tree[Symbol.iterator](); |
| 44 | + }; |
41 | 45 |
|
42 |
| -IndOrdSeq.prototype.dropUntil = function ( predicate ) { |
43 |
| - return new IndOrdSeq( this.tree.dropUntil( predicate ) ) ; |
44 |
| -} ; |
| 46 | + IndOrdSeq.prototype.split = function (predicate) { |
| 47 | + const [left, right] = this.tree.split(predicate); |
| 48 | + return [new IndOrdSeq(left), new IndOrdSeq(right)]; |
| 49 | + }; |
45 | 50 |
|
46 |
| -IndOrdSeq.prototype[Symbol.iterator] = function ( ) { |
47 |
| - return this.tree[Symbol.iterator]( ) ; |
48 |
| -} ; |
| 51 | + IndOrdSeq.prototype.partition = function (value) { |
| 52 | + const k = key.measure(value); |
| 53 | + return this.split((m) => m[0] >= k); |
| 54 | + }; |
49 | 55 |
|
50 |
| -IndOrdSeq.prototype.split = function ( predicate ) { |
51 |
| - const [ left , right ] = this.tree.split( predicate ) ; |
52 |
| - return [ new IndOrdSeq( left ) , new IndOrdSeq( right ) ] ; |
53 |
| -} ; |
| 56 | + IndOrdSeq.prototype.insert = function (value) { |
| 57 | + const k = key.measure(value); |
| 58 | + const [left, right] = this.tree.split((m) => m[0] >= k); |
| 59 | + return new IndOrdSeq(left.push(value).concat(right)); |
| 60 | + }; |
54 | 61 |
|
55 |
| -IndOrdSeq.prototype.partition = function ( value ) { |
56 |
| - const k = key.measure( value ) ; |
57 |
| - return this.split( m => m[0] >= k ) ; |
58 |
| -} ; |
| 62 | + IndOrdSeq.prototype.deleteAll = function (value) { |
| 63 | + const k = key.measure(value); |
| 64 | + const [l, r] = this.tree.split((m) => m[0] >= k); |
| 65 | + const [, R] = r.split((m) => m[0] > k); |
| 66 | + return new IndOrdSeq(l.concat(R)); |
| 67 | + }; |
59 | 68 |
|
60 |
| -IndOrdSeq.prototype.insert = function ( value ) { |
61 |
| - const k = key.measure( value ) ; |
62 |
| - const [ left , right ] = this.tree.split( m => m[0] >= k ) ; |
63 |
| - return new IndOrdSeq( left.push( value ).concat( right ) ) ; |
64 |
| -} ; |
| 69 | + IndOrdSeq.prototype.merge = function (other) { |
| 70 | + if (other.isEmpty()) return this; |
65 | 71 |
|
66 |
| -IndOrdSeq.prototype.deleteAll = function ( value ) { |
67 |
| - const k = key.measure( value ) ; |
68 |
| - const [ l , r ] = this.tree.split( m => m[0] >= k ) ; |
69 |
| - const [ _ , R ] = r.split( m => m[0] > k ) ; |
70 |
| - return new IndOrdSeq( l.concat( R ) ) ; |
71 |
| -} ; |
| 72 | + const a = other.head(); |
| 73 | + const k = key.measure(a); |
72 | 74 |
|
73 |
| -IndOrdSeq.prototype.merge = function ( other ) { |
| 75 | + const [l, r] = this.split((m) => m[0] > k); |
74 | 76 |
|
75 |
| - if ( other.isEmpty( ) ) return this ; |
| 77 | + return new IndOrdSeq(l.tree.push(a).concat(r.merge(other.tail()).tree)); |
| 78 | + }; |
76 | 79 |
|
77 |
| - const a = other.head( ) ; |
78 |
| - const k = key.measure( a ) ; |
| 80 | + IndOrdSeq.prototype.insertValues = function (values) { |
| 81 | + let s = this; |
79 | 82 |
|
80 |
| - const [ l , r ] = this.split( m => m[0] > k ) ; |
| 83 | + for (const value of values) s = s.insert(value); |
81 | 84 |
|
82 |
| - return new IndOrdSeq( l.tree.push( a ).concat( r.merge( other.tail( ) ).tree ) ) ; |
| 85 | + return s; |
| 86 | + }; |
83 | 87 |
|
84 |
| -} ; |
| 88 | + IndOrdSeq.prototype.len = function () { |
| 89 | + return this.tree.measure()[1]; |
| 90 | + }; |
85 | 91 |
|
86 |
| -IndOrdSeq.prototype.insertValues = function ( values ) { |
| 92 | + IndOrdSeq.prototype.get = function (index) { |
| 93 | + if (index < 0 || index >= this.len()) |
| 94 | + throw new Error(`wrong index '${index}'`); |
87 | 95 |
|
88 |
| - let s = this ; |
| 96 | + return this.tree.splitTree((m) => m[1] > index, measure.zero()).middle; |
| 97 | + }; |
89 | 98 |
|
90 |
| - for ( const value of values ) s = s.insert( value ) ; |
91 |
| - |
92 |
| - return s ; |
93 |
| - |
94 |
| -} ; |
95 |
| - |
96 |
| -IndOrdSeq.prototype.len = function ( ) { |
97 |
| - return this.tree.measure( )[1] ; |
98 |
| -} ; |
99 |
| - |
100 |
| -IndOrdSeq.prototype.get = function ( index ) { |
101 |
| - |
102 |
| - if ( index < 0 || index >= this.len( ) ) throw new Error( `wrong index '${index}'` ) ; |
103 |
| - |
104 |
| - return this.tree.splitTree( m => m[1] > index , measure.zero( ) ).middle ; |
105 |
| - |
106 |
| -} ; |
107 |
| - |
108 |
| -IndOrdSeq.prototype.splitAt = function ( index ) { |
109 |
| - return this.split( m => m[1] > index ) ; |
110 |
| -} ; |
111 |
| - |
112 |
| -return { |
113 |
| - empty : ( ) => new IndOrdSeq( empty( measure ) ) , |
114 |
| - from : ( iterable ) => new IndOrdSeq( empty( measure ) ).insertValues( iterable ) |
115 |
| -} ; |
| 99 | + IndOrdSeq.prototype.splitAt = function (index) { |
| 100 | + return this.split((m) => m[1] > index); |
| 101 | + }; |
116 | 102 |
|
| 103 | + return { |
| 104 | + empty: () => new IndOrdSeq(empty(measure)), |
| 105 | + from: (iterable) => new IndOrdSeq(empty(measure)).insertValues(iterable), |
| 106 | + }; |
117 | 107 | }
|
0 commit comments