Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion collections/src/main/scala/strawman/collection/View.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ object View extends IterableFactoryLike[View] {
}

private[collection] class Patched[A](underlying: Iterable[A], from: Int, other: IterableOnce[A], replaced: Int) extends View[A] {
if (from < 0 || (knownSize > -1 && from > knownSize)) throw new IndexOutOfBoundsException(from.toString)
def iterator(): Iterator[A] = underlying.iterator().patch(from, other.iterator(), replaced)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package mutable

import java.lang.{IndexOutOfBoundsException, IllegalArgumentException}

import scala.{AnyRef, Array, ArrayIndexOutOfBoundsException, Boolean, Exception, Int, Long, StringContext, Unit, math, Any, throws, Serializable, SerialVersionUID}
import scala.{AnyRef, Array, ArrayIndexOutOfBoundsException, Boolean, Exception, Int, `inline`, Long, StringContext, Unit, math, Any, throws, Serializable, SerialVersionUID}
import scala.Predef.intWrapper

/** An implementation of the `Buffer` class using an array to
Expand All @@ -30,7 +30,7 @@ import scala.Predef.intWrapper
* @define willNotTerminateInf
*/
@SerialVersionUID(1529165946227428979L)
class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)
class ArrayBuffer[A] private (initElems: Array[AnyRef], initSize: Int)
extends AbstractBuffer[A]
with IndexedSeq[A]
with IndexedSeqOps[A, ArrayBuffer, ArrayBuffer[A]]
Expand All @@ -40,48 +40,53 @@ class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)

def this() = this(new Array[AnyRef](16), 0)

def this(initLength: Int) = this(new Array[AnyRef](initLength), 0)
def this(initSize: Int) = this(new Array[AnyRef](initSize), 0)

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


/** Ensure that the internal array has at least `n` cells. */
private def ensureSize(n: Int): Unit =
array = RefArrayUtils.ensureSize(array, end, n)
array = RefArrayUtils.ensureSize(array, size0, n)

/** Reduce length to `n`, nulling out all dropped elements */
private def reduceToSize(n: Int): Unit = {
RefArrayUtils.nullElems(array, n, end)
end = n
RefArrayUtils.nullElems(array, n, size0)
size0 = n
}

private def checkWithinBounds(lo: Int, hi: Int) = {
@inline private def checkWithinBounds(lo: Int, hi: Int) = {
if (lo < 0) throw new IndexOutOfBoundsException(lo.toString)
if (hi > end) throw new IndexOutOfBoundsException(hi.toString)
if (hi > size0) throw new IndexOutOfBoundsException(hi.toString)
}

@throws[ArrayIndexOutOfBoundsException]
def apply(n: Int) = array(n).asInstanceOf[A]
def apply(n: Int) = {
checkWithinBounds(n, n + 1)
array(n).asInstanceOf[A]
}

def update(n: Int, elem: A): Unit = array(n) = elem.asInstanceOf[AnyRef]
def update(n: Int, elem: A): Unit = {
checkWithinBounds(n, n + 1)
array(n) = elem.asInstanceOf[AnyRef]
}

protected def finiteSize = end
protected def finiteSize = size0

override def view: ArrayBufferView[A] = new ArrayBufferView(array, end)
override def view: ArrayBufferView[A] = new ArrayBufferView(array, size0)

def iterableFactory: SeqFactory[ArrayBuffer] = ArrayBuffer

protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): ArrayBuffer[A] = fromIterable(coll)

protected[this] def newSpecificBuilder(): Builder[A, ArrayBuffer[A]] = ArrayBuffer.newBuilder()

def clear(): Unit =
end = 0
def clear(): Unit = reduceToSize(0)

def addOne(elem: A): this.type = {
ensureSize(end + 1)
this(end) = elem
end += 1
val i = size0
ensureSize(size0 + 1)
size0 += 1
this(i) = elem
this
}

Expand All @@ -97,18 +102,18 @@ class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)
case elems: ArrayBuffer[_] =>
ensureSize(size + elems.size)
Array.copy(elems.array, 0, array, size, elems.size)
end = size + elems.size
size0 = size + elems.size
case _ => super.addAll(elems)
}
this
}

def insert(idx: Int, elem: A): Unit = {
checkWithinBounds(idx, idx)
ensureSize(end + 1)
Array.copy(array, idx, array, idx + 1, end - idx)
ensureSize(size0 + 1)
Array.copy(array, idx, array, idx + 1, size0 - idx)
size0 += 1
this(idx) = elem
end += 1
}

