summaryrefslogtreecommitdiff
path: root/sources/scala/collection/immutable/Map.scala
diff options
context:
space:
mode:
authorschinz <schinz@epfl.ch>2003-07-08 08:35:16 +0000
committerschinz <schinz@epfl.ch>2003-07-08 08:35:16 +0000
commitd2df7c9c9a02cd91d2dabaf4709ab77235df13c2 (patch)
treeace55450cd241dc937f599078b7daad9eca33f0e /sources/scala/collection/immutable/Map.scala
parent1d24dc9093b581573f5b544f9a555c2a7a16d914 (diff)
downloadscala-d2df7c9c9a02cd91d2dabaf4709ab77235df13c2.tar.gz
scala-d2df7c9c9a02cd91d2dabaf4709ab77235df13c2.tar.bz2
scala-d2df7c9c9a02cd91d2dabaf4709ab77235df13c2.zip
*** empty log message ***
Diffstat (limited to 'sources/scala/collection/immutable/Map.scala')
-rw-r--r--sources/scala/collection/immutable/Map.scala62
1 files changed, 62 insertions, 0 deletions
diff --git a/sources/scala/collection/immutable/Map.scala b/sources/scala/collection/immutable/Map.scala
new file mode 100644
index 0000000000..7182d18e78
--- /dev/null
+++ b/sources/scala/collection/immutable/Map.scala
@@ -0,0 +1,62 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+** $Id$
+\* */
+
+package scala;
+
+trait ImmutableMap[A, B, This <: ImmutableMap[A, B, This]]: This with Map[A, B] {
+
+ def update(key: A, value: B): This;
+
+ def remove(key: A): This;
+
+ def clear: This;
+
+ def put(mappings: Pair[A, B]*): This = putMap(mappings);
+
+ def putMap(map: Iterable[Pair[A, B]]): This = {
+ val iter = map.elements;
+ var res = this;
+ while (iter.hasNext) {
+ val Pair(key, value) = iter.next;
+ res = res.update(key, value);
+ }
+ res;
+ }
+
+ def map(f: (A, B) => B): This = {
+ var res = this;
+ elements foreach {
+ case Pair(key, value) => res = res.update(key, f(key, value));
+ }
+ res;
+ }
+
+ def filter(p: (A, B) => Boolean): This = {
+ var res = this;
+ toList foreach {
+ case Pair(key, value) => if (p(key, value)) { res = res.remove(key); }
+ }
+ res;
+ }
+
+ override def toString() =
+ if (size == 0)
+ "{}"
+ else
+ "{" + {
+ val iter = elements;
+ var res = mappingToString(iter.next);
+ while (iter.hasNext) {
+ res = res + ", " + mappingToString(iter.next);
+ }
+ res;
+ } + "}";
+
+ def mappingToString(p: Pair[A, B]) = p._1.toString() + " -> " + p._2;
+}