|
1 | | -package strawman |
| 1 | +package scala |
2 | 2 | package collection.mutable |
3 | 3 |
|
4 | 4 | import scala.collection.{GenSeq, generic, mutable} |
@@ -80,20 +80,26 @@ class ArrayDeque[A] private(var array: Array[AnyRef], var start: Int, var end: I |
80 | 80 | override def insertAll(idx: Int, elems: scala.collection.Traversable[A]): Unit = { |
81 | 81 | ArrayDeque.checkIndex(idx, this) |
82 | 82 | if (elems.isEmpty) return |
83 | | - val srcLength = elems.size |
84 | | - val finalLength = srcLength + this.length |
85 | | - // Either we resize right away or move prefix right or suffix left |
86 | | - if (2*finalLength >= array.length - 1) { |
87 | | - val array2 = ArrayDeque.alloc(finalLength) |
88 | | - copySliceToArray(srcStart = 0, dest = array2, destStart = 0, maxItems = idx) |
89 | | - elems.copyToArray(array2.asInstanceOf[Array[A]], idx) |
90 | | - copySliceToArray(srcStart = idx, dest = array2, destStart = idx + srcLength, maxItems = size) |
91 | | - set(array = array2, start = 0, end = finalLength) |
92 | | - } else { |
93 | | - val suffix = drop(idx) |
94 | | - end = (start + idx) & mask |
95 | | - elems.foreach(appendAssumingCapacity) |
96 | | - suffix.foreach(appendAssumingCapacity) |
| 83 | + elems.sizeHintIfCheap match { |
| 84 | + case srcLength if srcLength >= 0 => |
| 85 | + val finalLength = srcLength + this.length |
| 86 | + // Either we resize right away or move prefix right or suffix left |
| 87 | + if (2*finalLength >= array.length - 1) { |
| 88 | + val array2 = ArrayDeque.alloc(finalLength) |
| 89 | + copySliceToArray(srcStart = 0, dest = array2, destStart = 0, maxItems = idx) |
| 90 | + elems.copyToArray(array2.asInstanceOf[Array[A]], idx) |
| 91 | + copySliceToArray(srcStart = idx, dest = array2, destStart = idx + srcLength, maxItems = size) |
| 92 | + set(array = array2, start = 0, end = finalLength) |
| 93 | + } else { |
| 94 | + val suffix = drop(idx) |
| 95 | + end = (start + idx) & mask |
| 96 | + elems.foreach(appendAssumingCapacity) |
| 97 | + suffix.foreach(appendAssumingCapacity) |
| 98 | + } |
| 99 | + case _ => //expensive to compute size |
| 100 | + val suffix = drop(idx) |
| 101 | + end = (start + idx) & mask |
| 102 | + this ++= elems ++= suffix |
97 | 103 | } |
98 | 104 | } |
99 | 105 |
|
@@ -257,7 +263,7 @@ class ArrayDeque[A] private(var array: Array[AnyRef], var start: Int, var end: I |
257 | 263 | private[this] def ensureCapacity() = { |
258 | 264 | /* We resize when we are 1 element short intentionally (and not when array is actually full) |
259 | 265 | * This is because when array is full, start is equal to end (which is also true when array is empty) |
260 | | - * Making it hard to distinguish between the full and empty case*/ |
| 266 | + * making it hard to distinguish between the full and empty cases */ |
261 | 267 | if (size == array.length - 1) resize(array.length) |
262 | 268 | } |
263 | 269 |
|
|
0 commit comments