Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
#20 Fix the way we grow Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrf committed Jan 25, 2017
1 parent 848e8b6 commit bbcb391
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/scala/strawman/collection/mutable/ArrayBuffer.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bbcb391

Please sign in to comment.