@@ -6,17 +6,16 @@ package dotty.tools.dotc.util
6
6
7
7
import java .lang .ref .{WeakReference , ReferenceQueue }
8
8
import scala .annotation .tailrec
9
- import scala .collection .generic .Clearable
10
9
import scala .collection .mutable .{Set => MSet }
11
10
12
11
/**
13
- * A HashSet where the elements are stored weakly. Elements in this set are elligible for GC if no other
12
+ * A HashSet where the elements are stored weakly. Elements in this set are eligible for GC if no other
14
13
* hard references are associated with them. Its primary use case is as a canonical reference
15
14
* identity holder (aka "hash-consing") via findEntryOrUpdate
16
15
*
17
16
* This Set implementation cannot hold null. Any attempt to put a null in it will result in a NullPointerException
18
17
*
19
- * This set implmeentation is not in general thread safe without external concurrency control. However it behaves
18
+ * This set implementation is not in general thread safe without external concurrency control. However it behaves
20
19
* properly when GC concurrently collects elements in this set.
21
20
*/
22
21
final class WeakHashSet [A >: Null <: AnyRef ](val initialCapacity : Int , val loadFactor : Double ) extends Set [A ] with Function1 [A , Boolean ] with MSet [A ] {
@@ -29,7 +28,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
29
28
30
29
/**
31
30
* queue of Entries that hold elements scheduled for GC
32
- * the removeStaleEntries() method works through the queue to remeove
31
+ * the removeStaleEntries() method works through the queue to remove
33
32
* stale entries from the table
34
33
*/
35
34
private [this ] val queue = new ReferenceQueue [A ]
@@ -44,7 +43,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
44
43
* power of two equal to or greater than the specified initial capacity
45
44
*/
46
45
private def computeCapacity = {
47
- if (initialCapacity < 0 ) throw new IllegalArgumentException (" initial capacity cannot be less than 0" );
46
+ if (initialCapacity < 0 ) throw new IllegalArgumentException (" initial capacity cannot be less than 0" )
48
47
var candidate = 1
49
48
while (candidate < initialCapacity) {
50
49
candidate *= 2
@@ -60,12 +59,12 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
60
59
/**
61
60
* the limit at which we'll increase the size of the hash table
62
61
*/
63
- var threshhold = computeThreshHold
62
+ private [ this ] var threshold = computeThreshold
64
63
65
- private [this ] def computeThreshHold : Int = (table.size * loadFactor).ceil.toInt
64
+ private [this ] def computeThreshold : Int = (table.size * loadFactor).ceil.toInt
66
65
67
66
/**
68
- * find the bucket associated with an elements 's hash code
67
+ * find the bucket associated with an element 's hash code
69
68
*/
70
69
private [this ] def bucketFor (hash : Int ): Int = {
71
70
// spread the bits around to try to avoid accidental collisions using the
@@ -125,7 +124,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
125
124
private [this ] def resize (): Unit = {
126
125
val oldTable = table
127
126
table = new Array [Entry [A ]](oldTable.size * 2 )
128
- threshhold = computeThreshHold
127
+ threshold = computeThreshold
129
128
130
129
@ tailrec
131
130
def tableLoop (oldBucket : Int ): Unit = if (oldBucket < oldTable.size) {
@@ -160,7 +159,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
160
159
case null => null .asInstanceOf [A ]
161
160
case _ => {
162
161
val entryElem = entry.get
163
- if (elem == entryElem) entryElem
162
+ if (elem.equals( entryElem) ) entryElem
164
163
else linkedListLoop(entry.tail)
165
164
}
166
165
}
@@ -180,7 +179,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
180
179
def add () = {
181
180
table(bucket) = new Entry (elem, hash, oldHead, queue)
182
181
count += 1
183
- if (count > threshhold ) resize()
182
+ if (count > threshold ) resize()
184
183
elem
185
184
}
186
185
@@ -189,7 +188,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
189
188
case null => add()
190
189
case _ => {
191
190
val entryElem = entry.get
192
- if (elem == entryElem) entryElem
191
+ if (elem.equals( entryElem) ) entryElem
193
192
else linkedListLoop(entry.tail)
194
193
}
195
194
}
@@ -210,14 +209,14 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
210
209
def add () = {
211
210
table(bucket) = new Entry (elem, hash, oldHead, queue)
212
211
count += 1
213
- if (count > threshhold ) resize()
212
+ if (count > threshold ) resize()
214
213
}
215
214
216
215
@ tailrec
217
216
def linkedListLoop (entry : Entry [A ]): Unit = entry match {
218
- case null => add()
219
- case _ if ( elem == entry.get) => ()
220
- case _ => linkedListLoop(entry.tail)
217
+ case null => add()
218
+ case _ if elem.equals( entry.get) => ()
219
+ case _ => linkedListLoop(entry.tail)
221
220
}
222
221
223
222
linkedListLoop(oldHead)
@@ -227,7 +226,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
227
226
228
227
def += (elem : A ) = this + elem
229
228
230
- // from scala.reflect.interanl .Set
229
+ // from scala.reflect.internal .Set
231
230
override def addEntry (x : A ) = { this += x }
232
231
233
232
// remove an element from this set and return this set
@@ -242,7 +241,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
242
241
@ tailrec
243
242
def linkedListLoop (prevEntry : Entry [A ], entry : Entry [A ]): Unit = entry match {
244
243
case null => ()
245
- case _ if ( elem == entry.get) => remove(bucket, prevEntry, entry)
244
+ case _ if elem.equals( entry.get) => remove(bucket, prevEntry, entry)
246
245
case _ => linkedListLoop(entry, entry.tail)
247
246
}
248
247
@@ -256,7 +255,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
256
255
// empty this set
257
256
override def clear (): Unit = {
258
257
table = new Array [Entry [A ]](table.size)
259
- threshhold = computeThreshHold
258
+ threshold = computeThreshold
260
259
count = 0
261
260
262
261
// drain the queue - doesn't do anything because we're throwing away all the values anyway
@@ -375,13 +374,13 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
375
374
* Number of buckets that hold collisions. Useful for diagnosing performance issues.
376
375
*/
377
376
def collisionBucketsCount : Int =
378
- (table filter (entry => entry != null && entry.tail != null )).size
377
+ (table count (entry => entry != null && entry.tail != null ))
379
378
380
379
/**
381
380
* Number of buckets that are occupied in this hash table.
382
381
*/
383
382
def fullBucketsCount : Int =
384
- (table filter (entry => entry != null )).size
383
+ (table count (entry => entry != null ))
385
384
386
385
/**
387
386
* Number of buckets in the table
0 commit comments