Skip to content

Commit 28f855d

Browse files
committed
Added EqLinkedHashSet, an identity-based linked hash set
1 parent 2fa6ddc commit 28f855d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package dotty.tools.dotc.util
2+
3+
import scala.collection.mutable.ArrayBuffer
4+
5+
class EqLinkedHashSet[T](
6+
initialCapacity: Int = 8, capacityMultiple: Int = 2
7+
) extends MutableSet[T] {
8+
9+
private val map: MutableMap[T, Unit] = new EqHashMap(initialCapacity, capacityMultiple)
10+
private val linkingArray: ArrayBuffer[T] = new ArrayBuffer(initialCapacity)
11+
12+
override def +=(x: T): Unit =
13+
map.update(x, ())
14+
if map.size != linkingArray.size then linkingArray += x
15+
16+
override def put(x: T): T =
17+
this += x
18+
x
19+
20+
override def -=(x: T): Unit =
21+
map -= x
22+
if map.size != linkingArray.size then linkingArray -= x
23+
24+
override def clear(): Unit =
25+
map.clear()
26+
linkingArray.clear()
27+
28+
override def lookup(x: T): T | Null = if map.contains(x) then x else null
29+
30+
override def size: Int = map.size
31+
32+
override def iterator: Iterator[T] = linkingArray.iterator
33+
34+
}
35+
36+
object EqLinkedHashSet {
37+
def apply[T](x: T): EqLinkedHashSet[T] =
38+
val set = new EqLinkedHashSet[T]
39+
set += x
40+
set
41+
42+
def apply[T](x: T, y: T): EqLinkedHashSet[T] =
43+
val set = EqLinkedHashSet(x)
44+
set += y
45+
set
46+
}

0 commit comments

Comments
 (0)