2
2
** ________ ___ / / ___ Scala API **
3
** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
3
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
4
4
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5
5
** /____/\___/_/ |_/____/_/ | | **
13
13
import mutable.{ Builder, MapBuilder }
14
import annotation.{migration, bridge}
14
import scala.annotation.{migration, bridge}
15
15
import parallel.ParMap
17
17
/** A template trait for maps, which associate keys with values.
91
91
* @param kv the key/value pair
92
92
* @tparam B1 the type of the value in the key/value pair.
93
93
* @return a new map with the new binding added to this map
94
95
* @usecase def + (kv: (A, B)): Map[A, B]
96
98
def + [B1 >: B] (kv: (A, B1)): Map[A, B1]
98
100
/** Removes a key from this map, returning a new map.
99
101
* @param key the key to be removed
100
102
* @return a new map without a binding for `key`
101
104
* @usecase def - (key: A): Map[A, B]
103
107
def - (key: A): This
115
119
* @tparam B1 the result type of the default computation.
116
120
* @return the value associated with `key` if it exists,
117
121
* otherwise the result of the `default` computation.
118
123
* @usecase def getOrElse(key: A, default: => B): B
120
126
def getOrElse[B1 >: B](key: A, default: => B1): B1 = get(key) match {
121
127
case Some(v) => v
141
147
* @param key the key
142
148
* @return `true` if there is a binding for `key` in this map, `false` otherwise.
144
def contains(key: A): Boolean = get(key) match {
150
def contains(key: A): Boolean = get(key).isDefined
149
152
/** Tests whether this map contains a binding for a key. This method,
150
153
* which implements an abstract method of trait `PartialFunction`,
163
166
/** The implementation class of the set returned by `keySet`.
165
protected class DefaultKeySet extends Set[A] {
168
protected class DefaultKeySet extends AbstractSet[A] with Set[A] with Serializable {
166
169
def contains(key : A) = self.contains(key)
167
170
def iterator = keysIterator
168
171
def + (elem: A): Set[A] = (Set[A]() ++ this + elem).asInstanceOf[Set[A]] // !!! concrete overrides abstract problem
169
172
def - (elem: A): Set[A] = (Set[A]() ++ this - elem).asInstanceOf[Set[A]] // !!! concrete overrides abstract problem
170
173
override def size = self.size
171
override def foreach[C](f: A => C) = for ((k, v) <- self) f(k)
174
override def foreach[C](f: A => C) = self.keysIterator foreach f
174
177
/** Creates an iterator for all keys.
176
179
* @return an iterator over all keys.
178
def keysIterator: Iterator[A] = new Iterator[A] {
181
def keysIterator: Iterator[A] = new AbstractIterator[A] {
179
182
val iter = self.iterator
180
183
def hasNext = iter.hasNext
181
184
def next() = iter.next._1
198
201
/** The implementation class of the iterable returned by `values`.
200
protected class DefaultValuesIterable extends Iterable[B] {
203
protected class DefaultValuesIterable extends AbstractIterable[B] with Iterable[B] with Serializable {
201
204
def iterator = valuesIterator
202
205
override def size = self.size
203
override def foreach[C](f: B => C) = for ((k, v) <- self) f(v)
206
override def foreach[C](f: B => C) = self.valuesIterator foreach f
206
209
/** Creates an iterator for all values in this map.
208
211
* @return an iterator over all values that are associated with some key in this map.
210
def valuesIterator: Iterator[B] = new Iterator[B] {
213
def valuesIterator: Iterator[B] = new AbstractIterator[B] {
211
214
val iter = self.iterator
212
215
def hasNext = iter.hasNext
213
216
def next() = iter.next._2
224
227
def default(key: A): B =
225
228
throw new NoSuchElementException("key not found: " + key)
230
protected class FilteredKeys(p: A => Boolean) extends AbstractMap[A, B] with DefaultMap[A, B] {
231
override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv)
232
def iterator = self.iterator.filter(kv => p(kv._1))
233
override def contains(key: A) = self.contains(key) && p(key)
234
def get(key: A) = if (!p(key)) None else self.get(key)
227
237
/** Filters this map by retaining only keys satisfying a predicate.
228
238
* @param p the predicate used to test keys
229
239
* @return an immutable map consisting only of those key value pairs of this map where the key satisfies
230
240
* the predicate `p`. The resulting map wraps the original map without copying any elements.
232
def filterKeys(p: A => Boolean): Map[A, B] = new DefaultMap[A, B] {
233
override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv)
234
def iterator = self.iterator.filter(kv => p(kv._1))
235
override def contains(key: A) = self.contains(key) && p(key)
236
def get(key: A) = if (!p(key)) None else self.get(key)
242
def filterKeys(p: A => Boolean): Map[A, B] = new FilteredKeys(p)
244
protected class MappedValues[C](f: B => C) extends AbstractMap[A, C] with DefaultMap[A, C] {
245
override def foreach[D](g: ((A, C)) => D): Unit = for ((k, v) <- self) g((k, f(v)))
246
def iterator = for ((k, v) <- self.iterator) yield (k, f(v))
247
override def size = self.size
248
override def contains(key: A) = self.contains(key)
249
def get(key: A) = self.get(key).map(f)
239
252
/** Transforms this map by applying a function to every retrieved value.
241
254
* @return a map view which maps every key of this map
242
255
* to `f(this(key))`. The resulting map wraps the original map without copying any elements.
244
def mapValues[C](f: B => C): Map[A, C] = new DefaultMap[A, C] {
245
override def foreach[D](g: ((A, C)) => D): Unit = for ((k, v) <- self) g((k, f(v)))
246
def iterator = for ((k, v) <- self.iterator) yield (k, f(v))
247
override def size = self.size
248
override def contains(key: A) = self.contains(key)
249
def get(key: A) = self.get(key).map(f)
252
@deprecated("use `mapValues' instead", "2.8.0")
253
def mapElements[C](f: B => C) = mapValues(f)
257
def mapValues[C](f: B => C): Map[A, C] = new MappedValues(f)
255
259
// The following 5 operations (updated, two times +, two times ++) should really be
256
260
// generic, returning This[B]. We need better covariance support to express that though.
261
265
* @param value the value
262
266
* @tparam B1 the type of the added value
263
267
* @return A new map with the new key/value mapping added to this map.
264
269
* @usecase def updated(key: A, value: B): Map[A, B]
266
272
def updated [B1 >: B](key: A, value: B1): Map[A, B1] = this + ((key, value))
275
281
* @param kvs the remaining key/value pairs
276
282
* @tparam B1 the type of the added values
277
283
* @return a new map with the given bindings added to this map
278
285
* @usecase def + (kvs: (A, B)*): Map[A, B]
279
* @param the key/value pairs
287
* @param kvs the key/value pairs
281
289
def + [B1 >: B] (kv1: (A, B1), kv2: (A, B1), kvs: (A, B1) *): Map[A, B1] =
282
290
this + kv1 + kv2 ++ kvs
284
292
/** Adds all key/value pairs in a traversable collection to this map, returning a new map.
286
* @param kvs the collection containing the added key/value pairs
294
* @param xs the collection containing the added key/value pairs
287
295
* @tparam B1 the type of the added values
288
296
* @return a new map with the given bindings added to this map
289
298
* @usecase def ++ (xs: Traversable[(A, B)]): Map[A, B]
291
301
def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): Map[A, B1] =
292
302
((repr: Map[A, B1]) /: xs.seq) (_ + _)
295
def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): Map[A, B1] = ++(xs: GenTraversableOnce[(A, B1)])
297
/** Returns a new map with all key/value pairs for which the predicate
304
/** Returns a new map obtained by removing all key/value pairs for which the predicate
298
305
* `p` returns `true`.
300
* '''Note:''' This method works by successively removing elements fro which the
301
* predicate is false from this set.
307
* '''Note:''' This method works by successively removing elements for which the
308
* predicate is true from this set.
302
309
* If removal is slow, or you expect that most elements of the set
303
310
* will be removed, you might consider using `filter`
304
311
* with a negated predicate instead.
315
/** Overridden for efficiency. */
322
/* Overridden for efficiency. */
316
323
override def toSeq: Seq[(A, B)] = toBuffer[(A, B)]
317
324
override def toBuffer[C >: (A, B)]: mutable.Buffer[C] = {
318
325
val result = new mutable.ArrayBuffer[C](size)