summaryrefslogtreecommitdiff
path: root/sources/scala/collection
diff options
context:
space:
mode:
authorMatthias Zenger <mzenger@gmail.com>2004-04-16 14:28:21 +0000
committerMatthias Zenger <mzenger@gmail.com>2004-04-16 14:28:21 +0000
commit4d40926c1e35394e244f655a5d31ba6787a2be78 (patch)
tree9e7a73f925bc2dd7933d5824fff9b5c147218414 /sources/scala/collection
parent76606401f9172445c846a464dcb648b826fb6114 (diff)
downloadscala-4d40926c1e35394e244f655a5d31ba6787a2be78.tar.gz
scala-4d40926c1e35394e244f655a5d31ba6787a2be78.tar.bz2
scala-4d40926c1e35394e244f655a5d31ba6787a2be78.zip
Added new traits to the Buffer abstraction.
Diffstat (limited to 'sources/scala/collection')
-rw-r--r--sources/scala/collection/mutable/Buffer.scala4
-rw-r--r--sources/scala/collection/mutable/BufferProxy.scala140
-rw-r--r--sources/scala/collection/mutable/ListBuffer.scala54
-rw-r--r--sources/scala/collection/mutable/SynchronizedBuffer.scala177
4 files changed, 346 insertions, 29 deletions
diff --git a/sources/scala/collection/mutable/Buffer.scala b/sources/scala/collection/mutable/Buffer.scala
index 67cd3351ec..ddb4f65bef 100644
--- a/sources/scala/collection/mutable/Buffer.scala
+++ b/sources/scala/collection/mutable/Buffer.scala
@@ -140,6 +140,6 @@ trait Buffer[A] with Seq[A] {
override def hashCode(): Int = error("unsuitable as hash key");
/** Defines the prefix of the string representation.
- */
- override protected def stringPrefix: String = "Buffer";
+ */
+ override protected def stringPrefix: String = "Buffer";
}
diff --git a/sources/scala/collection/mutable/BufferProxy.scala b/sources/scala/collection/mutable/BufferProxy.scala
new file mode 100644
index 0000000000..06142e4028
--- /dev/null
+++ b/sources/scala/collection/mutable/BufferProxy.scala
@@ -0,0 +1,140 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
+package scala.collection.mutable;
+
+
+/** This is a simple proxy class for <code>scala.collection.mutable.Buffer</code>.
+ * It is most useful for assembling customized set abstractions
+ * dynamically using object composition and forwarding.
+ *
+ * @author Matthias Zenger
+ * @version 1.0, 16/04/2004
+ */
+class BufferProxy[A](buf: Buffer[A]) extends Buffer[A] {
+
+ def length: Int = buf.length;
+
+ def elements: Iterator[A] = buf.elements;
+
+ def apply(n: Int): A = buf.apply(n);
+
+ /** Append a single element to this buffer and return
+ * the identity of the buffer.
+ *
+ * @param elem the element to append.
+ */
+ def +(elem: A): Buffer[A] = buf.+(elem);
+
+ /** Append a single element to this buffer.
+ *
+ * @param elem the element to append.
+ */
+ override def +=(elem: A): Unit = buf.+=(elem);
+
+ /** Appends a number of elements provided by an iterable object
+ * via its <code>elements</code> method. The identity of the
+ * buffer is returned.
+ *
+ * @param iter the iterable object.
+ */
+ override def ++(iter: Iterable[A]): Buffer[A] = buf.++(iter);
+
+ /** Appends a number of elements provided by an iterable object
+ * via its <code>elements</code> method.
+ *
+ * @param iter the iterable object.
+ */
+ override def ++=(iter: Iterable[A]): Unit = buf.++=(iter);
+
+ /** Appends a sequence of elements to this buffer.
+ *
+ * @param elems the elements to append.
+ */
+ override def append(elems: A*): Unit = buf.++=(elems);
+
+ /** Appends a number of elements provided by an iterable object
+ * via its <code>elements</code> method.
+ *
+ * @param iter the iterable object.
+ */
+ override def appendAll(iter: Iterable[A]): Unit = buf.appendAll(iter);
+
+ /** Prepend a single element to this buffer and return
+ * the identity of the buffer.
+ *
+ * @param elem the element to append.
+ */
+ def +:(elem: A): Buffer[A] = buf.+:(elem);
+
+ /** Prepends a number of elements provided by an iterable object
+ * via its <code>elements</code> method. The identity of the
+ * buffer is returned.
+ *
+ * @param iter the iterable object.
+ */
+ override def ++:(iter: Iterable[A]): Buffer[A] = buf.++:(iter);
+
+ /** Prepend an element to this list.
+ *
+ * @param elem the element to prepend.
+ */
+ override def prepend(elems: A*): Unit = buf.prependAll(elems);
+
+ /** Prepends a number of elements provided by an iterable object
+ * via its <code>elements</code> method. The identity of the
+ * buffer is returned.
+ *
+ * @param iter the iterable object.
+ */
+ override def prependAll(elems: Iterable[A]): Unit = buf.prependAll(elems);
+
+ /** Inserts new elements at the index <code>n</code>. Opposed to method
+ * <code>update</code>, this method will not replace an element with a
+ * one. Instead, it will insert the new elements at index <code>n</code>.
+ *
+ * @param n the index where a new element will be inserted.
+ * @param elems the new elements to insert.
+ */
+ override def insert(n: Int, elems: A*): Unit = buf.insertAll(n, elems);
+
+ /** Inserts new elements at the index <code>n</code>. Opposed to method
+ * <code>update</code>, this method will not replace an element with a
+ * one. Instead, it will insert a new element at index <code>n</code>.
+ *
+ * @param n the index where a new element will be inserted.
+ * @param iter the iterable object providing all elements to insert.
+ */
+ def insertAll(n: Int, iter: Iterable[A]): Unit = buf.insertAll(n, iter);
+
+ /** Replace element at index <code>n</code> with the new element
+ * <code>newelem</code>.
+ *
+ * @param n the index of the element to replace.
+ * @param newelem the new element.
+ */
+ def update(n: Int, newelem: A): Unit = buf.update(n, newelem);
+
+ /** Removes the element on a given index position.
+ *
+ * @param n the index which refers to the element to delete.
+ */
+ def remove(n: Int): A = buf.remove(n);
+
+ /** Clears the buffer contents.
+ */
+ def clear: Unit = buf.clear;
+
+ /** The hashCode method always yields an error, since it is not
+ * safe to use buffers as keys in hash tables.
+ *
+ * @return never.
+ */
+ override def hashCode(): Int = buf.hashCode();
+}
diff --git a/sources/scala/collection/mutable/ListBuffer.scala b/sources/scala/collection/mutable/ListBuffer.scala
index 82dbb9adc7..f8fd66ca8a 100644
--- a/sources/scala/collection/mutable/ListBuffer.scala
+++ b/sources/scala/collection/mutable/ListBuffer.scala
@@ -42,26 +42,26 @@ class ListBuffer[A] extends Buffer[A] with MutableList[A] {
* @param iter the iterable object providing all elements to insert.
*/
def insertAll(n: Int, iter: Iterable[A]): Unit = {
- val it = iter.elements;
- while (it.hasNext) {
- val newelem = it.next;
- if (n == 0)
- prepend(newelem);
- else if (n >= len)
- append(newelem);
- else {
- var elem = first;
- var i = n;
- while (i > 1) {
- elem = elem.next;
- if (elem == null)
- error("cannot insert element " + n + " in ListBuffer");
- i = i - 1;
- }
- val old = elem.next;
- elem.next = new LinkedList[A](newelem, old);
- }
- }
+ val it = iter.elements;
+ while (it.hasNext) {
+ val newelem = it.next;
+ if (n == 0)
+ prepend(newelem);
+ else if (n >= len)
+ append(newelem);
+ else {
+ var elem = first;
+ var i = n;
+ while (i > 1) {
+ elem = elem.next;
+ if (elem == null)
+ error("cannot insert element " + n + " in ListBuffer");
+ i = i - 1;
+ }
+ val old = elem.next;
+ elem.next = new LinkedList[A](newelem, old);
+ }
+ }
}
/** Replace element at index <code>n</code> with the new element
@@ -120,14 +120,14 @@ class ListBuffer[A] extends Buffer[A] with MutableList[A] {
* @return true, iff both buffers contain the same sequence of elements.
*/
override def equals(obj: Any): Boolean = obj match {
- case that: ListBuffer[A] =>
- elements.zip(that.elements).forall {
- case Pair(thiselem, thatelem) => thiselem == thatelem;
- }
- case _ => false
+ case that: ListBuffer[A] =>
+ elements.zip(that.elements).forall {
+ case Pair(thiselem, thatelem) => thiselem == thatelem;
+ }
+ case _ => false
}
/** Defines the prefix of the string representation.
- */
- override protected def stringPrefix: String = "ListBuffer";
+ */
+ override protected def stringPrefix: String = "ListBuffer";
}
diff --git a/sources/scala/collection/mutable/SynchronizedBuffer.scala b/sources/scala/collection/mutable/SynchronizedBuffer.scala
new file mode 100644
index 0000000000..d41da4f858
--- /dev/null
+++ b/sources/scala/collection/mutable/SynchronizedBuffer.scala
@@ -0,0 +1,177 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
+package scala.collection.mutable;
+
+
+/** This trait should be used as a mixin. It synchronizes the <code>Buffer</code>
+ * methods of the class into which it is mixed in.
+ *
+ * @author Matthias Zenger
+ * @version 1.0, 08/07/2003
+ */
+trait SynchronizedBuffer[A] extends Buffer[A] {
+
+ abstract override def length: Int = synchronized {
+ super.length;
+ }
+
+ abstract override def elements: Iterator[A] = synchronized {
+ super.elements;
+ }
+
+ abstract override def apply(n: Int): A = synchronized {
+ super.apply(n);
+ }
+
+ /** Append a single element to this buffer and return
+ * the identity of the buffer.
+ *
+ * @param elem the element to append.
+ */
+ abstract override def +(elem: A): Buffer[A] = synchronized {
+ super.+(elem);
+ }
+
+ /** Append a single element to this buffer.
+ *
+ * @param elem the element to append.
+ */
+ override def +=(elem: A): Unit = synchronized {
+ super.+=(elem);
+ }
+
+ /** Appends a number of elements provided by an iterable object
+ * via its <code>elements</code> method. The identity of the
+ * buffer is returned.
+ *
+ * @param iter the iterable object.
+ */
+ override def ++(iter: Iterable[A]): Buffer[A] = synchronized {
+ super.++(iter);
+ }
+
+ /** Appends a number of elements provided by an iterable object
+ * via its <code>elements</code> method.
+ *
+ * @param iter the iterable object.
+ */
+ override def ++=(iter: Iterable[A]): Unit = synchronized {
+ super.++=(iter);
+ }
+
+ /** Appends a sequence of elements to this buffer.
+ *
+ * @param elems the elements to append.
+ */
+ override def append(elems: A*): Unit = synchronized {
+ super.++=(elems);
+ }
+
+ /** Appends a number of elements provided by an iterable object
+ * via its <code>elements</code> method.
+ *
+ * @param iter the iterable object.
+ */
+ override def appendAll(iter: Iterable[A]): Unit = synchronized {
+ super.appendAll(iter);
+ }
+
+ /** Prepend a single element to this buffer and return
+ * the identity of the buffer.
+ *
+ * @param elem the element to append.
+ */
+ abstract override def +:(elem: A): Buffer[A] = synchronized {
+ super.+:(elem);
+ }
+
+ /** Prepends a number of elements provided by an iterable object
+ * via its <code>elements</code> method. The identity of the
+ * buffer is returned.
+ *
+ * @param iter the iterable object.
+ */
+ override def ++:(iter: Iterable[A]): Buffer[A] = synchronized {
+ super.++:(iter);
+ }
+
+ /** Prepend an element to this list.
+ *
+ * @param elem the element to prepend.
+ */
+ override def prepend(elems: A*): Unit = synchronized {
+ super.prependAll(elems);
+ }
+
+ /** Prepends a number of elements provided by an iterable object
+ * via its <code>elements</code> method. The identity of the
+ * buffer is returned.
+ *
+ * @param iter the iterable object.
+ */
+ override def prependAll(elems: Iterable[A]): Unit = synchronized {
+ super.prependAll(elems);
+ }
+
+ /** Inserts new elements at the index <code>n</code>. Opposed to method
+ * <code>update</code>, this method will not replace an element with a
+ * one. Instead, it will insert the new elements at index <code>n</code>.
+ *
+ * @param n the index where a new element will be inserted.
+ * @param elems the new elements to insert.
+ */
+ override def insert(n: Int, elems: A*): Unit = synchronized {
+ super.insertAll(n, elems);
+ }
+
+ /** Inserts new elements at the index <code>n</code>. Opposed to method
+ * <code>update</code>, this method will not replace an element with a
+ * one. Instead, it will insert a new element at index <code>n</code>.
+ *
+ * @param n the index where a new element will be inserted.
+ * @param iter the iterable object providing all elements to insert.
+ */
+ abstract override def insertAll(n: Int, iter: Iterable[A]): Unit = synchronized {
+ super.insertAll(n, iter);
+ }
+
+ /** Replace element at index <code>n</code> with the new element
+ * <code>newelem</code>.
+ *
+ * @param n the index of the element to replace.
+ * @param newelem the new element.
+ */
+ abstract override def update(n: Int, newelem: A): Unit = synchronized {
+ super.update(n, newelem);
+ }
+
+ /** Removes the element on a given index position.
+ *
+ * @param n the index which refers to the element to delete.
+ */
+ abstract override def remove(n: Int): A = synchronized {
+ super.remove(n);
+ }
+
+ /** Clears the buffer contents.
+ */
+ abstract override def clear: Unit = synchronized {
+ super.clear;
+ }
+
+ /** The hashCode method always yields an error, since it is not
+ * safe to use buffers as keys in hash tables.
+ *
+ * @return never.
+ */
+ override def hashCode(): Int = synchronized {
+ super.hashCode();
+ }
+}