summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/immutable/ListSet.scala
diff options
context:
space:
mode:
authormihaylov <mihaylov@epfl.ch>2006-09-12 12:18:44 +0000
committermihaylov <mihaylov@epfl.ch>2006-09-12 12:18:44 +0000
commit1874b9eba43766d3e601d8b8a5fb7ef6e58cef54 (patch)
tree93fdd04367bf7b706a80dd5bfae64c87cb97b5e6 /src/library/scala/collection/immutable/ListSet.scala
parentd1f14a8b11aba7a9ca011d6e54ffcf3c99d58bf1 (diff)
downloadscala-1874b9eba43766d3e601d8b8a5fb7ef6e58cef54.tar.gz
scala-1874b9eba43766d3e601d8b8a5fb7ef6e58cef54.tar.bz2
scala-1874b9eba43766d3e601d8b8a5fb7ef6e58cef54.zip
Small refactoring of the collection library
Diffstat (limited to 'src/library/scala/collection/immutable/ListSet.scala')
-rw-r--r--src/library/scala/collection/immutable/ListSet.scala35
1 files changed, 16 insertions, 19 deletions
diff --git a/src/library/scala/collection/immutable/ListSet.scala b/src/library/scala/collection/immutable/ListSet.scala
index a65babd3cf..2924e4ef96 100644
--- a/src/library/scala/collection/immutable/ListSet.scala
+++ b/src/library/scala/collection/immutable/ListSet.scala
@@ -36,7 +36,9 @@ class ListSet[A] extends AnyRef with Set[A] {
*/
def size: Int = 0
- def empty = new ListSet[A]
+ override def isEmpty: Boolean = true;
+
+ def empty[B] = new ListSet[B]
/** Checks if this set contains element <code>elem</code>.
*
@@ -54,18 +56,17 @@ class ListSet[A] extends AnyRef with Set[A] {
*/
def -(elem: A): ListSet[A] = this
- /** Creates a new iterator over all elements contained in this
- * object.
+ /** Creates a new iterator over all elements contained in this set.
*
* @return the new iterator
*/
- def elements: Iterator[A] = toList.elements
-
- /** Transform this set into a list of all elements.
- *
- * @return a list which enumerates all elements of this set.
- */
- override def toList: List[A] = Nil
+ def elements: Iterator[A] = new Iterator[A] {
+ var that: ListSet[A] = ListSet.this;
+ def hasNext = !that.isEmpty;
+ def next: A =
+ if (!hasNext) error("next on empty iterator")
+ else { val res = that.elem; that = that.next; res }
+ }
/** Compares two sets for equality.
* Two set are equal iff they contain the same elements.
@@ -77,9 +78,11 @@ class ListSet[A] extends AnyRef with Set[A] {
} else
false
- override def hashCode(): Int = 0
+ protected def elem: A = error("Set has no elelemnts");
+ protected def next: ListSet[A] = error("Next of an empty set");
- protected class Node(elem: A) extends ListSet[A] {
+ [serializable]
+ protected class Node(override protected val elem: A) extends ListSet[A] {
/** Returns the number of elements in this set.
*
* @return number of set elements.
@@ -110,12 +113,6 @@ class ListSet[A] extends AnyRef with Set[A] {
val tail = ListSet.this - e; new tail.Node(elem)
}
- /** Transform this set into a list of all elements.
- *
- * @return a list which enumerates all elements of this set.
- */
- override def toList: List[A] = elem :: ListSet.this.toList
-
- override def hashCode(): Int = elem.hashCode() + ListSet.this.hashCode()
+ override protected def next: ListSet[A] = ListSet.this;
}
}