diff options
Diffstat (limited to 'sources/scala/collection/immutable/Map.scala')
-rw-r--r-- | sources/scala/collection/immutable/Map.scala | 179 |
1 files changed, 89 insertions, 90 deletions
diff --git a/sources/scala/collection/immutable/Map.scala b/sources/scala/collection/immutable/Map.scala index 84b79bd813..0dd67df7b1 100644 --- a/sources/scala/collection/immutable/Map.scala +++ b/sources/scala/collection/immutable/Map.scala @@ -9,6 +9,7 @@ package scala.collection.immutable; + /** This trait extends the Map interface of collections that unambiguously map * keys to values (i.e. a key is mapped to at least one value). * This trait defines the interface for functional map implementations @@ -16,71 +17,70 @@ package scala.collection.immutable; * Concrete map implementations have to provide functionality for the * abstract methods in scala.collection.Map as well as for * <code>factory</code>, <code>update</code>, and -. - + * * @author Matthias Zenger, Erik Stenman * @version 1.0, 03/12/2003 */ -trait Map[KEY, VALUE] with scala.collection.Map[KEY, VALUE] { - - /** A factory to create empty maps of the same type of keys. - */ - val factory:MapFactory[KEY]; - - - /** This method allows one to create a new map with an - * additional mapping from <code>key</code> - * to <code>value</code>. If the map contains already a - * mapping for <code>key</code>, it will be overridden by this - * function. - */ - def update(key: KEY, value: VALUE): Map[KEY, VALUE]; - - /** This creates a new mapping without the given <code>key</code>. - * If the map does not contain a mapping for the given key, the - * method returns the same map. - */ - def -(key: KEY): Map[KEY, VALUE]; - - /** This method defines syntactic sugar for adding a - * mapping. It is typically used in the following way: - * <pre> - * map + key -> value; - * </pre> - */ - def +(key: KEY): MapTo = new MapTo(key); - - - /** <code>incl</code> can be used to add many mappings at the same time - * to the map. The method assumes that a mapping is represented - * by a <code>Pair</code> object who's first component denotes the - * key, and who's second component refers to the value. - */ - def incl(mappings: Pair[KEY, VALUE]*): Map[KEY, VALUE] = incl(mappings); - - /** <code>incl</code> can be used to add many mappings at the same time - * to the map. The method assumes that each mapping is represented - * by an Iterator over <code>Pair</code> objects who's first component - * denotes the key, and who's second component refers to the value. - */ - def incl(map: Iterable[Pair[KEY, VALUE]]): Map[KEY, VALUE] = { - val iter = map.elements; - var res = this; - while (iter.hasNext) { - val Pair(key, value) = iter.next; - res = res.update(key, value); +trait Map[A, B] with scala.collection.Map[A, B] { + + /** This method returns a new map instance of the same class + * mapping keys of the same type to values of type <code>C</code>. + */ + def empty[C]: Map[A, C]; + + /** This method allows one to create a new map with an + * additional mapping from <code>key</code> + * to <code>value</code>. If the map contains already a + * mapping for <code>key</code>, it will be overridden by this + * function. + */ + def update(key: A, value: B): Map[A, B]; + + /** This creates a new mapping without the given <code>key</code>. + * If the map does not contain a mapping for the given key, the + * method returns the same map. + */ + def -(key: A): Map[A, B]; + + /** This method defines syntactic sugar for adding a + * mapping. It is typically used in the following way: + * <pre> + * map + key -> value; + * </pre> + */ + def +(key: A): MapTo = new MapTo(key); + + /** <code>incl</code> can be used to add many mappings at the same time + * to the map. The method assumes that a mapping is represented + * by a <code>Pair</code> object who's first component denotes the + * key, and who's second component refers to the value. + */ + def incl(mappings: Pair[A, B]*): Map[A, B] = incl(mappings); + + /** <code>incl</code> can be used to add many mappings at the same time + * to the map. The method assumes that each mapping is represented + * by an Iterator over <code>Pair</code> objects who's first component + * denotes the key, and who's second component refers to the value. + */ + def incl(map: Iterable[Pair[A, B]]): Map[A, B] = { + val iter = map.elements; + var res = this; + while (iter.hasNext) { + val Pair(key, value) = iter.next; + res = res.update(key, value); + } + res } - res; - } - - /** This method will return a map where all the mappings - * for the given sequence of keys are removed from the map. - */ - def excl(keys: KEY*): Map[KEY, VALUE] = excl(keys); - - /** This method removes all the mappings for keys provided by an - * iterator over the elements of the <code>keys</code> object. - */ - def excl(keys: Iterable[KEY]): Map[KEY, VALUE] = { + + /** This method will return a map where all the mappings + * for the given sequence of keys are removed from the map. + */ + def excl(keys: A*): Map[A, B] = excl(keys); + + /** This method removes all the mappings for keys provided by an + * iterator over the elements of the <code>keys</code> object. + */ + def excl(keys: Iterable[A]): Map[A, B] = { val iter = keys.elements; var res = this; while (iter.hasNext) { @@ -89,11 +89,11 @@ trait Map[KEY, VALUE] with scala.collection.Map[KEY, VALUE] { res; } - /** This function transforms all the values of mappings contained + /** This function transforms all the values of mappings contained * in this map with function <code>f</code>. */ - def map[C <: Any](f: (KEY, VALUE) => C): Map[KEY, C] = { - var res = factory.Empty[C]; + def map[C](f: (A, B) => C): Map[A, C] = { + var res = empty[C]; elements foreach { case Pair(key, value) => res = res.update(key, f(key, value)); } @@ -103,7 +103,7 @@ trait Map[KEY, VALUE] with scala.collection.Map[KEY, VALUE] { /** This method removes all the mappings for which the predicate * <code>p</code> returns <code>false</code>. */ - def filter(p: (KEY, VALUE) => Boolean): Map[KEY, VALUE] = { + def filter(p: (A, B) => Boolean): Map[A, B] = { var res = this; toList foreach { case Pair(key, value) => if (p(key, value)) { res = res.excl(key); } @@ -127,32 +127,31 @@ trait Map[KEY, VALUE] with scala.collection.Map[KEY, VALUE] { res; } + "}"; - /** Compares two maps for equality. - * Two maps are equal iff they contain exactly the - * same key-value pairs. - */ - override def equals(obj: Any): Boolean = - if (obj.isInstanceOf[scala.collection.Map[KEY, VALUE]]) { - val that = obj.asInstanceOf[scala.collection.Map[KEY, VALUE]]; - if (size != that.size) false else elements.forall { - case Pair(key, value) => that.get(key) match { - case None => false; - case Some(v) => v == value; - } - }; - } else - false; + /** Compares two maps for equality. + * Two maps are equal iff they contain exactly the + * same key-value pairs. + */ + override def equals(obj: Any): Boolean = + if (obj.isInstanceOf[scala.collection.Map[A, B]]) { + val that = obj.asInstanceOf[scala.collection.Map[A, B]]; + if (size != that.size) + false + else + elements forall { + case Pair(key, value) => that.get(key) match { + case None => false; + case Some(v) => v == value; + } + } + } else + false; - /** This method controls how a mapping is represented in the string - * representation provided by method <code>toString</code>. - */ - def mappingToString(p: Pair[KEY, VALUE]) = p._1.toString() + " -> " + p._2; + /** This method controls how a mapping is represented in the string + * representation provided by method <code>toString</code>. + */ + def mappingToString(p: Pair[A, B]) = p._1.toString() + " -> " + p._2; - class MapTo(key: KEY) { - def ->(value: VALUE): Map[KEY, VALUE] = update(key, value); + class MapTo(key: A) { + def ->(value: B): Map[A, B] = update(key, value); } } - -abstract class MapFactory[KEY] { - def Empty[VALUE]:Map[KEY,VALUE]; -} |