diff options
author | Matthias Zenger <mzenger@gmail.com> | 2004-03-15 23:09:19 +0000 |
---|---|---|
committer | Matthias Zenger <mzenger@gmail.com> | 2004-03-15 23:09:19 +0000 |
commit | f410167a756064ef2d08c45a5073da7fb1482f9a (patch) | |
tree | 19767c15f0bad13d5f850c22566dd68d840bf277 /sources | |
parent | 1b56122b7427d6f22fcbcada5af4af1378ce5b80 (diff) | |
download | scala-f410167a756064ef2d08c45a5073da7fb1482f9a.tar.gz scala-f410167a756064ef2d08c45a5073da7fb1482f9a.tar.bz2 scala-f410167a756064ef2d08c45a5073da7fb1482f9a.zip |
*** empty log message ***
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/Array.java | 4 | ||||
-rw-r--r-- | sources/scala/List.scala | 16 | ||||
-rw-r--r-- | sources/scala/Seq.scala | 79 | ||||
-rw-r--r-- | sources/scala/Stream.scala | 4 | ||||
-rw-r--r-- | sources/scala/tools/scalac/ast/parser/Scanner.scala | 2 |
5 files changed, 86 insertions, 19 deletions
diff --git a/sources/scala/Array.java b/sources/scala/Array.java index 261908682f..83be225526 100644 --- a/sources/scala/Array.java +++ b/sources/scala/Array.java @@ -25,6 +25,10 @@ public abstract class Array */ public abstract int length(); + public boolean isDefinedAt(int x) { + return (x >= 0) && (x < length()); + } + /** @meta method (scala.Int, ?T) scala.Unit; */ public abstract void update(int i, java.lang.Object x); diff --git a/sources/scala/List.scala b/sources/scala/List.scala index fc5c8a63c5..8483983e80 100644 --- a/sources/scala/List.scala +++ b/sources/scala/List.scala @@ -509,7 +509,6 @@ trait List[+a] extends Seq[a] { sort_1(x::small, y::sort_1(z::large, acc)) } } - match { case Nil => this @@ -632,20 +631,6 @@ trait List[+a] extends Seq[a] { def reverse: List[a] = foldLeft(Nil : List[a])((xs, x) => x :: xs); - /** Fills the given array <code>xs</code> with the elements of - * this list starting at position <code>start</code>. Does not - * work with empty lists. - * - * @param xs the array to fill. - * @param start starting index. - * @return the given array <code>xs</code> filled with this list. - * @throws error if the list is empty. - */ - def copyToArray[b >: a](xs: Array[b], start: Int): Array[b] = match { - case Nil => xs - case y :: ys => xs(start) = y; ys.copyToArray(xs, start + 1) - } - /** Returns a string representation of this list. 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. @@ -675,7 +660,6 @@ trait List[+a] extends Seq[a] { * @param that must have the same length as the self list. * @return <code>[(a0,b0), ..., (an,bn)]</code> when * <code>[a0, ..., an] zip [b0, ..., bn]</code> is invoked. - * @throws java.lang.RuntimeException if lists have different lengths. */ def zip[b](that: List[b]): List[Pair[a,b]] = if (this.isEmpty || that.isEmpty) Nil diff --git a/sources/scala/Seq.scala b/sources/scala/Seq.scala index f419746335..0a7f4bc7ba 100644 --- a/sources/scala/Seq.scala +++ b/sources/scala/Seq.scala @@ -31,6 +31,85 @@ trait Seq[+A] with PartialFunction[Int, A] with Iterable[A] with Similarity { */ def isDefinedAt(x: Int): Boolean = (x >= 0) && (x < length); + /** Returns the index of the first occurence of the specified + * object in this sequence. + * + * @param elem element to search for. + * @return the index in this sequence of the first occurence of the specified + * element, or -1 if the sequence does not contain this element. + */ + def indexOf[B >: A](elem: B): Int = { + val it = elements; + var i = 0; + var found = false; + while (!found && it.hasNext) { + if (it.next == elem) { + found = true; + } else { + i = i + 1 + } + } + if (found) i else -1; + } + + /** Returns the index of the last occurence of the specified + * element in this sequence, or -1 if the sequence does not + * contain this element. + * + * @param elem element to search for. + * @return the index in this sequence of the last occurence of the + * specified element, or -1 if the sequence does not contain + * this element. + */ + def lastIndexOf[B >: A](elem: B): Int = { + var i = length; + var found = false; + while (!found && (i > 0)) { + i = i - 1; + if (this(i) == elem) { + found = true; + } + } + if (found) i else -1; + } + + /** Returns a subsequence starting from index <code>from</code> + * consisting of <code>len</code> elements. + */ + def subSequence(from: Int, len: Int): Seq[A] = + if ((from + len) <= length) new Seq[A] { + def apply(n: Int): A = Seq.this.apply(n - from); + def length: Int = len; + def elements: Iterator[A] = new Iterator[A] { + var i = from; + def hasNext = (i < (from + len)); + def next = { + val res = Seq.this.apply(i); + i = i + 1; + res + } + } + } else + error("cannot create subsequence"); + + /** Fills the given array <code>xs</code> with the elements of + * this list starting at position <code>start</code>. Does not + * work with empty lists. + * + * @param xs the array to fill. + * @param start starting index. + * @return the given array <code>xs</code> filled with this list. + */ + def copyToArray[B >: A](xs: Array[B], start: Int): Array[B] = { + val it = elements; + var i = start; + while (it.hasNext) { + xs(i) = it.next; + i = i + 1; + } + xs + } + /** Returns true if the elements in this sequence are equal * to the elements in another sequence */ diff --git a/sources/scala/Stream.scala b/sources/scala/Stream.scala index 1898848a38..a87b5187fd 100644 --- a/sources/scala/Stream.scala +++ b/sources/scala/Stream.scala @@ -109,8 +109,8 @@ trait Stream[+a] extends Seq[a] { // xs // } - def copyToArray[b >: a](xs: Array[b], start: int): int = - if (isEmpty) start + override def copyToArray[b >: a](xs: Array[b], start: int): Array[b] = + if (isEmpty) xs else { xs(start) = head; tail.copyToArray(xs, start + 1) } def zip[b](that: Stream[b]): Stream[Tuple2[a, b]] = diff --git a/sources/scala/tools/scalac/ast/parser/Scanner.scala b/sources/scala/tools/scalac/ast/parser/Scanner.scala index b9c83ce091..6b01b484f5 100644 --- a/sources/scala/tools/scalac/ast/parser/Scanner.scala +++ b/sources/scala/tools/scalac/ast/parser/Scanner.scala @@ -63,7 +63,7 @@ class Scanner(_unit: Unit) extends TokenData { /** the input buffer: */ var buf: Array[byte] = unit.source.bytes(); - private var bp: int = -1; + var bp: int = -1; /** the current character */ |