def prepend(elem: A): this.type = {
Expand All @@ -122,8 +127,8 @@ class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)
case elems: collection.Iterable[A] =>
val elemsLength = elems.size
ensureSize(size + elemsLength)
Array.copy(array, idx, array, idx + elemsLength, end - idx)
end = end + elemsLength
Array.copy(array, idx, array, idx + elemsLength, size0 - idx)
size0 = size0 + elemsLength
elems match {
case elems: ArrayBuffer[_] =>
Array.copy(elems.array, 0, array, idx, elemsLength)
Expand All @@ -143,16 +148,16 @@ class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)
def remove(idx: Int): A = {
checkWithinBounds(idx, idx + 1)
val res = this(idx)
Array.copy(array, idx + 1, array, idx, end - (idx + 1))
reduceToSize(end - 1)
Array.copy(array, idx + 1, array, idx, size0 - (idx + 1))
reduceToSize(size0 - 1)
res
}

def remove(idx: Int, count: Int): Unit =
if (count > 0) {
checkWithinBounds(idx, idx + count)
Array.copy(array, idx + count, array, idx, end - (idx + count))
reduceToSize(end - count)
Array.copy(array, idx + count, array, idx, size0 - (idx + count))
reduceToSize(size0 - count)
} else if (count < 0) {
throw new IllegalArgumentException("removing negative number of elements: " + count)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,16 @@ trait IndexedOptimizedBuffer[A] extends IndexedOptimizedSeq[A] with Buffer[A] {
}

def patchInPlace(from: Int, patch: strawman.collection.Seq[A], replaced: Int): this.type = {
val n = patch.size min replaced
var i = 0
while (i < n) { update(from + i, patch(i)); i += 1 }
if (i < patch.size) insertAll(from + i, patch.iterator().drop(i))
else if (i < replaced) remove(from + i, replaced - i)
val replaced0 = math.min(math.max(replaced, 0), size)
val i = math.min(math.max(from, 0), size)
var j = 0
val n = math.min(patch.size, replaced0)
while (j < n && i + j < size) {
update(i + j, patch(j))
j += 1
}
if (j < patch.size) insertAll(i + j, patch.iterator().drop(j))
else if (j < replaced0) remove(i + j, replaced0 - j)
this
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mutable

import scala.annotation.unchecked.uncheckedVariance
import scala.annotation.tailrec
import scala.{Any, Boolean, Int, Unit, throws, Serializable, SerialVersionUID}
import scala.{Any, Boolean, Int, math, Unit, throws, Serializable, SerialVersionUID}
import scala.Int._
import strawman.collection
import strawman.collection.immutable.{List, Nil, ::}
Expand Down Expand Up @@ -272,9 +272,11 @@ class ListBuffer[A]
}

def patchInPlace(from: Int, patch: collection.Seq[A], replaced: Int): this.type = {
val i = math.min(math.max(from, 0), length)
val n = math.min(math.max(replaced, 0), length)
ensureUnaliased()
val p = locate(from)
removeAfter(p, replaced `min` (len - from))
val p = locate(i)
removeAfter(p, math.min(n, len - i))
insertAfter(p, patch.iterator())
this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,65 @@ class ArrayBufferTest {
b4.trimEnd(10)
assertEquals(ArrayBuffer.range(0, 90), b4)
}

@Test
def testPatch: Unit = {
val buffer = ArrayBuffer(0, 1, 2, 3)
val patch = List(-3, -2, -1)
assertEquals(ArrayBuffer(-3, -2, -1, 0, 1, 2, 3), buffer.patch(from = -1, patch, replaced = -1))
assertEquals(ArrayBuffer(-3, -2, -1, 0, 1, 2, 3), buffer.patch(from = 0, patch, replaced = 0))
assertEquals(ArrayBuffer(0, -3, -2, -1, 2, 3), buffer.patch(from = 1, patch, replaced = 1))
assertEquals(ArrayBuffer(0, -3, -2, -1), buffer.patch(from = 1, patch, replaced = 3))
assertEquals(ArrayBuffer(0, 1, -3, -2, -1), buffer.patch(from = 2, patch, replaced = 2))
assertEquals(ArrayBuffer(0, 1, 2, 3, -3, -2, -1), buffer.patch(from = 10, patch, replaced = 10))
assertEquals(ArrayBuffer(-3, -2, -1), buffer.patch(from = 0, patch, replaced = 100))
}

@Test
def testPatchInPlace: Unit = {
def testPatchInPlace(from: Int, replaced: Int, expectation: ArrayBuffer[Int]) =
assertEquals(expectation, ArrayBuffer(0, 1, 2).patchInPlace(from, patch = List(-3, -2, -1), replaced))

testPatchInPlace(from = -1, replaced = -1, expectation = ArrayBuffer(-3, -2, -1, 0, 1, 2))
testPatchInPlace(from = 0, replaced = 0, expectation = ArrayBuffer(-3, -2, -1, 0, 1, 2))
testPatchInPlace(from = 1, replaced = 1, expectation = ArrayBuffer(0, -3, -2, -1, 2))
testPatchInPlace(from = 1, replaced = 2, expectation = ArrayBuffer(0, -3, -2, -1))
testPatchInPlace(from = 2, replaced = 1, expectation = ArrayBuffer(0, 1, -3, -2, -1))
testPatchInPlace(from = 10, replaced = 10, expectation = ArrayBuffer(0, 1, 2, -3, -2, -1))
testPatchInPlace(from = 0, replaced = 100, expectation = ArrayBuffer(-3, -2, -1))
}

@Test(expected = classOf[IndexOutOfBoundsException])
def testApplyWhenEmpty: Unit = {
new ArrayBuffer().apply(0)
}

@Test(expected = classOf[IndexOutOfBoundsException])
def testApplyAfterClearing: Unit = {
val buffer = ArrayBuffer(1, 2, 3)
buffer.clear()

buffer(0)
}

@Test(expected = classOf[IndexOutOfBoundsException])
def testUpdateWhenEmpty: Unit = {
new ArrayBuffer().update(0, 100)
}

@Test(expected = classOf[IndexOutOfBoundsException])
def testUpdateAfterClearing: Unit = {
val buffer = ArrayBuffer(1, 2, 3)
buffer.clear()

buffer.update(0, 100)
}

@Test
def testClear: Unit = {
val buffer = ArrayBuffer(1, 2, 3)
buffer.clear()

assertEquals(0, buffer.size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,31 @@ class ListBufferTest {
b4.trimEnd(10)
assertEquals(ListBuffer.range(0, 90), b4)
}

@Test
def testPatch: Unit = {
val buffer = ListBuffer(0, 1, 2, 3)
val patch = List(-3, -2, -1)
assertEquals(ListBuffer(-3, -2, -1, 0, 1, 2, 3), buffer.patch(from = -1, patch, replaced = -1))
assertEquals(ListBuffer(-3, -2, -1, 0, 1, 2, 3), buffer.patch(from = 0, patch, replaced = 0))
assertEquals(ListBuffer(0, -3, -2, -1, 2, 3), buffer.patch(from = 1, patch, replaced = 1))
assertEquals(ListBuffer(0, -3, -2, -1), buffer.patch(from = 1, patch, replaced = 3))
assertEquals(ListBuffer(0, 1, -3, -2, -1), buffer.patch(from = 2, patch, replaced = 2))
assertEquals(ListBuffer(0, 1, 2, 3, -3, -2, -1), buffer.patch(from = 10, patch, replaced = 10))
assertEquals(ListBuffer(-3, -2, -1), buffer.patch(from = 0, patch, replaced = 100))
}

@Test
def testPatchInPlace: Unit = {
def testPatchInPlace(from: Int, replaced: Int, expectation: ListBuffer[Int]) =
assertEquals(expectation, ListBuffer(0, 1, 2).patchInPlace(from, patch = List(-3, -2, -1), replaced))

testPatchInPlace(from = -1, replaced = -1, expectation = ListBuffer(-3, -2, -1, 0, 1, 2))
testPatchInPlace(from = 0, replaced = 0, expectation = ListBuffer(-3, -2, -1, 0, 1, 2))
testPatchInPlace(from = 1, replaced = 1, expectation = ListBuffer(0, -3, -2, -1, 2))
testPatchInPlace(from = 1, replaced = 2, expectation = ListBuffer(0, -3, -2, -1))
testPatchInPlace(from = 2, replaced = 1, expectation = ListBuffer(0, 1, -3, -2, -1))
testPatchInPlace(from = 10, replaced = 10, expectation = ListBuffer(0, 1, 2, -3, -2, -1))
testPatchInPlace(from = 0, replaced = 100, expectation = ListBuffer(-3, -2, -1))
}
}