@@ -381,8 +381,8 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
381
381
382
382
override def appended [B >: A ](value : B ): Vector [B ] = {
383
383
val result = if (endIndex != startIndex) {
384
- val blockIndex = endIndex & ~ 31
385
- val lo = endIndex & 31
384
+ val blockIndex = endIndex & ~ 31 // round down to nearest 32
385
+ val lo = endIndex & 31 // remainder of blockIndex / 32
386
386
387
387
if (endIndex != blockIndex) {
388
388
val s = new Vector (startIndex, endIndex + 1 , blockIndex)
@@ -616,6 +616,8 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
616
616
case _ => super .equals(o)
617
617
}
618
618
619
+ override def copyToArray [B >: A ](xs : Array [B ], start : Int , len : Int ): Int = iterator.copyToArray(xs, start, len)
620
+
619
621
override def toVector : Vector [A ] = this
620
622
621
623
override protected [this ] def className = " Vector"
@@ -628,12 +630,27 @@ class VectorIterator[+A](_startIndex: Int, endIndex: Int)
628
630
private [this ] var blockIndex : Int = _startIndex & ~ 31
629
631
private [this ] var lo : Int = _startIndex & 31
630
632
631
- private [this ] var endLo = math .min(endIndex - blockIndex, 32 )
633
+ private [this ] var endLo = Math .min(endIndex - blockIndex, 32 )
632
634
633
635
def hasNext = _hasNext
634
636
635
637
private [this ] var _hasNext = blockIndex + lo < endIndex
636
638
639
+ private [this ] def advanceToNextBlockIfNecessary (): Unit = {
640
+ if (lo == endLo) {
641
+ if (blockIndex + lo < endIndex) {
642
+ val newBlockIndex = blockIndex + 32
643
+ gotoNextBlockStart(newBlockIndex, blockIndex ^ newBlockIndex)
644
+
645
+ blockIndex = newBlockIndex
646
+ endLo = Math .min(endIndex - blockIndex, 32 )
647
+ lo = 0
648
+ } else {
649
+ _hasNext = false
650
+ }
651
+ }
652
+ }
653
+
637
654
override def drop (n : Int ): Iterator [A ] = {
638
655
if (n > 0 ) {
639
656
val longLo = lo.toLong + n
@@ -657,24 +674,25 @@ class VectorIterator[+A](_startIndex: Int, endIndex: Int)
657
674
658
675
def next (): A = {
659
676
if (! _hasNext) throw new NoSuchElementException (" reached iterator end" )
660
-
661
677
val res = display0(lo).asInstanceOf [A ]
662
678
lo += 1
679
+ advanceToNextBlockIfNecessary()
680
+ res
681
+ }
663
682
664
- if (lo == endLo) {
665
- if (blockIndex + lo < endIndex) {
666
- val newBlockIndex = blockIndex + 32
667
- gotoNextBlockStart(newBlockIndex, blockIndex ^ newBlockIndex)
668
-
669
- blockIndex = newBlockIndex
670
- endLo = math.min(endIndex - blockIndex, 32 )
671
- lo = 0
672
- } else {
673
- _hasNext = false
674
- }
683
+ override def copyToArray [ B >: A ]( xs : Array [ B ], start : Int , len : Int ) : Int = {
684
+ val xsLen = xs.length
685
+ val totalToBeCopied = IterableOnce .elemsToCopyToArray(remainingElementCount, xsLen, start, len)
686
+ var totalCopied = 0
687
+ while (hasNext && totalCopied < totalToBeCopied) {
688
+ val _start = start + totalCopied
689
+ val toBeCopied = IterableOnce .elemsToCopyToArray(endLo - lo, xsLen, _start, len - totalCopied )
690
+ Array .copy(display0, lo, xs, _start, toBeCopied)
691
+ totalCopied += toBeCopied
692
+ lo += toBeCopied
693
+ advanceToNextBlockIfNecessary()
675
694
}
676
-
677
- res
695
+ totalCopied
678
696
}
679
697
680
698
private [collection] def remainingElementCount : Int = (endIndex - (blockIndex + lo)) max 0
@@ -707,18 +725,31 @@ final class VectorBuilder[A]() extends ReusableBuilder[A, Vector[A]] with Vector
707
725
def isEmpty : Boolean = size == 0
708
726
def nonEmpty : Boolean = size != 0
709
727
710
- def addOne ( elem : A ): this . type = {
728
+ private [ this ] def advanceToNextBlockIfNecessary ( ): Unit = {
711
729
if (lo >= display0.length) {
712
730
val newBlockIndex = blockIndex + 32
713
731
gotoNextBlockStartWritable(newBlockIndex, blockIndex ^ newBlockIndex)
714
732
blockIndex = newBlockIndex
715
733
lo = 0
716
734
}
735
+ }
736
+
737
+ def addOne (elem : A ): this .type = {
738
+ advanceToNextBlockIfNecessary()
717
739
display0(lo) = elem.asInstanceOf [AnyRef ]
718
740
lo += 1
719
741
this
720
742
}
721
743
744
+ override def addAll (xs : IterableOnce [A ]): this .type = {
745
+ val it = (xs.iterator : Iterator [A ]).asInstanceOf [Iterator [AnyRef ]]
746
+ while (it.hasNext) {
747
+ advanceToNextBlockIfNecessary()
748
+ lo += it.copyToArray(xs = display0, start = lo, len = display0.length - lo)
749
+ }
750
+ this
751
+ }
752
+
722
753
def result (): Vector [A ] = {
723
754
val size = this .size
724
755
if (size == 0 )
0 commit comments