diff options
author | Matthias Zenger <mzenger@gmail.com> | 2004-04-29 10:38:17 +0000 |
---|---|---|
committer | Matthias Zenger <mzenger@gmail.com> | 2004-04-29 10:38:17 +0000 |
commit | 62ba1d3b91c448ed62f6c2accf9a8b4011ed8d29 (patch) | |
tree | 3d63fdb55c1c805650574ca8e92f2508d56ea544 | |
parent | e329fb0ec7dd1250428424d128369866eed03d51 (diff) | |
download | scala-62ba1d3b91c448ed62f6c2accf9a8b4011ed8d29.tar.gz scala-62ba1d3b91c448ed62f6c2accf9a8b4011ed8d29.tar.bz2 scala-62ba1d3b91c448ed62f6c2accf9a8b4011ed8d29.zip |
*** empty log message ***
-rw-r--r-- | config/list/library.lst | 3 | ||||
-rw-r--r-- | sources/scala/IterableProxy.scala | 101 | ||||
-rw-r--r-- | sources/scala/Ordered.scala | 4 | ||||
-rw-r--r-- | sources/scala/PartiallyOrdered.scala | 3 | ||||
-rw-r--r-- | sources/scala/Predef.scala | 61 | ||||
-rw-r--r-- | sources/scala/Proxy.scala | 25 | ||||
-rw-r--r-- | sources/scala/SeqProxy.scala | 68 | ||||
-rw-r--r-- | sources/scala/collection/MapProxy.scala | 6 | ||||
-rw-r--r-- | sources/scala/collection/SetProxy.scala | 6 | ||||
-rw-r--r-- | sources/scala/collection/mutable/BufferProxy.scala | 9 |
10 files changed, 237 insertions, 49 deletions
diff --git a/config/list/library.lst b/config/list/library.lst index f1774505c1..1f4b8f2990 100644 --- a/config/list/library.lst +++ b/config/list/library.lst @@ -27,6 +27,7 @@ Function8.java Function9.java Int.java Iterable.scala +IterableProxy.scala Iterator.scala List.scala Long.java @@ -38,9 +39,11 @@ Ordered.scala PartiallyOrdered.scala PartialFunction.scala Predef.scala +Proxy.scala Ref.java ScalaObject.java Seq.scala +SeqProxy.scala Short.java Some.scala Stream.scala diff --git a/sources/scala/IterableProxy.scala b/sources/scala/IterableProxy.scala new file mode 100644 index 0000000000..13dfebb4fc --- /dev/null +++ b/sources/scala/IterableProxy.scala @@ -0,0 +1,101 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala; + + +/** This class implements a proxy for iterable objects. It forwards + * all calls to a different iterable object. + * + * @author Matthias Zenger + * @version 1.0, 26/04/2004 + */ +class IterableProxy[+A](x: Iterable[A]) extends Iterable[A] with Proxy(x) { + + /** Creates a new iterator over all elements contained in this + * object. + * + * @return the new iterator + */ + def elements: Iterator[A] = x.elements; + + /** Apply a function <code>f</code> to all elements of this + * iterable object. + * + * @param f a function that is applied to every element. + */ + override def foreach(f: A => Unit): Unit = x.foreach(f); + + /** Apply a predicate <code>p</code> to all elements of this + * iterable object and return true, iff the predicate yields + * true for all elements. + * + * @param p the predicate + * @returns true, iff the predicate yields true for all elements. + */ + override def forall(p: A => Boolean): Boolean = x.forall(p); + + /** Apply a predicate <code>p</code> to all elements of this + * iterable object and return true, iff there is at least one + * element for which <code>p</code> yields true. + * + * @param p the predicate + * @returns true, iff the predicate yields true for at least one element. + */ + override def exists(p: A => Boolean): Boolean = x.exists(p); + + /** Find and return the first element of the iterable object satisfying a + * predicate, if any. + * + * @param p the predicate + * @return the first element in the iterable object satisfying <code>p</code>, + * or <code>None</code> if none exists. + */ + override def find(p: A => Boolean): Option[A] = x.find(p); + + /** Combines the elements of this list together using the binary + * operator <code>op</code>, from left to right, and starting with + * the value <code>z</code>. + * @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list + * is <code>List(a0, a1, ..., an)</code>. + */ + override def foldLeft[B](z: B)(op: (B, A) => B): B = x.foldLeft(z)(op); + + /** Combines the elements of this list together using the binary + * operator <code>op</code>, from rigth to left, and starting with + * the value <code>z</code>. + * @return <code>a0 op (... op (an op z)...)</code> if the list + * is <code>[a0, a1, ..., an]</code>. + */ + override def foldRight[B](z: B)(op: (A, B) => B): B = x.foldRight(z)(op); + + /** Similar to <code>foldLeft</code> but can be used as + * an operator with the order of list and zero arguments reversed. + * That is, <code>z /: xs</code> is the same as <code>xs foldLeft z</code> + */ + override def /:[B](z: B)(f: (B, A) => B): B = x./:(z)(f); + + /** An alias for <code>foldRight</code>. + * That is, <code>xs :\ z</code> is the same as <code>xs foldRight z</code> + */ + override def :\[B](z: B)(f: (A, B) => B): B = x.:\(z)(f); + + /** Transform this iterable object into a list of all elements. + * + * @return a list which enumerates all elements of this set. + */ + override def toList: List[A] = x.toList; + + /** Checks if the other iterable object contains the same elements. + * + * @param that the other iterable object + * @return true, iff both iterable objects contain the same elements. + */ + override def sameElements[B >: A](that: Iterable[B]): Boolean = x.sameElements(that); +} diff --git a/sources/scala/Ordered.scala b/sources/scala/Ordered.scala index 2a75d68923..99fd25dab9 100644 --- a/sources/scala/Ordered.scala +++ b/sources/scala/Ordered.scala @@ -11,6 +11,9 @@ package scala; /** A trait for totally ordered data. + * + * @author Martin Odersky + * @version 1.0, 23/04/2004 */ trait Ordered[+a] { @@ -30,4 +33,3 @@ trait Ordered[+a] { def >= [b >: a <% Ordered[b]](that: b): boolean = (this compareTo that) >= 0; } - diff --git a/sources/scala/PartiallyOrdered.scala b/sources/scala/PartiallyOrdered.scala index bc1de71800..fba76066e0 100644 --- a/sources/scala/PartiallyOrdered.scala +++ b/sources/scala/PartiallyOrdered.scala @@ -11,6 +11,9 @@ package scala; /** A trait for partially ordered data. + * + * @author Martin Odersky + * @version 1.0, 23/04/2004 */ trait PartiallyOrdered[+a] { diff --git a/sources/scala/Predef.scala b/sources/scala/Predef.scala index f6cc14a52f..1b31e3464d 100644 --- a/sources/scala/Predef.scala +++ b/sources/scala/Predef.scala @@ -75,7 +75,7 @@ object Predef { // views ------------------------------------------------------------- - def view(x: int): Ordered[int] = new Ordered[int] { + def view(x: int): Ordered[int] = new Ordered[int] with Proxy(x) { def compareTo [b >: int <% Ordered[b]](y: b): int = y match { case y1: int => if (x < y1) -1 @@ -85,7 +85,7 @@ object Predef { } } - def view(x: char): Ordered[char] = new Ordered[char] { + def view(x: char): Ordered[char] = new Ordered[char] with Proxy(x) { def compareTo [b >: char <% Ordered[b]](y: b): int = y match { case y1: char => if (x < y1) -1 @@ -95,7 +95,7 @@ object Predef { } } - def view(x: long): Ordered[long] = new Ordered[long] { + def view(x: long): Ordered[long] = new Ordered[long] with Proxy(x) { def compareTo [b >: long <% Ordered[b]](y: b): int = y match { case y1: long => if (x < y1) -1 @@ -105,7 +105,7 @@ object Predef { } } - def view(x: float): Ordered[float] = new Ordered[float] { + def view(x: float): Ordered[float] = new Ordered[float] with Proxy(x) { def compareTo [b >: float <% Ordered[b]](y: b): int = y match { case y1: float => if (x < y1) -1 @@ -115,7 +115,7 @@ object Predef { } } - def view(x: double): Ordered[double] = new Ordered[double] { + def view(x: double): Ordered[double] = new Ordered[double] with Proxy(x) { def compareTo [b >: double <% Ordered[b]](y: b): int = y match { case y1: double => if (x < y1) -1 @@ -125,7 +125,7 @@ object Predef { } } - def view(x: boolean): Ordered[boolean] = new Ordered[boolean] { + def view(x: boolean): Ordered[boolean] = new Ordered[boolean] with Proxy(x) { def compareTo [b >: boolean <% Ordered[b]](y: b): int = y match { case y1: boolean => if (x == y1) 0 @@ -135,34 +135,35 @@ object Predef { } } - def view(x: String): Ordered[String] = new Ordered[String] { - def compareTo [b >: String <% Ordered[b]](y: b): int = y match { - case y1: String => x compareTo y1; - case _ => -(y compareTo x) - } - } - def view[a <% Ordered[a]](x: Array[a]): Ordered[Array[a]] = new Ordered[Array[a]] { - def compareTo [b >: Array[a] <% Ordered[b]](y: b): int = y match { - case y1: Array[a] => compareArrays(x, y1); - case _ => -(y compareTo x) - } - private def compareArrays(xs: Array[a], ys: Array[a]): int = { - var i = 0; - while (i < xs.length && i < ys.length) { - if (xs(i) < ys(i)) return -1; - if (xs(i) > ys(i)) return 1; - i = i + 1 - } - if (i < xs.length) return 1 - else if (i < ys.length) return -1 - else 0 - } - } - def view[A](xs: Array[A]): Seq[A] = new Seq[A] { def length = xs.length; def elements = Iterator.fromArray(xs); def apply(n: Int) = xs(n); + override def hashCode(): Int = xs.hashCode(); + override def equals(y: Any): Boolean = xs.equals(y); override protected def stringPrefix: String = "Array"; } + + def view[A <% Ordered[A]](xs: Array[A]): Ordered[Array[A]] = new Ordered[Array[A]] with Proxy(xs) { + def compareTo[B >: Array[A] <% Ordered[B]](that: B): Int = that match { + case ys: Array[A] => + var i, res = 0; + while ((i < xs.length) && (i < ys.length) && (res == 0)) { + res = xs(i) compareTo ys(i); + i = i + 1; + } + if (res != 0) res + else if (i < xs.length) 1 + else -1 + case _ => + -(that compareTo xs) + } + } + + def view(x: String): Ordered[String] = new Ordered[String] with Proxy(x) { + def compareTo [b >: String <% Ordered[b]](y: b): int = y match { + case y1: String => x compareTo y1; + case _ => -(y compareTo x) + } + } } diff --git a/sources/scala/Proxy.scala b/sources/scala/Proxy.scala new file mode 100644 index 0000000000..28b28fccd4 --- /dev/null +++ b/sources/scala/Proxy.scala @@ -0,0 +1,25 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala; + + +/** This class implements a simple proxy that forwards all calls to + * methods of class <code>Any</code> to another object <code>x</code>. + * Please note that only those methods can be forwarded that are + * overridable and public. + * + * @author Matthias Zenger + * @version 1.0, 26/04/2004 + */ +class Proxy(x: Any) { + override def hashCode(): Int = x.hashCode(); + override def equals(y: Any): Boolean = x.equals(y); + override def toString(): String = x.toString(); +} diff --git a/sources/scala/SeqProxy.scala b/sources/scala/SeqProxy.scala new file mode 100644 index 0000000000..e8bd68781f --- /dev/null +++ b/sources/scala/SeqProxy.scala @@ -0,0 +1,68 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala; + + +/** Class <code>Seq[A]</code> represents finite sequences of elements + * of type <code>A</code>. + * + * @author Martin Odersky + * @author Matthias Zenger + * @version 1.0, 16/07/2003 + */ +class SeqProxy[+A](x: Seq[A]) extends IterableProxy(x) { + + /** Returns the length of the sequence. + * + * @return the sequence length. + */ + def length: Int = x.length; + + /** Is this partial function defined for the index <code>x</code>? + * + * @return true, iff <code>x</code> is a legal sequence index. + */ + def isDefinedAt(y: Int): Boolean = x.isDefinedAt(y); + + /** 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 = x.indexOf(elem); + + /** 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 = x.lastIndexOf(elem); + + /** Returns a subsequence starting from index <code>from</code> + * consisting of <code>len</code> elements. + */ + def subSequence(from: Int, len: Int): Seq[A] = x.subSequence(from, len); + + /** 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] = x.copyToArray(xs, start); +} diff --git a/sources/scala/collection/MapProxy.scala b/sources/scala/collection/MapProxy.scala index 23d63d6425..2886c1f1f9 100644 --- a/sources/scala/collection/MapProxy.scala +++ b/sources/scala/collection/MapProxy.scala @@ -17,7 +17,7 @@ package scala.collection; * @author Matthias Zenger * @version 1.0, 21/07/2003 */ -class MapProxy[A, +B](map: Map[A, B]) extends Map[A, B] { +class MapProxy[A, +B](map: Map[A, B]) extends Map[A, B] with IterableProxy(map) { def size: Int = map.size; @@ -38,8 +38,4 @@ class MapProxy[A, +B](map: Map[A, B]) extends Map[A, B] { override def foreach(f: (A, B) => Unit) = map.foreach(f); override def toList: List[Pair[A, B]] = map.toList; - - override def toString() = map.toString(); - - def elements: Iterator[Pair[A, B]] = map.elements; } diff --git a/sources/scala/collection/SetProxy.scala b/sources/scala/collection/SetProxy.scala index be20c64251..9d030f58b2 100644 --- a/sources/scala/collection/SetProxy.scala +++ b/sources/scala/collection/SetProxy.scala @@ -17,7 +17,7 @@ package scala.collection; * @author Matthias Zenger * @version 1.0, 21/07/2003 */ -class SetProxy[A](set: Set[A]) extends Set[A] { +class SetProxy[A](set: Set[A]) extends Set[A] with IterableProxy(set) { def size: Int = set.size; @@ -32,8 +32,4 @@ class SetProxy[A](set: Set[A]) extends Set[A] { override def exists(p: A => Boolean): Boolean = set.exists(p); override def toList: List[A] = set.toList; - - override def toString() = set.toString(); - - def elements: Iterator[A] = set.elements; } diff --git a/sources/scala/collection/mutable/BufferProxy.scala b/sources/scala/collection/mutable/BufferProxy.scala index 06142e4028..8a30964006 100644 --- a/sources/scala/collection/mutable/BufferProxy.scala +++ b/sources/scala/collection/mutable/BufferProxy.scala @@ -17,7 +17,7 @@ package scala.collection.mutable; * @author Matthias Zenger * @version 1.0, 16/04/2004 */ -class BufferProxy[A](buf: Buffer[A]) extends Buffer[A] { +class BufferProxy[A](buf: Buffer[A]) extends Buffer[A] with Proxy(buf) { def length: Int = buf.length; @@ -130,11 +130,4 @@ class BufferProxy[A](buf: Buffer[A]) extends Buffer[A] { /** Clears the buffer contents. */ def clear: Unit = buf.clear; - - /** The hashCode method always yields an error, since it is not - * safe to use buffers as keys in hash tables. - * - * @return never. - */ - override def hashCode(): Int = buf.hashCode(); } |