Skip to content

Commit 216877c

Browse files
committed
Refine .nn usages
1 parent b098cf2 commit 216877c

File tree

13 files changed

+117
-107
lines changed

13 files changed

+117
-107
lines changed

library/src/scala/collection/convert/impl/BinaryTreeStepper.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private[collection] object BinaryTreeStepper {
4848
*/
4949
private[collection] abstract class BinaryTreeStepperBase[A, T <: AnyRef, Sub, Semi <: Sub with BinaryTreeStepperBase[A, T, _, _]](
5050
protected var maxLength: Int, protected var myCurrent: T | Null, protected var stack: Array[AnyRef | Null], protected var index: Int,
51-
protected val left: T => T, protected val right: T => T
51+
protected val left: T => T | Null, protected val right: T => T | Null
5252
)
5353
extends EfficientSplit {
5454
/** Unrolls a subtree onto the stack starting from a particular node, returning
@@ -144,7 +144,7 @@ extends EfficientSplit {
144144

145145

146146
private[collection] final class AnyBinaryTreeStepper[A, T <: AnyRef](
147-
_maxLength: Int, _myCurrent: T | Null, _stack: Array[AnyRef | Null], _index: Int, _left: T => T, _right: T => T, protected val extract: T => A
147+
_maxLength: Int, _myCurrent: T | Null, _stack: Array[AnyRef | Null], _index: Int, _left: T => T | Null, _right: T => T | Null, protected val extract: T => A
148148
)
149149
extends BinaryTreeStepperBase[A, T, AnyStepper[A], AnyBinaryTreeStepper[A, T]](_maxLength, _myCurrent, _stack, _index, _left, _right)
150150
with AnyStepper[A] {
@@ -161,7 +161,7 @@ with AnyStepper[A] {
161161
new AnyBinaryTreeStepper[A, T](maxL, myC, stk, ix, left, right, extract)
162162
}
163163
private[collection] object AnyBinaryTreeStepper {
164-
def from[A, T <: AnyRef](maxLength: Int, root: T | Null, left: T => T, right: T => T, extract: T => A): AnyBinaryTreeStepper[A, T] = {
164+
def from[A, T <: AnyRef](maxLength: Int, root: T | Null, left: T => T | Null, right: T => T | Null, extract: T => A): AnyBinaryTreeStepper[A, T] = {
165165
val ans = new AnyBinaryTreeStepper(0, null, BinaryTreeStepper.emptyStack, -1, left, right, extract)
166166
ans.initialize(root, maxLength)
167167
ans
@@ -170,7 +170,7 @@ private[collection] object AnyBinaryTreeStepper {
170170

171171

172172
private[collection] final class DoubleBinaryTreeStepper[T <: AnyRef](
173-
_maxLength: Int, _myCurrent: T | Null, _stack: Array[AnyRef | Null], _index: Int, _left: T => T, _right: T => T, protected val extract: T => Double
173+
_maxLength: Int, _myCurrent: T | Null, _stack: Array[AnyRef | Null], _index: Int, _left: T => T | Null, _right: T => T | Null, protected val extract: T => Double
174174
)
175175
extends BinaryTreeStepperBase[Double, T, DoubleStepper, DoubleBinaryTreeStepper[T]](_maxLength, _myCurrent, _stack, _index, _left, _right)
176176
with DoubleStepper {
@@ -187,7 +187,7 @@ with DoubleStepper {
187187
new DoubleBinaryTreeStepper[T](maxL, myC, stk, ix, left, right, extract)
188188
}
189189
private [collection] object DoubleBinaryTreeStepper {
190-
def from[T <: AnyRef](maxLength: Int, root: T | Null, left: T => T, right: T => T, extract: T => Double): DoubleBinaryTreeStepper[T] = {
190+
def from[T <: AnyRef](maxLength: Int, root: T | Null, left: T => T | Null, right: T => T | Null, extract: T => Double): DoubleBinaryTreeStepper[T] = {
191191
val ans = new DoubleBinaryTreeStepper(0, null, BinaryTreeStepper.emptyStack, -1, left, right, extract)
192192
ans.initialize(root, maxLength)
193193
ans
@@ -196,7 +196,7 @@ private [collection] object DoubleBinaryTreeStepper {
196196

197197

198198
private[collection] final class IntBinaryTreeStepper[T <: AnyRef](
199-
_maxLength: Int, _myCurrent: T | Null, _stack: Array[AnyRef | Null], _index: Int, _left: T => T, _right: T => T, protected val extract: T => Int
199+
_maxLength: Int, _myCurrent: T | Null, _stack: Array[AnyRef | Null], _index: Int, _left: T => T | Null, _right: T => T | Null, protected val extract: T => Int
200200
)
201201
extends BinaryTreeStepperBase[Int, T, IntStepper, IntBinaryTreeStepper[T]](_maxLength, _myCurrent, _stack, _index, _left, _right)
202202
with IntStepper {
@@ -213,7 +213,7 @@ with IntStepper {
213213
new IntBinaryTreeStepper[T](maxL, myC, stk, ix, left, right, extract)
214214
}
215215
private [collection] object IntBinaryTreeStepper {
216-
def from[T <: AnyRef](maxLength: Int, root: T | Null, left: T => T, right: T => T, extract: T => Int): IntBinaryTreeStepper[T] = {
216+
def from[T <: AnyRef](maxLength: Int, root: T | Null, left: T => T | Null, right: T => T | Null, extract: T => Int): IntBinaryTreeStepper[T] = {
217217
val ans = new IntBinaryTreeStepper(0, null, BinaryTreeStepper.emptyStack, -1, left, right, extract)
218218
ans.initialize(root, maxLength)
219219
ans
@@ -223,7 +223,7 @@ private [collection] object IntBinaryTreeStepper {
223223

224224

225225
private[collection] final class LongBinaryTreeStepper[T <: AnyRef](
226-
_maxLength: Int, _myCurrent: T | Null, _stack: Array[AnyRef | Null], _index: Int, _left: T => T, _right: T => T, protected val extract: T => Long
226+
_maxLength: Int, _myCurrent: T | Null, _stack: Array[AnyRef | Null], _index: Int, _left: T => T | Null, _right: T => T | Null, protected val extract: T => Long
227227
)
228228
extends BinaryTreeStepperBase[Long, T, LongStepper, LongBinaryTreeStepper[T]](_maxLength, _myCurrent, _stack, _index, _left, _right)
229229
with LongStepper {
@@ -240,7 +240,7 @@ with LongStepper {
240240
new LongBinaryTreeStepper[T](maxL, myC, stk, ix, left, right, extract)
241241
}
242242
private [collection] object LongBinaryTreeStepper {
243-
def from[T <: AnyRef](maxLength: Int, root: T | Null, left: T => T, right: T => T, extract: T => Long): LongBinaryTreeStepper[T] = {
243+
def from[T <: AnyRef](maxLength: Int, root: T | Null, left: T => T | Null, right: T => T | Null, extract: T => Long): LongBinaryTreeStepper[T] = {
244244
val ans = new LongBinaryTreeStepper(0, null, BinaryTreeStepper.emptyStack, -1, left, right, extract)
245245
ans.initialize(root, maxLength)
246246
ans

library/src/scala/collection/immutable/RedBlackTree.scala

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,26 +1097,26 @@ private[collection] object RedBlackTree {
10971097
if (tl eq null) tr
10981098
else if (tr eq null) tl
10991099
else if (tl.isRed) {
1100-
if (tr.isRed) {
1101-
//tl is red, tr is red
1102-
val bc = append(tl.right, tr.left)
1103-
if (isRedTree(bc)) bc.nn.withLeftRight(tl.withRight(bc.nn.left), tr.withLeft(bc.nn.right))
1104-
else tl.withRight(tr.withLeft(bc))
1105-
} else {
1106-
//tl is red, tr is black
1107-
tl.withRight(append(tl.right, tr))
1108-
}
1109-
} else {
1110-
if (tr.isBlack) {
1111-
//tl is black tr is black
1112-
val bc = append(tl.right, tr.left)
1113-
if (isRedTree(bc)) bc.nn.withLeftRight(tl.withRight(bc.nn.left), tr.withLeft(bc.nn.right))
1114-
else balLeft(tl, tl.left, tr.withLeft(bc))
1115-
} else {
1116-
//tl is black tr is red
1117-
tr.withLeft(append(tl, tr.left))
1118-
}
1119-
}
1100+
if (tr.isRed) {
1101+
//tl is red, tr is red
1102+
val bc = append(tl.right, tr.left)
1103+
if (isRedTree(bc)) bc.nn.withLeftRight(tl.withRight(bc.nn.left), tr.withLeft(bc.nn.right))
1104+
else tl.withRight(tr.withLeft(bc))
1105+
} else {
1106+
//tl is red, tr is black
1107+
tl.withRight(append(tl.right, tr))
1108+
}
1109+
} else {
1110+
if (tr.isBlack) {
1111+
//tl is black tr is black
1112+
val bc = append(tl.right, tr.left)
1113+
if (isRedTree(bc)) bc.nn.withLeftRight(tl.withRight(bc.nn.left), tr.withLeft(bc.nn.right))
1114+
else balLeft(tl, tl.left, tr.withLeft(bc))
1115+
} else {
1116+
//tl is black tr is red
1117+
tr.withLeft(append(tl, tr.left))
1118+
}
1119+
}
11201120
}
11211121

11221122

library/src/scala/collection/immutable/TreeMap.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ final class TreeMap[K, +V] private (private val tree: RB.Tree[K, V] | Null)(impl
100100
override def stepper[S <: Stepper[_]](implicit shape: StepperShape[(K, V), S]): S with EfficientSplit =
101101
shape.parUnbox(
102102
scala.collection.convert.impl.AnyBinaryTreeStepper.from[(K, V), RB.Tree[K, V]](
103-
size, tree, _.left.nn, _.right.nn, x => (x.key, x.value)
103+
size, tree, _.left, _.right, x => (x.key, x.value)
104104
)
105105
)
106106

107107
override def keyStepper[S <: Stepper[_]](implicit shape: StepperShape[K, S]): S with EfficientSplit = {
108108
import scala.collection.convert.impl._
109109
type T = RB.Tree[K, V]
110110
val s = shape.shape match {
111-
case StepperShape.IntShape => IntBinaryTreeStepper.from[T] (size, tree, _.left.nn, _.right.nn, _.key.asInstanceOf[Int])
112-
case StepperShape.LongShape => LongBinaryTreeStepper.from[T] (size, tree, _.left.nn, _.right.nn, _.key.asInstanceOf[Long])
113-
case StepperShape.DoubleShape => DoubleBinaryTreeStepper.from[T](size, tree, _.left.nn, _.right.nn, _.key.asInstanceOf[Double])
114-
case _ => shape.parUnbox(AnyBinaryTreeStepper.from[K, T](size, tree, _.left.nn, _.right.nn, _.key))
111+
case StepperShape.IntShape => IntBinaryTreeStepper.from[T] (size, tree, _.left, _.right, _.key.asInstanceOf[Int])
112+
case StepperShape.LongShape => LongBinaryTreeStepper.from[T] (size, tree, _.left, _.right, _.key.asInstanceOf[Long])
113+
case StepperShape.DoubleShape => DoubleBinaryTreeStepper.from[T](size, tree, _.left, _.right, _.key.asInstanceOf[Double])
114+
case _ => shape.parUnbox(AnyBinaryTreeStepper.from[K, T](size, tree, _.left, _.right, _.key))
115115
}
116116
s.asInstanceOf[S with EfficientSplit]
117117
}
@@ -120,10 +120,10 @@ final class TreeMap[K, +V] private (private val tree: RB.Tree[K, V] | Null)(impl
120120
import scala.collection.convert.impl._
121121
type T = RB.Tree[K, V]
122122
val s = shape.shape match {
123-
case StepperShape.IntShape => IntBinaryTreeStepper.from[T] (size, tree, _.left.nn, _.right.nn, _.value.asInstanceOf[Int])
124-
case StepperShape.LongShape => LongBinaryTreeStepper.from[T] (size, tree, _.left.nn, _.right.nn, _.value.asInstanceOf[Long])
125-
case StepperShape.DoubleShape => DoubleBinaryTreeStepper.from[T] (size, tree, _.left.nn, _.right.nn, _.value.asInstanceOf[Double])
126-
case _ => shape.parUnbox(AnyBinaryTreeStepper.from[V, T] (size, tree, _.left.nn, _.right.nn, _.value.asInstanceOf[V]))
123+
case StepperShape.IntShape => IntBinaryTreeStepper.from[T] (size, tree, _.left, _.right, _.value.asInstanceOf[Int])
124+
case StepperShape.LongShape => LongBinaryTreeStepper.from[T] (size, tree, _.left, _.right, _.value.asInstanceOf[Long])
125+
case StepperShape.DoubleShape => DoubleBinaryTreeStepper.from[T] (size, tree, _.left, _.right, _.value.asInstanceOf[Double])
126+
case _ => shape.parUnbox(AnyBinaryTreeStepper.from[V, T] (size, tree, _.left, _.right, _.value.asInstanceOf[V]))
127127
}
128128
s.asInstanceOf[S with EfficientSplit]
129129
}

library/src/scala/collection/immutable/TreeSet.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ final class TreeSet[A] private[immutable] (private[immutable] val tree: RB.Tree[
138138
import scala.collection.convert.impl._
139139
type T = RB.Tree[A, Any]
140140
val s = shape.shape match {
141-
case StepperShape.IntShape => IntBinaryTreeStepper.from[T] (size, tree, _.left.nn, _.right.nn, _.key.asInstanceOf[Int])
142-
case StepperShape.LongShape => LongBinaryTreeStepper.from[T] (size, tree, _.left.nn, _.right.nn, _.key.asInstanceOf[Long])
143-
case StepperShape.DoubleShape => DoubleBinaryTreeStepper.from[T](size, tree, _.left.nn, _.right.nn, _.key.asInstanceOf[Double])
144-
case _ => shape.parUnbox(AnyBinaryTreeStepper.from[A, T](size, tree, _.left.nn, _.right.nn, _.key))
141+
case StepperShape.IntShape => IntBinaryTreeStepper.from[T] (size, tree, _.left, _.right, _.key.asInstanceOf[Int])
142+
case StepperShape.LongShape => LongBinaryTreeStepper.from[T] (size, tree, _.left, _.right, _.key.asInstanceOf[Long])
143+
case StepperShape.DoubleShape => DoubleBinaryTreeStepper.from[T](size, tree, _.left, _.right, _.key.asInstanceOf[Double])
144+
case _ => shape.parUnbox(AnyBinaryTreeStepper.from[A, T](size, tree, _.left, _.right, _.key))
145145
}
146146
s.asInstanceOf[S with EfficientSplit]
147147
}

library/src/scala/collection/mutable/CollisionProofHashMap.scala

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ final class CollisionProofHashMap[K, V](initialCapacity: Int, loadFactor: Double
564564
}
565565
else if (z.right eq null) {
566566
x = z.left
567-
root = transplant(root, z, z.left.nn)
567+
root = transplant(root, z, z.left)
568568
xParent = z.parent
569569
}
570570
else {
571-
y = CollisionProofHashMap.minNodeNonNull(z.right.nn)
571+
y = CollisionProofHashMap.minNodeNonNull(z.right)
572572
yIsRed = y.red
573573
x = y.right
574574

@@ -663,7 +663,7 @@ final class CollisionProofHashMap[K, V](initialCapacity: Int, loadFactor: Double
663663
x.right = y.left
664664

665665
val xp = x.parent
666-
if (y.left ne null) y.left.nn.parent = x
666+
if (y.left ne null) y.left.parent = x
667667
y.parent = xp
668668

669669
if (xp eq null) root = y
@@ -681,7 +681,7 @@ final class CollisionProofHashMap[K, V](initialCapacity: Int, loadFactor: Double
681681
x.left = y.right
682682

683683
val xp = x.parent
684-
if (y.right ne null) y.right.nn.parent = x
684+
if (y.right ne null) y.right.parent = x
685685
y.parent = xp
686686

687687
if (xp eq null) root = y
@@ -700,8 +700,8 @@ final class CollisionProofHashMap[K, V](initialCapacity: Int, loadFactor: Double
700700
private[this] def transplant(_root: RBNode, to: RBNode, from: RBNode): RBNode = {
701701
var root = _root
702702
if (to.parent eq null) root = from
703-
else if (to eq to.parent.nn.left) to.parent.nn.left = from
704-
else to.parent.nn.right = from
703+
else if (to eq to.parent.left) to.parent.left = from
704+
else to.parent.right = from
705705
if (from ne null) from.parent = to.parent
706706
root
707707
}
@@ -791,53 +791,61 @@ object CollisionProofHashMap extends SortedMapFactory[CollisionProofHashMap] {
791791

792792
/////////////////////////// Red-Black Tree Node
793793

794-
final class RBNode[K, V](var key: K, var hash: Int, var value: V, var red: Boolean, var left: RBNode[K, V] | Null, var right: RBNode[K, V] | Null, var parent: RBNode[K, V] | Null) extends Node {
794+
final class RBNode[K, V](
795+
var key: K, var hash: Int, var value: V, var red: Boolean,
796+
@annotation.stableNull
797+
var left: RBNode[K, V] | Null,
798+
@annotation.stableNull
799+
var right: RBNode[K, V] | Null,
800+
@annotation.stableNull
801+
var parent: RBNode[K, V] | Null
802+
) extends Node {
795803
override def toString: String = "RBNode(" + key + ", " + hash + ", " + value + ", " + red + ", " + left + ", " + right + ")"
796804

797805
@tailrec def getNode(k: K, h: Int)(implicit ord: Ordering[K]): RBNode[K, V] | Null = {
798806
val cmp = compare(k, h, this)
799807
if (cmp < 0) {
800-
if(left ne null) left.nn.getNode(k, h) else null
808+
if(left ne null) left.getNode(k, h) else null
801809
} else if (cmp > 0) {
802-
if(right ne null) right.nn.getNode(k, h) else null
810+
if(right ne null) right.getNode(k, h) else null
803811
} else this
804812
}
805813

806814
def foreach[U](f: ((K, V)) => U): Unit = {
807-
if(left ne null) left.nn.foreach(f)
815+
if(left ne null) left.foreach(f)
808816
f((key, value))
809-
if(right ne null) right.nn.foreach(f)
817+
if(right ne null) right.foreach(f)
810818
}
811819

812820
def foreachEntry[U](f: (K, V) => U): Unit = {
813-
if(left ne null) left.nn.foreachEntry(f)
821+
if(left ne null) left.foreachEntry(f)
814822
f(key, value)
815-
if(right ne null) right.nn.foreachEntry(f)
823+
if(right ne null) right.foreachEntry(f)
816824
}
817825

818826
def foreachNode[U](f: RBNode[K, V] => U): Unit = {
819-
if(left ne null) left.nn.foreachNode(f)
827+
if(left ne null) left.foreachNode(f)
820828
f(this)
821-
if(right ne null) right.nn.foreachNode(f)
829+
if(right ne null) right.foreachNode(f)
822830
}
823831
}
824832

825833
@`inline` private def leaf[A, B](key: A, hash: Int, value: B, red: Boolean, parent: RBNode[A, B] | Null): RBNode[A, B] =
826834
new RBNode(key, hash, value, red, null, null, parent)
827835

828836
@tailrec private def minNodeNonNull[A, B](node: RBNode[A, B]): RBNode[A, B] =
829-
if (node.left eq null) node else minNodeNonNull(node.left.nn)
837+
if (node.left eq null) node else minNodeNonNull(node.left)
830838

831839
/**
832840
* Returns the node that follows `node` in an in-order tree traversal. If `node` has the maximum key (and is,
833841
* therefore, the last node), this method returns `null`.
834842
*/
835843
private def successor[A, B](node: RBNode[A, B]): RBNode[A, B] | Null = {
836-
if (node.right ne null) minNodeNonNull(node.right.nn)
844+
if (node.right ne null) minNodeNonNull(node.right)
837845
else {
838846
var x = node
839847
var y = x.parent
840-
while ((y ne null) && (x eq y.nn.right)) {
848+
while ((y ne null) && (x eq y.right)) {
841849
x = y
842850
y = y.parent
843851
}

library/src/scala/collection/mutable/HashMap.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -384,36 +384,36 @@ class HashMap[K, V](initialCapacity: Int, loadFactor: Double)
384384
if(size == 0) table = new Array[Node[K, V] | Null](newlen)
385385
else {
386386
table = java.util.Arrays.copyOf(table, newlen)
387-
val preLow: Node[K, V] | Null = new Node(null.asInstanceOf[K], 0, null.asInstanceOf[V], null)
388-
val preHigh: Node[K, V] | Null = new Node(null.asInstanceOf[K], 0, null.asInstanceOf[V], null)
387+
val preLow: Node[K, V] = new Node(null.asInstanceOf[K], 0, null.asInstanceOf[V], null)
388+
val preHigh: Node[K, V] = new Node(null.asInstanceOf[K], 0, null.asInstanceOf[V], null)
389389
// Split buckets until the new length has been reached. This could be done more
390390
// efficiently when growing an already filled table to more than double the size.
391391
while(oldlen < newlen) {
392392
var i = 0
393393
while (i < oldlen) {
394394
val old = table(i)
395395
if(old ne null) {
396-
preLow.nn.next = null
397-
preHigh.nn.next = null
398-
var lastLow: Node[K, V] | Null = preLow
399-
var lastHigh: Node[K, V] | Null = preHigh
396+
preLow.next = null
397+
preHigh.next = null
398+
var lastLow: Node[K, V] = preLow
399+
var lastHigh: Node[K, V] = preHigh
400400
var n: Node[K, V] | Null = old
401401
while(n ne null) {
402402
val next = n.next
403403
if((n.hash & oldlen) == 0) { // keep low
404-
lastLow.nn.next = n
404+
lastLow.next = n
405405
lastLow = n
406406
} else { // move to high
407-
lastHigh.nn.next = n
407+
lastHigh.next = n
408408
lastHigh = n
409409
}
410410
n = next
411411
}
412-
lastLow.nn.next = null
413-
if(old ne preLow.nn.next) table(i) = preLow.nn.next
414-
if(preHigh.nn.next ne null) {
415-
table(i + oldlen) = preHigh.nn.next
416-
lastHigh.nn.next = null
412+
lastLow.next = null
413+
if(old ne preLow.next) table(i) = preLow.next
414+
if(preHigh.next ne null) {
415+
table(i + oldlen) = preHigh.next
416+
lastHigh.next = null
417417
}
418418
}
419419
i += 1
@@ -626,7 +626,7 @@ object HashMap extends MapFactory[HashMap] {
626626
def newBuilder: Builder[(K, V), HashMap[K, V]] = HashMap.newBuilder(tableLength, loadFactor)
627627
}
628628

629-
private[collection] final class Node[K, V](_key: K, _hash: Int, private[this] var _value: V, private[this] var _next: Node[K, V] | Null) {
629+
private[collection] final class Node[K, V](_key: K, _hash: Int, private[this] var _value: V, @annotation.stableNull private[this] var _next: Node[K, V] | Null) {
630630
def key: K = _key
631631
def hash: Int = _hash
632632
def value: V = _value
@@ -638,18 +638,18 @@ object HashMap extends MapFactory[HashMap] {
638638
def findNode(k: K, h: Int): Node[K, V] | Null =
639639
if(h == _hash && k == _key) this
640640
else if((_next eq null) || (_hash > h)) null
641-
else _next.nn.findNode(k, h)
641+
else _next.findNode(k, h)
642642

643643
@tailrec
644644
def foreach[U](f: ((K, V)) => U): Unit = {
645645
f((_key, _value))
646-
if(_next ne null) _next.nn.foreach(f)
646+
if(_next ne null) _next.foreach(f)
647647
}
648648

649649
@tailrec
650650
def foreachEntry[U](f: (K, V) => U): Unit = {
651651
f(_key, _value)
652-
if(_next ne null) _next.nn.foreachEntry(f)
652+
if(_next ne null) _next.foreachEntry(f)
653653
}
654654

655655
override def toString = s"Node($key, $value, $hash) -> $next"

0 commit comments

Comments
 (0)