@@ -15,7 +15,6 @@ package collection
15
15
16
16
import scala .language .`2.13`
17
17
import language .experimental .captureChecking
18
- import scala .annotation .unchecked .uncheckedVariance
19
18
20
19
import scala .annotation .nowarn
21
20
import scala .collection .generic .DefaultSerializable
@@ -96,18 +95,6 @@ transparent trait StrictMapOps[K, +V, +CC[_, _] <: IterableOps[_, AnyConstr, _]
96
95
// The original keySet implementation, with a lazy iterator over the keys,
97
96
// is only correct if we have a strict Map.
98
97
// We restore it here.
99
- override def keySet : Set [K ] = new LazyKeySet
100
-
101
- /** The implementation class of the set returned by `keySet`, for pure maps.
102
- */
103
- private class LazyKeySet extends AbstractSet [K ] with DefaultSerializable {
104
- def diff (that : Set [K ]): Set [K ] = LazyKeySet .this .fromSpecific(this .view.filterNot(that))
105
- def iterator : Iterator [K ] = StrictMapOps .this .keysIterator
106
- def contains (key : K ): Boolean = StrictMapOps .this .contains(key)
107
- override def size : Int = StrictMapOps .this .size
108
- override def knownSize : Int = StrictMapOps .this .knownSize
109
- override def isEmpty : Boolean = StrictMapOps .this .isEmpty
110
- }
111
98
}
112
99
113
100
/** Base Map implementation type
@@ -214,7 +201,15 @@ transparent trait MapOps[K, +V, +CC[_, _] <: IterableOps[_, AnyConstr, _], +C]
214
201
*
215
202
* @return a set representing the keys contained by this map
216
203
*/
217
- def keySet : Set [K ] = new KeySet
204
+ def keySet : Set [K ] =
205
+ // If we know one of the strict implementations inside this library, simply return LazyKeySet
206
+ import MapOps .LazyKeySet
207
+ this match
208
+ case s : SeqMap [K , V ] => new LazyKeySet (s)
209
+ case s : SortedMap [K , V ] => new LazyKeySet (s)
210
+ case s : immutable.MapOps [K , V , immutable.Map , immutable.Map [K , V ]] => new LazyKeySet (s)
211
+ case s : mutable.MapOps [K , V , mutable.Map , mutable.Map [K , V ]] => new LazyKeySet (s)
212
+ case _ => new KeySet
218
213
219
214
/** The implementation class of the set returned by `keySet`.
220
215
*/
@@ -250,7 +245,7 @@ transparent trait MapOps[K, +V, +CC[_, _] <: IterableOps[_, AnyConstr, _], +C]
250
245
* @return an [[Iterable ]] collection of the keys contained by this map
251
246
*/
252
247
@ deprecatedOverriding(" This method should be an alias for keySet" , since= " 2.13.13" )
253
- def keys : Iterable [K ]^ {this } = keySet
248
+ def keys : Iterable [K ]^ {this } = this . keySet
254
249
255
250
/** Collects all values of this map in an iterable collection.
256
251
*
@@ -436,6 +431,17 @@ object MapOps {
436
431
437
432
}
438
433
434
+
435
+ /** The implementation class of the set returned by `keySet`, for pure maps.
436
+ */
437
+ private class LazyKeySet [K , + V , + CC [_, _] <: IterableOps [_, AnyConstr , _], + C ](mp : MapOps [K , V , CC , C ]) extends AbstractSet [K ] with DefaultSerializable {
438
+ def iterator : Iterator [K ] = mp.keysIterator
439
+ def diff (that : Set [K ]): Set [K ] = LazyKeySet .this .fromSpecific(this .view.filterNot(that))
440
+ def contains (key : K ): Boolean = mp.contains(key)
441
+ override def size : Int = mp.size
442
+ override def knownSize : Int = mp.knownSize
443
+ override def isEmpty : Boolean = mp.isEmpty
444
+ }
439
445
}
440
446
441
447
/**
0 commit comments