1
1
/** Taken from the original implementation of WeakHashSet in scala-reflect
2
- *
3
- * @author : Eugene Burmako
4
2
*/
5
3
package dotty .tools .dotc .util
6
4
7
- import java .lang .ref .{WeakReference , ReferenceQueue }
5
+ import java .lang .ref .{ReferenceQueue , WeakReference }
6
+
8
7
import scala .annotation .tailrec
9
- import scala .collection .mutable .{ Set => MSet }
8
+ import scala .collection .mutable
10
9
11
10
/**
12
11
* A HashSet where the elements are stored weakly. Elements in this set are eligible for GC if no other
@@ -18,7 +17,7 @@ import scala.collection.mutable.{Set => MSet}
18
17
* This set implementation is not in general thread safe without external concurrency control. However it behaves
19
18
* properly when GC concurrently collects elements in this set.
20
19
*/
21
- final class WeakHashSet [A >: Null <: AnyRef ](val initialCapacity : Int , val loadFactor : Double ) extends Set [ A ] with Function1 [ A , Boolean ] with MSet [A ] {
20
+ final class WeakHashSet [A <: AnyRef ](initialCapacity : Int , loadFactor : Double ) extends mutable. Set [A ] {
22
21
23
22
import WeakHashSet ._
24
23
@@ -63,6 +62,8 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
63
62
64
63
private [this ] def computeThreshold : Int = (table.size * loadFactor).ceil.toInt
65
64
65
+ def get (elem : A ): Option [A ] = Option (findEntry(elem))
66
+
66
67
/**
67
68
* find the bucket associated with an element's hash code
68
69
*/
@@ -146,8 +147,10 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
146
147
tableLoop(0 )
147
148
}
148
149
150
+ def contains (elem : A ): Boolean = findEntry(elem) ne null
151
+
149
152
// from scala.reflect.internal.Set, find an element or null if it isn't contained
150
- override def findEntry (elem : A ): A = elem match {
153
+ def findEntry (elem : A ): A = elem match {
151
154
case null => throw new NullPointerException (" WeakHashSet cannot hold nulls" )
152
155
case _ => {
153
156
removeStaleEntries()
@@ -198,15 +201,15 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
198
201
}
199
202
200
203
// add an element to this set unless it's already in there and return this set
201
- override def + (elem : A ): this .type = elem match {
204
+ override def += (elem : A ): this .type = elem match {
202
205
case null => throw new NullPointerException (" WeakHashSet cannot hold nulls" )
203
206
case _ => {
204
207
removeStaleEntries()
205
208
val hash = elem.hashCode
206
209
val bucket = bucketFor(hash)
207
210
val oldHead = table(bucket)
208
211
209
- def add () = {
212
+ def add (): Unit = {
210
213
table(bucket) = new Entry (elem, hash, oldHead, queue)
211
214
count += 1
212
215
if (count > threshold) resize()
@@ -224,13 +227,8 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
224
227
}
225
228
}
226
229
227
- def += (elem : A ): this .type = this + elem
228
-
229
- // from scala.reflect.internal.Set
230
- override def addEntry (x : A ): Unit = { this += x }
231
-
232
230
// remove an element from this set and return this set
233
- override def - (elem : A ): this .type = elem match {
231
+ override def -= (elem : A ): this .type = elem match {
234
232
case null => this
235
233
case _ => {
236
234
removeStaleEntries()
@@ -250,8 +248,6 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
250
248
}
251
249
}
252
250
253
- def -= (elem : A ): this .type = this - elem
254
-
255
251
// empty this set
256
252
override def clear (): Unit = {
257
253
table = new Array [Entry [A ]](table.size)
@@ -272,8 +268,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
272
268
count
273
269
}
274
270
275
- override def apply (x : A ): Boolean = this contains x
276
-
271
+ override def isEmpty : Boolean = size == 0
277
272
override def foreach [U ](f : A => U ): Unit = iterator foreach f
278
273
279
274
// It has the `()` because iterator runs `removeStaleEntries()`
@@ -283,7 +278,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
283
278
override def iterator : Iterator [A ] = {
284
279
removeStaleEntries()
285
280
286
- new Iterator [A ] {
281
+ new collection. AbstractIterator [A ] {
287
282
288
283
/**
289
284
* the bucket currently being examined. Initially it's set past the last bucket and will be decremented
@@ -342,7 +337,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
342
337
* the entries must be stable. If any are garbage collected during validation
343
338
* then an assertion may inappropriately fire.
344
339
*/
345
- def fullyValidate : Unit = {
340
+ def fullyValidate () : Unit = {
346
341
var computedCount = 0
347
342
var bucket = 0
348
343
while (bucket < table.size) {
@@ -368,7 +363,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
368
363
/**
369
364
* Produces a diagnostic dump of the table that underlies this hash set.
370
365
*/
371
- def dump : IndexedSeq [ Any ] = table.deep
366
+ def dump : String = java.util. Arrays .toString( table.asInstanceOf [ Array [ AnyRef ]])
372
367
373
368
/**
374
369
* Number of buckets that hold collisions. Useful for diagnosing performance issues.
@@ -401,9 +396,9 @@ object WeakHashSet {
401
396
*/
402
397
private class Entry [A ](element : A , val hash : Int , var tail : Entry [A ], queue : ReferenceQueue [A ]) extends WeakReference [A ](element, queue)
403
398
404
- val defaultInitialCapacity : Int = 16
405
- val defaultLoadFactor : Double = .75
399
+ private final val defaultInitialCapacity = 16
400
+ private final val defaultLoadFactor = .75
406
401
407
- def apply [A >: Null <: AnyRef ](initialCapacity : Int = WeakHashSet . defaultInitialCapacity, loadFactor : Double = WeakHashSet . defaultLoadFactor): WeakHashSet [A ] =
408
- new WeakHashSet [ A ] (initialCapacity, defaultLoadFactor )
402
+ def apply [A <: AnyRef ](initialCapacity : Int = defaultInitialCapacity, loadFactor : Double = defaultLoadFactor): WeakHashSet [A ] =
403
+ new WeakHashSet (initialCapacity, loadFactor )
409
404
}
0 commit comments