diff options
author | Josh Suereth <Joshua.Suereth@gmail.com> | 2012-06-28 12:58:04 -0700 |
---|---|---|
committer | Josh Suereth <Joshua.Suereth@gmail.com> | 2012-06-28 12:58:04 -0700 |
commit | bad1e8e6d7f513f37353c9f7f23dbde5f59cb038 (patch) | |
tree | 662ed324ab11f09581cb5c509403dd59b615d6a0 /src | |
parent | 0c1acec0a02b9128f3d138ecef84d903296d6b2a (diff) | |
parent | b379ff4f59c139ff7d2b77e7e808f0b74aa9f268 (diff) | |
download | scala-bad1e8e6d7f513f37353c9f7f23dbde5f59cb038.tar.gz scala-bad1e8e6d7f513f37353c9f7f23dbde5f59cb038.tar.bz2 scala-bad1e8e6d7f513f37353c9f7f23dbde5f59cb038.zip |
Merge pull request #796 from axel22/issue/5846,4597,4027,4112
Issue/5846,4597,4027,4112
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/DefaultMap.scala | 4 | ||||
-rw-r--r-- | src/library/scala/collection/Map.scala | 1 | ||||
-rw-r--r-- | src/library/scala/collection/MapLike.scala | 4 | ||||
-rw-r--r-- | src/library/scala/collection/SortedMap.scala | 19 | ||||
-rw-r--r-- | src/library/scala/collection/SortedMapLike.scala | 17 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/MapLike.scala | 22 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/SortedMap.scala | 31 |
7 files changed, 75 insertions, 23 deletions
diff --git a/src/library/scala/collection/DefaultMap.scala b/src/library/scala/collection/DefaultMap.scala index d00414751a..d961bd84bb 100644 --- a/src/library/scala/collection/DefaultMap.scala +++ b/src/library/scala/collection/DefaultMap.scala @@ -27,7 +27,7 @@ import generic._ * @since 2.8 */ trait DefaultMap[A, +B] extends Map[A, B] { self => - + /** A default implementation which creates a new immutable map. */ override def +[B1 >: B](kv: (A, B1)): Map[A, B1] = { @@ -41,7 +41,7 @@ trait DefaultMap[A, +B] extends Map[A, B] { self => */ override def - (key: A): Map[A, B] = { val b = newBuilder - b ++= this filter (key != _) + b ++= this filter (key != _._1) b.result } } diff --git a/src/library/scala/collection/Map.scala b/src/library/scala/collection/Map.scala index 42a56a9c5a..19c24c9791 100644 --- a/src/library/scala/collection/Map.scala +++ b/src/library/scala/collection/Map.scala @@ -51,6 +51,7 @@ object Map extends MapFactory[Map] { def iterator = underlying.iterator override def default(key: A): B = d(key) } + } /** Explicit instantiation of the `Map` trait to reduce class file size in subclasses. */ diff --git a/src/library/scala/collection/MapLike.scala b/src/library/scala/collection/MapLike.scala index 75f9ff93db..55d482f6c8 100644 --- a/src/library/scala/collection/MapLike.scala +++ b/src/library/scala/collection/MapLike.scala @@ -226,7 +226,7 @@ self => */ def default(key: A): B = throw new NoSuchElementException("key not found: " + key) - + protected class FilteredKeys(p: A => Boolean) extends AbstractMap[A, B] with DefaultMap[A, B] { override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv) def iterator = self.iterator.filter(kv => p(kv._1)) @@ -240,7 +240,7 @@ self => * the predicate `p`. The resulting map wraps the original map without copying any elements. */ def filterKeys(p: A => Boolean): Map[A, B] = new FilteredKeys(p) - + protected class MappedValues[C](f: B => C) extends AbstractMap[A, C] with DefaultMap[A, C] { override def foreach[D](g: ((A, C)) => D): Unit = for ((k, v) <- self) g((k, f(v))) def iterator = for ((k, v) <- self.iterator) yield (k, f(v)) diff --git a/src/library/scala/collection/SortedMap.scala b/src/library/scala/collection/SortedMap.scala index 3f92908848..e32e0977df 100644 --- a/src/library/scala/collection/SortedMap.scala +++ b/src/library/scala/collection/SortedMap.scala @@ -30,9 +30,26 @@ trait SortedMap[A, +B] extends Map[A, B] with SortedMapLike[A, B, SortedMap[A, B * @since 2.8 */ object SortedMap extends SortedMapFactory[SortedMap] { - def empty[A, B](implicit ord: Ordering[A]): immutable.SortedMap[A, B] = immutable.SortedMap.empty[A, B](ord) + def empty[A, B](implicit ord: Ordering[A]): SortedMap[A, B] = immutable.SortedMap.empty[A, B](ord) implicit def canBuildFrom[A, B](implicit ord: Ordering[A]): CanBuildFrom[Coll, (A, B), SortedMap[A, B]] = new SortedMapCanBuildFrom[A, B] + + private[collection] trait Default[A, +B] extends SortedMap[A, B] { + self => + override def +[B1 >: B](kv: (A, B1)): SortedMap[A, B1] = { + val b = SortedMap.newBuilder[A, B1] + b ++= this + b += ((kv._1, kv._2)) + b.result + } + + override def - (key: A): SortedMap[A, B] = { + val b = newBuilder + for (kv <- this; if kv._1 != key) b += kv + b.result + } + } + } diff --git a/src/library/scala/collection/SortedMapLike.scala b/src/library/scala/collection/SortedMapLike.scala index 6b25151192..f9e95230ea 100644 --- a/src/library/scala/collection/SortedMapLike.scala +++ b/src/library/scala/collection/SortedMapLike.scala @@ -72,7 +72,17 @@ self => for (e <- elems) m = m + e m } - + + override def filterKeys(p: A => Boolean): SortedMap[A, B] = new FilteredKeys(p) with SortedMap.Default[A, B] { + implicit def ordering: Ordering[A] = self.ordering + override def rangeImpl(from : Option[A], until : Option[A]): SortedMap[A, B] = self.rangeImpl(from, until).filterKeys(p) + } + + override def mapValues[C](f: B => C): SortedMap[A, C] = new MappedValues(f) with SortedMap.Default[A, C] { + implicit def ordering: Ordering[A] = self.ordering + override def rangeImpl(from : Option[A], until : Option[A]): SortedMap[A, C] = self.rangeImpl(from, until).mapValues(f) + } + /** Adds a number of elements provided by a traversable object * and returns a new collection with the added elements. * @@ -80,4 +90,9 @@ self => */ override def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): SortedMap[A, B1] = ((repr: SortedMap[A, B1]) /: xs.seq) (_ + _) + } + + + + diff --git a/src/library/scala/collection/immutable/MapLike.scala b/src/library/scala/collection/immutable/MapLike.scala index 6ae2d78188..2dc08fff24 100644 --- a/src/library/scala/collection/immutable/MapLike.scala +++ b/src/library/scala/collection/immutable/MapLike.scala @@ -48,7 +48,8 @@ import parallel.immutable.ParMap trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]] extends scala.collection.MapLike[A, B, This] with Parallelizable[(A, B), ParMap[A, B]] -{ self => +{ +self => protected[this] override def parCombiner = ParMap.newCombiner[A, B] @@ -84,31 +85,20 @@ trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]] */ override def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): immutable.Map[A, B1] = ((repr: immutable.Map[A, B1]) /: xs.seq) (_ + _) - + /** Filters this map by retaining only keys satisfying a predicate. * @param p the predicate used to test keys * @return an immutable map consisting only of those key value pairs of this map where the key satisfies * the predicate `p`. The resulting map wraps the original map without copying any elements. */ - override def filterKeys(p: A => Boolean): Map[A, B] = new AbstractMap[A, B] with DefaultMap[A, B] { - override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv) - def iterator = self.iterator.filter(kv => p(kv._1)) - override def contains(key: A) = self.contains(key) && p(key) - def get(key: A) = if (!p(key)) None else self.get(key) - } - + override def filterKeys(p: A => Boolean): Map[A, B] = new FilteredKeys(p) with DefaultMap[A, B] + /** Transforms this map by applying a function to every retrieved value. * @param f the function used to transform values of this map. * @return a map view which maps every key of this map * to `f(this(key))`. The resulting map wraps the original map without copying any elements. */ - override def mapValues[C](f: B => C): Map[A, C] = new AbstractMap[A, C] with DefaultMap[A, C] { - override def foreach[D](g: ((A, C)) => D): Unit = for ((k, v) <- self) g((k, f(v))) - def iterator = for ((k, v) <- self.iterator) yield (k, f(v)) - override def size = self.size - override def contains(key: A) = self.contains(key) - def get(key: A) = self.get(key).map(f) - } + override def mapValues[C](f: B => C): Map[A, C] = new MappedValues(f) with DefaultMap[A, C] /** Collects all keys of this map in a set. * @return a set containing all keys of this map. diff --git a/src/library/scala/collection/immutable/SortedMap.scala b/src/library/scala/collection/immutable/SortedMap.scala index 526f7a1ffe..f147b673f7 100644 --- a/src/library/scala/collection/immutable/SortedMap.scala +++ b/src/library/scala/collection/immutable/SortedMap.scala @@ -30,7 +30,9 @@ import annotation.unchecked.uncheckedVariance trait SortedMap[A, +B] extends Map[A, B] with scala.collection.SortedMap[A, B] with MapLike[A, B, SortedMap[A, B]] - with SortedMapLike[A, B, SortedMap[A, B]] { self => + with SortedMapLike[A, B, SortedMap[A, B]] +{ +self => override protected[this] def newBuilder : Builder[(A, B), SortedMap[A, B]] = SortedMap.newBuilder[A, B] @@ -76,6 +78,17 @@ trait SortedMap[A, +B] extends Map[A, B] */ override def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): SortedMap[A, B1] = ((repr: SortedMap[A, B1]) /: xs.seq) (_ + _) + + override def filterKeys(p: A => Boolean): SortedMap[A, B] = new FilteredKeys(p) with SortedMap.Default[A, B] { + implicit def ordering: Ordering[A] = self.ordering + override def rangeImpl(from : Option[A], until : Option[A]): SortedMap[A, B] = self.rangeImpl(from, until).filterKeys(p) + } + + override def mapValues[C](f: B => C): SortedMap[A, C] = new MappedValues(f) with SortedMap.Default[A, C] { + implicit def ordering: Ordering[A] = self.ordering + override def rangeImpl(from : Option[A], until : Option[A]): SortedMap[A, C] = self.rangeImpl(from, until).mapValues(f) + } + } /** $factoryInfo @@ -86,4 +99,20 @@ object SortedMap extends ImmutableSortedMapFactory[SortedMap] { /** $sortedMapCanBuildFromInfo */ implicit def canBuildFrom[A, B](implicit ord: Ordering[A]): CanBuildFrom[Coll, (A, B), SortedMap[A, B]] = new SortedMapCanBuildFrom[A, B] def empty[A, B](implicit ord: Ordering[A]): SortedMap[A, B] = TreeMap.empty[A, B] + + private[collection] trait Default[A, +B] extends SortedMap[A, B] with collection.SortedMap.Default[A, B] { + self => + override def +[B1 >: B](kv: (A, B1)): SortedMap[A, B1] = { + val b = SortedMap.newBuilder[A, B1] + b ++= this + b += ((kv._1, kv._2)) + b.result + } + + override def - (key: A): SortedMap[A, B] = { + val b = newBuilder + for (kv <- this; if kv._1 != key) b += kv + b.result + } + } } |