Bring back fastutil usage

This commit is contained in:
DBotThePony 2024-02-03 18:04:51 +07:00
parent 36b2f0670c
commit 1979627817
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -1,5 +1,6 @@
package ru.dbotthepony.kommons.collect
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap
import java.util.function.BooleanSupplier
import java.util.stream.Stream
@ -7,15 +8,13 @@ import java.util.stream.Stream
* Set, with values appearing and disappearing by conditions
*/
class ConditionalSet<E : Any> : Set<E> {
private class Entry<E>(val value: E, var condition: BooleanSupplier)
private val backing = ArrayList<Entry<E>>()
private val mapped = HashMap<E, Entry<E>>()
private val backing = Object2ObjectLinkedOpenHashMap<E, BooleanSupplier>()
override val size: Int
get() = backing.stream().filter { it.condition.asBoolean }.count().toInt()
get() = backing.values.stream().filter { it.asBoolean }.count().toInt()
override fun contains(element: E): Boolean {
return mapped[element]?.condition?.asBoolean ?: false
return backing[element]?.asBoolean ?: false
}
override fun containsAll(elements: Collection<E>): Boolean {
@ -23,67 +22,43 @@ class ConditionalSet<E : Any> : Set<E> {
}
override fun isEmpty(): Boolean {
return mapped.isEmpty() || mapped.values.stream().noneMatch { it.condition.asBoolean }
return backing.isEmpty() || backing.values.stream().noneMatch { it.asBoolean }
}
override fun stream(): Stream<E> {
return backing.stream().filter { it.condition.asBoolean }.map { it.value }
return backing.entries.stream().filter { it.value.asBoolean }.map { it.key }
}
override fun iterator(): Iterator<E> {
return backing.iterator().filter { it.condition.asBoolean }.map { it.value }
return backing.entries.iterator().filter { it.value.asBoolean }.map { it.key }
}
fun actuallyContains(element: E): Boolean {
return element in mapped
return element in backing
}
fun add(element: E, condition: BooleanSupplier): Boolean {
if (element in mapped) return false
val entry = Entry(element, condition)
mapped[element] = entry
backing.add(entry)
if (element in backing) return false
backing[element] = condition
return true
}
fun addFirst(element: E, condition: BooleanSupplier): Boolean {
if (element in mapped) return false
val entry = Entry(element, condition)
mapped[element] = entry
backing.add(0, entry)
if (element in backing) return false
backing.putAndMoveToFirst(element, condition)
return true
}
fun replace(element: E, condition: BooleanSupplier) {
val entry = mapped[element]
if (entry != null) {
entry.condition = condition
} else {
add(element, condition)
}
backing[element] = condition
}
fun replaceFirst(element: E, condition: BooleanSupplier) {
val entry = mapped[element]
if (entry != null) {
entry.condition = condition
backing.remove(entry)
backing.add(0, entry)
} else {
addFirst(element, condition)
}
backing.remove(element)
backing.putAndMoveToFirst(element, condition)
}
fun remove(element: E): Boolean {
val entry = mapped.remove(element)
if (entry != null) {
backing.remove(entry)
return true
}
return false
return backing.remove(element) != null
}
}