summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authorMatthias Zenger <mzenger@gmail.com>2003-06-15 01:15:50 +0000
committerMatthias Zenger <mzenger@gmail.com>2003-06-15 01:15:50 +0000
commit8044852c6f604245acec3e2642c7035e3d2b84c9 (patch)
tree468a0ce67e714c1b6a1d868e4d3799be117b8be9 /sources
parentaa579de50f6c0f3588e5ef2b8188f8e2bcd32cd9 (diff)
downloadscala-8044852c6f604245acec3e2642c7035e3d2b84c9.tar.gz
scala-8044852c6f604245acec3e2642c7035e3d2b84c9.tar.bz2
scala-8044852c6f604245acec3e2642c7035e3d2b84c9.zip
Factored out a default map implementation that ...
Factored out a default map implementation that implements all map operations in terms of three simple operations: find, add, and remove.
Diffstat (limited to 'sources')
-rw-r--r--sources/scala/HashMap.scala33
-rw-r--r--sources/scala/ListMap.scala29
-rw-r--r--sources/scala/MapImpl.scala50
3 files changed, 82 insertions, 30 deletions
diff --git a/sources/scala/HashMap.scala b/sources/scala/HashMap.scala
index 4aa4bc7ab5..18af2a174e 100644
--- a/sources/scala/HashMap.scala
+++ b/sources/scala/HashMap.scala
@@ -11,36 +11,9 @@ package scala;
/** I promise, there will be some documentation soon! :-) Matthias
*/
-class HashMap[A, B] extends MutableMap[A, B] with HashTable[A] {
-
- def get(key: A) = findEntry(key) match {
- case None => None
- case Some(e) => Some(e.value);
- }
-
- def update(key: A, value: B) = findEntry(key) match {
- case None => addEntry(new Entry(key, value));
- case Some(e) => e.value = value;
- }
-
- def remove(key: A) = {
- val old = apply(key);
- removeEntry(key);
- old;
- }
-
- def elements = new Iterator[Pair[A, B]] {
- val iter = entries;
- def hasNext = iter.hasNext;
- def next = iter.next.toPair;
- }
-
- protected class Entry(k: A, v: B) {
- def key = k;
- var value = v;
- def toPair = Pair(k, value);
- override def toString() = k.toString() + " -> " + value;
- }
+class HashMap[A, B] extends MutableMap[A, B]
+ with HashTable[A]
+ with MapImpl[A, B] {
protected def entryKey(e: Entry) = e.key;
}
diff --git a/sources/scala/ListMap.scala b/sources/scala/ListMap.scala
new file mode 100644
index 0000000000..fa4a556607
--- /dev/null
+++ b/sources/scala/ListMap.scala
@@ -0,0 +1,29 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
+package scala;
+
+
+class ListMap[A, B] extends MutableMap[A, B]
+ with MapImpl[A, B] {
+
+ var xs: List[Entry] = Nil;
+
+ def size: Int = xs.length;
+
+ def clear: Unit = { xs = Nil; }
+
+ protected def findEntry(key: A) = xs find {e => e.key == key};
+
+ protected def addEntry(e: Entry) = { xs = e :: xs; }
+
+ protected def removeEntry(key: A) = { xs = xs filter {e => e.key != key}; }
+
+ protected def entries = xs.elements;
+}
diff --git a/sources/scala/MapImpl.scala b/sources/scala/MapImpl.scala
new file mode 100644
index 0000000000..0082f102b9
--- /dev/null
+++ b/sources/scala/MapImpl.scala
@@ -0,0 +1,50 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
+package scala;
+
+
+trait MapImpl[A, B] extends MutableMap[A, B] {
+
+ protected def findEntry(key: A): Option[Entry];
+
+ protected def addEntry(e: Entry): Unit;
+
+ protected def removeEntry(key: A): Unit;
+
+ protected def entries: Iterator[Entry];
+
+ def get(key: A) = findEntry(key) match {
+ case None => None
+ case Some(e) => Some(e.value);
+ }
+
+ def update(key: A, value: B) = findEntry(key) match {
+ case None => addEntry(new Entry(key, value));
+ case Some(e) => e.value = value;
+ }
+
+ def remove(key: A) = findEntry(key) match {
+ case None => null;
+ case Some(e) => removeEntry(key); e.value;
+ }
+
+ def elements = new Iterator[Pair[A, B]] {
+ val iter = entries;
+ def hasNext = iter.hasNext;
+ def next = iter.next.toPair;
+ }
+
+ protected class Entry(k: A, v: B) {
+ def key = k;
+ var value = v;
+ def toPair = Pair(k, value);
+ override def toString() = k.toString() + " -> " + value;
+ }
+}