~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to src/library/scala/collection/MapLike.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*                     __                                               *\
2
2
**     ________ ___   / /  ___     Scala API                            **
3
 
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
 
3
**    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             **
4
4
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
5
5
** /____/\___/_/ |_/____/_/ | |                                         **
6
6
**                          |/                                          **
11
11
 
12
12
import generic._
13
13
import mutable.{ Builder, MapBuilder }
14
 
import annotation.{migration, bridge}
 
14
import scala.annotation.{migration, bridge}
15
15
import parallel.ParMap
16
16
 
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
   *
94
95
   *  @usecase  def + (kv: (A, B)): Map[A, B]
 
96
   *    @inheritdoc
95
97
   */
96
98
  def + [B1 >: B] (kv: (A, B1)): Map[A, B1]
97
99
 
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`
 
103
   *
101
104
   *  @usecase  def - (key: A): Map[A, B]
 
105
   *    @inheritdoc
102
106
   */
103
107
  def - (key: A): This
104
108
 
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.
 
122
   *
118
123
   *   @usecase def getOrElse(key: A, default: => B): B
 
124
   *     @inheritdoc
119
125
   */
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.
143
149
   */
144
 
  def contains(key: A): Boolean = get(key) match {
145
 
    case None => false
146
 
    case Some(_) => true
147
 
  }
 
150
  def contains(key: A): Boolean = get(key).isDefined
148
151
 
149
152
  /** Tests whether this map contains a binding for a key. This method,
150
153
   *  which implements an abstract method of trait `PartialFunction`,
162
165
 
163
166
  /** The implementation class of the set returned by `keySet`.
164
167
   */
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
172
175
  }
173
176
 
174
177
  /** Creates an iterator for all keys.
175
178
   *
176
179
   *  @return an iterator over all keys.
177
180
   */
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
197
200
 
198
201
  /** The implementation class of the iterable returned by `values`.
199
202
   */
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
204
207
  }
205
208
 
206
209
  /** Creates an iterator for all values in this map.
207
210
   *
208
211
   *  @return an iterator over all values that are associated with some key in this map.
209
212
   */
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)
226
229
 
 
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)
 
235
  }
 
236
 
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.
231
241
   */
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)
 
243
 
 
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)
237
250
  }
238
251
 
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.
243
256
   */
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)
250
 
  }
251
 
 
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)
254
258
 
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.
 
268
   *
264
269
   *  @usecase  def updated(key: A, value: B): Map[A, B]
 
270
   *    @inheritdoc
265
271
   */
266
272
  def updated [B1 >: B](key: A, value: B1): Map[A, B1] = this + ((key, value))
267
273
 
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
 
284
   *
278
285
   *  @usecase  def + (kvs: (A, B)*): Map[A, B]
279
 
   *  @param    the key/value pairs
 
286
   *    @inheritdoc
 
287
   *    @param    kvs the key/value pairs
280
288
   */
281
289
  def + [B1 >: B] (kv1: (A, B1), kv2: (A, B1), kvs: (A, B1) *): Map[A, B1] =
282
290
    this + kv1 + kv2 ++ kvs
283
291
 
284
292
  /** Adds all key/value pairs in a traversable collection to this map, returning a new map.
285
293
   *
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
 
297
   *
289
298
   *  @usecase  def ++ (xs: Traversable[(A, B)]): Map[A, B]
 
299
   *    @inheritdoc
290
300
   */
291
301
  def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): Map[A, B1] =
292
302
    ((repr: Map[A, B1]) /: xs.seq) (_ + _)
293
303
 
294
 
  @bridge
295
 
  def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): Map[A, B1] = ++(xs: GenTraversableOnce[(A, B1)])
296
 
 
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`.
299
306
   *
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.
312
319
    res
313
320
  }
314
321
 
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)