diff options
author | mihaylov <mihaylov@epfl.ch> | 2006-09-12 12:18:44 +0000 |
---|---|---|
committer | mihaylov <mihaylov@epfl.ch> | 2006-09-12 12:18:44 +0000 |
commit | 1874b9eba43766d3e601d8b8a5fb7ef6e58cef54 (patch) | |
tree | 93fdd04367bf7b706a80dd5bfae64c87cb97b5e6 /src/library/scala/collection/immutable/ListMap.scala | |
parent | d1f14a8b11aba7a9ca011d6e54ffcf3c99d58bf1 (diff) | |
download | scala-1874b9eba43766d3e601d8b8a5fb7ef6e58cef54.tar.gz scala-1874b9eba43766d3e601d8b8a5fb7ef6e58cef54.tar.bz2 scala-1874b9eba43766d3e601d8b8a5fb7ef6e58cef54.zip |
Small refactoring of the collection library
Diffstat (limited to 'src/library/scala/collection/immutable/ListMap.scala')
-rw-r--r-- | src/library/scala/collection/immutable/ListMap.scala | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/library/scala/collection/immutable/ListMap.scala b/src/library/scala/collection/immutable/ListMap.scala index a8c765c3ed..b4a6687cc9 100644 --- a/src/library/scala/collection/immutable/ListMap.scala +++ b/src/library/scala/collection/immutable/ListMap.scala @@ -62,11 +62,13 @@ class ListMap[A, B] extends AnyRef with Map[A, B] { /** This returns an iterator over key-value pairs. */ - def elements: Iterator[Pair[A, B]] = toList.elements - - /** This return a list of key-value pairs. - */ - override def toList: List[Pair[A, B]] = Nil + def elements: Iterator[Pair[A,B]] = new Iterator[Pair[A,B]] { + var that: ListMap[A,B] = ListMap.this; + def hasNext = !that.isEmpty; + def next: Pair[A,B] = + if (!hasNext) error("next on empty iterator") + else { val res = Pair(that.key, that.value); that = that.next; res } + } /** Compares two maps for equality. * Two maps are equal iff they contain exactly the @@ -86,7 +88,14 @@ class ListMap[A, B] extends AnyRef with Map[A, B] { override def hashCode(): Int = 0 - protected class Node(key: A, value: B) extends ListMap[A, B] { + protected def key: A = error("empty map"); + protected def value: B = error("empty map"); + protected def next: ListMap[A, B] = error("empty map"); + + [serializable] + protected class Node(override protected val key: A, override protected val value: B) + extends ListMap[A, B] + { /** Returns the number of mappings in this map. * * @return number of mappings. @@ -144,13 +153,9 @@ class ListMap[A, B] extends AnyRef with Map[A, B] { val tail = ListMap.this - k; new tail.Node(key, value) } - /** This return a list of key-value pairs. - */ - override def toList: List[Pair[A, B]] = - Pair(key, value) :: ListMap.this.toList - override def hashCode(): Int = (key.hashCode() ^ value.hashCode()) + ListMap.this.hashCode() + + override protected def next: ListMap[A,B] = ListMap.this; } } - |