1
1
package scala .collection
2
2
3
- import annotation .unchecked .uncheckedVariance
4
3
import scala .util .hashing .MurmurHash3
5
4
6
5
/**
@@ -14,10 +13,11 @@ trait MultiDict[K, V]
14
13
with MultiDictOps [K , V , MultiDict , MultiDict [K , V ]]
15
14
with Equals {
16
15
17
- def multiMapFactory : MapFactory [MultiDictCC ] = MultiDict
18
-
19
- override protected [this ] def fromSpecificIterable (coll : Iterable [(K , V )]): MultiDictCC [K , V ] = multiMapFactory.from(coll)
20
- override protected [this ] def newSpecificBuilder : mutable.Builder [(K , V ), MultiDictCC [K , V ]] = multiMapFactory.newBuilder[K , V ]
16
+ def multiDictFactory : MapFactory [MultiDict ] = MultiDict
17
+ override protected def fromSpecific (coll : IterableOnce [(K , V )]): MultiDict [K , V ] = multiDictFactory.from(coll)
18
+ override protected def newSpecificBuilder : mutable.Builder [(K , V ), MultiDict [K , V ]] = multiDictFactory.newBuilder
19
+ override def empty : MultiDict [K , V ] = multiDictFactory.empty
20
+ override def withFilter (p : ((K , V )) => Boolean ): MultiDictOps .WithFilter [K , V , Iterable , MultiDict ] = new MultiDictOps .WithFilter (this , p)
21
21
22
22
def canEqual (that : Any ): Boolean = true
23
23
@@ -43,18 +43,16 @@ trait MultiDict[K, V]
43
43
trait MultiDictOps [K , V , + CC [X , Y ] <: MultiDict [X , Y ], + C <: MultiDict [K , V ]]
44
44
extends IterableOps [(K , V ), Iterable , C ] {
45
45
46
- protected [this ] type MultiDictCC [K , V ] = CC [K , V ] @ uncheckedVariance
47
-
48
- def multiMapFactory : MapFactory [MultiDictCC ]
46
+ def multiDictFactory : MapFactory [CC ]
49
47
50
- protected [ this ] def multiMapFromIterable [L , W ](it : Iterable [(L , W )]): CC [L , W ] =
51
- multiMapFactory .from(it)
48
+ protected def multiDictFromIterable [L , W ](it : Iterable [(L , W )]): CC [L , W ] =
49
+ multiDictFactory .from(it)
52
50
53
- protected [ this ] def fromSpecificSets (it : Iterable [(K , Set [V ])]): C =
54
- fromSpecificIterable (it.view.flatMap { case (k, vs) => vs.view.map(v => (k, v)) })
51
+ protected def fromSpecificSets (it : Iterable [(K , Set [V ])]): C =
52
+ fromSpecific (it.view.flatMap { case (k, vs) => vs.view.map(v => (k, v)) })
55
53
56
- protected [ this ] def fromSets [L , W ](it : Iterable [(L , Set [W ])]): CC [L , W ] =
57
- multiMapFromIterable (it.view.flatMap { case (k, vs) => vs.view.map(v => (k, v)) })
54
+ protected def fromSets [L , W ](it : Iterable [(L , Set [W ])]): CC [L , W ] =
55
+ multiDictFromIterable (it.view.flatMap { case (k, vs) => vs.view.map(v => (k, v)) })
58
56
59
57
/**
60
58
* @return All the elements contained in this multidict, grouped by key
@@ -104,7 +102,7 @@ trait MultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
104
102
* @tparam W new type of values
105
103
*/
106
104
def map [L , W ](f : ((K , V )) => (L , W )): CC [L , W ] =
107
- multiMapFromIterable (new View .Map (toIterable, f))
105
+ multiDictFromIterable (new View .Map (toIterable, f))
108
106
109
107
/**
110
108
* @return a multidict that contains all the entries of `this` multidict,
@@ -115,7 +113,7 @@ trait MultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
115
113
* @tparam W new type of values
116
114
*/
117
115
def flatMap [L , W ](f : ((K , V )) => IterableOnce [(L , W )]): CC [L , W ] =
118
- multiMapFromIterable (new View .FlatMap (toIterable, f))
116
+ multiDictFromIterable (new View .FlatMap (toIterable, f))
119
117
120
118
/**
121
119
* @return a multidict that contains all the entries of `this` multidict
@@ -133,11 +131,10 @@ trait MultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
133
131
)
134
132
135
133
/** Concatenate the entries given in `that` iterable to `this` multidict */
136
- def concat (that : Iterable [(K , V )]): C =
137
- fromSpecificIterable(new View .Concat (toIterable, that))
138
-
139
- override def withFilter (p : ((K , V )) => Boolean ): MultiDictOps .WithFilter [K , V , IterableCC , CC ] =
140
- new MultiDictOps .WithFilter (this , p)
134
+ def concat (that : IterableOnce [(K , V )]): C = fromSpecific(that match {
135
+ case that : collection.Iterable [(K , V )] => new View .Concat (toIterable, that)
136
+ case _ => iterator ++ that.iterator
137
+ })
141
138
142
139
/**
143
140
* @return Whether there exists a value associated with the given `key`
@@ -209,10 +206,10 @@ object MultiDictOps {
209
206
) extends IterableOps .WithFilter [(K , V ), IterableCC ](`this`, p) {
210
207
211
208
def map [L , W ](f : ((K , V )) => (L , W )): CC [L , W ] =
212
- `this`.multiMapFactory .from(new View .Map (filtered, f))
209
+ `this`.multiDictFactory .from(new View .Map (filtered, f))
213
210
214
211
def flatMap [L , W ](f : ((K , V )) => IterableOnce [(L , W )]): CC [L , W ] =
215
- `this`.multiMapFactory .from(new View .FlatMap (filtered, f))
212
+ `this`.multiDictFactory .from(new View .FlatMap (filtered, f))
216
213
217
214
override def withFilter (q : ((K , V )) => Boolean ): WithFilter [K , V , IterableCC , CC ] =
218
215
new WithFilter [K , V , IterableCC , CC ](`this`, (kv : (K , V )) => p(kv) && q(kv))
0 commit comments