Skip to content

Commit 30f03e8

Browse files
committed
Add ReadOnlySet
1 parent 8a5d9a2 commit 30f03e8

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

compiler/src/dotty/tools/dotc/util/MutableSet.scala

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package dotty.tools.dotc.util
22

33
/** A common class for lightweight mutable sets.
44
*/
5-
abstract class MutableSet[T] {
6-
7-
/** The entry in the set such that `isEqual(x, entry)`, or else `null`. */
8-
def lookup(x: T): T | Null
5+
abstract class MutableSet[T] extends ReadOnlySet[T]:
96

107
/** Add element `x` to the set */
118
def +=(x: T): Unit
@@ -17,14 +14,3 @@ abstract class MutableSet[T] {
1714

1815
def clear(): Unit
1916

20-
def size: Int
21-
22-
def iterator: Iterator[T]
23-
24-
def contains(x: T): Boolean = lookup(x) != null
25-
26-
def foreach[U](f: T => U): Unit = iterator foreach f
27-
28-
def toList: List[T] = iterator.toList
29-
30-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dotty.tools.dotc.util
2+
3+
/** A class for the readonly part of mutable sets.
4+
*/
5+
abstract class ReadOnlySet[T]:
6+
7+
/** The entry in the set such that `isEqual(x, entry)`, or else `null`. */
8+
def lookup(x: T): T | Null
9+
10+
def size: Int
11+
12+
def iterator: Iterator[T]
13+
14+
def contains(x: T): Boolean = lookup(x) != null
15+
16+
def foreach[U](f: T => U): Unit = iterator.foreach(f)
17+
18+
def toList: List[T] = iterator.toList
19+
20+
def isEmpty = size == 0
21+
22+
object ReadOnlySet:
23+
def empty[T]: ReadOnlySet[T] = HashSet[T](4)
24+

0 commit comments

Comments
 (0)