summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorBurak Emir <emir@epfl.ch>2006-10-04 15:44:42 +0000
committerBurak Emir <emir@epfl.ch>2006-10-04 15:44:42 +0000
commit93275f2d34ec534049baf529493dc6f2836a85ea (patch)
tree146f98f9953acf406c3b1e2c9bb2e28bca789440 /src/library
parent7d72618b375a21387e18fc42b07a834cee3f2616 (diff)
downloadscala-93275f2d34ec534049baf529493dc6f2836a85ea.tar.gz
scala-93275f2d34ec534049baf529493dc6f2836a85ea.tar.bz2
scala-93275f2d34ec534049baf529493dc6f2836a85ea.zip
fixed bugs in ListBuffer and ArrayBuffer, added...
fixed bugs in ListBuffer and ArrayBuffer, added unit test
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/collection/mutable/ArrayBuffer.scala51
-rw-r--r--src/library/scala/collection/mutable/ListBuffer.scala29
2 files changed, 47 insertions, 33 deletions
diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala
index f33256782c..97bfe642a4 100644
--- a/src/library/scala/collection/mutable/ArrayBuffer.scala
+++ b/src/library/scala/collection/mutable/ArrayBuffer.scala
@@ -55,6 +55,16 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
this
}
+ /** returns the i-th element of this ArrayBuffer. Throws IndexOutOfBoundException if
+ * i is out of bounds.
+ */
+ override def apply(i: Int) = {
+ if ((i < 0) || (i >= size))
+ throw new IndexOutOfBoundsException(i.toString())
+ else
+ array(i)
+ }
+
/** Prepends a number of elements provided by an iterable object
* via its <code>elements</code> method. The identity of the
* buffer is returned.
@@ -72,13 +82,13 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
*/
def insertAll(n: Int, iter: Iterable[A]): Unit = {
if ((n < 0) || (n > size))
- error("cannot insert element " + n + " in ListBuffer")
- val xs = iter.elements.toList
- val len = xs.length
- ensureSize(size + len)
- copy(n, n + len, size - n)
- xs.copyToArray(array, n)
- size = size + len
+ throw new IndexOutOfBoundsException("cannot insert element at " + n);
+ val xs = iter.elements.toList;
+ val len = xs.length;
+ ensureSize(size+len);
+ copy(n, n + len, size - n);
+ xs.copyToArray(array, n);
+ size = size + len;
}
/** Replace element at index <code>n</code> with the new element
@@ -87,14 +97,15 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
* @param n the index of the element to replace.
* @param newelem the new element.
*/
- def update(n: Int, newelem: A): Unit =
+ def update(n: Int, newelem: A): Unit = {
if ((n < 0) || (n >= size))
- error("cannot update element " + n + " in ArrayBuffer")
+ throw new IndexOutOfBoundsException("cannot update element at " + n);
else {
val res = array(n)
array(n) = newelem
res
}
+ }
/** Removes the element on a given index position.
*
@@ -102,24 +113,26 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
*/
def remove(n: Int): A = {
if ((n < 0) || (n >= size))
- error("cannot remove element " + n + " in Buffer")
- val res = array(n)
- copy(n + 1, n, size - n - 1)
- size = size - 1
+ throw new IndexOutOfBoundsException("cannot remove element at " + n);
+ val res = array(n);
+ copy(n + 1, n, size - n - 1);
+ size = size - 1;
res
}
/** Clears the buffer contents.
*/
- def clear: Unit = { size = 0 }
+ def clear: Unit = {
+ size = 0;
+ }
/** Return a clone of this buffer.
*
* @return an <code>ArrayBuffer</code> with the same elements.
*/
override def clone(): Buffer[A] = {
- val res = new ArrayBuffer[A]
- res ++= this
+ val res = new ArrayBuffer[A];
+ res ++= this;
res
}
@@ -130,9 +143,9 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
override def equals(obj: Any): Boolean = obj match {
case that: ArrayBuffer[A] =>
this.length == that.length &&
- elements.zip(that.elements).forall {
- case Pair(thiselem, thatelem) => thiselem == thatelem
- }
+ elements.zip(that.elements).forall {
+ case Pair(thiselem, thatelem) => thiselem == thatelem
+ }
case _ =>
false
}
diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala
index b031a4a9aa..e1f1ff4d72 100644
--- a/src/library/scala/collection/mutable/ListBuffer.scala
+++ b/src/library/scala/collection/mutable/ListBuffer.scala
@@ -66,10 +66,13 @@ final class ListBuffer[A] extends Buffer[A] {
else if (start.head == x) start = start.tail
else {
var cursor = start
- while (!cursor.tail.isEmpty && cursor.tail.head != x)
- cursor = cursor.tail
- if (!cursor.tail.isEmpty)
- cursor.asInstanceOf[scala.::[A]].tl = cursor.tail.tail
+ while (!cursor.tail.isEmpty && cursor.tail.head != x) { cursor = cursor.tail }
+ if (!cursor.tail.isEmpty) {
+ val z = cursor.asInstanceOf[scala.::[A]]
+ if(z.tl == last)
+ last = z
+ z.tl = cursor.tail.tail
+ }
}
}
@@ -110,21 +113,18 @@ final class ListBuffer[A] extends Buffer[A] {
*/
def length: int = start.length
- private def noElem(n: int): All =
- error("element " + n + " does not exist in buffer")
-
/** Returns the <code>n</code>th element of this list. This method
* yields an error if the element does not exist.
*/
def apply(n: Int): A = try {
start(n)
} catch {
- case ex: Error => noElem(n)
+ case ex: Error => throw new IndexOutOfBoundsException(n.toString())
}
/** Replace element at index <code>n</code> with the new element
- * <code>newelem</code>.
- *
+ * <code>newelem</code>. Throws IndexOutOfBoundsException if
+ * n is out of bounds.
* @param n the index of the element to replace.
* @param x the new element.
*/
@@ -146,13 +146,13 @@ final class ListBuffer[A] extends Buffer[A] {
cursor.asInstanceOf[scala.::[A]].tl = newElem
}
} catch {
- case ex: Error => noElem(n)
+ case ex: Error => throw new IndexOutOfBoundsException(n.toString())
}
/** Inserts new elements at the index <code>n</code>. Opposed to method
* <code>update</code>, this method will not replace an element with a new
* one. Instead, it will insert a new element at index <code>n</code>.
- *
+ * Throws IndexOutOfBoundsException if n is out of bounds.
* @param n the index where a new element will be inserted.
* @param iter the iterable object providing all elements to insert.
*/
@@ -181,9 +181,10 @@ final class ListBuffer[A] extends Buffer[A] {
}
}
} catch {
- case ex: Error => noElem(n)
+ case ex: Error => throw new IndexOutOfBoundsException(n.toString())
}
+
/** Removes the element on a given index position.
*
* @param n the index which refers to the element to delete.
@@ -208,7 +209,7 @@ final class ListBuffer[A] extends Buffer[A] {
}
old
} catch {
- case ex: Error => noElem(n)
+ case ex: Error => throw new IndexOutOfBoundsException(n.toString())
}
/** Returns an iterator over all elements of this list.