diff options
Diffstat (limited to 'src/library/scalax/collection/mutable')
7 files changed, 906 insertions, 0 deletions
diff --git a/src/library/scalax/collection/mutable/Appendable.scala b/src/library/scalax/collection/mutable/Appendable.scala new file mode 100644 index 0000000000..b8e8569a14 --- /dev/null +++ b/src/library/scalax/collection/mutable/Appendable.scala @@ -0,0 +1,94 @@ +/* __ *\ +** ________ ___ / / ___ 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 + +/** 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/ArrayBuffer.scala b/src/library/scalax/collection/mutable/ArrayBuffer.scala new file mode 100644 index 0000000000..3e4877cef7 --- /dev/null +++ b/src/library/scalax/collection/mutable/ArrayBuffer.scala @@ -0,0 +1,118 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: ArrayBuffer.scala 15407 2008-06-20 09:26:36Z stepancheg $ + + +package scalax.collection.mutable + +/** 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 + * linear in the buffer size. + * + * @author Matthias Zenger + * @author Martin Odersky + * @version 2.8 + */ +@serializable +class ArrayBuffer[A] extends Buffer[A] with Builder[ArrayBuffer, A] with ResizableArray[A] { + + def clear() { + size0 = 0 + } + + /** Appends a single element to this buffer and returns + * the identity of the buffer. It takes constant time. + * + * @param elem the element to append. + */ + def +=(elem: A) { + ensureSize(size0 + 1) + array(size0) = elem.asInstanceOf[AnyRef] + size0 += 1 + } + + /** 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. + */ + override def ++=(iter: Iterable[A]) { iter copyToBuffer this } + + /** Prepends a single element to this buffer and return + * the identity of the buffer. It takes time linear in + * the buffer size. + * + * @param elem the element to append. + * @return the updated buffer. + */ + def +:(elem: A): this.type = { + ensureSize(size0 + 1) + copy(0, 1, size0) + array(0) = elem.asInstanceOf[AnyRef] + size0 += 1 + this + } + + /** 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. + * @return the updated buffer. + */ + override def ++:(iter: Iterable[A]): this.type = { insertAll(0, iter); this } + + /** 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. + * @throws Predef.IndexOutOfBoundsException if <code>n</code> is out of bounds. + */ + def insertAll(n: Int, iter: Iterable[A]) { + if ((n < 0) || (n > size0)) + throw new IndexOutOfBoundsException(n.toString) + val xs = iter.elements.toList + val len = xs.length + ensureSize(size0 + len) + copy(n, n + len, size0 - n) + xs.copyToArray(array.asInstanceOf[Array[Any]], n) + size0 += len + } + + /** Removes the element on a given index position. It takes time linear in + * the buffer size. + * + * @param n the index which refers to the element to delete. + * @return the updated array buffer. + * @throws Predef.IndexOutOfBoundsException if <code>n</code> is out of bounds. + */ + def remove(n: Int, count: Int) { + if ((n < 0) || (n >= size0)) + throw new IndexOutOfBoundsException(n.toString) + copy(n + count, n, size0 - (n + count)) + size0 -= count + } + + /** Return a clone of this buffer. + * + * @return an <code>ArrayBuffer</code> with the same elements. + */ + override def clone(): Buffer[A] = new ArrayBuffer[A] ++ this + + def result: ArrayBuffer[A] = this + + /** Defines the prefix of the string representation. + */ + override def stringPrefix: String = "ArrayBuffer" +} diff --git a/src/library/scalax/collection/mutable/Buffer.scala b/src/library/scalax/collection/mutable/Buffer.scala new file mode 100644 index 0000000000..c11fa37409 --- /dev/null +++ b/src/library/scalax/collection/mutable/Buffer.scala @@ -0,0 +1,264 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Buffer.scala 15799 2008-08-15 18:23:54Z odersky $ + + +package scalax.collection.mutable + +import generic.mutable.VectorTemplate + +/** Buffers are used to create sequences of elements incrementally by + * appending, prepending, or inserting new elements. It is also + * possible to access and modify elements in a random access fashion + * via the index of the element in the current sequence. + * + * @author Matthias Zenger + * @author Martin Odersky + * @version 2.8 + */ +@cloneable +trait Buffer[A] extends Vector[A] with VectorTemplate[Buffer, A] with Appendable[A] +// with Scriptable[Message[(Location, A)]] + with CloneableCollection +{ + +// Abstract methods from Vector: + + /** Return element at index `n` + * @throws IndexOutofBoundsException if the index is not valid + */ + 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>. + * + * @param n the index of the element to replace. + * @param newelem the new element. + * @throws IndexOutofBoundsException if the index is not valid + */ + def update(n: Int, newelem: A): Unit + +// Abstract methods from Appendable + + /** Append a single element to this buffer. + * + * @param elem the element to append. + */ + def +=(elem: A): Unit + + /** Clears the buffer contents. + */ + def clear() + +// Abstract methods new in this class + + /** Prepend a single element to this buffer and return + * the identity of the buffer. + * @param elem the element to prepend. + */ + def +:(elem: A): this.type + + /** 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. + * @throws IndexOutofBoundsException if the index is not valid + */ + def insertAll(n: Int, iter: Iterable[A]): Unit + + /** Removes a number of elements from a given index position. + * + * @param n the index which refers to the element to delete. + * @param count the number of elements to delete + * @throws IndexOutofBoundsException if the index is not valid + */ + def remove(n: Int, count: Int): Unit + +// Concrete methods + + /** Removes the element on a given index position. + * + * @param n the index which refers to the element to delete. + */ + def remove(n: Int): A = { + val elem = this(n) + remove(n, 1) + elem + } + + /** Removes a single element from this buffer, at its first occurrence. + * If the list does not contain that element, it is unchanged + * + * @param x the element to remove. + */ + def -= (x: A) { + val i = indexOf(x) + 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. + */ + def +:(elem1: A, elem2: A, elems: A*): this.type = + elem1 +: elem2 +: (elems.asInstanceOf[Iterable[A]]) ++: this // !!! + + /** 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. + */ + def ++:(iter: Iterable[A]): this.type = { for (x <- iter) x +: this; this } + + /** Prepends a number of elements provided by an iterator + * The identity of the buffer is returned. + * + * @param iter the iterator + * @return the updated buffer. + */ + def ++:(iter: Iterator[A]): this.type = { for (x <- iter) x +: this; this } + + /** Appends a elements to this buffer. + * + * @param elems the elements to append. + */ + def append(elems: A*) { this ++= elems.asInstanceOf[Iterable[A]] } // !!! + + /** Appends a number of elements provided by an iterable object + * via its <code>elements</code> method. + * + * @param iter the iterable object. + */ + def appendAll(iter: Iterable[A]) { this ++= iter } + + /** Prepend given elements to this list. + * + * @param elem the element to prepend. + */ + def prepend(elems: A*) { elems.asInstanceOf[Iterable[A]] ++: this } // !!! + + /** 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. + */ + def prependAll(iter: Iterable[A]) { iter ++: this } + + /** 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. + */ + def prependAll(iter: Iterator[A]) { iter ++: this } + + /** 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. + */ + def insert(n: Int, elems: A*) { insertAll(n, elems.asInstanceOf[Iterable[A]]) } // !!! + + /** Removes the first <code>n</code> elements. + * + * @param n the number of elements to remove from the beginning + * of this buffer. + */ + def trimStart(n: Int) { remove(0, n) } + + /** Removes the last <code>n</code> elements. + * + * @param n the number of elements to remove from the end + * of this buffer. + */ + def trimEnd(n: Int) { remove(length - n max 0, n) } + + /** Send a message to this scriptable object. + * + * @param cmd the message to send. + * !!!! todo: rewrite location, msg etc with enumerations or else pack in a subpackage + def <<(cmd: Message[(Location, A)]) { + cmd match { + case Include((l, elem)) => l match { + case Start => prepend(elem) + case End => append(elem) + case Index(n) => insert(n, elem) + case _ => throw new UnsupportedOperationException("message " + cmd + " not understood") + } + case Update((l, elem)) => l match { + case Start => update(0, elem) + case End => update(length - 1, elem) + case Index(n) => update(n, elem) + case _ => throw new UnsupportedOperationException("message " + cmd + " not understood") + } + case Remove((l, _)) => l match { + case Start => remove(0) + case End => remove(length - 1) + case Index(n) => remove(n) + case _ => throw new UnsupportedOperationException("message " + cmd + " not understood") + } + case Reset() => clear + case s: Script[_] => s.elements foreach << + case _ => throw new UnsupportedOperationException("message " + cmd + " not understood") + } + } + */ + + /** Return a clone of this buffer. + * + * @return a buffer with the same elements. + */ + override def clone(): Buffer[A] = super.clone().asInstanceOf[Buffer[A]] + + /** Defines the prefix of the string representation. + */ + override def stringPrefix: String = "Buffer" + + /** Adds a number of elements in an array + * + * @param src the array + * @param start the first element to append + * @param len the number of elements to append + * @deprecated replace by: <code>buf ++= src.view(start, end)</code> + */ + @deprecated def ++=(src: Array[A], start: Int, len: Int) { + var i = start + val end = i + len + while (i < end) { + this += src(i) + i += 1 + } + } + +} + + + + diff --git a/src/library/scalax/collection/mutable/CloneableCollection.scala b/src/library/scalax/collection/mutable/CloneableCollection.scala new file mode 100644 index 0000000000..ab2c210134 --- /dev/null +++ b/src/library/scalax/collection/mutable/CloneableCollection.scala @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: CloneableCollection.scala 12003 2007-06-13 12:14:15Z mihaylov $ + + +package scalax.collection.mutable + +/** The J2ME version of the library defined this trait with a clone method + * to substitute for the lack of Object.clone there + */ +trait CloneableCollection { + override def clone(): AnyRef = super.clone() +} diff --git a/src/library/scalax/collection/mutable/ListBuffer.scala b/src/library/scalax/collection/mutable/ListBuffer.scala new file mode 100644 index 0000000000..adc8cbdb26 --- /dev/null +++ b/src/library/scalax/collection/mutable/ListBuffer.scala @@ -0,0 +1,288 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: ListBuffer.scala 14378 2008-03-13 11:39:05Z dragos $ + + +package scalax.collection.mutable + +import generic.SequenceForwarder +import immutable.List +import collection.immutable.{List, Nil, ::} + +/** A Buffer implementation back up by a list. It provides constant time + * prepend and append. Most other operations are linear. + * + * @author Matthias Zenger + * @author Martin Odersky + * @version 2.8 + */ +@serializable +final class ListBuffer[A] + extends Buffer[A] + with Builder[List, A] + with SequenceForwarder[A] +{ + private var start: List[A] = Nil + private var last0: ::[A] = _ + private var exported: Boolean = false + + protected def underlying: Sequence[A] = start + + // Implementations of abstract methods in Buffer + + /** Replaces element at index <code>n</code> with the new element + * <code>newelem</code>. Takes time linear in the buffer size. (except the first + * element, which is updated in constant time). + * + * @param n the index of the element to replace. + * @param x the new element. + * @throws Predef.IndexOutOfBoundsException if <code>n</code> is out of bounds. + */ + def update(n: Int, x: A) { + try { + if (exported) copy() + if (n == 0) { + val newElem = new :: (x, start.tail); + if (last0 eq start) last0 = newElem + start = newElem + } else { + var cursor = start + var i = 1 + while (i < n) { + cursor = cursor.tail + i += 1 + } + val newElem = new :: (x, cursor.tail.tail) + if (last0 eq cursor.tail) last0 = newElem + cursor.asInstanceOf[::[A]].tl = newElem + } + } catch { + case ex: Exception => throw new IndexOutOfBoundsException(n.toString()) + } + } + + /** Appends a single element to this buffer. This operation takes constant time. + * + * @param x the element to append. + */ + def += (x: A) { + if (exported) copy() + if (start.isEmpty) { + last0 = new :: (x, Nil) + start = last0 + } else { + val last1 = last0 + last0 = new :: (x, Nil) + last1.tl = last0 + } + } + + /** Clears the buffer contents. + */ + def clear() { + start = Nil + exported = false + } + + /** Prepends a single element to this buffer. This operation takes constant + * time. + * + * @param x the element to prepend. + * @return this buffer. + */ + def +: (x: A): this.type = { + if (exported) copy() + val newElem = new :: (x, start) + if (start.isEmpty) last0 = newElem + start = newElem + this + } + + /** 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>. + * + * @param n the index where a new element will be inserted. + * @param iter the iterable object providing all elements to insert. + * @throws Predef.IndexOutOfBoundsException if <code>n</code> is out of bounds. + */ + def insertAll(n: Int, iter: Iterable[A]) { + try { + if (exported) copy() + var elems = iter.elements.toList.reverse + if (n == 0) { + while (!elems.isEmpty) { + val newElem = new :: (elems.head, start) + if (start.isEmpty) last0 = newElem + start = newElem + elems = elems.tail + } + } else { + var cursor = start + var i = 1 + while (i < n) { + cursor = cursor.tail + i += 1 + } + while (!elems.isEmpty) { + val newElem = new :: (elems.head, cursor.tail) + if (cursor.tail.isEmpty) last0 = newElem + cursor.asInstanceOf[::[A]].tl = newElem + elems = elems.tail + } + } + } catch { + case ex: Exception => + throw new IndexOutOfBoundsException(n.toString()) + } + } + + /** Removes the element on a given index position. This operation takes time linear in + * the buffer size. + * + * @param n the index which refers to the element to delete. + * @return the updated array buffer. + * @throws Predef.IndexOutOfBoundsException if <code>n</code> is out of bounds. + */ + def remove(n: Int, count: Int) { + try { + if (exported) copy() + var old = start.head; + if (n == 0) { + var c = count + while (c > 0) { + start = start.tail + c -= 1 + } + } else { + var cursor = start + var i = 1 + while (i < n) { + cursor = cursor.tail + i += 1 + } + var c = count + while (c > 0) { + if (last0 eq cursor.tail) last0 = cursor.asInstanceOf[::[A]] + cursor.asInstanceOf[::[A]].tl = cursor.tail.tail + c -= 1 + } + } + old + } catch { + case ex: Exception => + throw new IndexOutOfBoundsException(n.toString()) + } + } + +// Implementation of abstract method in Builder + + def result = toList + + /** Converts this buffer to a list. Takes constant time. The buffer is + * copied lazily, the first time it is mutated. + */ + override def toList: List[A] = { + exported = !start.isEmpty + start + } + +// New methods in ListBuffer + + /** Prepends the elements of this buffer to a given list + * + * @param xs the list to which elements are prepended + */ + def prependToList(xs: List[A]): List[A] = + if (start.isEmpty) xs + else { last0.tl = xs; toList } + +// Overrides of methods in Buffer + + /** Removes the element on a given index position. Takes time linear in + * the buffer size (except for the first element, which is removed in constant + * time). + * + * @param n the index which refers to the element to delete. + * @return n the element that was formerly at position <code>n</code>. + * @pre an element exists at position <code>n</code> + * @throws Predef.IndexOutOfBoundsException if <code>n</code> is out of bounds. + */ + override def remove(n: Int): A = try { + if (exported) copy() + var old = start.head; + if (n == 0) { + start = start.tail + } else { + var cursor = start + var i = 1 + while (i < n) { + cursor = cursor.tail + i += 1 + } + old = cursor.tail.head + if (last0 eq cursor.tail) last0 = cursor.asInstanceOf[::[A]] + cursor.asInstanceOf[::[A]].tl = cursor.tail.tail + } + old + } catch { + case ex: Exception => + throw new IndexOutOfBoundsException(n.toString()) + } + + /** Remove a single element from this buffer. This operation takes linear time + * (except removing the first element, which is done in constant time). + * + * @param x the element to remove. + */ + override def -= (x: A) { + if (exported) copy() + if (start.isEmpty) {} + 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) { + val z = cursor.asInstanceOf[::[A]] + if (z.tl == last0) + last0 = z + z.tl = cursor.tail.tail + } + } + } + + /** expose the underlying list but do not mark it as exported */ + override def readOnly : List[A] = start + + // Private methods + + /** Copy contents of this buffer */ + private def copy() { + var cursor = start + val limit = last0.tail + clear + while (cursor ne limit) { + this += cursor.head + cursor = cursor.tail + } + } + + /** Returns a clone of this buffer. + * + * @return a <code>ListBuffer</code> with the same elements. + */ + override def clone(): Buffer[A] = (new ListBuffer[A]) ++ this + + /** Defines the prefix of the string representation. + * + * @return the string representation of this buffer. + */ + override def stringPrefix: String = "ListBuffer" +} + diff --git a/src/library/scalax/collection/mutable/ResizableArray.scala b/src/library/scalax/collection/mutable/ResizableArray.scala new file mode 100644 index 0000000000..e4dd8bd47d --- /dev/null +++ b/src/library/scalax/collection/mutable/ResizableArray.scala @@ -0,0 +1,103 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: ResizableArray.scala 15407 2008-06-20 09:26:36Z stepancheg $ + + +package scalax.collection.mutable + +/** This class is used internally to implement data structures that + * are based on resizable arrays. + * + * @author Matthias Zenger, Burak Emir + * @version 1.0, 03/05/2004 + */ +trait ResizableArray[A] extends Vector[A] { + + protected def initialSize: Int = 16 + protected var array: Array[AnyRef] = new Array[AnyRef](initialSize min 1) + + protected var size0: Int = 0 + + //########################################################################## + // implement/override methods of Vector[A] + + /** Returns the length of this resizable array. + */ + def length: Int = size0 + + def apply(idx: Int) = { + if (idx >= size0) throw new IndexOutOfBoundsException(idx.toString) + array(idx).asInstanceOf[A] + } + + def update(idx: Int, elem: A) { + if (idx >= size0) throw new IndexOutOfBoundsException(idx.toString) + array(idx) = elem.asInstanceOf[AnyRef] + } + + /** Fills the given array <code>xs</code> with the elements of + * this sequence starting at position <code>start</code>. + * + * @param xs the array to fill. + * @param start starting index. + */ + override def copyToArray[B >: A](xs: Array[B], start: Int) { + Array.copy(array, 0, xs, start, size0) + } + + /** Copy all elements to a buffer + * @param The buffer to which elements are copied + */ + override def copyToBuffer[B >: A](dest: Buffer[B]) { + dest ++= array.asInstanceOf[Iterable[A]] // !!! + } + + override def foreach(f: A => Unit) { + var i = 0 + while (i < size) { + f(array(i).asInstanceOf[A]) + i += 1 + } + } + + //########################################################################## + + /** remove elements of this array at indices after <code>sz</code> + */ + def reduceToSize(sz: Int) { + require(sz <= size0) + size0 = sz + } + + /** ensure that the internal array has at n cells */ + protected def ensureSize(n: Int) { + if (n > array.length) { + var newsize = array.length * 2 + while (n > newsize) + newsize = newsize * 2 + val newar: Array[AnyRef] = new Array(newsize) + Array.copy(array, 0, newar, 0, size0) + array = newar + } + } + + /** Swap two elements of this array. + */ + protected def swap(a: Int, b: Int) { + val h = array(a) + array(a) = array(b) + array(b) = h + } + + /** Move parts of the array. + */ + protected def copy(m: Int, n: Int, len: Int) { + Array.copy(array, m, array, n, len) + } +} diff --git a/src/library/scalax/collection/mutable/Vector.scala b/src/library/scalax/collection/mutable/Vector.scala new file mode 100644 index 0000000000..b181e93303 --- /dev/null +++ b/src/library/scalax/collection/mutable/Vector.scala @@ -0,0 +1,20 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2008, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Vector.scala 15437 2008-06-25 16:22:45Z stepancheg $ + +package scalax.collection.mutable + +trait Vector[A] extends collection.Vector[A] with generic.mutable.VectorTemplate[Vector, A] + +object Vector extends generic.nonvariant.SequenceFactory[Vector] { + + /** The empty iterable */ + def apply[A](args: A*): Vector[A] = null // !!! + +} |