summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/generic/ImmutableMapTemplate.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2009-05-12 09:58:25 +0000
committerMartin Odersky <odersky@gmail.com>2009-05-12 09:58:25 +0000
commitca3d31e7b25e4709839671f2ee5c5bd643cbc08e (patch)
treee44fe21fe1202a7c4ebc026a9bc2bd851b16bdec /src/library/scala/collection/generic/ImmutableMapTemplate.scala
parente4a8be83c10545e318fcb53bea39e86b26a71555 (diff)
downloadscala-ca3d31e7b25e4709839671f2ee5c5bd643cbc08e.tar.gz
scala-ca3d31e7b25e4709839671f2ee5c5bd643cbc08e.tar.bz2
scala-ca3d31e7b25e4709839671f2ee5c5bd643cbc08e.zip
separated mutable and immutable maps
Diffstat (limited to 'src/library/scala/collection/generic/ImmutableMapTemplate.scala')
-rw-r--r--src/library/scala/collection/generic/ImmutableMapTemplate.scala103
1 files changed, 99 insertions, 4 deletions
diff --git a/src/library/scala/collection/generic/ImmutableMapTemplate.scala b/src/library/scala/collection/generic/ImmutableMapTemplate.scala
index cfb602fc21..165cc365f7 100644
--- a/src/library/scala/collection/generic/ImmutableMapTemplate.scala
+++ b/src/library/scala/collection/generic/ImmutableMapTemplate.scala
@@ -17,8 +17,8 @@ package scala.collection.generic
*
* def get(key: A): Option[B]
* def elements: Iterator[(A, B)]
- * def add(key: A, value: B): This
- * def -(key: A): This
+ * def updated(key: A, value: B): This
+ * def minus(key: A): This
*
* If you wish that methods like, take, drop, filter return the same kind of map, you should also
* override:
@@ -33,6 +33,86 @@ self =>
override protected[this] def newBuilder: Builder[(A, B), This] = new ImmutableMapBuilder[A, B, This](empty)
+ /** A new immutable map containing updating this map with a given key/value mapping.
+ * @param key the key
+ * @param value the value
+ * @return A new map with the new key/value mapping
+ */
+ def updated [B1 >: B](key: A, value: B1): immutable.Map[A, B1]
+
+ /** Add a key/value pair to this map, returning a new map.
+ * @param kv the key/value pair
+ * @return A new map with the new binding added to this map
+ */
+ override def plus [B1 >: B] (kv: (A, B1)): immutable.Map[A, B1] = updated(kv._1, kv._2)
+
+ /** Add a key/value pair to this map, returning a new map.
+ * @param kv the key/value pair
+ * @return A new map with the new binding added to this map
+ * @note same as `plus`
+ */
+ override def + [B1 >: B] (kv: (A, B1)): immutable.Map[A, B1] = updated(kv._1, kv._2)
+
+ /** Adds two or more elements to this collection and returns
+ * a new collection.
+ *
+ * @param elem1 the first element to add.
+ * @param elem2 the second element to add.
+ * @param elems the remaining elements to add.
+ */
+ override def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): immutable.Map[A, B1] =
+ this plus elem1 plus elem2 plusAll elems
+
+ /** Adds two or more elements to this collection and returns
+ * a new collection.
+ *
+ * @param elem1 the first element to add.
+ * @param elem2 the second element to add.
+ * @param elems the remaining elements to add.
+ */
+ override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): immutable.Map[A, B1] =
+ plus(elem1, elem2, elems: _*)
+
+ /** Adds a number of elements provided by a traversable object
+ * and returns a new collection with the added elements.
+ *
+ * @param elems the traversable object.
+ */
+ override def plusAll[B1 >: B](elems: Traversable[(A, B1)]): immutable.Map[A, B1] =
+ ((thisCollection: immutable.Map[A, B1]) /: elems) (_ plus _)
+
+ /** Adds a number of elements provided by a traversable object
+ * and returns a new collection with the added elements.
+ *
+ * @param elems the traversable object.
+ * @note same as `plusAll`
+ * @note This is a more efficient version of Traversable.++ which avoids
+ * copying of the collection's elements. However, it applies only if
+ * the type of the added elements is a subtype of the element type of the
+ * collection.
+ */
+ override def ++ [B1 >: B](elems: Traversable[(A, B1)]): immutable.Map[A, B1] = plusAll(elems)
+
+ /** Adds a number of elements provided by an iterator
+ * and returns a new collection with the added elements.
+ *
+ * @param iter the iterator
+ */
+ override def plusAll[B1 >: B] (iter: Iterator[(A, B1)]): immutable.Map[A, B1] =
+ ((thisCollection: immutable.Map[A, B1]) /: iter) (_ plus _)
+
+ /** Adds a number of elements provided by an iterator
+ * and returns a new collection with the added elements.
+ *
+ * @param iter the iterator
+ * @note same as `plusAll`
+ * @note This is a more efficient version of Traversable.++ which avoids
+ * copying of the collection's elements. However, it applies only if
+ * the type of the added elements is a subtype of the element type of the
+ * collection.
+ */
+ override def ++ [B1 >: B](iter: Iterator[(A, B1)]): immutable.Map[A, B1] = plusAll(iter)
+
/** This function transforms all the values of mappings contained
* in this map with function <code>f</code>.
*
@@ -45,6 +125,21 @@ self =>
b.result
}
- /** @deprecated use add instead */
- @deprecated def update[B1 >: B](key: A, value: B1): immutable.Map[A, B1] = add(key, value).asInstanceOf[immutable.Map[A, B1]]
+ /** Returns a new map with all key/value pairs for which the predicate
+ * <code>p</code> returns <code>true</code>.
+ *
+ * @param p A predicate over key-value pairs
+ * @note This method works by successively removing elements fro which the predicate is false from this set.
+ * If removal is slow, or you expect that most elements of the set will be removed,
+ * you might consider using `filter` with a negated predicate instead.
+ */
+ override def filterNot(p: ((A, B)) => Boolean): This = {
+ var res: This = thisCollection
+ for (kv <- this)
+ if (p(kv)) res = (res minus kv._1).asInstanceOf[This] // !!! concrete overrides abstract problem
+ res
+ }
+
+ /** @deprecated use updated instead */
+ @deprecated def update[B1 >: B](key: A, value: B1): immutable.Map[A, B1] = updated(key, value).asInstanceOf[immutable.Map[A, B1]]
}