Skip to content

Commit bad1e8e

Browse files
committed
Merge pull request scala#796 from axel22/issue/5846,4597,4027,4112
Issue/5846,4597,4027,4112
2 parents 0c1acec + b379ff4 commit bad1e8e

File tree

10 files changed

+124
-23
lines changed

10 files changed

+124
-23
lines changed

src/library/scala/collection/DefaultMap.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import generic._
2727
* @since 2.8
2828
*/
2929
trait DefaultMap[A, +B] extends Map[A, B] { self =>
30-
30+
3131
/** A default implementation which creates a new immutable map.
3232
*/
3333
override def +[B1 >: B](kv: (A, B1)): Map[A, B1] = {
@@ -41,7 +41,7 @@ trait DefaultMap[A, +B] extends Map[A, B] { self =>
4141
*/
4242
override def - (key: A): Map[A, B] = {
4343
val b = newBuilder
44-
b ++= this filter (key != _)
44+
b ++= this filter (key != _._1)
4545
b.result
4646
}
4747
}

src/library/scala/collection/Map.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ object Map extends MapFactory[Map] {
5151
def iterator = underlying.iterator
5252
override def default(key: A): B = d(key)
5353
}
54+
5455
}
5556

5657
/** Explicit instantiation of the `Map` trait to reduce class file size in subclasses. */

src/library/scala/collection/MapLike.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ self =>
226226
*/
227227
def default(key: A): B =
228228
throw new NoSuchElementException("key not found: " + key)
229-
229+
230230
protected class FilteredKeys(p: A => Boolean) extends AbstractMap[A, B] with DefaultMap[A, B] {
231231
override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv)
232232
def iterator = self.iterator.filter(kv => p(kv._1))
@@ -240,7 +240,7 @@ self =>
240240
* the predicate `p`. The resulting map wraps the original map without copying any elements.
241241
*/
242242
def filterKeys(p: A => Boolean): Map[A, B] = new FilteredKeys(p)
243-
243+
244244
protected class MappedValues[C](f: B => C) extends AbstractMap[A, C] with DefaultMap[A, C] {
245245
override def foreach[D](g: ((A, C)) => D): Unit = for ((k, v) <- self) g((k, f(v)))
246246
def iterator = for ((k, v) <- self.iterator) yield (k, f(v))

src/library/scala/collection/SortedMap.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,26 @@ trait SortedMap[A, +B] extends Map[A, B] with SortedMapLike[A, B, SortedMap[A, B
3030
* @since 2.8
3131
*/
3232
object SortedMap extends SortedMapFactory[SortedMap] {
33-
def empty[A, B](implicit ord: Ordering[A]): immutable.SortedMap[A, B] = immutable.SortedMap.empty[A, B](ord)
33+
def empty[A, B](implicit ord: Ordering[A]): SortedMap[A, B] = immutable.SortedMap.empty[A, B](ord)
3434

3535
implicit def canBuildFrom[A, B](implicit ord: Ordering[A]): CanBuildFrom[Coll, (A, B), SortedMap[A, B]] = new SortedMapCanBuildFrom[A, B]
36+
37+
private[collection] trait Default[A, +B] extends SortedMap[A, B] {
38+
self =>
39+
override def +[B1 >: B](kv: (A, B1)): SortedMap[A, B1] = {
40+
val b = SortedMap.newBuilder[A, B1]
41+
b ++= this
42+
b += ((kv._1, kv._2))
43+
b.result
44+
}
45+
46+
override def - (key: A): SortedMap[A, B] = {
47+
val b = newBuilder
48+
for (kv <- this; if kv._1 != key) b += kv
49+
b.result
50+
}
51+
}
52+
3653
}
3754

3855

src/library/scala/collection/SortedMapLike.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,27 @@ self =>
7272
for (e <- elems) m = m + e
7373
m
7474
}
75-
75+
76+
override def filterKeys(p: A => Boolean): SortedMap[A, B] = new FilteredKeys(p) with SortedMap.Default[A, B] {
77+
implicit def ordering: Ordering[A] = self.ordering
78+
override def rangeImpl(from : Option[A], until : Option[A]): SortedMap[A, B] = self.rangeImpl(from, until).filterKeys(p)
79+
}
80+
81+
override def mapValues[C](f: B => C): SortedMap[A, C] = new MappedValues(f) with SortedMap.Default[A, C] {
82+
implicit def ordering: Ordering[A] = self.ordering
83+
override def rangeImpl(from : Option[A], until : Option[A]): SortedMap[A, C] = self.rangeImpl(from, until).mapValues(f)
84+
}
85+
7686
/** Adds a number of elements provided by a traversable object
7787
* and returns a new collection with the added elements.
7888
*
7989
* @param xs the traversable object.
8090
*/
8191
override def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): SortedMap[A, B1] =
8292
((repr: SortedMap[A, B1]) /: xs.seq) (_ + _)
93+
8394
}
95+
96+
97+
98+

src/library/scala/collection/immutable/MapLike.scala

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ import parallel.immutable.ParMap
4848
trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]]
4949
extends scala.collection.MapLike[A, B, This]
5050
with Parallelizable[(A, B), ParMap[A, B]]
51-
{ self =>
51+
{
52+
self =>
5253

5354
protected[this] override def parCombiner = ParMap.newCombiner[A, B]
5455

@@ -84,31 +85,20 @@ trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]]
8485
*/
8586
override def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): immutable.Map[A, B1] =
8687
((repr: immutable.Map[A, B1]) /: xs.seq) (_ + _)
87-
88+
8889
/** Filters this map by retaining only keys satisfying a predicate.
8990
* @param p the predicate used to test keys
9091
* @return an immutable map consisting only of those key value pairs of this map where the key satisfies
9192
* the predicate `p`. The resulting map wraps the original map without copying any elements.
9293
*/
93-
override def filterKeys(p: A => Boolean): Map[A, B] = new AbstractMap[A, B] with DefaultMap[A, B] {
94-
override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv)
95-
def iterator = self.iterator.filter(kv => p(kv._1))
96-
override def contains(key: A) = self.contains(key) && p(key)
97-
def get(key: A) = if (!p(key)) None else self.get(key)
98-
}
99-
94+
override def filterKeys(p: A => Boolean): Map[A, B] = new FilteredKeys(p) with DefaultMap[A, B]
95+
10096
/** Transforms this map by applying a function to every retrieved value.
10197
* @param f the function used to transform values of this map.
10298
* @return a map view which maps every key of this map
10399
* to `f(this(key))`. The resulting map wraps the original map without copying any elements.
104100
*/
105-
override def mapValues[C](f: B => C): Map[A, C] = new AbstractMap[A, C] with DefaultMap[A, C] {
106-
override def foreach[D](g: ((A, C)) => D): Unit = for ((k, v) <- self) g((k, f(v)))
107-
def iterator = for ((k, v) <- self.iterator) yield (k, f(v))
108-
override def size = self.size
109-
override def contains(key: A) = self.contains(key)
110-
def get(key: A) = self.get(key).map(f)
111-
}
101+
override def mapValues[C](f: B => C): Map[A, C] = new MappedValues(f) with DefaultMap[A, C]
112102

