11/** Taken from the original implementation of WeakHashSet in scala-reflect
2- *
3- * @author : Eugene Burmako
42 */
53package dotty .tools .dotc .util
64
7- import java .lang .ref .{WeakReference , ReferenceQueue }
5+ import java .lang .ref .{ReferenceQueue , WeakReference }
6+
87import scala .annotation .tailrec
9- import scala .collection .mutable .{ Set => MSet }
8+ import scala .collection .mutable
109
1110/**
1211 * 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}
1817 * This set implementation is not in general thread safe without external concurrency control. However it behaves
1918 * properly when GC concurrently collects elements in this set.
2019 */
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 ] {
2221
2322 import WeakHashSet ._
2423
@@ -63,6 +62,8 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
6362
6463 private [this ] def computeThreshold : Int = (table.size * loadFactor).ceil.toInt
6564
65+ def get (elem : A ): Option [A ] = Option (findEntry(elem))
66+
6667 /**
6768 * find the bucket associated with an element's hash code
6869 */
@@ -146,8 +147,10 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
146147 tableLoop(0 )
147148 }
148149
150+ def contains (elem : A ): Boolean = findEntry(elem) ne null
151+
149152 // 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 {
151154 case null => throw new NullPointerException (" WeakHashSet cannot hold nulls" )
152155 case _ => {
153156 removeStaleEntries()
@@ -198,15 +201,15 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
198201 }
199202
200203 // 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 {
202205 case null => throw new NullPointerException (" WeakHashSet cannot hold nulls" )
203206 case _ => {
204207 removeStaleEntries()
205208 val hash = elem.hashCode
206209 val bucket = bucketFor(hash)
207210 val oldHead = table(bucket)
208211
209- def add () = {
212+ def add (): Unit = {
210213 table(bucket) = new Entry (elem, hash, oldHead, queue)
211214 count += 1
212215 if (count > threshold) resize()
@@ -224,13 +227,8 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
224227 }
225228 }
226229
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-
232230 // 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 {
234232 case null => this
235233 case _ => {
236234 removeStaleEntries()
@@ -250,8 +248,6 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
250248 }
251249 }
252250
253- def -= (elem : A ): this .type = this - elem
254-
255251 // empty this set
256252 override def clear (): Unit = {
257253 table = new Array [Entry [A ]](table.size)
@@ -272,8 +268,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
272268 count
273269 }
274270
275- override def apply (x : A ): Boolean = this contains x
276-
271+ override def isEmpty : Boolean = size == 0
277272 override def foreach [U ](f : A => U ): Unit = iterator foreach f
278273
279274 // It has the `()` because iterator runs `removeStaleEntries()`
@@ -283,7 +278,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
283278 override def iterator : Iterator [A ] = {
284279 removeStaleEntries()
285280
286- new Iterator [A ] {
281+ new collection. AbstractIterator [A ] {
287282
288283 /**
289284 * 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
342337 * the entries must be stable. If any are garbage collected during validation
343338 * then an assertion may inappropriately fire.
344339 */
345- def fullyValidate : Unit = {
340+ def fullyValidate () : Unit = {
346341 var computedCount = 0
347342 var bucket = 0
348343 while (bucket < table.size) {
@@ -368,7 +363,7 @@ final class WeakHashSet[A >: Null <: AnyRef](val initialCapacity: Int, val loadF
368363 /**
369364 * Produces a diagnostic dump of the table that underlies this hash set.
370365 */
371- def dump : IndexedSeq [ Any ] = table.deep
366+ def dump : String = java.util. Arrays .toString( table.asInstanceOf [ Array [ AnyRef ]])
372367
373368 /**
374369 * Number of buckets that hold collisions. Useful for diagnosing performance issues.
@@ -401,9 +396,9 @@ object WeakHashSet {
401396 */
402397 private class Entry [A ](element : A , val hash : Int , var tail : Entry [A ], queue : ReferenceQueue [A ]) extends WeakReference [A ](element, queue)
403398
404- val defaultInitialCapacity : Int = 16
405- val defaultLoadFactor : Double = .75
399+ private final val defaultInitialCapacity = 16
400+ private final val defaultLoadFactor = .75
406401
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 )
409404}
0 commit comments