Skip to content

Commit 0cf4715

Browse files
committed
Remove KeySet overload, we can remove StrictMapOps now
1 parent 4e8df36 commit 0cf4715

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

library/src/scala/collection/Map.scala

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package collection
1515

1616
import scala.language.`2.13`
1717
import language.experimental.captureChecking
18-
import scala.annotation.unchecked.uncheckedVariance
1918

2019
import scala.annotation.nowarn
2120
import scala.collection.generic.DefaultSerializable
@@ -96,18 +95,6 @@ transparent trait StrictMapOps[K, +V, +CC[_, _] <: IterableOps[_, AnyConstr, _]
9695
// The original keySet implementation, with a lazy iterator over the keys,
9796
// is only correct if we have a strict Map.
9897
// 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-
}
11198
}
11299

113100
/** Base Map implementation type
@@ -214,7 +201,15 @@ transparent trait MapOps[K, +V, +CC[_, _] <: IterableOps[_, AnyConstr, _], +C]
214201
*
215202
* @return a set representing the keys contained by this map
216203
*/
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
218213

219214
/** The implementation class of the set returned by `keySet`.
220215
*/
@@ -250,7 +245,7 @@ transparent trait MapOps[K, +V, +CC[_, _] <: IterableOps[_, AnyConstr, _], +C]
250245
* @return an [[Iterable]] collection of the keys contained by this map
251246
*/
252247
@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
254249

255250
/** Collects all values of this map in an iterable collection.
256251
*
@@ -436,6 +431,17 @@ object MapOps {
436431

437432
}
438433

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+
}
439445
}
440446

441447
/**

0 commit comments

Comments
 (0)