From bbcb3916dc43b84eaee85f209f5d12bed0a93c62 Mon Sep 17 00:00:00 2001 From: Julien Richard-Foy Date: Wed, 25 Jan 2017 14:42:48 +0100 Subject: [PATCH] #20 Fix the way we grow Arrays --- .../scala/strawman/collection/mutable/ArrayBuffer.scala | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/scala/strawman/collection/mutable/ArrayBuffer.scala b/src/main/scala/strawman/collection/mutable/ArrayBuffer.scala index 37c8d25853..c70df6ad97 100644 --- a/src/main/scala/strawman/collection/mutable/ArrayBuffer.scala +++ b/src/main/scala/strawman/collection/mutable/ArrayBuffer.scala @@ -1,7 +1,7 @@ package strawman.collection.mutable import java.lang.IndexOutOfBoundsException -import scala.{Array, Int, Long, Boolean, Unit, AnyRef} +import scala.{Array, Exception, Int, Long, Boolean, math, StringContext, Unit, AnyRef} import strawman.collection import strawman.collection.{IterableFactory, IterableOnce, SeqLike, IndexedView} import scala.Predef.intWrapper @@ -147,11 +147,14 @@ object RefArrayUtils { // Use a Long to prevent overflows val arrayLength: Long = array.length def growArray = { - var newSize: Long = arrayLength * 2 + var newSize: Long = math.max(arrayLength * 2, 8) while (n > newSize) newSize = newSize * 2 // Clamp newSize to Int.MaxValue - if (newSize > Int.MaxValue) newSize = Int.MaxValue + if (newSize > Int.MaxValue) { + if (end == Int.MaxValue) throw new Exception(s"Collections can not have more than ${Int.MaxValue} elements") + newSize = Int.MaxValue + } val newArray: Array[AnyRef] = new Array(newSize.toInt) Array.copy(array, 0, newArray, 0, end)