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

Conversation

@marcelocenerine
Copy link
Contributor

@marcelocenerine marcelocenerine commented Feb 8, 2018

Note: each fix is in a separate commit


private var array: Array[AnyRef] = initElems
private var end = initLength
private var size0 = initSize
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed end to size0 as the former is misleading

def apply(n: Int) = array(n).asInstanceOf[A]
def apply(n: Int) = {
checkWithinBounds(n, n + 1)
array(n).asInstanceOf[A]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren’t bounds already checked by the underlying array access?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the underlying array can be larger than the ArrayBuffer in many situations:

import strawman.collection.mutable.ArrayBuffer
val a = ArrayBuffer(1)
a: strawman.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)

scala> a.remove(0)
res2: Int = 1

scala> a(0)
res3: Int = 0

scala> val b = ArrayBuffer.empty[Int]
b: strawman.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> b(0)
res4: Int = 0

scala> val c = ArrayBuffer(1, 2, 3)
c: strawman.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)

scala> c.clear()

scala> c.size
res6: Int = 0

scala> c(0)
res7: Int = 1

Copy link
Contributor Author

@marcelocenerine marcelocenerine Feb 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in summary:

  • when an empty ArrayBuffer is created, its underlying array will have size == 16
  • when an ArrayBuffer's element is removed, the underlying array remains with the same size (only the buffer's size gets decremented)

@julienrf julienrf merged commit 9f67563 into scala:master Feb 9, 2018
@julienrf
Copy link
Contributor

julienrf commented Feb 9, 2018

Thanks Marcelo :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

2 participants