summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/Array.scala1
-rw-r--r--src/library/scala/BufferedIterator.scala204
-rw-r--r--src/library/scala/Collection.scala12
-rw-r--r--src/library/scala/CollectionProxy.scala2
-rw-r--r--src/library/scala/CountedIterator.scala8
-rw-r--r--src/library/scala/DefaultBufferedIterator.scala61
-rw-r--r--src/library/scala/Iterable.scala24
-rw-r--r--src/library/scala/IterableProxy.scala4
-rw-r--r--src/library/scala/Iterator.scala18
-rw-r--r--src/library/scala/List.scala18
-rw-r--r--src/library/scala/Predef.scala7
-rw-r--r--src/library/scala/Proxy.scala9
-rw-r--r--src/library/scala/RandomAccessSeq.scala76
-rw-r--r--src/library/scala/Range.scala21
-rw-r--r--src/library/scala/Seq.scala153
-rw-r--r--src/library/scala/Stream.scala2
-rw-r--r--src/library/scala/collection/BitSet.scala15
-rw-r--r--src/library/scala/collection/RollbackIterator.scala5
-rw-r--r--src/library/scala/collection/Set.scala2
-rw-r--r--src/library/scala/collection/jcl/ArrayList.scala17
-rw-r--r--src/library/scala/collection/jcl/Buffer.scala30
-rw-r--r--src/library/scala/collection/jcl/LinkedList.scala2
-rw-r--r--src/library/scala/collection/jcl/MapWrapper.scala2
-rw-r--r--src/library/scala/collection/jcl/MutableIterator.scala3
-rw-r--r--src/library/scala/collection/jcl/MutableSeq.scala2
-rw-r--r--src/library/scala/collection/mutable/ArrayBuffer.scala2
-rw-r--r--src/library/scala/collection/mutable/Buffer.scala4
-rw-r--r--src/library/scala/collection/mutable/BufferProxy.scala2
-rw-r--r--src/library/scala/collection/mutable/ListBuffer.scala7
-rw-r--r--src/library/scala/collection/mutable/PriorityQueueProxy.scala4
-rw-r--r--src/library/scala/compat/StringBuilder.scala1
-rw-r--r--src/library/scala/io/Source.scala2
-rw-r--r--src/library/scala/runtime/BoxedArray.scala2
-rw-r--r--src/library/scala/runtime/RichString.scala43
-rw-r--r--src/library/scala/runtime/RichStringBuilder.scala51
-rw-r--r--src/library/scala/xml/dtd/Scanner.scala2
36 files changed, 569 insertions, 249 deletions
diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala
index 773c64c5af..871fa97b0d 100644
--- a/src/library/scala/Array.scala
+++ b/src/library/scala/Array.scala
@@ -244,6 +244,7 @@ final class Array[A](_length: Int) extends RandomAccessSeq[A] {
*/
override def slice(from: Int, end: Int): Array[A] = throw new Error()
+
/** Returns an array consisting of all elements of this array that satisfy the
* predicate <code>p</code>. The order of the elements is preserved.
*
diff --git a/src/library/scala/BufferedIterator.scala b/src/library/scala/BufferedIterator.scala
index c0479f4eba..1b4ebef3f8 100644
--- a/src/library/scala/BufferedIterator.scala
+++ b/src/library/scala/BufferedIterator.scala
@@ -15,63 +15,177 @@ package scala
* element without discarding it.
*
* @author Martin Odersky
- * @version 1.0, 16/07/2003
+ * @author SeanMcDirmid
+ * @version 2.0, 16/04/2007
*/
trait BufferedIterator[+A] extends Iterator[A] {
- /** returns the first <code>sz</code> elements that will be iterated by this iterator,
- * or fewer if the iterator has less elements left to iterate over
- */
- def peekList(sz : Int) : Seq[A]
-
/** Checks what the next available element is.
- *
- * @return the current element
- */
- def head: A = peek(0)
- /** return the <code>n</code>th element that will be iterated by this iterator */
- def peek(n : Int) : A = {
- var m = n
- val lst = peekList(n + 1)
- if (m == 0 && !lst.isEmpty) return lst(0)
- for (a <- lst) {
- if (m == 0) return a
- m = m - 1
- }
- return defaultPeek
- }
- /** element returned when no element left to iterate over;
- * throws <code>NoSuchElementException</code> by default
- */
- protected def defaultPeek : A = throw new Predef.NoSuchElementException
-
+ *
+ * @return the current element
+ */
+ def head : A
+ def headOpt = if (!hasNext) None else Some(head)
+ override def buffered: this.type = this
/** iterates over and applies <code>f</code> to the next element
* if this iterator has a next element that <code>f</code> is defined for.
*/
def readIf[T](f : PartialFunction[A,T]) : Option[T] =
- if (hasNext && f.isDefinedAt(head)) Some(f(next))
+ if (hasNext && f.isDefinedAt(head))
+ Some(f(next))
else None
/** iterates over elements as long as <code>f</code> is true
- * for each element, returns the elements iterated over
+ * for each element, returns whether anything was read
*/
- def readWhile(f : A => Boolean) : Seq[A] = {
- import scala.collection.mutable.ArrayBuffer
- var read = new ArrayBuffer[A]
- while (hasNext && f(head))
- read += next
+ def readWhile(f : A => Boolean) : Boolean = {
+ var read = false
+ while (hasNext && f(head)) {
+ next
+ read = true
+ }
read
}
- /** true if elements of <code>seq</code> will be iterated over next in this iterator
- */
- def startsWith(seq : Seq[Any]) : Boolean = {
- var sz = seq.length
- val j = peekList(sz).elements
- val i = seq.elements
- while (i.hasNext && j.hasNext)
- if (i.next != j.next) return false
- return !i.hasNext && !j.hasNext
+ def advanced : BufferedIterator.Advanced[A] = new BufferedIterator.Default[A] {
+ protected def fill : Seq[A] = if (BufferedIterator.this.hasNext) (BufferedIterator.this.next) :: Nil else Nil
}
- override def buffered: BufferedIterator[A] = this
- override def hasNext = !peekList(1).isEmpty
- override def toString = if (hasNext) "peek " + peekList(1).reverse else "empty"
}
+
+object BufferedIterator {
+ import collection.mutable.{Buffer, ListBuffer}
+
+ trait Advanced[+A] extends BufferedIterator[A] {
+ /** returns the first <code>sz</code> elements that will be iterated by this iterator,
+ * or fewer if the iterator has less elements left to iterate over
+ */
+ def peekList(sz : Int) : Seq[A]
+
+ /** Checks what the next available element is.
+ *
+ * @return the current element
+ */
+ def head: A = peek(0)
+
+ /** return the <code>n</code>th element that will be iterated by this iterator */
+ def peek(n : Int) : A = {
+ val lst = peekList(n + 1);
+ if (n == 0) {
+ return if (lst.isEmpty) defaultPeek
+ else lst(0)
+ }
+
+ val i = lst.elements
+ var m = 0
+ while (m < n && i.hasNext) {
+ i.next; m += 1
+ }
+ if (!i.hasNext) defaultPeek
+ else i.next
+ }
+ /** element returned when no element left to iterate over;
+ * throws <code>NoSuchElementException</code> by default
+ */
+ protected def defaultPeek : A = throw new Predef.NoSuchElementException
+
+ /** true if elements of <code>seq</code> will be iterated over next in this iterator
+ */
+ def startsWith(seq : Seq[Any]) : Boolean = {
+ var sz = seq.length
+ val j = peekList(sz).elements
+ val i = seq.elements
+ while (i.hasNext && j.hasNext)
+ if (i.next != j.next) return false
+ return !i.hasNext
+ }
+ def readIfStartsWith(seq : Seq[Any]) : Boolean = {
+ if (startsWith(seq)) {
+ var i = 0
+ while (i < seq.length) {
+ next; i = i + 1
+ }
+ true
+ } else false
+ }
+ override def counted : CountedIterator[A] with Advanced[A] = new CountedIterator[A] with Advanced[A] {
+ private var cnt = -1
+ def count = cnt
+ override def hasNext: Boolean = Advanced.this.hasNext
+ override def next: A = { cnt += 1; Advanced.this.next }
+ override def peekList(sz : Int) : Seq[A] = Advanced.this.peekList(sz)
+ override protected def defaultPeek : A = Advanced.this.defaultPeek
+ override def counted : this.type = this
+ }
+ override def hasNext = !peekList(1).isEmpty
+ override def toString = {
+ val list = peekList(0)
+ if (!list.isEmpty) "peek " + list else "***"
+ }
+
+ override def advanced : this.type = this
+ }
+
+ trait PutBack[+A] extends Advanced[A] {
+ protected[this] def putBack(a : A) : Unit
+ override def counted : CountedIterator[A] with PutBack[A] = new CountedIterator[A] with PutBack[A] {
+ private var cnt = -1
+ def count = cnt
+ override protected def defaultPeek : A = PutBack.this.defaultPeek
+ override def next: A = { cnt += 1; PutBack.this.next }
+ override def hasNext: Boolean = PutBack.this.hasNext
+ override def peekList(sz : Int) : Seq[A] = PutBack.this.peekList(sz)
+ override protected[this] def putBack(a : A) : Unit = {
+ if (cnt <= 0) throw new IllegalArgumentException
+ PutBack.this.putBack(a)
+ cnt = cnt - 1
+ }
+ override def counted : this.type = this
+ }
+ protected[this] def flushFrom[B <% Seq[A]](i : Default[B]) =
+ i.forget.reverse.foreach(_.reverse.foreach(putBack))
+ }
+
+
+ abstract class Default[+A] extends PutBack[A] {
+ import scala.collection.mutable.ListBuffer
+ private[this] val lookahead = new ListBuffer[A] // = Nil
+ override protected[this] def putBack(a : A) : Unit = a +: lookahead
+ override protected def defaultPeek : A = throw new Predef.NoSuchElementException
+ override def hasNext = !lookahead.isEmpty || super.hasNext
+
+ /** used to fill lookahead buffer */
+ protected def fill : Seq[A]
+
+ private[BufferedIterator] def forget : List[A] = {
+ val ret = lookahead.toList
+ lookahead.clear
+ ret
+ }
+
+ override def peekList(sz : Int) : Seq[A] = {
+ if (sz == 0) return lookahead.readOnly
+ else if (sz == 1) {
+ if (!lookahead.isEmpty) return lookahead.readOnly
+ fill match {
+ case Seq.singleton(x) => lookahead += x
+ case next => lookahead ++= next
+ }
+ return lookahead.readOnly
+ }
+ var sz0 = lookahead.length
+ while (sz0 < sz) {
+ val next = fill
+ if (next.isEmpty) return lookahead.readOnly
+ sz0 += next.length
+ lookahead ++= next
+ }
+ lookahead.readOnly
+ }
+ override def next : A = {
+ val lst = peekList(1)
+ if (lst.isEmpty) throw new Predef.NoSuchElementException
+ lookahead.remove(0)
+ lst(0)
+ }
+ }
+}
+
+
diff --git a/src/library/scala/Collection.scala b/src/library/scala/Collection.scala
index 530af51e9c..da86a5c951 100644
--- a/src/library/scala/Collection.scala
+++ b/src/library/scala/Collection.scala
@@ -28,10 +28,8 @@ trait Collection[+A] extends Iterable[A] {
*/
def size : Int
/** Converts this iterable to a fresh Array with elements.
- *
- * @deprecated This method is broken for BitSet. BitSet.toArray will be updated to new behavior in a future release.
*/
- @deprecated def toArray[B >: A]: Array[B] = toList.toArray
+ def toArray[B >: A]: Array[B] = toList.toArray
override def toString = mkString(stringPrefix + "(", ", ", ")")
@@ -45,11 +43,5 @@ trait Collection[+A] extends Iterable[A] {
if (idx2 != -1) string = string.substring(0, idx2)
string
}
- def equalWith[B](that : Collection[B], f : (A,B) => Boolean) : Boolean = {
- if (size != that.size) return false
- val i = elements
- val j = that.elements
- while (i.hasNext) if (!f(i.next, j.next)) return false
- return true
- }
}
+
diff --git a/src/library/scala/CollectionProxy.scala b/src/library/scala/CollectionProxy.scala
index 5c46e6a1e7..c64d329ff7 100644
--- a/src/library/scala/CollectionProxy.scala
+++ b/src/library/scala/CollectionProxy.scala
@@ -26,7 +26,7 @@ trait CollectionProxy[+A] extends Collection[A] with IterableProxy[A] {
def self: Collection[A]
override def size = self.size
- @deprecated override def toArray[B >: A] : Array[B] = self.toArray
+ override def toArray[B >: A] : Array[B] = self.toArray
/*
override def elements: Iterator[A] = self.elements
override def map[B](f: A => B): Collection[B] = self map f
diff --git a/src/library/scala/CountedIterator.scala b/src/library/scala/CountedIterator.scala
index 56850dd97a..6d6f0669e7 100644
--- a/src/library/scala/CountedIterator.scala
+++ b/src/library/scala/CountedIterator.scala
@@ -20,4 +20,12 @@ trait CountedIterator[+A] extends Iterator[A] {
/** counts the elements in this iterator; counts start at 0
*/
def count: Int
+
+ override def counted : this.type = this
+ override def buffered: BufferedIterator[A] with CountedIterator[A] = new BufferedIterator.Default[A] with CountedIterator[A] {
+ protected def fill = if (CountedIterator.this.hasNext) (CountedIterator.this.next) :: Nil else Nil
+ override def count = CountedIterator.this.count - peekList(0).length
+ override def counted : this.type = this
+ override def buffered : this.type = this
+ }
}
diff --git a/src/library/scala/DefaultBufferedIterator.scala b/src/library/scala/DefaultBufferedIterator.scala
deleted file mode 100644
index 6608230beb..0000000000
--- a/src/library/scala/DefaultBufferedIterator.scala
+++ /dev/null
@@ -1,61 +0,0 @@
-/* __ *\
-** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
-** /____/\___/_/ |_/____/_/ | | **
-** |/ **
-\* */
-
-package scala
-import Predef._
-import collection.mutable.{Buffer, ListBuffer}
-
-abstract class DefaultBufferedIterator[+A] extends BufferedIterator[A] {
- private[this] var lookahead : List[A] = Nil
-
- protected[this] def putBack(a : Any) : Unit = lookahead = a.asInstanceOf[A] :: lookahead
-
- override protected def defaultPeek : A = throw new NoSuchElementException
- override def hasNext = !lookahead.isEmpty || super.hasNext
-
- /** used to fill lookahead buffer */
- protected def fill : Seq[A]
-
- override def peekList(sz : Int) : Seq[A] = {
- if (sz == 0) return lookahead
- else if (sz == 1) {
- if (!lookahead.isEmpty) return lookahead
- val next = fill
- lookahead = next.toList
- return lookahead
- }
- var sz0 = sz
- var hd = lookahead
- while (sz0 > 0) {
- if (hd.isEmpty) { // add n elements
- import scala.collection.mutable.ArrayBuffer
- val buf0 = new ArrayBuffer[A]
- while (sz0 > 0 && {
- val next = fill
- if (next.isEmpty) false
- else {
- buf0 ++= next
- sz0 = sz0 - next.length
- true
- }
- }) {}
- lookahead = lookahead ::: buf0.toList
- return lookahead
- }
- sz0 = sz0 - 1
- hd = hd.tail
- }
- lookahead
- }
- override def next : A = {
- val lst = peekList(1)
- if (lst.isEmpty) throw new NoSuchElementException
- lookahead = lookahead.tail
- lst(0)
- }
-}
diff --git a/src/library/scala/Iterable.scala b/src/library/scala/Iterable.scala
index e7202c035c..fb96ec5d40 100644
--- a/src/library/scala/Iterable.scala
+++ b/src/library/scala/Iterable.scala
@@ -183,7 +183,7 @@ trait Iterable[+A] {
* the predicate <code>p</code>.
*/
def takeWhile(p: A => Boolean): Collection[A] =
- new ArrayBuffer[A] ++ elements.takeWhile(p)
+ (new ArrayBuffer[A] ++ elements.takeWhile(p))
/** Returns the longest suffix of this iterable whose first element
* does not satisfy the predicate <code>p</code>.
@@ -194,28 +194,30 @@ trait Iterable[+A] {
* does not satisfy the predicate <code>p</code>.
*/
def dropWhile(p: A => Boolean): Collection[A] =
- new ArrayBuffer[A] ++ elements.dropWhile(p)
+ (new ArrayBuffer[A] ++ elements.dropWhile(p))
/** Returns an iterable consisting only over the first <code>n</code>
* elements of this iterable, or else the whole iterable, if it has less
* than <code>n</code> elements.
*
+ * @deprecated API doesn't make sense for non-ordered collections
* @param n the number of elements to take
* @return the new iterable
*/
- def take(n: Int): Collection[A] =
- new ArrayBuffer[A] ++ elements.take(n)
+ @deprecated def take(n: Int): Collection[A] =
+ (new ArrayBuffer[A] ++ elements.take(n))
/** Returns this iterable without its <code>n</code> first elements
* If this iterable has less than <code>n</code> elements, the empty
* iterable is returned.
*
* @note Will not terminate for infinite-sized collections.
+ * @deprecated API doesn't make sense for non-ordered collections
* @param n the number of elements to drop
* @return the new iterable
*/
- def drop(n: Int): Collection[A] =
- new ArrayBuffer[A] ++ elements.drop(n)
+ @deprecated def drop(n: Int): Collection[A] =
+ (new ArrayBuffer[A] ++ elements.drop(n))
/** Apply a function <code>f</code> to all elements of this
* iterable object.
@@ -416,6 +418,16 @@ trait Iterable[+A] {
*/
def mkString(sep: String): String = this.mkString("", sep, "")
+ /** Converts a collection into a flat <code>String</code> by each element's toString method.
+ * @note Will not terminate for infinite-sized collections.
+ */
+ def mkString = {
+ val buf = new StringBuilder
+ foreach(buf append _.toString)
+ buf.toString
+ }
+
+
/** Write all elements of this string into given string builder.
*
diff --git a/src/library/scala/IterableProxy.scala b/src/library/scala/IterableProxy.scala
index 3a24d4fb51..757f50ae7a 100644
--- a/src/library/scala/IterableProxy.scala
+++ b/src/library/scala/IterableProxy.scala
@@ -33,8 +33,8 @@ trait IterableProxy[+A] extends Iterable[A] with Proxy {
override def filter(p: A => Boolean): Iterable[A] = self filter p
override def takeWhile(p: A => Boolean): Collection[A] = self takeWhile p
override def dropWhile(p: A => Boolean): Collection[A] = self dropWhile p
- override def take(n: Int): Collection[A] = self take n
- override def drop(n: Int): Collection[A] = self drop n
+ @deprecated override def take(n: Int): Collection[A] = self take n
+ @deprecated override def drop(n: Int): Collection[A] = self drop n
override def foreach(f: A => Unit): Unit = self foreach f
override def forall(p: A => Boolean): Boolean = self forall p
override def exists(p: A => Boolean): Boolean = self exists p
diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala
index 35c356f428..d65abe2ef3 100644
--- a/src/library/scala/Iterator.scala
+++ b/src/library/scala/Iterator.scala
@@ -46,8 +46,9 @@ object Iterator {
/**
* @param xs the array of elements
* @return the iterator on <code>xs</code>.
+ * @deprecated replaced by RandomAccessSeq.elements and slice
*/
- def fromArray[a](xs: Array[a]): Iterator[a] =
+ @deprecated def fromArray[a](xs: Array[a]): Iterator[a] =
fromArray(xs, 0, xs.length)
/**
@@ -55,9 +56,10 @@ object Iterator {
* @param start ...
* @param length ...
* @return ...
+ * @deprecated replaced by RandomAccessSeq.elements and slice
*/
- def fromArray[a](xs: Array[a], start: Int, length: Int): Iterator[a] =
- new BufferedIterator[a] {
+ @deprecated def fromArray[a](xs: Array[a], start: Int, length: Int): Iterator[a] =
+ new BufferedIterator.Advanced[a] {
private var i = start
val end = if ((start + length) < xs.length) start else xs.length
override def hasNext: Boolean = i < end
@@ -73,9 +75,10 @@ object Iterator {
/**
* @param str the given string
* @return the iterator on <code>str</code>
+ * @deprecated replaced by <code>str.elements</code>
*/
- def fromString(str: String): Iterator[Char] =
- new BufferedIterator[Char] {
+ @deprecated def fromString(str: String): Iterator[Char] =
+ new BufferedIterator.Advanced[Char] {
private var i = 0
private val len = str.length()
override def hasNext = i < len
@@ -199,6 +202,7 @@ trait Iterator[+A] {
*/
def next(): A
+
/** Returns a new iterator that iterates only over the first <code>n</code>
* elements.
*
@@ -270,7 +274,7 @@ trait Iterator[+A] {
} else throw new NoSuchElementException("next on empty iterator")
}
- protected class PredicatedIterator(p : A => Boolean) extends DefaultBufferedIterator[A] {
+ protected class PredicatedIterator(p : A => Boolean) extends BufferedIterator.Default[A] {
protected def skip0 : Seq[A] = fill
protected override def fill : Seq[A] =
if (!Iterator.this.hasNext) return Nil
@@ -497,7 +501,7 @@ trait Iterator[+A] {
/** Returns a buffered iterator from this iterator.
*/
- def buffered: BufferedIterator[A] = new DefaultBufferedIterator[A] {
+ def buffered: BufferedIterator[A] = new BufferedIterator.Default[A] {
protected def fill = if (Iterator.this.hasNext) (Iterator.this.next) :: Nil else Nil
}
diff --git a/src/library/scala/List.scala b/src/library/scala/List.scala
index 641accec7a..787d37727f 100644
--- a/src/library/scala/List.scala
+++ b/src/library/scala/List.scala
@@ -209,9 +209,10 @@ object List {
*
* @param str the string to convert.
* @return the string as a list of characters.
+ * @deprecated use <code>str.toList</code> instead
*/
- def fromString(str: String): List[Char] =
- Iterator.fromString(str).toList
+ @deprecated def fromString(str: String): List[Char] =
+ str.toList
/** Returns the given list of characters as a string.
*
@@ -549,9 +550,13 @@ sealed abstract class List[+A] extends Seq[A] {
b += these.head
these = these.tail
}
- b.toList
+ if (these.isEmpty) this
+ else b.toList
}
+
+ override def slice(from : Int, until : Int) : List[A] = drop(from).take(until - from)
+
/** Returns the list without its <code>n</code> first elements.
* If this list has less than <code>n</code> elements, the empty list is returned.
*
@@ -1123,6 +1128,13 @@ sealed abstract class List[+A] extends Seq[A] {
b.toList
}
+ def flatten[B](implicit f : A => Iterable[B]) : List[B] = {
+ val buf = new ListBuffer[B]
+ foreach(f(_).foreach(buf += _))
+ buf.toList
+ }
+
+
/** Computes the intersection between this list and the given list
* <code>that</code>.
*
diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala
index 61364836d2..947f141111 100644
--- a/src/library/scala/Predef.scala
+++ b/src/library/scala/Predef.scala
@@ -35,8 +35,12 @@ object Predef {
type boolean = scala.Boolean
type unit = scala.Unit
- @deprecated type All = Nothing
+ @deprecated type All = Nothing
@deprecated type AllRef = Null
+ /** @deprecated use <code>Int</code> instead */
+ @deprecated type Integer = java.lang.Integer
+ /** @deprecated use <code>Char</code> instead */
+ @deprecated type Character = java.lang.Character
type String = java.lang.String
type StringBuilder = compat.StringBuilder
@@ -167,6 +171,7 @@ object Predef {
implicit def booleanWrapper(x: Boolean) = new runtime.RichBoolean(x)
implicit def stringWrapper(x: String) = new runtime.RichString(x)
+ implicit def stringBuilderWrapper(x : StringBuilder) = new runtime.RichStringBuilder(x)
implicit def any2stringadd(x: Any) = new runtime.StringAdd(x)
diff --git a/src/library/scala/Proxy.scala b/src/library/scala/Proxy.scala
index cbd16ce061..8cd3547d03 100644
--- a/src/library/scala/Proxy.scala
+++ b/src/library/scala/Proxy.scala
@@ -24,7 +24,10 @@ import Predef._
*/
trait Proxy {
def self: Any
- override def hashCode(): Int = self.hashCode()
- override def equals(y: Any): Boolean = self.equals(y)
- override def toString(): String = self.toString()
+ override def hashCode: Int = self.hashCode
+ override def equals(that: Any): Boolean = that match {
+ case that : Proxy => self.equals(that.self)
+ case that => self.equals(that)
+ }
+ override def toString: String = self.toString
}
diff --git a/src/library/scala/RandomAccessSeq.scala b/src/library/scala/RandomAccessSeq.scala
index 90f9b57a59..7270d42074 100644
--- a/src/library/scala/RandomAccessSeq.scala
+++ b/src/library/scala/RandomAccessSeq.scala
@@ -5,31 +5,30 @@ object RandomAccessSeq {
override def projection = this
protected class MapProjection[B](f : A => B) extends super.MapProjection(f) with Projection[B]
override def map[B](f: A => B) : Projection[B] = new MapProjection[B](f)
- /** non-strict */
- override def drop(n: Int): Projection[A] = {
- new Projection[A] {
- def length = Projection.this.length - n
- def apply(idx : Int) = Projection.this.apply(idx + n)
- override def stringPrefix = Projection.this.stringPrefix + "D" + n
- }
+
+ }
+ /** A random access sequence that supports update (e.g., an array) */
+ trait Mutable[A] extends RandomAccessSeq[A] {
+ def update(idx : Int, what : A) : Unit
+ override def projection : MutableProjection[A] = new MutableProjection[A] {
+ def update(idx : Int, what : A) : Unit = Mutable.this.update(idx, what)
+ def length = Mutable.this.length
+ def apply(idx : Int) = Mutable.this.apply(idx)
}
- /** non-strict */
- override def take(n: Int): Projection[A] = {
- if (n >= length) Projection.this
- else new Projection[A] {
- def length = n
- def apply(idx : Int) = Projection.this.apply(idx)
- override def stringPrefix = Projection.this.stringPrefix + "T" + n
- }
+ def readOnly : RandomAccessSeq[A] = new RandomAccessSeq[A] {
+ def length = Mutable.this.length
+ def apply(idx : Int) = Mutable.this.apply(idx)
+ override def stringPrefix = Mutable.this.stringPrefix + "RO"
}
- /** non-strict */
- override def slice(from : Int, until : Int) : Projection[A] = drop(from).take(until)
- /** non-strict */
- override def reverse : Projection[A] = new Projection[A] {
- def length = Projection.this.length
- def apply(idx : Int) = Projection.this.apply(length - idx - 1)
- override def stringPrefix = Projection.this.stringPrefix + "R"
- override def reverse : Projection[A] = Projection.this
+ }
+ trait MutableProjection[A] extends Projection[A] with Mutable[A] {
+ override def projection : MutableProjection[A] = this
+ override def reverse : MutableProjection[A] = new MutableProjection[A] {
+ def update(idx : Int, what : A) : Unit = MutableProjection.this.update(length - idx - 1, what)
+ def length = MutableProjection.this.length
+ def apply(idx : Int) = MutableProjection.this.apply(length - idx - 1)
+ override def stringPrefix = MutableProjection.this.stringPrefix + "R"
+ override def reverse : MutableProjection[A] = MutableProjection.this
}
}
}
@@ -44,9 +43,13 @@ trait RandomAccessSeq[+A] extends Seq[A] {
override def elements = RandomAccessSeq.this.elements
override def stringPrefix = RandomAccessSeq.this.stringPrefix + "P"
}
- override def elements : Iterator[A] = new Iterator[A] {
+ override def elements : Iterator[A] = new BufferedIterator.Advanced[A] {
var idx = 0
- def hasNext = idx < RandomAccessSeq.this.length
+ override def peekList(sz : Int) = new RandomAccessSeq[A] {
+ override def length = RandomAccessSeq.this.length - idx
+ override def apply(jdx : Int) = RandomAccessSeq.this.apply(jdx + idx)
+ }
+ override def hasNext = idx < RandomAccessSeq.this.length
def next = {
if (!hasNext) throw new Predef.NoSuchElementException
val ret = RandomAccessSeq.this.apply(idx)
@@ -61,4 +64,27 @@ trait RandomAccessSeq[+A] extends Seq[A] {
if (idx < RandomAccessSeq.this.length) RandomAccessSeq.this(idx)
else that(idx - RandomAccessSeq.this.length)
}
+ override def drop( from: Int): RandomAccessSeq[A] = slice(from, length)
+ override def take(until: Int): RandomAccessSeq[A] = slice(0, until)
+ override def slice(from : Int, until : Int) : RandomAccessSeq[A] = {
+ if (from == 0 && until >= length) return projection
+ else if (from >= until) new RandomAccessSeq.Projection[A] {
+ def length = 0
+ def apply(idx : Int) = throw new Predef.IndexOutOfBoundsException
+ } else new RandomAccessSeq.Projection[A] {
+ def length = until - from
+ def apply(idx : Int) = if (idx < 0 || idx >= length) throw new Predef.IndexOutOfBoundsException
+ else RandomAccessSeq.this.apply(from + idx)
+ }
+ }
+
+ override def reverse : Seq[A] = new RandomAccessSeq.Projection[A] {
+ def length = RandomAccessSeq.this.length
+ def apply(idx : Int) = RandomAccessSeq.this.apply(length - idx - 1)
+ override def stringPrefix = RandomAccessSeq.this.stringPrefix + "R"
+ override def reverse : RandomAccessSeq.Projection[A] = RandomAccessSeq.this.projection
+ }
+ /** will return false if index is out of bounds */
+ final def safeIs(idx : Int, a : Any) = if (idx >= 0 && idx < length) this(idx) == a else false
+
}
diff --git a/src/library/scala/Range.scala b/src/library/scala/Range.scala
index 7da93d4afb..9a3ab8f597 100644
--- a/src/library/scala/Range.scala
+++ b/src/library/scala/Range.scala
@@ -27,29 +27,12 @@ import Predef._
* @author Stephane Micheloud
* @version 1.0, 01/05/2007
*/
-class Range(val start: Int, val end: Int, val step: Int) extends BufferedIterator[Int] {
+class Range(val start: Int, val end: Int, val step: Int) extends RandomAccessSeq.Projection[Int] {
if (step == 0) throw new Predef.IllegalArgumentException
- private var jdx = 0
/** create a new range with the start and end values of this range and a new <code>step</code> */
def by(step : Int) = new Range(start, end, step)
- override def peekList(sz : Int) : Seq[Int] = return new RandomAccessSeq.Projection[Int] {
- def length = Range.this.length - jdx
- def apply(idx : Int) = Range.this.apply(jdx + idx)
- }
- override def hasNext = if (step == 0) true else jdx < length
- override def next = {
- val ret = apply(jdx)
- jdx = jdx + 1
- ret
- }
- def seqOfRange = new RandomAccessSeq.Projection[Int] {
- def length = Range.this.length
- def apply(idx : Int) = Range.this.apply(idx)
- }
-
-
def length : Int = {
if (this.step == 0) throw new Predef.UnsupportedOperationException
if (start < end && this.step < 0) return 0
@@ -70,6 +53,4 @@ class Range(val start: Int, val end: Int, val step: Int) extends BufferedIterato
def contains(x : Int): Boolean =
x >= start && x < end && (((x - start) % step) == 0)
- /** a <code>Iterator.contains</code>, not a <code>Seq.contains</code>! */
- override def contains(elem: Any): Boolean = super.contains(elem)
}
diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala
index 60b1488cde..606cc351f8 100644
--- a/src/library/scala/Seq.scala
+++ b/src/library/scala/Seq.scala
@@ -17,7 +17,7 @@ import collection.mutable.ArrayBuffer
object Seq {
/** The empty sequence */
- val empty = new Seq[Nothing] {
+ val empty : Seq[Nothing] = new RandomAccessSeq[Nothing] {
def length = 0
def apply(i: Int): Nothing = throw new NoSuchElementException("empty sequence")
override def elements = Iterator.empty
@@ -30,17 +30,20 @@ object Seq {
*/
def unapplySeq[A](x: Seq[A]): Some[Seq[A]] = Some(x)
+ case class singleton[A](value : A) extends RandomAccessSeq[A] {
+ override def length = 1
+ override def isDefinedAt(idx : Int) : Boolean = idx == 0
+ override def apply(idx : Int) = idx match {
+ case 0 => value
+ case _ => throw new Predef.IndexOutOfBoundsException
+ }
+ }
+
/** Builds a singleton sequence.
*
- * @param x ...
- * @return ...
+ * @deprecated use <code>singleton</code> instead.
*/
- def single[A](x: A) = new Seq[A] {
- def length = 1
- override def elements = Iterator.single(x)
- override def isDefinedAt(x: Int): Boolean = (x == 0)
- def apply(i: Int) = x // caller's responsibility to check isDefinedAt
- }
+ @deprecated def single[A](x: A) = singleton(x)
/*
implicit def view[A <% Ordered[A]](xs: Seq[A]): Ordered[Seq[A]] =
new Ordered[Seq[A]] with Proxy {
@@ -129,7 +132,7 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
val buf = new ArrayBuffer[B]
this copyToBuffer buf
that copyToBuffer buf
- buf
+ buf.readOnly
}
/** Returns the last element of this list.
@@ -141,6 +144,15 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
case 0 => throw new Predef.NoSuchElementException
case n => this(n - 1)
}
+ /** Returns as an option the last element of this list or None if list is empty.
+ *
+ */
+ def lastOption : Option[A] = length match {
+ case 0 => None
+ case n => Some(this(n-1))
+ }
+ def headOption : Option[A] = if (isEmpty) None else Some(apply(0))
+
/** Appends two iterable objects.
*/
@@ -148,7 +160,7 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
val buf = new ArrayBuffer[B]
this copyToBuffer buf
that copyToBuffer buf
- buf
+ buf.readOnly
}
/** Is this partial function defined for the index <code>x</code>?
@@ -191,7 +203,7 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
val buf = new ArrayBuffer[B]
val elems = elements
while (elems.hasNext) buf += f(elems.next)
- buf
+ buf.readOnly
}
/** Applies the given function <code>f</code> to each element of
@@ -205,7 +217,7 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
val buf = new ArrayBuffer[B]
val elems = elements
while (elems.hasNext) f(elems.next) copyToBuffer buf
- buf
+ buf.readOnly
}
/** Returns all the elements of this sequence that satisfy the
@@ -218,21 +230,70 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
/** Returns a sequence consisting only over the first <code>n</code>
* elements of this sequence, or else the whole sequence, if it has less
- * than <code>n</code> elements.
+ * than <code>n</code> elements. (non-strict)
*
* @param n the number of elements to take
- * @return the new sequence
+ * @return a possibly projected sequence
*/
- override def take(n: Int): Seq[A] = super.take(n).asInstanceOf[Seq[A]]
+ override def take(n: Int): Seq[A] = {
+ var m = 0
+ val result = new scala.collection.mutable.ListBuffer[A]
+ val i = elements
+ while (m < n && i.hasNext) {
+ result += i.next; m = m + 1
+ }
+ result.toList
+ }
+
+ /*
+ new Seq.Projection[A] {
+ def length = {
+ val sz = Seq.this.length
+ if (n <= sz) n else sz
+ }
+ def apply(idx : Int) =
+ if (idx >= n) throw new Predef.IndexOutOfBoundsException
+ else (Seq.this.apply(n))
+ override def stringPrefix = Seq.this.stringPrefix + "T" + n
+ }
+ */
/** Returns this sequence without its <code>n</code> first elements
* If this sequence has less than <code>n</code> elements, the empty
- * sequence is returned.
+ * sequence is returned. (non-strict)
*
* @param n the number of elements to drop
* @return the new sequence
*/
- override def drop(n: Int): Seq[A] = super.drop(n).asInstanceOf[Seq[A]]
+ override def drop(n: Int): Seq[A] = {
+ var m = 0
+ val result = new scala.collection.mutable.ListBuffer[A]
+ val i = elements
+ while (m < n && i.hasNext) {
+ i.next; m = m + 1
+ }
+ while (i.hasNext) result += i.next
+ result.toList
+ }
+
+ /** A sub-sequence of <code>len</code> elements
+ * starting at index <code>from</code> (non-strict)
+ *
+ * @param from The index of the first element of the slice
+ * @param until The index of the element following the slice
+ * @throws IndexOutOfBoundsException if <code>from &lt; 0</code>
+ * or <code>length &lt; from + len<code>
+ */
+ def slice(from : Int, until : Int) : Seq[A] = drop(from).take(until - from)
+
+ /*new Seq.Projection[A] {
+ def length = {
+ val sz = Seq.this.length
+ if (n <= sz) sz - n else sz
+ }
+ def apply(idx : Int) = (Seq.this.apply(idx + n))
+ override def stringPrefix = Seq.this.stringPrefix + "D" + n
+ }*/
/** Returns the longest prefix of this sequence whose elements satisfy
* the predicate <code>p</code>.
@@ -272,11 +333,6 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
/** Returns a subsequence starting from index <code>from</code>
* consisting of <code>len</code> elements.
- */
- def slice(from: Int, len: Int): Seq[A] = this.drop(from).take(len)
-
- /** Returns a subsequence starting from index <code>from</code>
- * consisting of <code>len</code> elements.
*
* @deprecated use <code>slice</code> instead
*/
@@ -284,6 +340,7 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
def subseq(from: Int, end: Int): Seq[A] = slice(from, end - from)
+
/** Converts this sequence to a fresh Array with <code>length</code> elements.
*/
override def toArray[B >: A]: Array[B] = {
@@ -297,6 +354,56 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
def apply(idx : Int) = (Seq.this.apply(idx))
override def stringPrefix = Seq.this.stringPrefix + "P"
}
+ def equalsWith[B](that : Seq[B])(f : (A,B) => Boolean) : Boolean = {
+ if (size != that.size) return false
+ val i = elements
+ val j = that.elements
+ while (i.hasNext) if (!f(i.next, j.next)) return false
+ return true
+ }
+ /** @returns true if this sequence start with that sequences
+ * @see String.startsWith
+ */
+ def startsWith[B](that : Seq[B]) : Boolean = {
+ val i = elements
+ val j = that.elements
+ var result = true
+ while (j.hasNext && i.hasNext && result)
+ result = i.next == j.next
+ result && !j.hasNext
+ }
+ /** @returns true if this sequence end with that sequence
+ * @see String.endsWith
+ */
+ def endsWith[B](that : Seq[B]) : Boolean = {
+ val length = this.length
+ val j = that.elements
+ var i = 0
+ var result = true
+ while (result && i < length && j.hasNext)
+ result = apply(length - i - 1) == j.next
+ result && !j.hasNext
+ }
+ /** @returns -1 if <code>that</code> not contained in this, otherwise the index where <code>that</code> is contained
+ * @see String.indexOf
+ */
+ def indexOf[B](that : Seq[B]) : Int = {
+ val i = this.elements.counted
+ var j = that.elements
+ var idx = 0
+ while (i.hasNext && j.hasNext) {
+ if (i.next != j.next) {
+ j = that.elements
+ idx = -1
+ } else if (idx == -1) {
+ idx = i.count - 1
+ }
+ }
+ idx
+ }
+ /** Is <code>that</code> a slice in this?
+ */
+ def containsSlice[B](that : Seq[B]) : Boolean = indexOf(that) != -1
class Filter(p : A => Boolean) extends Seq.Projection[A] {
override def stringPrefix = Seq.this.stringPrefix + "F"
diff --git a/src/library/scala/Stream.scala b/src/library/scala/Stream.scala
index 365a12c5ca..59235e72a4 100644
--- a/src/library/scala/Stream.scala
+++ b/src/library/scala/Stream.scala
@@ -171,7 +171,7 @@ object Stream {
* @author Martin Odersky, Matthias Zenger
* @version 1.1 08/08/03
*/
-trait Stream[+A] extends Seq[A] {
+trait Stream[+A] extends Seq.Projection[A] {
/** is this stream empty? */
override def isEmpty: Boolean
diff --git a/src/library/scala/collection/BitSet.scala b/src/library/scala/collection/BitSet.scala
index a4473f8aa8..c483555ac0 100644
--- a/src/library/scala/collection/BitSet.scala
+++ b/src/library/scala/collection/BitSet.scala
@@ -152,19 +152,6 @@ abstract class BitSet extends Set[Int] {
arraycopy(this.arr, 0, newarr, 0, length)
newarr
}
- /**
- * @return a copy of the array underlying this bitset
- *
- * @deprecated This method does not currently implement behavior specified in Iterable.toArray.
- * This method will implement the Iterbale.toArray behavior in a future release.
- * Please use <code>underlying</code> to get previous <code>toArray</code> behavior.
- */
- @deprecated override def toArray[B >: Int]: Array[B] = {
- val ret0 = underlying
- val ret1 = new Array[B](ret0.length)
- for (i <- 0.until(ret0.length))
- ret1(i) = (ret0(i) : Any).asInstanceOf[B]
- ret1
- }
+
protected override def stringPrefix = "Set"
}
diff --git a/src/library/scala/collection/RollbackIterator.scala b/src/library/scala/collection/RollbackIterator.scala
index 19eded61a1..8436155957 100644
--- a/src/library/scala/collection/RollbackIterator.scala
+++ b/src/library/scala/collection/RollbackIterator.scala
@@ -5,7 +5,7 @@ import scala.collection.mutable.{ArrayBuffer}
*
* @author Sean McDirmid
*/
-class RollbackIterator[+A](underlying : Iterator[A]) extends DefaultBufferedIterator[A] {
+class RollbackIterator[+A](underlying : Iterator[A]) extends BufferedIterator.Default[A] {
private[this] var rollback : ArrayBuffer[A] = null
protected def fill : Seq[A] = if (underlying.hasNext) underlying.next :: Nil else Nil
@@ -61,10 +61,9 @@ class RollbackIterator[+A](underlying : Iterator[A]) extends DefaultBufferedIter
def read(f : => Unit) : Boolean = remember[Boolean]{
f; seq => !seq.isEmpty
}
-
/** if elements of <code>seq</code> will be iterated over next in this iterator,
* returns true and iterates over these elements
*/
- def readIfStartsWith(seq : Seq[Any]) : Boolean =
+ override def readIfStartsWith(seq : Seq[Any]) : Boolean =
!tryRead{if (seq.forall(a => hasNext && next == a)) Some(()) else None}.isEmpty
} \ No newline at end of file
diff --git a/src/library/scala/collection/Set.scala b/src/library/scala/collection/Set.scala
index 049228de0d..cdad6e4638 100644
--- a/src/library/scala/collection/Set.scala
+++ b/src/library/scala/collection/Set.scala
@@ -92,7 +92,7 @@ trait Set[A] extends (A => Boolean) with Collection[A] {
(0 /: this)((hash, e) => hash + e.hashCode())
- @deprecated override def toArray[B >: A]: Array[B] = {
+ override def toArray[B >: A]: Array[B] = {
val result = new Array[B](size)
copyToArray(result, 0)
result
diff --git a/src/library/scala/collection/jcl/ArrayList.scala b/src/library/scala/collection/jcl/ArrayList.scala
index 93b7cdff61..2b433cd059 100644
--- a/src/library/scala/collection/jcl/ArrayList.scala
+++ b/src/library/scala/collection/jcl/ArrayList.scala
@@ -14,7 +14,22 @@ package scala.collection.jcl;
*
* @author Sean McDirmid
*/
-class ArrayList[A](override val underlying : java.util.ArrayList) extends BufferWrapper[A] {
+class ArrayList[A](override val underlying : java.util.ArrayList) extends RandomAccessSeq.Mutable[A] with BufferWrapper[A] {
def this() = this(new java.util.ArrayList);
override def elements = super[BufferWrapper].elements;
+
+ trait Projection0[A] extends MutableSeq.Projection[A] with RandomAccessSeq.Projection[A] {
+ override def projection : Projection0[A] = this
+ override def elements : SeqIterator[Int,A] = new DefaultSeqIterator
+
+ protected class MapProjection[B](f : A => B) extends super.MapProjection[B](f) with Projection0[B] {
+ override def projection = this
+ }
+ override def map[B](f: A => B) : Projection0[B] = new MapProjection[B](f)
+ }
+ class Projection extends Buffer.Projection[A] with RandomAccessSeq.MutableProjection[A] with Projection0[A] {
+ override def elements : BufferIterator[Int,A] = new DefaultBufferIterator
+ override def projection : Projection = this
+ }
+ override def projection : Projection = new Projection
}
diff --git a/src/library/scala/collection/jcl/Buffer.scala b/src/library/scala/collection/jcl/Buffer.scala
index 62479172e8..d6dd45c501 100644
--- a/src/library/scala/collection/jcl/Buffer.scala
+++ b/src/library/scala/collection/jcl/Buffer.scala
@@ -44,11 +44,35 @@ trait Buffer[A] extends Ranged[Int,A] with MutableSeq[A] with Collection[A] {
/** Indices are compared through subtraction. */
final def compare(k0 : Int, k1 : Int) = k0 - k1;
- /** Removes the element at index "idx" */
+ /** Removes the element at index <code>idx</code> */
def remove(idx : Int) = {
val i = elements;
val ret = i.seek(idx); i.remove; ret;
}
+ /** Removes N elements from index <code>idx</code> */
+ def remove(idx : Int, length : Int) = {
+ val i = elements
+ i.seek(idx)
+ for (j <- 0.until(length)) i.remove
+ }
+ /** replaces */
+ def replace(from : Int, length : Int, added : Seq[A]) = {
+ val min = if (length < added.length) length else added.length
+ val i = added.elements
+ var j = 0
+ while (j < length && i.hasNext) {
+ set(from + j, i.next); j = j + 1
+ }
+ assert(j == min)
+ if (i.hasNext) {
+ val slice = added.drop(length)
+ assert(!slice.isEmpty)
+ addAll(from + min, slice)
+ } else if (j < length) {
+ assert(length > min)
+ remove(from + min, length - min)
+ }
+ }
/** Replaces the element at index "idx" with "a."
* @returns the element replaced.
@@ -176,6 +200,10 @@ trait Buffer[A] extends Ranged[Int,A] with MutableSeq[A] with Collection[A] {
}
}
}
+ def replace(startOffset : Int, length : Int, added : Iterable[A]) : Unit = {
+
+ }
+
}
object Buffer {
trait Projection[A] extends MutableSeq.Projection[A] with Collection.Projection[A] with Buffer[A] {
diff --git a/src/library/scala/collection/jcl/LinkedList.scala b/src/library/scala/collection/jcl/LinkedList.scala
index b744e72f79..535c926f46 100644
--- a/src/library/scala/collection/jcl/LinkedList.scala
+++ b/src/library/scala/collection/jcl/LinkedList.scala
@@ -16,7 +16,7 @@ package scala.collection.jcl;
*
* @author Sean McDirmid
*/
-class LinkedList[A](override val underlying : java.util.LinkedList) extends CollectionWrapper[A] with BufferWrapper[A] {
+class LinkedList[A](override val underlying : java.util.LinkedList) extends BufferWrapper[A] {
def this() = this(new java.util.LinkedList);
override def elements = super[BufferWrapper].elements;
override def add(idx : Int, a : A) =
diff --git a/src/library/scala/collection/jcl/MapWrapper.scala b/src/library/scala/collection/jcl/MapWrapper.scala
index 0af497d382..48e4b47003 100644
--- a/src/library/scala/collection/jcl/MapWrapper.scala
+++ b/src/library/scala/collection/jcl/MapWrapper.scala
@@ -20,7 +20,7 @@ trait MapWrapper[K,E] extends jcl.Map[K,E] {
override def isEmpty = underlying.isEmpty;
override def clear() = underlying.clear;
override def put(key : K, elem : E) = {
- if (elem == null) throw new IllegalArgumentException;
+ //if (elem == null) throw new IllegalArgumentException;
val ret = underlying.put(key,elem);
if (ret == null) None else Some(ret.asInstanceOf[E]);
}
diff --git a/src/library/scala/collection/jcl/MutableIterator.scala b/src/library/scala/collection/jcl/MutableIterator.scala
index fadb45e5f5..56ece90dd2 100644
--- a/src/library/scala/collection/jcl/MutableIterator.scala
+++ b/src/library/scala/collection/jcl/MutableIterator.scala
@@ -26,7 +26,7 @@ trait MutableIterator[A] extends Iterator[A] {
override def map[B](f: A => B) : MutableIterator[B] = new Map(f);
/** A type-safe version of contains.
- **/
+b **/
def has(a: A) = exists(b => a == a);
/** Finds and removes the first instance of "a" through the iterator.
@@ -68,6 +68,7 @@ trait MutableIterator[A] extends Iterator[A] {
if (hasNext) Some(peekNext) else None;
}
class Filter(p : A => Boolean) extends MutableIterator[A] {
+ // XXX: broken
private[jcl] def peekNext = Buffered.this.seekNext(p) match {
case Some(result) => result;
case None => throw new NoSuchElementException;
diff --git a/src/library/scala/collection/jcl/MutableSeq.scala b/src/library/scala/collection/jcl/MutableSeq.scala
index 634f07b7f6..559965c9ea 100644
--- a/src/library/scala/collection/jcl/MutableSeq.scala
+++ b/src/library/scala/collection/jcl/MutableSeq.scala
@@ -37,7 +37,7 @@ trait MutableSeq[A] extends Seq[A] with MutableIterable[A] {
}
def remove = throw new UnsupportedOperationException
}
- override def elements : SeqIterator[Int,A] = new DefaultSeqIterator;
+ override def elements : SeqIterator[Int,A] = new DefaultSeqIterator
override def isEmpty = super[MutableIterable].isEmpty;
diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala
index 7a7a765e86..ad3e1eb6af 100644
--- a/src/library/scala/collection/mutable/ArrayBuffer.scala
+++ b/src/library/scala/collection/mutable/ArrayBuffer.scala
@@ -21,7 +21,7 @@ import Predef._
* @version 1.0, 15/03/2004
*/
@serializable
-class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
+class ArrayBuffer[A] extends RandomAccessSeq.Mutable[A] with Buffer[A] with ResizableArray[A] {
/** Appends a single element to this buffer and returns
* the identity of the buffer.
*
diff --git a/src/library/scala/collection/mutable/Buffer.scala b/src/library/scala/collection/mutable/Buffer.scala
index 0d8c4539e1..5180bbbe0c 100644
--- a/src/library/scala/collection/mutable/Buffer.scala
+++ b/src/library/scala/collection/mutable/Buffer.scala
@@ -76,6 +76,9 @@ trait Buffer[A] extends AnyRef
i += 1
}
}
+ /** return a read only alias of this buffer
+ */
+ def readOnly : Seq[A]
/** Appends a number of elements provided by an iterable object
* via its <code>elements</code> method. The identity of the
@@ -246,4 +249,5 @@ trait Buffer[A] extends AnyRef
/** Defines the prefix of the string representation.
*/
override protected def stringPrefix: String = "Buffer"
+
}
diff --git a/src/library/scala/collection/mutable/BufferProxy.scala b/src/library/scala/collection/mutable/BufferProxy.scala
index 31b94f1ea5..b56449dd4b 100644
--- a/src/library/scala/collection/mutable/BufferProxy.scala
+++ b/src/library/scala/collection/mutable/BufferProxy.scala
@@ -44,6 +44,8 @@ trait BufferProxy[A] extends Buffer[A] with Proxy {
*/
def +=(elem: A): Unit = self.+=(elem)
+ override def readOnly = self.readOnly
+
/** Appends a number of elements provided by an iterable object
* via its <code>elements</code> method. The identity of the
* buffer is returned.
diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala
index 71ed732204..fb230966e0 100644
--- a/src/library/scala/collection/mutable/ListBuffer.scala
+++ b/src/library/scala/collection/mutable/ListBuffer.scala
@@ -38,6 +38,8 @@ final class ListBuffer[A] extends Buffer[A] {
this
}
+
+
/** Appends a single element to this buffer.
*
* @param x the element to append.
@@ -88,6 +90,8 @@ final class ListBuffer[A] extends Buffer[A] {
exported = !start.isEmpty
start
}
+ /** expose the underlying list but do not mark it as exported */
+ override def readOnly : List[A] = start
/** Prepends the elements of this buffer to a given list
*
@@ -121,6 +125,9 @@ final class ListBuffer[A] extends Buffer[A] {
*/
def length: Int = start.length
+ // will be faster since this is a list
+ override def isEmpty = start.isEmpty
+
/** Returns the n-th element of this list. This method
* yields an error if the element does not exist.
*
diff --git a/src/library/scala/collection/mutable/PriorityQueueProxy.scala b/src/library/scala/collection/mutable/PriorityQueueProxy.scala
index 1162570b12..83b84cef07 100644
--- a/src/library/scala/collection/mutable/PriorityQueueProxy.scala
+++ b/src/library/scala/collection/mutable/PriorityQueueProxy.scala
@@ -90,6 +90,10 @@ abstract class PriorityQueueProxy[A <% Ordered[A]] extends PriorityQueue[A]
*/
override def toQueue: Queue[A] = self.toQueue
+ override def take(until : Int) = self take until
+ override def drop(from : Int) = self drop from
+ override def slice(from : Int, until : Int) = self.slice(from, until)
+
/** This method clones the priority queue.
*
* @return a priority queue with the same elements.
diff --git a/src/library/scala/compat/StringBuilder.scala b/src/library/scala/compat/StringBuilder.scala
index 72af084c96..98e9918c8e 100644
--- a/src/library/scala/compat/StringBuilder.scala
+++ b/src/library/scala/compat/StringBuilder.scala
@@ -33,6 +33,7 @@ final class StringBuilder(val self: StringBuffer) extends (Int => Char) with Pro
def charAt(i: Int): Char = self.charAt(i)
def apply(i: Int): Char = self.charAt(i)
+ def deleteCharAt(index : Int) = self.deleteCharAt(index)
def setCharAt(index: Int, c: Char) { self.setCharAt(index, c) }
def update(i: Int, c: Char) { self.setCharAt(i, c)}
diff --git a/src/library/scala/io/Source.scala b/src/library/scala/io/Source.scala
index 55629d1acc..3c13210863 100644
--- a/src/library/scala/io/Source.scala
+++ b/src/library/scala/io/Source.scala
@@ -77,7 +77,7 @@ object Source {
* @return ...
*/
def fromString(s: String): Source = {
- val it = Iterator.fromString(s)
+ val it = s.elements
new Source {
def reset() = fromString(s)
val iter = it
diff --git a/src/library/scala/runtime/BoxedArray.scala b/src/library/scala/runtime/BoxedArray.scala
index 33977fb15d..6c65705ae0 100644
--- a/src/library/scala/runtime/BoxedArray.scala
+++ b/src/library/scala/runtime/BoxedArray.scala
@@ -22,7 +22,7 @@ import compat.StringBuilder
* @author Martin Odersky, Stephane Micheloud
* @version 1.0
*/
-abstract class BoxedArray extends RandomAccessSeq[Any] {
+abstract class BoxedArray extends RandomAccessSeq.Mutable[Any] {
/** The length of the array */
def length: Int
diff --git a/src/library/scala/runtime/RichString.scala b/src/library/scala/runtime/RichString.scala
index c8f5160351..f63ee0a967 100644
--- a/src/library/scala/runtime/RichString.scala
+++ b/src/library/scala/runtime/RichString.scala
@@ -14,21 +14,38 @@ package scala.runtime
import Predef._
-final class RichString(val self: String) extends Seq[Char] with Ordered[String] with Proxy {
-
- // Ordered[String]
- def compare(other: String) = self compareTo other
+final class RichString(val self: String) extends Proxy with RandomAccessSeq[Char] with Ordered[String] {
+ override def apply(n: Int) = self charAt n
+ override def length = self.length
+ override def toString = self
+ override def mkString = self
+ override def slice(from : Int, until : Int) : RichString = {
+ val from0 = if (from < 0) 0 else from
+ val until0 = if (from >= until || from >= self.length) return new RichString("")
+ else if (until > self.length) self.length else until
+ new RichString(self.substring(from0, until0))
+ }
- // Seq[Char]
- def length = self.length
- override def elements = Iterator.fromString(self)
+ override def take(until : Int) : RichString = slice(0, until)
+ override def drop(from : Int) : RichString = slice(from, self.length)
+ override def startsWith[B](that : Seq[B]) = that match {
+ case that : RichString => self startsWith that.self
+ case that => super.startsWith(that)
+ }
+ override def endsWith[B](that : Seq[B]) = that match {
+ case that : RichString => self endsWith that.self
+ case that => super.endsWith(that)
+ }
+ override def indexOf[B](that : Seq[B]) = that match {
+ case that : RichString => self indexOf that.self
+ case that => super.indexOf(that)
+ }
+ override def containsSlice[B](that : Seq[B]) = that match {
+ case that : RichString => self contains that.self
+ case that => super.containsSlice(that)
+ }
- /** Retrieve the n-th character of the string
- *
- * @param index into the string
- * @return the character at position <code>index</code>.
- */
- def apply(n: Int) = self charAt n
+ override def compare(other: String) = self compareTo other
private final val LF: Char = 0x0A
private final val FF: Char = 0x0C
diff --git a/src/library/scala/runtime/RichStringBuilder.scala b/src/library/scala/runtime/RichStringBuilder.scala
new file mode 100644
index 0000000000..8213cda8be
--- /dev/null
+++ b/src/library/scala/runtime/RichStringBuilder.scala
@@ -0,0 +1,51 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+// $Id: RichString.scala 11110 2007-05-21 12:40:17Z mcdirmid $
+
+
+package scala.runtime
+
+
+import Predef._
+import scala.collection.mutable.Buffer
+
+final class RichStringBuilder(val self : StringBuilder) extends RandomAccessSeq.Mutable[Char] with Proxy with Buffer[Char] {
+ override def length = self.length
+ override def apply(idx : Int) = self.charAt(idx)
+ override def mkString = self.toString
+ override def update(idx : Int, c : Char) = self.setCharAt(idx, c)
+ override def +=(c: Char): Unit = self append c
+ override def ++=(iter: Iterable[Char]): Unit = iter match {
+ case str : RichString => self append str.self
+ case str : Array[Char] => self append str
+ case iter => super.++=(iter)
+ }
+ override def ++(iter: Iterable[Char]): this.type = { this ++= iter; this }
+
+
+ override def insertAll(idx: Int, iter: Iterable[Char]): Unit = iter match {
+ case str : RichString => self.insert(idx, str)
+ case str : Array[Char] => self.insert(idx, str)
+ case iter =>
+ val i = iter.elements
+ var jdx = idx
+ while (i.hasNext) {
+ self.insert(jdx, i.next)
+ jdx = jdx + 1
+ }
+ }
+ override def +:(c : Char) = self.insert(0, c)
+ def ensureSize(size : Int) = self.ensureCapacity(size)
+ override def remove(idx : Int) = {
+ val c = self.charAt(idx)
+ self.deleteCharAt(idx)
+ c
+ }
+ override def clear = self.setLength(0)
+}
diff --git a/src/library/scala/xml/dtd/Scanner.scala b/src/library/scala/xml/dtd/Scanner.scala
index 9ccc167fa5..9cb5ef917e 100644
--- a/src/library/scala/xml/dtd/Scanner.scala
+++ b/src/library/scala/xml/dtd/Scanner.scala
@@ -31,7 +31,7 @@ class Scanner extends Tokens with parsing.TokenTests {
final def initScanner(s: String) {
//Console.println("[scanner init on \""+s+"\"]");
value = ""
- it = Iterator.fromString(s)
+ it = (s).elements
token = 1+END
next
nextToken