Skip to content

Commit e3fc2a2

Browse files
committed
Address reviewers comments
1 parent 778c601 commit e3fc2a2

File tree

5 files changed

+24
-29
lines changed

5 files changed

+24
-29
lines changed

src/main/scala/strawman/collection/Iterable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ trait IterableOps[+A] extends Any {
5454
protected def coll: Iterable[A]
5555
private def iterator() = coll.iterator()
5656

57-
/** Apply `f` to each element for tis side effects
57+
/** Apply `f` to each element for its side effects
5858
* Note: [U] parameter needed to help scalac's type inference.
5959
*/
6060
def foreach[U](f: A => U): Unit = iterator().foreach(f)

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)
2828
end = n
2929
}
3030

31+
private def checkWithinBounds(lo: Int, hi: Int) = {
32+
if (lo < 0) throw new IndexOutOfBoundsException(lo.toString)
33+
if (hi > end) throw new IndexOutOfBoundsException(hi.toString)
34+
}
35+
3136
def apply(n: Int) = array(n).asInstanceOf[A]
3237

3338
def update(n: Int, elem: A): Unit = array(n) = elem.asInstanceOf[AnyRef]
@@ -69,14 +74,14 @@ class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)
6974
def result = this
7075

7176
def insert(idx: Int, elem: A): Unit = {
72-
if (idx < 0 || idx > end) throw new IndexOutOfBoundsException
77+
checkWithinBounds(idx, idx)
7378
ensureSize(end + 1)
7479
Array.copy(array, idx, array, idx + 1, end - idx)
7580
this(idx) = elem
7681
}
7782

7883
def insertAll(idx: Int, elems: IterableOnce[A]): Unit = {
79-
if (idx < 0 || idx > end) throw new IndexOutOfBoundsException
84+
checkWithinBounds(idx, idx)
8085
elems match {
8186
case elems: collection.Iterable[A] =>
8287
val elemsLength = elems.size
@@ -100,7 +105,7 @@ class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)
100105
}
101106

102107
def remove(idx: Int): A = {
103-
if (idx < 0 || idx >= end) throw new IndexOutOfBoundsException
108+
checkWithinBounds(idx, idx + 1)
104109
val res = this(idx)
105110
Array.copy(array, idx + 1, array, idx, end - (idx + 1))
106111
reduceToSize(end - 1)
@@ -109,7 +114,7 @@ class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int)
109114

