Skip to content

Commit bbcb391

Browse files
committed
#20 Fix the way we grow Arrays
1 parent 848e8b6 commit bbcb391

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/main/scala/strawman/collection/mutable/ArrayBuffer.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package strawman.collection.mutable
22

33
import java.lang.IndexOutOfBoundsException
4-
import scala.{Array, Int, Long, Boolean, Unit, AnyRef}
4+
import scala.{Array, Exception, Int, Long, Boolean, math, StringContext, Unit, AnyRef}
55
import strawman.collection
66
import strawman.collection.{IterableFactory, IterableOnce, SeqLike, IndexedView}
77
import scala.Predef.intWrapper
@@ -147,11 +147,14 @@ object RefArrayUtils {
147147
// Use a Long to prevent overflows
148148
val arrayLength: Long = array.length
149149
def growArray = {
150-
var newSize: Long = arrayLength * 2
150+
var newSize: Long = math.max(arrayLength * 2, 8)
151151
while (n > newSize)
152152
newSize = newSize * 2
153153
// Clamp newSize to Int.MaxValue
154-
if (newSize > Int.MaxValue) newSize = Int.MaxValue
154+
if (newSize > Int.MaxValue) {
155+
if (end == Int.MaxValue) throw new Exception(s"Collections can not have more than ${Int.MaxValue} elements")
156+
newSize = Int.MaxValue
157+
}
155158

156159
val newArray: Array[AnyRef] = new Array(newSize.toInt)
157160
Array.copy(array, 0, newArray, 0, end)

0 commit comments

Comments
 (0)