113103
/** Collects all keys of this map in a set.
114104
* @return a set containing all keys of this map.

src/library/scala/collection/immutable/SortedMap.scala

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import annotation.unchecked.uncheckedVariance
3030
trait SortedMap[A, +B] extends Map[A, B]
3131
with scala.collection.SortedMap[A, B]
3232
with MapLike[A, B, SortedMap[A, B]]
33-
with SortedMapLike[A, B, SortedMap[A, B]] { self =>
33+
with SortedMapLike[A, B, SortedMap[A, B]]
34+
{
35+
self =>
3436

3537
override protected[this] def newBuilder : Builder[(A, B), SortedMap[A, B]] =
3638
SortedMap.newBuilder[A, B]
@@ -76,6 +78,17 @@ trait SortedMap[A, +B] extends Map[A, B]
7678
*/
7779
override def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): SortedMap[A, B1] =
7880
((repr: SortedMap[A, B1]) /: xs.seq) (_ + _)
81+
82+
override def filterKeys(p: A => Boolean): SortedMap[A, B] = new FilteredKeys(p) with SortedMap.Default[A, B] {
83+
implicit def ordering: Ordering[A] = self.ordering
84+
override def rangeImpl(from : Option[A], until : Option[A]): SortedMap[A, B] = self.rangeImpl(from, until).filterKeys(p)
85+
}
86+
87+
override def mapValues[C](f: B => C): SortedMap[A, C] = new MappedValues(f) with SortedMap.Default[A, C] {
88+
implicit def ordering: Ordering[A] = self.ordering
89+
override def rangeImpl(from : Option[A], until : Option[A]): SortedMap[A, C] = self.rangeImpl(from, until).mapValues(f)
90+
}
91+
7992
}
8093

