diff options
author | michelou <michelou@epfl.ch> | 2005-05-27 11:37:00 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2005-05-27 11:37:00 +0000 |
commit | cb0e7af1e833a02dc05bafc5f3861ef7768dfa09 (patch) | |
tree | 9d413783c90286b00c71d7f71a8a0859312ec045 /sources | |
parent | 5ba805cbfc764efd4c04a7f2d05136b2c3bd5105 (diff) | |
download | scala-cb0e7af1e833a02dc05bafc5f3861ef7768dfa09.tar.gz scala-cb0e7af1e833a02dc05bafc5f3861ef7768dfa09.tar.bz2 scala-cb0e7af1e833a02dc05bafc5f3861ef7768dfa09.zip |
- changed indentation (4 -> 2).
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/collection/mutable/Map.scala | 272 |
1 files changed, 147 insertions, 125 deletions
diff --git a/sources/scala/collection/mutable/Map.scala b/sources/scala/collection/mutable/Map.scala index 88fe6c55b2..1432b13394 100644 --- a/sources/scala/collection/mutable/Map.scala +++ b/sources/scala/collection/mutable/Map.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -19,128 +19,150 @@ package scala.collection.mutable; */ trait Map[A, B] extends AnyRef with scala.collection.Map[A, B] with Scriptable[Message[Pair[A, B]]] with Cloneable { - /** This method allows one to add a new mapping from <code>key</code> - * to <code>value</code> to the map. If the map already contains a - * mapping for <code>key</code>, it will be overridden by this - * function. - */ - def update(key: A, value: B): Unit; - - /** This method defines syntactic sugar for adding or modifying - * mappings. It is typically used in the following way: - * <pre> - * map += key -> value; - * </pre> - */ - def +=(key: A): MapTo = new MapTo(key); - - /** This method adds all the mappings provided by an iterator of - * parameter <code>map</code> to the map. - */ - def ++=(map: Iterable[Pair[A, B]]): Unit = ++=(map.elements); - - /** This method adds all the mappings provided by an iterator of - * parameter <code>map</code> to the map. - */ - def ++=(it: Iterator[Pair[A, B]]): Unit = it foreach { - case Pair(key, value) => update(key, value); - } - - /** <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]*): Unit = ++=(mappings.elements); - - /** This method removes a mapping from the given <code>key</code>. - * If the map does not contain a mapping for the given key, the - * method does nothing. - */ - def -=(key: A): Unit; - - /** This method removes all the mappings for keys provided by an - * iterator over the elements of the <code>keys</code> object. - */ - def --=(keys: Iterable[A]): Unit = --=(keys.elements); - - /** This method removes all the mappings for keys provided by an - * iterator over the elements of the <code>keys</code> object. - */ - def --=(it: Iterator[A]): Unit = it foreach -=; - - /** This method will remove all the mappings for the given sequence - * of keys from the map. - */ - def excl(keys: A*): Unit = --=(keys.elements); - - /** Removes all mappings from the map. After this operation is - * completed, the map is empty. - */ - def clear: Unit = keys foreach -=; - - /** This function transforms all the values of mappings contained - * in this map with function <code>f</code>. - */ - def map(f: (A, B) => B): Unit = elements foreach { - case Pair(key, value) => update(key, f(key, value)); - } - - /** This method removes all the mappings for which the predicate - * <code>p</code> returns <code>false</code>. - */ - def filter(p: (A, B) => Boolean): Unit = toList foreach { - case Pair(key, value) => if (!p(key, value)) -=(key); - } - - /** Send a message to this scriptable object. - * - * @param cmd the message to send. - */ - def <<(cmd: Message[Pair[A, B]]): Unit = cmd match { - case Include(Pair(k, v)) => update(k, v); - case Update(Pair(k, v)) => update(k, v); - case Remove(Pair(k, _)) => this -= k; - case Reset() => clear; - case s: Script[Pair[A, B]] => s.elements foreach <<; - case _ => error("message " + cmd + " not understood"); - } - - /** Return a clone of this map. - * - * @return an map with the same elements. - */ - override def clone(): Map[A, B] = super.clone().asInstanceOf[Map[A, B]]; - - /** The hashCode method always yields an error, since it is not - * safe to use mutable maps as keys in hash tables. - * - * @return never. - */ - override def hashCode(): Int = error("unsuitable as hash key"); - - /** Returns a string representation of this map which shows - * all the mappings. - */ - override def toString() = - if (size == 0) - "{}" - else - "{" + { - val iter = elements; - var res = mappingToString(iter.next); - while (iter.hasNext) { - res = res + ", " + mappingToString(iter.next); - } - res; - } + "}"; - - /** 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: A) { - def ->(value: B): Unit = update(key, value); - } + /** This method allows one to add a new mapping from <code>key</code> + * to <code>value</code> to the map. If the map already contains a + * mapping for <code>key</code>, it will be overridden by this + * function. + * + * @param key + * @param value + */ + def update(key: A, value: B): Unit; + + /** This method defines syntactic sugar for adding or modifying + * mappings. It is typically used in the following way: + * <pre> + * map += key -> value; + * </pre> + */ + def +=(key: A): MapTo = new MapTo(key); + + /** This method adds all the mappings provided by an iterator of + * parameter <code>map</code> to the map. + * + * @param map + */ + def ++=(map: Iterable[Pair[A, B]]): Unit = ++=(map.elements); + + /** This method adds all the mappings provided by an iterator of + * parameter <code>map</code> to the map. + * + * @param it + */ + def ++=(it: Iterator[Pair[A, B]]): Unit = it foreach { + case Pair(key, value) => update(key, value); + } + + /** <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. + * + * @param mappings + */ + def incl(mappings: Pair[A, B]*): Unit = ++=(mappings.elements); + + /** This method removes a mapping from the given <code>key</code>. + * If the map does not contain a mapping for the given key, the + * method does nothing. + */ + def -=(key: A): Unit; + + /** This method removes all the mappings for keys provided by an + * iterator over the elements of the <code>keys</code> object. + * + * @param keys + */ + def --=(keys: Iterable[A]): Unit = --=(keys.elements); + + /** This method removes all the mappings for keys provided by an + * iterator over the elements of the <code>keys</code> object. + * + * @param it + */ + def --=(it: Iterator[A]): Unit = it foreach -=; + + /** This method will remove all the mappings for the given sequence + * of keys from the map. + * + * @param keys + */ + def excl(keys: A*): Unit = --=(keys.elements); + + /** Removes all mappings from the map. After this operation is + * completed, the map is empty. + */ + def clear: Unit = keys foreach -=; + + /** This function transforms all the values of mappings contained + * in this map with function <code>f</code>. + * + * @param f + */ + def map(f: (A, B) => B): Unit = elements foreach { + case Pair(key, value) => update(key, f(key, value)); + } + + /** This method removes all the mappings for which the predicate + * <code>p</code> returns <code>false</code>. + * + * @param p + */ + def filter(p: (A, B) => Boolean): Unit = toList foreach { + case Pair(key, value) => if (!p(key, value)) -=(key); + } + + /** Send a message to this scriptable object. + * + * @param cmd the message to send. + */ + def <<(cmd: Message[Pair[A, B]]): Unit = cmd match { + case Include(Pair(k, v)) => update(k, v); + case Update(Pair(k, v)) => update(k, v); + case Remove(Pair(k, _)) => this -= k; + case Reset() => clear; + case s: Script[Pair[A, B]] => s.elements foreach <<; + case _ => error("message " + cmd + " not understood"); + } + + /** Return a clone of this map. + * + * @return an map with the same elements. + */ + override def clone(): Map[A, B] = super.clone().asInstanceOf[Map[A, B]]; + + /** The hashCode method always yields an error, since it is not + * safe to use mutable maps as keys in hash tables. + * + * @return never. + */ + override def hashCode(): Int = error("unsuitable as hash key"); + + /** Returns a string representation of this map which shows + * all the mappings. + */ + override def toString() = + if (size == 0) + "{}" + else + "{" + { + val iter = elements; + var res = mappingToString(iter.next); + while (iter.hasNext) { + res = res + ", " + mappingToString(iter.next); + } + res; + } + "}"; + + /** This method controls how a mapping is represented in the string + * representation provided by method <code>toString</code>. + * + * @param p + */ + def mappingToString(p: Pair[A, B]) = p._1.toString() + " -> " + p._2; + + class MapTo(key: A) { + def ->(value: B): Unit = update(key, value); + } + } |