11package scala .collection
22
3- import annotation .unchecked .uncheckedVariance
43import scala .util .hashing .MurmurHash3
54
65/**
@@ -14,10 +13,11 @@ trait MultiDict[K, V]
1413 with MultiDictOps [K , V , MultiDict , MultiDict [K , V ]]
1514 with Equals {
1615
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)
2121
2222 def canEqual (that : Any ): Boolean = true
2323
@@ -43,18 +43,16 @@ trait MultiDict[K, V]
4343trait MultiDictOps [K , V , + CC [X , Y ] <: MultiDict [X , Y ], + C <: MultiDict [K , V ]]
4444 extends IterableOps [(K , V ), Iterable , C ] {
4545
46- protected [this ] type MultiDictCC [K , V ] = CC [K , V ] @ uncheckedVariance
47-
48- def multiMapFactory : MapFactory [MultiDictCC ]
46+ def multiDictFactory : MapFactory [CC ]
4947
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)
5250
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)) })
5553
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)) })
5856
5957 /**
6058 * @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]]
104102 * @tparam W new type of values
105103 */
106104 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))
108106
109107 /**
110108 * @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]]
115113 * @tparam W new type of values
116114 */
117115 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))
119117
120118 /**
121119 * @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]]
133131 )
134132
135133 /** 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+ })
141138
142139 /**
143140 * @return Whether there exists a value associated with the given `key`
@@ -209,10 +206,10 @@ object MultiDictOps {
209206 ) extends IterableOps .WithFilter [(K , V ), IterableCC ](`this`, p) {
210207
211208 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))
213210
214211 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))
216213
217214 override def withFilter (q : ((K , V )) => Boolean ): WithFilter [K , V , IterableCC , CC ] =
218215 new WithFilter [K , V , IterableCC , CC ](`this`, (kv : (K , V )) => p(kv) && q(kv))
0 commit comments