8194
/** $factoryInfo
@@ -86,4 +99,20 @@ object SortedMap extends ImmutableSortedMapFactory[SortedMap] {
8699
/** $sortedMapCanBuildFromInfo */
87100
implicit def canBuildFrom[A, B](implicit ord: Ordering[A]): CanBuildFrom[Coll, (A, B), SortedMap[A, B]] = new SortedMapCanBuildFrom[A, B]
88101
def empty[A, B](implicit ord: Ordering[A]): SortedMap[A, B] = TreeMap.empty[A, B]
102+
103+
private[collection] trait Default[A, +B] extends SortedMap[A, B] with collection.SortedMap.Default[A, B] {
104+
self =>
105+
override def +[B1 >: B](kv: (A, B1)): SortedMap[A, B1] = {
106+
val b = SortedMap.newBuilder[A, B1]
107+
b ++= this
108+
b += ((kv._1, kv._2))
109+
b.result
110+
}
111+
112+
override def - (key: A): SortedMap[A, B] = {
113+
val b = newBuilder
114+
for (kv <- this; if kv._1 != key) b += kv
115+
b.result
116+
}
117+
}
89118
}

test/files/pos/t5846.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
4+
5+
/** Return the most general sorted map type. */
6+
object Test extends App {
7+
8+
val empty: collection.SortedMap[String, String] = collection.SortedMap.empty[String, String]
9+
10+
}

test/files/run/t4027.check

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Map(2 -> true, 4 -> true)
2+
Map(1 -> false!, 2 -> true!, 3 -> false!, 4 -> true!)
3+
Map(2 -> 4, 4 -> 4)
4+
Map(1 -> 6, 2 -> 5, 3 -> 6, 4 -> 5)
5+
Map()
6+
Map(1 -> false!)
7+
Map(2 -> true, 4 -> true)
8+
Map(1 -> false!, 2 -> true!, 3 -> false!, 4 -> true!)
9+
Map(2 -> 4, 4 -> 4)
10+
Map(1 -> 6, 2 -> 5, 3 -> 6, 4 -> 5)
11+
Map()
12+
Map(1 -> false!)

test/files/run/t4027.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
import collection._
4+
5+
6+
/** Sorted maps should have `filterKeys` and `mapValues` which return sorted maps.
7+
* Mapping, filtering, etc. on these views should return sorted maps again.
8+
*/
9+
object Test extends App {
10+
11+
val sortedmap = SortedMap(1 -> false, 2 -> true, 3 -> false, 4 -> true)
12+
println(sortedmap.filterKeys(_ % 2 == 0): SortedMap[Int, Boolean])
13+
println(sortedmap.mapValues(_ + "!"): SortedMap[Int, String])
14+
println(sortedmap.filterKeys(_ % 2 == 0).map(t => (t._1, t._2.toString.length)): SortedMap[Int, Int])
15+
println(sortedmap.mapValues(_ + "!").map(t => (t._1, t._2.toString.length)): SortedMap[Int, Int])
16+
println(sortedmap.filterKeys(_ % 2 == 0).filter(t => t._1 < 2): SortedMap[Int, Boolean])
17+
println(sortedmap.mapValues(_ + "!").filter(t => t._1 < 2): SortedMap[Int, String])
18+
19+
val immsortedmap = immutable.SortedMap(1 -> false, 2 -> true, 3 -> false, 4 -> true)
20+
println(immsortedmap.filterKeys(_ % 2 == 0): immutable.SortedMap[Int, Boolean])
21+
println(immsortedmap.mapValues(_ + "!"): immutable.SortedMap[Int, String])
22+
println(immsortedmap.filterKeys(_ % 2 == 0).map(t => (t._1, t._2.toString.length)): immutable.SortedMap[Int, Int])
23+
println(immsortedmap.mapValues(_ + "!").map(t => (t._1, t._2.toString.length)): immutable.SortedMap[Int, Int])
24+
println(immsortedmap.filterKeys(_ % 2 == 0).filter(t => t._1 < 2): immutable.SortedMap[Int, Boolean])
25+
println(immsortedmap.mapValues(_ + "!").filter(t => t._1 < 2): immutable.SortedMap[Int, String])
26+
27+
}

0 commit comments

Comments
 (0)