Fix ref hash strategy
This commit is contained in:
parent
159c82cadd
commit
9d79e52c43
@ -4,42 +4,15 @@ import it.unimi.dsi.fastutil.Hash
|
|||||||
import java.lang.ref.Reference
|
import java.lang.ref.Reference
|
||||||
|
|
||||||
object ReferenceHashStrategy : Hash.Strategy<Any?> {
|
object ReferenceHashStrategy : Hash.Strategy<Any?> {
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
override fun equals(a: Any?, b: Any?): Boolean {
|
override fun equals(a: Any?, b: Any?): Boolean {
|
||||||
if (a === b) return true
|
if (a === b) return true
|
||||||
|
if (a == null || b == null) return false
|
||||||
|
|
||||||
if (a is Reference<*>) {
|
if (a is Reference<*>) {
|
||||||
if (a.refersTo(null) || b == null) return false
|
if (b is Reference<*>) return a.get() == b.get()
|
||||||
|
return a.get() == b
|
||||||
if (b is Reference<*>) {
|
|
||||||
if (b.refersTo(null)) return false
|
|
||||||
return (b as Reference<Any>).refersTo(a.get() ?: return false)
|
|
||||||
} else {
|
|
||||||
return (a as Reference<Any>).refersTo(b)
|
|
||||||
}
|
|
||||||
} else if (b is Reference<*>) {
|
} else if (b is Reference<*>) {
|
||||||
if (b.refersTo(null) || a == null) return false
|
return b.get() == a
|
||||||
return (b as Reference<Any>).refersTo(a)
|
|
||||||
}
|
|
||||||
|
|
||||||
return a == b
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hashCode(o: Any?): Int {
|
|
||||||
return o.hashCode()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
object StrictReferenceHashStrategy : Hash.Strategy<Any?> {
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
override fun equals(a: Any?, b: Any?): Boolean {
|
|
||||||
if (a === b) return true
|
|
||||||
|
|
||||||
if (a is Reference<*>) {
|
|
||||||
if (b is Reference<*>) return false
|
|
||||||
return b != null && (a as Reference<Any>).refersTo(b)
|
|
||||||
} else if (b is Reference<*>) {
|
|
||||||
return a != null && (b as Reference<Any>).refersTo(a)
|
|
||||||
} else {
|
} else {
|
||||||
return a == b
|
return a == b
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import java.lang.ref.ReferenceQueue
|
|||||||
|
|
||||||
class WeakHashSet<E : Any>(initialCapacity: Int = 16, loadFactor: Float = 0.75f, linked: Boolean = false) : MutableSet<E> {
|
class WeakHashSet<E : Any>(initialCapacity: Int = 16, loadFactor: Float = 0.75f, linked: Boolean = false) : MutableSet<E> {
|
||||||
private val queue = ReferenceQueue<E>()
|
private val queue = ReferenceQueue<E>()
|
||||||
private val backing: ObjectSet<Any> = if (linked) ObjectLinkedOpenCustomHashSet(initialCapacity, loadFactor, StrictReferenceHashStrategy) else ObjectOpenCustomHashSet(initialCapacity, loadFactor, StrictReferenceHashStrategy)
|
private val backing: ObjectSet<Any> = if (linked) ObjectLinkedOpenCustomHashSet(initialCapacity, loadFactor, ReferenceHashStrategy) else ObjectOpenCustomHashSet(initialCapacity, loadFactor, ReferenceHashStrategy)
|
||||||
|
|
||||||
private fun purge() {
|
private fun purge() {
|
||||||
var next = queue.poll()
|
var next = queue.poll()
|
||||||
|
Loading…
Reference in New Issue
Block a user