diff options
author | Martin Odersky <odersky@gmail.com> | 2009-02-05 19:25:43 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2009-02-05 19:25:43 +0000 |
commit | 0ecacced0347e4e3b83989a274b9de53868a3a57 (patch) | |
tree | cf5bb351b3acef82ad88862bee320b9c6e32a4c3 /src/library/scalax/collection/mutable | |
parent | 48355ee28a930f19000901d761f24ca44b324c1f (diff) | |
download | scala-0ecacced0347e4e3b83989a274b9de53868a3a57.tar.gz scala-0ecacced0347e4e3b83989a274b9de53868a3a57.tar.bz2 scala-0ecacced0347e4e3b83989a274b9de53868a3a57.zip |
added support for Strings, arrays, sets.
Diffstat (limited to 'src/library/scalax/collection/mutable')
-rw-r--r-- | src/library/scalax/collection/mutable/Appendable.scala | 94 | ||||
-rwxr-xr-x | src/library/scalax/collection/mutable/Array.scala | 410 | ||||
-rw-r--r-- | src/library/scalax/collection/mutable/ArrayBuffer.scala | 26 | ||||
-rw-r--r-- | src/library/scalax/collection/mutable/Buffer.scala | 40 | ||||
-rwxr-xr-x | src/library/scalax/collection/mutable/Iterable.scala | 32 | ||||
-rw-r--r-- | src/library/scalax/collection/mutable/ListBuffer.scala | 15 | ||||
-rwxr-xr-x | src/library/scalax/collection/mutable/OrderedIterable.scala | 22 | ||||
-rw-r--r-- | src/library/scalax/collection/mutable/ResizableArray.scala | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | src/library/scalax/collection/mutable/Sequence.scala (renamed from src/library/scalax/collection/mutable/BuilderProxy.scala) | 21 | ||||
-rw-r--r-- | src/library/scalax/collection/mutable/Set.scala | 119 | ||||
-rwxr-xr-x | src/library/scalax/collection/mutable/StringBuilder.scala | 10 | ||||
-rw-r--r-- | src/library/scalax/collection/mutable/Vector.scala | 11 |
12 files changed, 664 insertions, 138 deletions
diff --git a/src/library/scalax/collection/mutable/Appendable.scala b/src/library/scalax/collection/mutable/Appendable.scala deleted file mode 100644 index 8a45724bff..0000000000 --- a/src/library/scalax/collection/mutable/Appendable.scala +++ /dev/null @@ -1,94 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id: Iterable.scala 15188 2008-05-24 15:01:02Z stepancheg $ - -package scalax.collection.mutable - -/** This class represents collections that can be augmented using a += operator. - * - * @autor Martin Odersky - * @owner Martin Odersky - * @version 2.8 - */ -trait Appendable[A] { - - /** Append a single element to this buffer. - * - * @param elem the element to append. - */ - def +=(elem: A): Unit - - /** Append a two or more elements to this buffer. - * - * @param elem1 the first element to append. - * @param elem2 the second element to append. - * @param elems the remaining elements to append. - */ - def +=(elem1: A, elem2: A, elems: A*) { - this += elem1 - this += elem2 - this ++= elems.asInstanceOf[Iterable[A]] // !@! - } - - /** Appends a number of elements provided by an iterator - * - * @param iter the iterator. - */ - def ++=(iter: collection.Iterator[A]) { iter foreach += } - - /** Appends a number of elements provided by an iterable object - * via its <code>elements</code> method. - * - * @param iter the iterable object. - */ - def ++=(iter: collection.Iterable[A]) { iter foreach += } - - /** Append a single element to this buffer and return - * the identity of the buffer. - * - * @param elem the element to append. - */ - def +(elem: A): this.type = { this += elem; this } - - /** Append two or more elements to this buffer and return - * the identity of the buffer. - * - * @param elem1 the first element to append. - * @param elem2 the second element to append. - * @param elems the remaining elements to append. - */ - def +(elem1: A, elem2: A, elems: A*): this.type = - this + elem1 + elem2 ++ elems.asInstanceOf[Iterable[A]] // !@! - - /** 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. - * @return the updated buffer. - */ - def ++(iter: Iterable[A]): this.type = { this ++= iter; this } - - /** Appends a number of elements provided by an iterator - * via its <code>elements</code> method. The identity of the - * buffer is returned. - * - * @param iter the iterator - * @return the updated buffer. - */ - def ++(iter: Iterator[A]): this.type = { this ++= iter; this } - - /** Clears the buffer contents. - */ - def clear() -} - - - - diff --git a/src/library/scalax/collection/mutable/Array.scala b/src/library/scalax/collection/mutable/Array.scala new file mode 100755 index 0000000000..c2b4d19101 --- /dev/null +++ b/src/library/scalax/collection/mutable/Array.scala @@ -0,0 +1,410 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Array.scala 17000 2009-01-29 13:05:53Z odersky $ + + +package scalax.collection.mutable + +import scalax.collection.generic._ +import compat.Platform.arraycopy + +/** This object contains utility methods operating on arrays. + * + * @author Martin Odersky + * @version 1.0 + */ +object Array extends SequenceFactory[Array] { + + /** Copy one array to another. + * Equivalent to + * <code>System.arraycopy(src, srcPos, dest, destPos, length)</code>, + * except that this works also for polymorphic and boxed arrays. + * + * @param src ... + * @param srcPos ... + * @param dest ... + * @param destPos ... + * @param length ... + */ + def copy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int) { + src match { + case xs: runtime.BoxedArray[_] => + xs.copyTo(srcPos, dest, destPos, length) + case _ => + dest match { + case xs: runtime.BoxedArray[_] => + xs.copyFrom(src, srcPos, destPos, length) + case _ => + def fillDest[T](da: Array[T], sa: Int=>T) { + var d = destPos + for (s <- srcPos to srcPos+length-1) { + da(d) = sa(s); d += 1 + } + } + if (dest.isInstanceOf[Array[Any]]) { + def fill(sa: Int=>Any) = fillDest(dest.asInstanceOf[Array[Any]], sa) + src match { + case sa:Array[Int] => fill(s=>Int.box(sa(s))) +/*!!! + case sa:Array[Long] => fill(s=>Long.box(sa(s))) + case sa:Array[Char] => fill(s=>Char.box(sa(s))) + case sa:Array[Boolean] => fill(s=>Boolean.box(sa(s))) + case sa:Array[Byte] => fill(s=>Byte.box(sa(s))) + case sa:Array[Short] => fill(s=>Short.box(sa(s))) + case sa:Array[Double] => fill(s=>Double.box(sa(s))) + case sa:Array[Float] => fill(s=>Float.box(sa(s))) +*/ + case _ => arraycopy(src, srcPos, dest, destPos, length) + } + } else if (dest.isInstanceOf[Array[AnyVal]]) { + def fill(sa: Int=>AnyVal) = fillDest(dest.asInstanceOf[Array[AnyVal]], sa) + src match { + case sa:Array[Int] => fill(sa(_)) +/*!!! + case sa:Array[Long] => fill(sa(_)) + case sa:Array[Char] => fill(sa(_)) + case sa:Array[Boolean] => fill(sa(_)) + case sa:Array[Byte] => fill(sa(_)) + case sa:Array[Short] => fill(sa(_)) + case sa:Array[Double] => fill(sa(_)) + case sa:Array[Float] => fill(sa(_)) +*/ + case _ => arraycopy(src, srcPos, dest, destPos, length) + } + } else + arraycopy(src, srcPos, dest, destPos, length) + } + } + } + + /** Concatenate all argument sequences into a single array. + * + * @param xs the given argument sequences + * @return the array created from the concatenated arguments + */ + def concat[T](xs: Seq[T]*): Array[T] = { + var len = 0 + for (x <- xs) len += x.length + val result = new Array[T](len) + var start = 0 + for (x <- xs) { + copy(x.toArray, 0, result, start, x.length) + start += x.length + } + result + } + + /** Create an array with given elements. + * + * @param xs the elements to put in the array + * @return the array containing elements xs. + */ + def apply[A](xs: A*): Array[A] = { + val array = new Array[A](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } +/*!!! enable when arrays in Scala + def apply(xs: Boolean*): Array[Boolean] = { + val array = new Array[Boolean](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + def apply(xs: Byte*): Array[Byte] = { + val array = new Array[Byte](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + def apply(xs: Short*): Array[Short] = { + val array = new Array[Short](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + def apply(xs: Char*): Array[Char] = { + val array = new Array[Char](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + def apply(xs: Int*): Array[Int] = { + val array = new Array[Int](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + def apply(xs: Long*): Array[Long] = { + val array = new Array[Long](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + def apply(xs: Float*): Array[Float] = { + val array = new Array[Float](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + def apply(xs: Double*): Array[Double] = { + val array = new Array[Double](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + def apply(xs: Unit*): Array[Unit] = { + val array = new Array[Unit](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } +*/ + /** Create an array containing several copies of an element. + * + * @param n the length of the resulting array + * @param elem the element composing the resulting array + * @return an array composed of n elements all equal to elem + * @deprecated use `Array.fill` instead. + */ + @deprecated def make[A](n: Int, elem: A): Array[A] = { + val a = new Array[A](n) + var i = 0 + while (i < n) { + a(i) = elem + i += 1 + } + a + } + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n)</code> + * @deprecated use `Array.tabulate` instead. + */ + @deprecated def fromFunction[A](f: Int => A)(n: Int): Array[A] = { + val a = new Array[A](n) + var i = 0 + while (i < n) { + a(i) = f(i) + i += 1 + } + a + } + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n1, 0..n2)</code> + * @deprecated use `Array.tabulate` instead. + */ + @deprecated def fromFunction[A](f: (Int, Int) => A)(n1: Int, n2: Int): Array[Array[A]] = + fromFunction(i => fromFunction(f(i, _))(n2))(n1) + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n1, 0..n2, 0..n3)</code> + * @deprecated use `Array.tabulate` instead. + */ + @deprecated def fromFunction[A](f: (Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int): Array[Array[Array[A]]] = + fromFunction(i => fromFunction(f(i, _, _))(n2, n3))(n1) + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n1, 0..n2, 0..n3, 0..n4)</code> + * @deprecated use `Array.tabulate` instead. + */ + @deprecated def fromFunction[A](f: (Int, Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int, n4: Int): Array[Array[Array[Array[A]]]] = + fromFunction(i => fromFunction(f(i, _, _, _))(n2, n3, n4))(n1) + + /** Create an array containing the values of a given function <code>f</code> + * over given range <code>[0..n1, 0..n2, 0..n3, 0..n4, 0..n5)</code> + * @deprecated use `Array.tabulate` instead. + */ + @deprecated def fromFunction[A](f: (Int, Int, Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int, n4: Int, n5: Int): Array[Array[Array[Array[Array[A]]]]] = + fromFunction(i => fromFunction(f(i, _, _, _, _))(n2, n3, n4, n5))(n1) + + /** Create array with given dimensions */ + def ofDim[A](n1: Int): Array[A] = + new Array[A](n1) + def ofDim[A](n1: Int, n2: Int): Array[Array[A]] = + tabulate(n1)(_ => ofDim[A](n2)) + def ofDim[A](n1: Int, n2: Int, n3: Int): Array[Array[Array[A]]] = + tabulate(n1)(_ => ofDim[A](n2, n3)) + def ofDim[A](n1: Int, n2: Int, n3: Int, n4: Int): Array[Array[Array[Array[A]]]] = + tabulate(n1)(_ => ofDim[A](n2, n3, n4)) + def ofDim[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int): Array[Array[Array[Array[Array[A]]]]] = + tabulate(n1)(_ => ofDim[A](n2, n3, n4, n5)) +} + +/** This class represents polymorphic arrays. <code>Array[T]</code> is Scala's representation + * for Java's <code>T[]</code>. + * + * @author Martin Odersky + * @version 1.0 + */ +final class Array[A](_length: Int) extends Vector[A] with mutable.VectorTemplate[Array, A] { + + /** Multidimensional array creation + * @deprecated use Array.ofDim instead + */ + @deprecated def this(dim1: Int, dim2: Int) = { + this(dim1) + throw new Error() + } + + /** Multidimensional array creation + * @deprecated use Array.ofDim instead */ + @deprecated def this(dim1: Int, dim2: Int, dim3: Int) = { + this(dim1) + throw new Error() + } + + /** Multidimensional array creation + * @deprecated use Array.ofDim instead */ + @deprecated def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int) = { + this(dim1) + throw new Error() + } + + /** Multidimensional array creation + * @deprecated use Array.ofDim instead */ + @deprecated def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int) = { + this(dim1); + throw new Error() + } + + /** Multidimensional array creation + * @deprecated use Array.ofDim instead */ + @deprecated def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int, dim6: Int) = { + this(dim1) + throw new Error() + } + + /** Multidimensional array creation + * @deprecated use Array.ofDim instead */ + @deprecated def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int, dim6: Int, dim7: Int) = { + this(dim1) + throw new Error() + } + + /** Multidimensional array creation + * @deprecated use Array.ofDim instead */ + @deprecated def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int, dim6: Int, dim7: Int, dim8: Int) = { + this(dim1) + throw new Error() + } + + /** Multidimensional array creation + * @deprecated use Array.ofDim instead */ + @deprecated def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int, dim6: Int, dim7: Int, dim8: Int, dim9: Int) = { + this(dim1) + throw new Error() + } + + /** The length of the array */ + def length: Int = throw new Error() + + /** The element at given index. + * <p> + * Indices start a <code>0</code>; <code>xs.apply(0)</code> is the first + * element of array <code>xs</code>. + * </p> + * <p> + * Note the indexing syntax <code>xs(i)</code> is a shorthand for + * <code>xs.apply(i)</code>. + * </p> + * + * @param i the index + * @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or + * <code>length <= i</code> + */ + def apply(i: Int): A = throw new Error() + + /* Create a new array builder */ + def newBuilder[B]: Builder[Array, B] = throw new Error() + + /** <p> + * Update the element at given index. + * </p> + * <p> + * Indices start a <code>0</code>; <code>xs.apply(0)</code> is the first + * element of array <code>xs</code>. + * </p> + * <p> + * Note the indexing syntax <code>xs(i) = x</code> is a shorthand + * for <code>xs.update(i, x)</code>. + * </p> + * + * @param i the index + * @param x the value to be written at index <code>i</code> + * @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or + * <code>length <= i</code> + */ + override def update(i: Int, x: A) { throw new Error() } + + /** + * @return a deep string representation of this array. + */ + def deepToString(): String = throw new Error() + + /** <p> + * Returns a string representation of this array object. The resulting string + * begins with the string <code>start</code> and is finished by the string + * <code>end</code>. Inside, the string representations of elements (w.r.t. + * the method <code>deepToString()</code>) are separated by the string + * <code>sep</code>. For example: + * </p> + * <p> + * <code>Array(Array(1, 2), Array(3)).deepMkString("[", "; ", "]") = "[[1; 2]; [3]]"</code> + * </p> + * + * @param start starting string. + * @param sep separator string. + * @param end ending string. + * @return a string representation of this array object. + */ + def deepMkString(start: String, sep: String, end: String): String = + throw new Error() + + /** Returns a string representation of this array object. The string + * representations of elements (w.r.t. the method <code>deepToString()</code>) + * are separated by the string <code>sep</code>. + * + * @param sep separator string. + * @return a string representation of this array object. + */ + def deepMkString(sep: String): String = throw new Error() + + /** <p> + * Returns <code>true</code> if the two specified arrays are + * <em>deeply equal</em> to one another. + * </p> + * <p> + * Two array references are considered deeply equal if both are null, + * or if they refer to arrays that contain the same number of elements + * and all corresponding pairs of elements in the two arrays are deeply + * equal. + * </p> + * <p> + * See also method <code>deepEquals</code> in the Java class + * <a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html" + * target="_top">java.utils.Arrays</a> + * </p> + * + * @param that the second + * @return <code>true</code> iff both arrays are deeply equal. + */ + def deepEquals(that: Any): Boolean = throw new Error() + +} diff --git a/src/library/scalax/collection/mutable/ArrayBuffer.scala b/src/library/scalax/collection/mutable/ArrayBuffer.scala index df7f6ba758..dbdb96e004 100644 --- a/src/library/scalax/collection/mutable/ArrayBuffer.scala +++ b/src/library/scalax/collection/mutable/ArrayBuffer.scala @@ -11,6 +11,13 @@ package scalax.collection.mutable +import generic.SequenceFactory + +/* Factory object for `ArrayBuffer` class */ +object ArrayBuffer extends SequenceFactory[ArrayBuffer] { + def apply[A](args: A*): ArrayBuffer[A] = new ArrayBuffer[A] ++ args.asInstanceOf[Iterable[A]] // !@! +} + /** An implementation of the <code>Buffer</code> class using an array to * represent the assembled sequence internally. Append, update and random * access take constant time (amortized time). Prepends and removes are @@ -21,7 +28,14 @@ package scalax.collection.mutable * @version 2.8 */ @serializable -class ArrayBuffer[A] extends Buffer[A] with Builder[ArrayBuffer, A] with ResizableArray[A] { +class ArrayBuffer[A](override protected val initialSize: Int) + extends Buffer[A] + with Vector[A] + with generic.mutable.VectorTemplate[ArrayBuffer, A] + with Builder[ArrayBuffer, A] + with ResizableArray[A] { + + def this() = this(16) def clear() { reduceToSize(0) } @@ -40,14 +54,14 @@ class ArrayBuffer[A] extends Buffer[A] with Builder[ArrayBuffer, A] with Resizab * via its <code>elements</code> method. The identity of the * buffer is returned. * - * @param iter the iterable object. + * @param iter the iterfable object. * @return the updated buffer. */ - override def ++=(iter: Iterable[A]) = iter match { - case v: Vector[A] => + override def ++=(iter: collection.Iterable[A]) = iter match { + case v: Vector[_] => val n = v.length ensureSize(size0 + n) - v.copyToArray(array.asInstanceOf[Array[Any]], n) + v.copyToArray(array.asInstanceOf[scala.Array[Any]], n) case _ => super.++=(iter) } @@ -91,7 +105,7 @@ class ArrayBuffer[A] extends Buffer[A] with Builder[ArrayBuffer, A] with Resizab val len = xs.length ensureSize(size0 + len) copy(n, n + len, size0 - n) - xs.copyToArray(array.asInstanceOf[Array[Any]], n) + xs.copyToArray(array.asInstanceOf[scala.Array[Any]], n) size0 += len } diff --git a/src/library/scalax/collection/mutable/Buffer.scala b/src/library/scalax/collection/mutable/Buffer.scala index 2a9eb263e9..502f99758a 100644 --- a/src/library/scalax/collection/mutable/Buffer.scala +++ b/src/library/scalax/collection/mutable/Buffer.scala @@ -11,7 +11,13 @@ package scalax.collection.mutable -import generic.mutable.VectorTemplate +import generic.{SequenceFactory, SequenceTemplate} +import generic.mutable.{Growable, Shrinkable} + +/* Factory object for `Buffer` trait */ +object Buffer extends SequenceFactory[Buffer] { + def apply[A](args: A*): Buffer[A] = ArrayBuffer.apply(args: _*) +} /** Buffers are used to create sequences of elements incrementally by * appending, prepending, or inserting new elements. It is also @@ -23,9 +29,12 @@ import generic.mutable.VectorTemplate * @version 2.8 */ @cloneable -trait Buffer[A] extends Vector[A] with VectorTemplate[Buffer, A] with Appendable[A] +trait Buffer[A] extends mutable.Sequence[A] + with SequenceTemplate[Buffer, A] + with Growable[A] + with Shrinkable[A] // with Scriptable[Message[(Location, A)]] - with CloneableCollection + with CloneableCollection { // Abstract methods from Vector: @@ -35,13 +44,6 @@ trait Buffer[A] extends Vector[A] with VectorTemplate[Buffer, A] with Appendable */ def apply(n: Int): A - /** Return number of elements in the buffer - */ - def length: Int - - /** Create a new buffer of the same kind as this one */ - def newBuilder[B]: Builder[Buffer, B] = new ArrayBuffer[B] - /** Replace element at index <code>n</code> with the new element * <code>newelem</code>. * @@ -51,6 +53,16 @@ trait Buffer[A] extends Vector[A] with VectorTemplate[Buffer, A] with Appendable */ def update(n: Int, newelem: A): Unit + /** Return number of elements in the buffer + */ + def length: Int + + /** Create a new buffer of the same kind as this one */ + def newBuilder[B]: Builder[Buffer, B] = new ArrayBuffer[B] + + override def newBuilder[B](sizeHint: Int): Builder[Buffer, B] = + new ArrayBuffer[B](sizeHint) + // Abstract methods from Appendable /** Append a single element to this buffer. @@ -111,14 +123,6 @@ trait Buffer[A] extends Vector[A] with VectorTemplate[Buffer, A] with Appendable if (i != -1) remove(i) } - /** Removes a single element from this buffer, at its first occurrence, - * and returns the identity of the buffer. - * If the buffer does not contain that element, it is unchanged - * - * @param x the element to remove. - */ - def - (x: A): this.type = { -=(x); this } - /** Prepend two ore more elements to this buffer and return * the identity of the buffer. * @param elem the element to prepend. diff --git a/src/library/scalax/collection/mutable/Iterable.scala b/src/library/scalax/collection/mutable/Iterable.scala new file mode 100755 index 0000000000..0872c3c549 --- /dev/null +++ b/src/library/scalax/collection/mutable/Iterable.scala @@ -0,0 +1,32 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2008, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Iterable.scala 15188 2008-05-24 15:01:02Z stepancheg $ + +package scalax.collection.mutable + +import generic._ + +/** Collection classes mixing in this class provide a method + * <code>elements</code> which returns an iterator over all the + * elements contained in the collection. + * + * @note If a collection has a known <code>size</code>, it should also sub-type <code>SizedIterable</code>. + * + * @author Matthias Zenger + * @autor Martin Odersky + * @owner Martin Odersky + * @version 2.8 + */ +trait Iterable[A] extends collection.Iterable[A] with IterableTemplate[Iterable, A] + +/* Factory object for `Iterable` class */ +object Iterable extends IterableFactory[Iterable] { + /** The empty iterable */ + def apply[A](args: A*): Iterable[A] = ArrayBuffer.apply(args: _*) // !!! swicth to Array? +} diff --git a/src/library/scalax/collection/mutable/ListBuffer.scala b/src/library/scalax/collection/mutable/ListBuffer.scala index 3b7587d6d2..dff29f6453 100644 --- a/src/library/scalax/collection/mutable/ListBuffer.scala +++ b/src/library/scalax/collection/mutable/ListBuffer.scala @@ -11,10 +11,15 @@ package scalax.collection.mutable -import generic.SequenceForwarder +import generic.{SequenceForwarder, SequenceFactory, SequenceTemplate} import immutable.List import collection.immutable.{List, Nil, ::} +/* Factory object for `ListBuffer` class */ +object ListBuffer extends SequenceFactory[ListBuffer] { + def apply[A](args: A*): ListBuffer[A] = new ListBuffer[A] +} + /** A Buffer implementation back up by a list. It provides constant time * prepend and append. Most other operations are linear. * @@ -25,9 +30,12 @@ import collection.immutable.{List, Nil, ::} @serializable final class ListBuffer[A] extends Buffer[A] + with SequenceTemplate[ListBuffer, A] with Builder[List, A] with SequenceForwarder[A] { + import collection.immutable.Sequence + private var start: List[A] = Nil private var last0: ::[A] = _ private var exported: Boolean = false @@ -178,8 +186,7 @@ final class ListBuffer[A] } catch { case ex: Exception => throw new IndexOutOfBoundsException(n.toString()) - } - } + } } // Implementation of abstract method in Builder @@ -258,7 +265,7 @@ final class ListBuffer[A] } /** expose the underlying list but do not mark it as exported */ - override def readOnly : List[A] = start + def readOnly: List[A] = start // Private methods diff --git a/src/library/scalax/collection/mutable/OrderedIterable.scala b/src/library/scalax/collection/mutable/OrderedIterable.scala new file mode 100755 index 0000000000..90ef8018c7 --- /dev/null +++ b/src/library/scalax/collection/mutable/OrderedIterable.scala @@ -0,0 +1,22 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2008, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Iterable.scala 15188 2008-05-24 15:01:02Z stepancheg $ + +package scalax.collection.mutable + +import generic._ + +trait OrderedIterable[A] extends Iterable[A] with collection.OrderedIterable[A] with OrderedIterableTemplate[OrderedIterable, A] + +/* Factory object for `OrderedIterable` class */ +object OrderedIterable extends IterableFactory[OrderedIterable] { + /** The empty iterable */ + def apply[A](args: A*): OrderedIterable[A] = ArrayBuffer.apply(args: _*) // !!! swicth to Array? +} + diff --git a/src/library/scalax/collection/mutable/ResizableArray.scala b/src/library/scalax/collection/mutable/ResizableArray.scala index 93a4c7753a..11c3659712 100644 --- a/src/library/scalax/collection/mutable/ResizableArray.scala +++ b/src/library/scalax/collection/mutable/ResizableArray.scala @@ -19,6 +19,8 @@ package scalax.collection.mutable */ trait ResizableArray[A] extends Vector[A] { + import scala.Array // !!! + protected def initialSize: Int = 16 protected var array: Array[AnyRef] = new Array[AnyRef](initialSize min 1) diff --git a/src/library/scalax/collection/mutable/BuilderProxy.scala b/src/library/scalax/collection/mutable/Sequence.scala index cf5b3d69a5..c4d72231d5 100644..100755 --- a/src/library/scalax/collection/mutable/BuilderProxy.scala +++ b/src/library/scalax/collection/mutable/Sequence.scala @@ -1,22 +1,23 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2008, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** \* */ -// $Id: ListBuffer.scala 14378 2008-03-13 11:39:05Z dragos $ +// $Id: Iterable.scala 15188 2008-05-24 15:01:02Z stepancheg $ package scalax.collection.mutable -abstract class BuilderProxy[+CC[B], A] extends Builder[CC, A] with Proxy { - val self: Builder[DD, A] forSome { type DD[X] } - def +=(x: A) = self += x - def elements: Iterator[A] = self.elements - def result: CC[A] - def clear() = self.clear() - override def ++=(xs: Iterator[A]) = self ++= xs - override def ++=(xs: Iterable[A]) = self ++= xs +import generic._ + +trait Sequence[A] extends OrderedIterable[A] with collection.Sequence[A] with SequenceTemplate[Sequence, A] + +/* Factory object for `Sequence` class */ +object Sequence extends SequenceFactory[Sequence] { + /** The empty sequence */ + def apply[A](args: A*): Sequence[A] = ArrayBuffer.apply(args: _*) // !!! swicth to Array? } + diff --git a/src/library/scalax/collection/mutable/Set.scala b/src/library/scalax/collection/mutable/Set.scala new file mode 100644 index 0000000000..40dc643012 --- /dev/null +++ b/src/library/scalax/collection/mutable/Set.scala @@ -0,0 +1,119 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Set.scala 16893 2009-01-13 13:09:22Z cunei $ + + +package scalax.collection.mutable + +import collection.generic._ +import collection.generic.mutable.{Growable, Shrinkable} + +/** The canonical factory methods for <a href="Set.html">mutable sets</a>. + * Currently these return <a href="HashSet.html">HashSet's</a>. + */ +object Set extends generic.SetFactory[Set] { + /** The empty map of this type; this is implemented as a hashtable */ + def empty[A]: Set[A] = null // !!! new HashSet[A] +} + +/** This class represents mutable sets. Concrete set implementations + * just have to provide functionality for the abstract methods in + * <a href="../Set.html" target="contentFrame"> + * <code>scala.collection.Set</code></a> as well as for <code>+=</code>, + * <code>-= and <code>clear</code>. + * + * @author Matthias Zenger + * @author Martin Odersky + * @version 2.8 + */ +@cloneable +trait Set[A] extends OrderedIterable[A] + with collection.Set[A] + with SetTemplate[Set, A] + with Growable[A] + with Shrinkable[A] + with Cloneable[A] { +self => + + /** This method allows one to add or remove an element <code>elem</code> + * from this set depending on the value of parameter <code>included</code>. + * Typically, one would use the following syntax: + * <pre>set(elem) = true</pre> + * + */ + def update(elem: A, included: Boolean) { + if (included) this += elem else this -= elem + } + + /** Add a new element to the set. + * + * @param elem the element to be added + */ + def +=(elem: A) + + /** Removes a single element from a set. + * @param elem The element to be removed. + */ + def -=(elem: A) + + // Bridge methods to methods in Growable/Shrinkable; + // we can't directly inherit these because they override nothing. + + override def - (elem: A): this.type = super.-(elem) + override def - (elem1: A, elem2: A, elems: A*): this.type = super.-(elem1, elem2, elems: _*) + override def -- (elems: collection.Iterable[A]): this.type = super.--(elems) + override def -- (elems: Iterator[A]): this.type = super.--(elems) + + override def + (elem: A): this.type = super.+(elem) + override def + (elem1: A, elem2: A, elems: A*): this.type = super.+(elem1, elem2, elems: _*) + override def ++ (elems: collection.Iterable[A]): this.type = super.++(elems) + override def ++ (elems: Iterator[A]): this.type = super.++(elems) + + /** This method computes an intersection with set <code>that</code>. + * It removes all the elements that are not present in <code>that</code>. + * + * @param that the set to intersect with. + */ + def intersect(that: Set[A]) { retain(that.contains) } + + /** Method <code>retain removes all elements from the set for + * which the predicate <code>p</code> yields the value <code>false</code>. + */ + def retain(p: A => Boolean): Unit = foreach (elem => if (!p(elem)) -=(elem)) + + /** Removes all elements from the set. After this operation is completed, + * the set will be empty. + */ + def clear() { elements foreach -= } + + /** Send a message to this scriptable object. + * + * @param cmd the message to send. + * @throws <code>Predef.UnsupportedOperationException</code> + * if the message was not understood. + def <<(cmd: Message[A]): Unit = cmd match { + case Include(elem) => this += elem + case Remove(elem) => this -= elem + case Reset() => clear + case s: Script[_] => s.elements foreach << + case _ => throw new UnsupportedOperationException("message " + cmd + " not understood") + } + */ + + /** Return a read-only projection of this set !!! just us an (immutable) setProxy? */ + def readOnly : collection.Set[A] = new collection.Set[A] { + def contains(item : A) = Set.this.contains(item) + override def size = self.size + override def +(elem: A) = self + elem + override def -(elem: A) = self - elem + override def elements = self.elements + override def foreach(f: A => Unit) = self.foreach(f) + override def newBuilder[B]: Builder[collection.Set, B] = self.newBuilder[B] + } +} diff --git a/src/library/scalax/collection/mutable/StringBuilder.scala b/src/library/scalax/collection/mutable/StringBuilder.scala index 10fc47ff04..3152182dc4 100755 --- a/src/library/scalax/collection/mutable/StringBuilder.scala +++ b/src/library/scalax/collection/mutable/StringBuilder.scala @@ -12,8 +12,10 @@ package scalax.collection.mutable import scalax.collection.generic._ +import scalax.collection.generic.mutable.Growable import scalax.runtime._ + /** <p> * A mutable sequence of characters. This class provides an API compatible * with <a class="java/lang/StringBuilder" href="" target="_top"> @@ -27,9 +29,13 @@ import scalax.runtime._ @serializable @SerialVersionUID(0 - 8525408645367278351L) final class StringBuilder(initCapacity: Int, private val initValue: String) - extends PartialFunction[Int, Char] with Appendable[Any] with java.lang.CharSequence { + extends PartialFunction[Int, Char] + with Growable[Any] + with java.lang.CharSequence { require(initCapacity > 0) + type Array[T] = scala.Array[T] // !!! + /** The value is used for character storage. */ private var array = new Array[Char](initCapacity + initValue.length) @@ -850,6 +856,8 @@ final class StringBuilder(initCapacity: Int, private val initValue: String) object StringBuilder { + type Array[T] = scala.Array[T] // !!! + private val MIN_HIGH_SURROGATE = '\uD800' private val MAX_HIGH_SURROGATE = '\uDBFF' diff --git a/src/library/scalax/collection/mutable/Vector.scala b/src/library/scalax/collection/mutable/Vector.scala index 5569bfa283..aef1c75d94 100644 --- a/src/library/scalax/collection/mutable/Vector.scala +++ b/src/library/scalax/collection/mutable/Vector.scala @@ -10,11 +10,12 @@ package scalax.collection.mutable -trait Vector[A] extends collection.Vector[A] with generic.mutable.VectorTemplate[Vector, A] +import generic._ -object Vector extends generic.SequenceFactory[Vector] { - - /** The empty iterable */ - def apply[A](args: A*): Vector[A] = new ArrayBuffer[A] ++ args.asInstanceOf[Iterable[A]] // !@! +trait Vector[A] extends Sequence[A] with collection.Vector[A] with generic.mutable.VectorTemplate[Vector, A] +/* Factory object for `Vector` class */ +object Vector extends SequenceFactory[Vector] { + /** The empty vector */ + def apply[A](args: A*): Vector[A] = ArrayBuffer.apply(args: _*) // !!! swicth to Array? } |