13
13
14
14
package de .sciss .lucre .data
15
15
16
+ import de .sciss .lucre .data .HASkipList .Node
16
17
import de .sciss .lucre .stm .impl .MutableImpl
17
18
import de .sciss .lucre .stm .{Base , Ident , NewImmutSerializer , Sink , TxSerializer , Txn , Var }
18
19
import de .sciss .serial .{DataInput , DataOutput , Serializer }
@@ -81,32 +82,33 @@ object HASkipList {
81
82
override def toString = " HASkipList.Map.serializer"
82
83
}
83
84
84
- def debugFindLevel [T <: Txn [T ], A ](list : SkipList [T , A , _], key : A )(implicit tx : T ): Int = list match {
85
- case impl0 : Impl [T , A , _] => // wrong warning
86
- val h = impl0.height
87
-
88
- @ tailrec
89
- def stepRight [E ](impl : Impl [T , A , E ], n : Node [T , A , E ], lvl : Int ): Int = {
90
- val idx = impl.indexInNodeR(key, n)
91
- if (idx < 0 ) lvl
92
- else if (n.isLeaf) - 1
93
- else {
94
- val c = n.asBranch.down(idx)
95
- if (idx < n.size - 1 ) stepLeft(impl, c, lvl - 1 ) else stepRight(impl, c, lvl - 1 )
85
+ def debugFindLevel [T <: Txn [T ], A , E ](list : SkipList [T , A , E ], key : A )(implicit tx : T ): Int =
86
+ list match {
87
+ case impl0 : HASkipList [T , A , E ] =>
88
+ val h = impl0.height
89
+
90
+ @ tailrec
91
+ def stepRight (impl : HASkipList [T , A , E ], n : Node [T , A , E ], lvl : Int ): Int = {
92
+ val idx = impl.indexInNodeR(key, n)
93
+ if (idx < 0 ) lvl
94
+ else if (n.isLeaf) - 1
95
+ else {
96
+ val c = n.asBranch.down(idx)
97
+ if (idx < n.size - 1 ) stepLeft(impl, c, lvl - 1 ) else stepRight(impl, c, lvl - 1 )
98
+ }
96
99
}
97
- }
98
-
99
- @ tailrec
100
- def stepLeft [E ](impl : Impl [T , A , E ], n : Node [T , A , E ], lvl : Int ): Int = {
101
- val idx = impl.indexInNodeL(key, n)
102
- if (idx < 0 ) lvl else if (n.isLeaf) - 1 else stepLeft(impl, n.asBranch.down(idx), lvl - 1 )
103
- }
104
-
105
- val c = impl0.top.orNull // topN
106
- if (c eq null ) - 1 else stepRight(impl0, c, h)
107
-
108
- case _ => sys.error(s " Not a HA Skip List: $list" )
109
- }
100
+
101
+ @ tailrec
102
+ def stepLeft [E ](impl : HASkipList [T , A , E ], n : Node [T , A , E ], lvl : Int ): Int = {
103
+ val idx = impl.indexInNodeL(key, n)
104
+ if (idx < 0 ) lvl else if (n.isLeaf) - 1 else stepLeft(impl, n.asBranch.down(idx), lvl - 1 )
105
+ }
106
+
107
+ val c = impl0.top.orNull // topN
108
+ if (c eq null ) - 1 else stepRight(impl0, c, h)
109
+
110
+ case _ => sys.error(s " Not a HA Skip List: $list" )
111
+ }
110
112
111
113
private final class SetImpl [T <: Txn [T ], A ](val id : Ident [T ], val minGap : Int ,
112
114
protected val keyObserver : SkipList .KeyObserver [T , A ],
@@ -268,20 +270,14 @@ object HASkipList {
268
270
}
269
271
270
272
private sealed trait Impl [T <: Txn [T ], /* @spec(KeySpec) */ A , E ]
271
- extends HeadOrBranch [T , A , E ] with TxSerializer [T , Node [T , A , E ]] with MutableImpl [T ] {
273
+ extends HASkipList [ T , A , E ] with HeadOrBranch [T , A , E ] with TxSerializer [T , Node [T , A , E ]] with MutableImpl [T ] {
272
274
impl =>
273
275
274
276
// ---- abstract ----
275
277
276
278
protected def downNode : Var [T , Node [T , A , E ]]
277
- protected def minGap : Int
278
- protected def ordering : scala.Ordering [A ]
279
279
protected def keyObserver : SkipList .KeyObserver [T , A ]
280
280
281
- def keySerializer : NewImmutSerializer [A ]
282
-
283
- def id : Ident [T ]
284
-
285
281
def writeEntry (entry : E , out : DataOutput ): Unit
286
282
287
283
protected def newLeaf (entry : E ): Leaf [T , A , E ]
@@ -326,20 +322,20 @@ object HASkipList {
326
322
// }
327
323
}
328
324
329
- protected final def disposeData ()(implicit tx : T ): Unit =
325
+ protected override final def disposeData ()(implicit tx : T ): Unit =
330
326
downNode.dispose()
331
327
332
- def size (implicit tx : T ): Int = {
328
+ override def size (implicit tx : T ): Int = {
333
329
val c = topN
334
330
if (c eq null ) 0 else c.leafSizeSum - 1
335
331
}
336
332
337
- final def maxGap : Int = (minGap << 1 ) + 1 // aka arrMaxSz - 1
333
+ final override def maxGap : Int = (minGap << 1 ) + 1 // aka arrMaxSz - 1
338
334
339
- final def isEmpty (implicit tx : T ): Boolean = topN eq null
340
- final def nonEmpty (implicit tx : T ): Boolean = ! isEmpty
335
+ final override def isEmpty (implicit tx : T ): Boolean = topN eq null
336
+ final override def nonEmpty (implicit tx : T ): Boolean = ! isEmpty
341
337
342
- final def height (implicit tx : T ): Int = {
338
+ final override def height (implicit tx : T ): Int = {
343
339
var n = topN
344
340
if (n eq null ) 0
345
341
else {
@@ -356,12 +352,12 @@ object HASkipList {
356
352
357
353
@ inline protected final def topN (implicit tx : T ): Node [T , A , E ] = downNode()
358
354
359
- final def debugPrint ()(implicit tx : T ): String = topN.printNode(isRight = true ).mkString(" \n " )
355
+ final override def debugPrint ()(implicit tx : T ): String = topN.printNode(isRight = true ).mkString(" \n " )
360
356
361
- final def toIndexedSeq (implicit tx : T ): Vec [E ] = fillBuilder(Vector .newBuilder)
362
- final def toList (implicit tx : T ): List [E ] = fillBuilder(List .newBuilder)
363
- final def toSeq (implicit tx : T ): Seq [E ] = fillBuilder(Seq .newBuilder)
364
- final def toSet (implicit tx : T ): ISet [E ] = fillBuilder(ISet .newBuilder)
357
+ final override def toIndexedSeq (implicit tx : T ): Vec [E ] = fillBuilder(Vector .newBuilder)
358
+ final override def toList (implicit tx : T ): List [E ] = fillBuilder(List .newBuilder)
359
+ final override def toSeq (implicit tx : T ): Seq [E ] = fillBuilder(Seq .newBuilder)
360
+ final override def toSet (implicit tx : T ): ISet [E ] = fillBuilder(ISet .newBuilder)
365
361
366
362
private [this ] def fillBuilder [Res ](b : mutable.Builder [E , Res ])(implicit tx : T ): Res = {
367
363
val i = iterator
@@ -380,13 +376,13 @@ object HASkipList {
380
376
}
381
377
}
382
378
383
- final def head (implicit tx : T ): E = {
379
+ final override def head (implicit tx : T ): E = {
384
380
val n0 = topN
385
381
if (n0 eq null ) throw new NoSuchElementException (" head of empty list" )
386
382
else headImpl(n0)
387
383
}
388
384
389
- final def headOption (implicit tx : T ): Option [E ] = {
385
+ final override def headOption (implicit tx : T ): Option [E ] = {
390
386
val n0 = topN
391
387
if (n0 eq null ) None
392
388
else Some (headImpl(n0))
@@ -401,13 +397,13 @@ object HASkipList {
401
397
}
402
398
}
403
399
404
- final def last (implicit tx : T ): E = {
400
+ final override def last (implicit tx : T ): E = {
405
401
val n0 = topN
406
402
if (n0 eq null ) throw new NoSuchElementException (" last of empty list" )
407
403
else lastImpl(n0)
408
404
}
409
405
410
- final def lastOption (implicit tx : T ): Option [E ] = {
406
+ final override def lastOption (implicit tx : T ): Option [E ] = {
411
407
val n0 = topN
412
408
if (n0 eq null ) None
413
409
else Some (lastImpl(n0))
@@ -421,7 +417,7 @@ object HASkipList {
421
417
* @return if `Some`, holds the leaf and index for the floor element (whose key is <= the search key),
422
418
* if `None`, there is no key smaller than or equal to the search key in the list
423
419
*/
424
- final def floor (key : A )(implicit tx : T ): Option [E ] = {
420
+ final override def floor (key : A )(implicit tx : T ): Option [E ] = {
425
421
// the algorithm is as follows: find the index of the search key in the current level.
426
422
// if the key was found, just go down straight to the leaf. if not:
427
423
// - the index points to an element greater than the search key
@@ -479,7 +475,7 @@ object HASkipList {
479
475
* @return if `Some`, holds the leaf and index for the ceiling element (whose key is >= the search key),
480
476
* if `None`, there is no key greater than or equal to the search key in the list
481
477
*/
482
- final def ceil (key : A )(implicit tx : T ): Option [E ] = {
478
+ final override def ceil (key : A )(implicit tx : T ): Option [E ] = {
483
479
@ tailrec
484
480
def step (n : Node [T , A , E ], isRight : Boolean ): Option [E ] = {
485
481
val idx = if (isRight) indexInNodeR(key, n) else indexInNodeL(key, n)
@@ -495,7 +491,7 @@ object HASkipList {
495
491
if (c eq null ) None else step(c, isRight = true )
496
492
}
497
493
498
- final def isomorphicQuery (ord : Ordered [T , A ])(implicit tx : T ): (E , Int ) = {
494
+ final override def isomorphicQuery (ord : Ordered [T , A ])(implicit tx : T ): (E , Int ) = {
499
495
def isoIndexR (n : Node [T , A , E ]): Int = {
500
496
var idx = 0
501
497
val sz = n.size - 1
@@ -560,7 +556,7 @@ object HASkipList {
560
556
561
557
// ---- set support ----
562
558
563
- final def contains (v : A )(implicit tx : T ): Boolean = {
559
+ final override def contains (v : A )(implicit tx : T ): Boolean = {
564
560
@ tailrec
565
561
def stepRight (n : Node [T , A , E ]): Boolean = {
566
562
val idx = indexInNodeR(v, n)
@@ -582,15 +578,6 @@ object HASkipList {
582
578
if (c eq null ) false else stepRight(c)
583
579
}
584
580
585
- /** Finds the right-most key which
586
- * is greater than or equal to the query key.
587
- *
588
- * @param key the key to search for
589
- * @param n the branch or leaf from which to go down
590
- *
591
- * @return the index to go down (a node whose key is greater than `key`),
592
- * or `-(index+1)` if `key` was found at `index`
593
- */
594
581
final /* protected */ def indexInNodeR (key : A , n : Node [T , A , E ])(implicit tx : T ): Int = {
595
582
var idx = 0
596
583
val sz = n.size - 1
@@ -696,7 +683,7 @@ object HASkipList {
696
683
}
697
684
}
698
685
699
- final def -= (key : A )(implicit tx : T ): this .type = {
686
+ final override def -= (key : A )(implicit tx : T ): this .type = {
700
687
removeEntry(key); this
701
688
}
702
689
@@ -1064,9 +1051,11 @@ object HASkipList {
1064
1051
}
1065
1052
1066
1053
sealed trait Node [T <: Txn [T ], A , E ] {
1054
+
1067
1055
private [HASkipList ] def removeColumn (idx : Int )(implicit tx : T , list : Impl [T , A , E ]): Node [T , A , E ]
1068
1056
1069
1057
def size : Int
1058
+
1070
1059
def key (i : Int ): A
1071
1060
1072
1061
private [HASkipList ] def write (out : DataOutput )(implicit list : Impl [T , A , E ]): Unit
@@ -1130,34 +1119,35 @@ object HASkipList {
1130
1119
private final class SetLeaf [T <: Txn [T ], A ](private [HASkipList ] val entries : Vector [A ])
1131
1120
extends Leaf [T , A , A ] {
1132
1121
1133
- protected def copy (newEntries : Vector [A ]): Leaf [T , A , A ] = new SetLeaf (newEntries)
1122
+ protected override def copy (newEntries : Vector [A ]): Leaf [T , A , A ] = new SetLeaf (newEntries)
1134
1123
1135
- def key (idx : Int ): A = entries(idx)
1124
+ override def key (idx : Int ): A = entries(idx)
1136
1125
}
1137
1126
1138
1127
private final class MapLeaf [T <: Txn [T ], A , B ](private [HASkipList ] val entries : Vector [(A , B )])
1139
1128
extends Leaf [T , A , (A , B )] {
1140
1129
1141
- protected def copy (newEntries : Vector [(A , B )]): Leaf [T , A , (A , B )] = new MapLeaf (newEntries)
1130
+ protected override def copy (newEntries : Vector [(A , B )]): Leaf [T , A , (A , B )] = new MapLeaf (newEntries)
1142
1131
1143
- def key (idx : Int ): A = entries(idx)._1
1132
+ override def key (idx : Int ): A = entries(idx)._1
1144
1133
}
1145
1134
1146
1135
sealed trait Leaf [T <: Txn [T ], A , E ] extends Node [T , A , E ] {
1147
1136
override def toString : String = entries.mkString(" Leaf(" , " ," , " )" )
1148
1137
1149
1138
private [HASkipList ] def entries : Vector [E ]
1139
+
1150
1140
final def entry (idx : Int ): E = entries(idx)
1151
1141
1152
1142
protected def copy (newEntries : Vector [E ]): Leaf [T , A , E ]
1153
1143
1154
- final def size : Int = entries.size
1144
+ final override def size : Int = entries.size
1155
1145
1156
- final def isLeaf : Boolean = true
1157
- final def isBranch : Boolean = false
1146
+ final override def isLeaf : Boolean = true
1147
+ final override def isBranch : Boolean = false
1158
1148
1159
- final def asLeaf : Leaf [T , A , E ] = this
1160
- final def asBranch : Branch [T , A , E ] = opNotSupported
1149
+ final override def asLeaf : Leaf [T , A , E ] = this
1150
+ final override def asBranch : Branch [T , A , E ] = opNotSupported
1161
1151
1162
1152
private [HASkipList ] final def leafSizeSum (implicit tx : T ): Int = size
1163
1153
@@ -1264,11 +1254,11 @@ object HASkipList {
1264
1254
1265
1255
override def toString : String = keys.mkString(" Branch(" , " ," , " )" )
1266
1256
1267
- def isLeaf : Boolean = false
1268
- def isBranch : Boolean = true
1257
+ override def isLeaf : Boolean = false
1258
+ override def isBranch : Boolean = true
1269
1259
1270
- def asLeaf : Leaf [T , A , B ] = opNotSupported
1271
- def asBranch : Branch [T , A , B ] = this
1260
+ override def asLeaf : Leaf [T , A , B ] = opNotSupported
1261
+ override def asBranch : Branch [T , A , B ] = this
1272
1262
1273
1263
private [HASkipList ] def mergeRight (sib : Node [T , A , B ])(implicit tx : T ): Node [T , A , B ] = {
1274
1264
val bSib = sib.asBranch
@@ -1320,9 +1310,9 @@ object HASkipList {
1320
1310
}
1321
1311
}
1322
1312
1323
- def key (idx : Int ): A = keys(idx)
1313
+ override def key (idx : Int ): A = keys(idx)
1324
1314
1325
- def size : Int = keys.length
1315
+ override def size : Int = keys.length
1326
1316
1327
1317
private [HASkipList ] def downRef (i : Int ): Var [T , Node [T , A , B ]] = downs(i)
1328
1318
@@ -1453,9 +1443,7 @@ object HASkipList {
1453
1443
new SetSer [T , A ](keyObserver)
1454
1444
}
1455
1445
1456
- sealed trait Set [T <: Txn [T ], A ] extends SkipList .Set [T , A ] {
1457
- def top (implicit tx : T ): Option [HASkipList .Set .Node [T , A ]]
1458
- }
1446
+ trait Set [T <: Txn [T ], A ] extends SkipList .Set [T , A ] with HASkipList [T , A , A ]
1459
1447
1460
1448
object Map {
1461
1449
type Node [T <: Txn [T ], A , B ] = HASkipList .Node [T , A , (A , B )]
@@ -1526,7 +1514,22 @@ object HASkipList {
1526
1514
new MapSer [T , A , B ](keyObserver)
1527
1515
}
1528
1516
1529
- sealed trait Map [T <: Txn [T ], /* @spec(KeySpec) */ A , /* @spec(ValueSpec) */ B ] extends SkipList .Map [T , A , B ] {
1530
- def top (implicit tx : T ): Option [HASkipList .Node [T , A , (A , B )]]
1531
- }
1517
+ trait Map [T <: Txn [T ], /* @spec(KeySpec) */ A , /* @spec(ValueSpec) */ B ]
1518
+ extends SkipList .Map [T , A , B ] with HASkipList [T , A , (A , B )]
1519
+ }
1520
+ trait HASkipList [T <: Txn [T ], A , E ] extends SkipList [T , A , E ] {
1521
+ def top (implicit tx : T ): Option [HASkipList .Node [T , A , E ]]
1522
+
1523
+ /** Finds the right-most key which
1524
+ * is greater than or equal to the query key.
1525
+ *
1526
+ * @param key the key to search for
1527
+ * @param n the branch or leaf from which to go down
1528
+ *
1529
+ * @return the index to go down (a node whose key is greater than `key`),
1530
+ * or `-(index+1)` if `key` was found at `index`
1531
+ */
1532
+ def indexInNodeR (key : A , n : HASkipList .Node [T , A , E ])(implicit tx : T ): Int
1533
+
1534
+ def indexInNodeL (key : A , n : HASkipList .Node [T , A , E ])(implicit tx : T ): Int
1532
1535
}
0 commit comments