110115
def remove(from: Int, n: Int): Unit =
111116
if (n > 0) {
112-
if (from < 0 || from + n > end) throw new IndexOutOfBoundsException
117+
checkWithinBounds(from, from + n)
113118
Array.copy(array, from + n, array, from, end - (from + n))
114119
reduceToSize(end - n)
115120
}
@@ -158,6 +163,7 @@ object RefArrayUtils {
158163
/** Remove elements of this array at indices after `sz`.
159164
*/
160165
def nullElems(array: Array[AnyRef], start: Int, end: Int): Unit = {
166+
// Maybe use `fill` instead?
161167
var i = start
162168
while (i < end) {
163169
array(i) = null

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
/* __ *\
2-
** ________ ___ / / ___ Scala API **
3-
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
4-
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5-
** /____/\___/_/ |_/____/_/ | | **
6-
** |/ **
7-
\* */
8-
91
package strawman.collection.mutable
102

113
import strawman.collection.IterableOnce
@@ -16,14 +8,6 @@ import strawman.collection.{toOldSeq, toNewSeq}
168
/** This trait forms part of collections that can be augmented
179
* using a `+=` operator and that can be cleared of all elements using
1810
* a `clear` method.
19-
*
20-
* @author Martin Odersky
21-
* @version 2.8
22-
* @since 2.8
23-
* @define coll growable collection
24-
* @define Coll `Growable`
25-
* @define add add
26-
* @define Add add
2711
*/
2812
trait Growable[-A] {
2913

@@ -59,6 +43,8 @@ trait Growable[-A] {
5943
case xs: scala.collection.LinearSeq[_] => loop(xs.asInstanceOf[scala.collection.LinearSeq[A]])
6044
case xs => xs.iterator() foreach += // Deviation: IterableOnce does not define `foreach`.
6145
}
46+
// @ichoran writes: Right now, this actually isn't any faster than using an iterator
47+
// for List. Maybe we should just simplify the code by deferring to iterator foreach?
6248
this
6349
}
6450

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class ListBuffer[A]
6666
if (i == 0) null
6767
else if (i == len) last
6868
else {
69-
assert(i > 0 && i < len)
7069
var j = i - 1
7170
var p = first
7271
while (j > 0) {
@@ -188,7 +187,7 @@ class ListBuffer[A]
188187
def patchInPlace(from: Int, patch: collection.Seq[A], replaced: Int): this.type = {
189188
ensureUnaliased()
190189
val p = locate(from)
191-
removeAfter(p, replaced `min` length - from)
190+
removeAfter(p, replaced `min` (length - from))
192191
insertAfter(p, patch.iterator())
193192
this
194193
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package strawman.collection.mutable
22

3-
import java.lang.IndexOutOfBoundsException
43
import scala.{Int, Long, Unit, Boolean, Array}
54
import strawman.collection
65
import strawman.collection.{IterableOnce, toNewSeq, toOldSeq}
@@ -25,9 +24,11 @@ trait GrowableSeq[A] extends Seq[A]
2524
def patchInPlace(from: Int, patch: collection.Seq[A], replaced: Int): this.type
2625

2726
// +=, ++=, clear inherited from Growable
28-
def +=:(elem: A): this.type = { insert(0, elem); this }
29-
def +=:(elem1: A, elem2: A, elems: A*): this.type = elem1 +=: elem2 +=: elems.toStrawman ++=: this
30-
def ++=:(elems: IterableOnce[A]): this.type = { insertAll(0, elems); this }
27+
// Per remark of @ichoran, we should preferably not have these:
28+
//
29+
// def +=:(elem: A): this.type = { insert(0, elem); this }
30+
// def +=:(elem1: A, elem2: A, elems: A*): this.type = elem1 +=: elem2 +=: elems.toStrawman ++=: this
31+
// def ++=:(elems: IterableOnce[A]): this.type = { insertAll(0, elems); this }
3132

3233
def dropInPlace(n: Int): this.type = { remove(0, n); this }
3334
def dropRightInPlace(n: Int): this.type = { remove(length - n, n); this }
@@ -52,13 +53,15 @@ trait GrowableSeq[A] extends Seq[A]
5253
trait IndexedOptimizedSeq[A] extends Seq[A] {
5354
def mapInPlace(f: A => A): this.type = {
5455
var i = 0
55-
while (i < size) { this(i) = f(this(i)); i += 1 }
56+
val siz = size
57+
while (i < siz) { this(i) = f(this(i)); i += 1 }
5658
this
5759
}
5860
}
5961

6062
trait IndexedOptimizedGrowableSeq[A] extends IndexedOptimizedSeq[A] with GrowableSeq[A] {
6163
def flatMapInPlace(f: A => IterableOnce[A]): this.type = {
64+
// There's scope for a better implementation which copies elements in place.
6265
var i = 0
6366
val newElemss = new Array[IterableOnce[A]](size)
6467
while (i < size) { newElemss(i) = f(this(i)); i += 1 }
@@ -69,7 +72,8 @@ trait IndexedOptimizedGrowableSeq[A] extends IndexedOptimizedSeq[A] with Growabl
6972
}
7073
def filterInPlace(p: A => Boolean): this.type = {
7174
var i = 0
72-
var j = 0
75+
while (i < size && p(apply(i))) i += 1
76+
var j = 1
7377
while (i < size) {
7478
if (p(apply(i))) {
7579
this(j) = this(i)

0 commit comments

Comments
 (0)