8000 Applied further cleanup to Vector · scala/scala@74db1d3 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 74db1d3

Browse files
committed
Applied further cleanup to Vector
1 parent e06d383 commit 74db1d3

File tree

1 file changed

+63
-56
lines changed

1 file changed

+63
-56
lines changed

src/library/scala/collection/immutable/Vector.scala

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ extends AbstractSeq[A]
277277
s
278278
} else {
279279

280-
val freeSpace = (1 << (5 * depth)) - endIndex // free space at the right given the current tree-structure depth
281-
val shift = freeSpace & ~((1 << (5 * (depth - 1))) - 1) // number of elements by which we'll shift right (only move at top level)
282-
val shiftBlocks = freeSpace >>> (5 * (depth - 1)) // number of top-level blocks
280+
val freeSpace = (1 << (5 * depth)) - endIndex // free space at the right given the current tree-structure depth
281+
val shift = freeSpace & ~((1 << (5 * (depth - 1))) - 1) // number of elements by which we'll shift right (only move at top level)
282+
val shiftBlocks = freeSpace >>> (5 * (depth - 1)) // number of top-level blocks
283283

284284
if (shift != 0) {
285285
// case A: we can shift right on the top level
@@ -354,13 +354,14 @@ extends AbstractSeq[A]
354354
s.display0(lo) = value.asInstanceOf[AnyRef]
355355
s
356356
} else {
357-
val shift = startIndex & ~((1 << (5 * (depth - 1))) - 1)
357+
val shift = startIndex & ~((1 << (5 * (depth - 1))) - 1)
358358
val shiftBlocks = startIndex >>> (5 * (depth - 1))
359359

360360
if (shift != 0) {
361361
if (depth > 1) {
362362
val newBlockIndex = blockIndex - shift
363363
val newFocus = focus - shift
364+
364365
val s = new Vector(startIndex - shift, endIndex + 1 - shift, newBlockIndex)
365366
s.initFrom(this)
366367
s.dirty = dirty
@@ -371,6 +372,7 @@ extends AbstractSeq[A]
371372
} else {
372373
val newBlockIndex = blockIndex - 32
373374
val newFocus = focus
375+
374376
val s = new Vector(startIndex - shift, endIndex + 1 - shift, newBlockIndex)
375377
s.initFrom(this)
376378
s.dirty = dirty
@@ -405,18 +407,12 @@ extends AbstractSeq[A]
405407
// low-level implementation (needs cleanup, maybe move to util class)
406408

407409
private def shiftTopLevel(oldLeft: Int, newLeft: Int) = (depth - 1) match {
408-
case 0 =>
409-
display0 = copyRange(display0, oldLeft, newLeft)
410-
case 1 =>
411-
display1 = copyRange(display1, oldLeft, newLeft)
412-
case 2 =>
413-
display2 = copyRange(display2, oldLeft, newLeft)
414-
case 3 =>
415-
display3 = copyRange(display3, oldLeft, newLeft)
416-
case 4 =>
417-
display4 = copyRange(display4, oldLeft, newLeft)
418-
case 5 =>
419-
display5 = copyRange(display5, oldLeft, newLeft)
410+
case 0 => display0 = copyRange(display0, oldLeft, newLeft)
411+
case 1 => display1 = copyRange(display1, oldLeft, newLeft)
412+
case 2 => display2 = copyRange(display2, oldLeft, newLeft)
413+
case 3 => display3 = copyRange(display3, oldLeft, newLeft)
414+
case 4 => display4 = copyRange(display4, oldLeft, newLeft)
415+
case 5 => display5 = copyRange(display5, oldLeft, newLeft)
420416
}
421417

422418
private def zeroLeft(array: Array[AnyRef], index: Int): Unit = {
@@ -588,9 +584,9 @@ extends AbstractSeq[A]
588584
}
589585

590586
class VectorIterator[+A](_startIndex: Int, endIndex: Int)
591-
extends AbstractIterator[A]
592-
with Iterator[A]
593-
with VectorPointer[A @uncheckedVariance] {
587+
extends AbstractIterator[A]
588+
with Iterator[A]
589+
with VectorPointer[A @uncheckedVariance] {
594590

595591
private var blockIndex: Int = _startIndex & ~31
596592
private var lo: Int = _startIndex & 31
@@ -728,17 +724,38 @@ private[immutable] trait VectorPointer[T] {
728724
// requires structure is at pos oldIndex = xor ^ index
729725
private[immutable] final def getElem(index: Int, xor: Int): T = {
730726
if (xor < (1 << 5)) { // level = 0
731-
display0(index & 31).asInstanceOf[T]
727+
(display0
728+
(index & 31).asInstanceOf[T])
732729
} else if (xor < (1 << 10)) { // level = 1
733-
display1((index >>> 5) & 31).asInstanceOf[Array[AnyRef]](index & 31).asInstanceOf[T]
730+
(display1
731+
((index >>> 5) & 31).asInstanceOf[Array[AnyRef]]
732+
(index & 31).asInstanceOf[T])
734733
} else if (xor < (1 << 15)) { // level = 2
735-
display2((index >>> 10) & 31).asInstanceOf[Array[AnyRef]]((index >>> 5) & 31).asInstanceOf[Array[AnyRef]](index & 31).asInstanceOf[T]
734+
(display2
735+
((index >>> 10) & 31).asInstanceOf[Array[AnyRef]]
736+
((index >>> 5) & 31).asInstanceOf[Array[AnyRef]]
737+
(index & 31).asInstanceOf[T])
736738
} else if (xor < (1 << 20)) { // level = 3
737-
display3((index >>> 15) & 31).asInstanceOf[Array[AnyRef]]((index >>> 10) & 31).asInstanceOf[Array[AnyRef]]((index >>> 5) & 31).asInstanceOf[Array[AnyRef]](index & 31).asInstanceOf[T]
739+
(display3
740+
((index >>> 15) & 31).asInstanceOf[Array[AnyRef]]
741+
((index >>> 10) & 31).asInstanceOf[Array[AnyRef]]
742+
((index >>> 5) & 31).asInstanceOf[Array[AnyRef]]
743+
(index & 31).asInstanceOf[T])
738744
} else if (xor < (1 << 25)) { // level = 4
739-
display4((index >>> 20) & 31).asInstanceOf[Array[AnyRef]]((index >>> 15) & 31).asInstanceOf[Array[AnyRef]]((index >>> 10) & 31).asInstanceOf[Array[AnyRef]]((index >>> 5) & 31).asInstanceOf[Array[AnyRef]](index & 31).asInstanceOf[T]
745+
(display4
746+
((index >>> 20) & 31).asInstanceOf[Array[AnyRef]]
747+
((index >>> 15) & 31).asInstanceOf[Array[AnyRef]]
748+
((index >>> 10) & 31).asInstanceOf[Array[AnyRef]]
749+
((index >>> 5) & 31).asInstanceOf[Array[AnyRef]]
750+
(index & 31).asInstanceOf[T])
740751
} else if (xor < (1 << 30)) { // level = 5
741-
display5((index >>> 25) & 31).asInstanceOf[Array[AnyRef]]((index >>> 20) & 31).asInstanceOf[Array[AnyRef]]((index >>> 15) & 31).asInstanceOf[Array[AnyRef]]((index >>> 10) & 31).asInstanceOf[Array[AnyRef]]((index >>> 5) & 31).asInstanceOf[Array[AnyRef]](index & 31).asInstanceOf[T]
752+
(display5
753+
((index >>> 25) & 31).asInstanceOf[Array[AnyRef]]
754+
((index >>> 20) & 31).asInstanceOf[Array[AnyRef]]
755+
((index >>> 15) & 31).asInstanceOf[Array[AnyRef]]
756+
((index >>> 10) & 31).asInstanceOf[Array[AnyRef]]
757+
((index >>> 5) & 31).asInstanceOf[Array[AnyRef]]
758+
(index & 31).asInstanceOf[T])
742759
} else { // level = 6
743760
throw new IllegalArgumentException()
744761
}
@@ -809,55 +826,45 @@ private[immutable] trait VectorPointer[T] {
809826
// xor: oldIndex ^ index
810827
private[immutable] final def gotoNextBlockStartWritable(index: Int, xor: Int): Unit = { // goto block start pos
811828
if (xor < (1 << 10)) { // level = 1
812-
if (depth == 1) {
813-
display1 = new Array(32); display1(0) = display0; depth += 1
814-
}
829+
if (depth == 1) { display1 = new Array(32); display1(0) = display0; depth += 1 }
815830
display0 = new Array(32)
816-
display1((index >>> 5) & 31) = display0
831+
display1((index >>> 5) & 31) = display0
817832
} else if (xor < (1 << 15)) { // level = 2
818-
if (depth == 2) {
819-
display2 = new Array(32); display2(0) = display1; depth += 1
820-
}
833+
if (depth == 2) { display2 = new Array(32); display2(0) = display1; depth += 1 }
821834
display0 = new Array(32)
822835
display1 = new Array(32)
823-
display1((index >>> 5) & 31) = display0
824-
display2((index >>> 10) & 31) = display1
836+
display1((index >>> 5) & 31) = display0
837+
display2((index >>> 10) & 31) = display1
825838
} else if (xor < (1 << 20)) { // level = 3
826-
if (depth == 3) {
827-
display3 = new Array(32); display3(0) = display2; depth += 1
828-
}
839+
if (depth == 3) { display3 = new Array(32); display3(0) = display2; depth += 1 }
829840
display0 = new Array(32)
830841
display1 = new Array(32)
831842
display2 = new Array(32)
832-
display1((index >>> 5) & 31) = display0
833-
display2((index >>> 10) & 31) = display1
834-
display3((index >>> 15) & 31) = display2
843+
display1((index >>> 5) & 31) = display0
844+
display2((index >>> 10) & 31) = display1
845+
display3((index >>> 15) & 31) = display2
835846
} else if (xor < (1 << 25)) { // level = 4
836-
if (depth == 4) {
837-
display4 = new Array(32); display4(0) = display3; depth += 1
838-
}
847+
if (depth == 4) { display4 = new Array(32); display4(0) = display3; depth += 1 }
839848
display0 = new Array(32)
840849
display1 = new Array(32)
841850
display2 = new Array(32)
842851
display3 = new Array(32)
843-
display1((index >>> 5) & 31) = display0
844-
display2((index >>> 10) & 31) = display1
845-
display3((index >>> 15) & 31) = display2
846-
display4((index >>> 20) & 31) = display3
852+
display1((index >>> 5) & 31) = display0
853+
display2((index >>> 10) & 31) = display1
854+
display3((index >>> 15) & 31) = display2
855+
display4((index >>> 20) & 31) = display3
847856
} else if (xor < (1 << 30)) { // level = 5
848-
if (depth == 5) {
849-
display5 = new Array(32); display5(0) = display4; depth + CD7A = 1
850-
}
857+
if (depth == 5) { display5 = new Array(32); display5(0) = display4; depth += 1 }
851858
display0 = new Array(32)
852859
display1 = new Array(32)
853860
display2 = new Array(32)
854861
display3 = new Array(32)
855862
display4 = new Array(32)
856-
display1((index >>> 5) & 31) = display0
857-
display2((index >>> 10) & 31) = display1
858-
display3((index >>> 15) & 31) = display2
859-
display4((index >>> 20) & 31) = display3
860-
display5((index >>> 25) & 31) = display4
863+
display1((index >>> 5) & 31) = display0
864+
display2((index >>> 10) & 31) = display1
865+
display3((index >>> 15) & 31) = display2
866+
display4((index >>> 20) & 31) = display3
867+
display5((index >>> 25) & 31) = display4
861868
} else { // level = 6
862869
throw new IllegalArgumentException()
863870
}
@@ -1102,4 +1109,4 @@ private[immutable] trait VectorPointer[T] {
11021109
stabilize(oldIndex)
11031110
gotoFreshPosWritable0(oldIndex, newIndex, xor)
11041111
}
1105-
}
1112+
}

0 commit comments

Comments
 (0)
0