CoW non empty indices in container

This commit is contained in:
DBotThePony 2023-08-03 17:25:01 +07:00
parent f18a3eaaaa
commit 7b317a2efb
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -26,10 +26,14 @@ import ru.dbotthepony.mc.otm.core.util.ItemValueCodec
import ru.dbotthepony.mc.otm.core.util.VarIntValueCodec import ru.dbotthepony.mc.otm.core.util.VarIntValueCodec
import ru.dbotthepony.mc.otm.network.synchronizer.FieldSynchronizer import ru.dbotthepony.mc.otm.network.synchronizer.FieldSynchronizer
import ru.dbotthepony.mc.otm.network.synchronizer.IField import ru.dbotthepony.mc.otm.network.synchronizer.IField
import java.lang.ref.PhantomReference
import java.lang.ref.ReferenceQueue
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
import java.util.concurrent.CopyOnWriteArrayList
import java.util.function.Supplier import java.util.function.Supplier
import kotlin.NoSuchElementException import kotlin.NoSuchElementException
import kotlin.collections.ArrayList
@Suppress("UNUSED") @Suppress("UNUSED")
open class MatteryContainer(protected val watcher: Runnable, private val size: Int) : Container, Iterable<ItemStack>, INBTSerializable<Tag?> { open class MatteryContainer(protected val watcher: Runnable, private val size: Int) : Container, Iterable<ItemStack>, INBTSerializable<Tag?> {
@ -41,7 +45,16 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
private val slots = Array(size) { ItemStack.EMPTY } private val slots = Array(size) { ItemStack.EMPTY }
private val nonEmptyFlags = BitSet() private val nonEmptyFlags = BitSet()
private val nonEmptyIndices = IntArrayList() private var nonEmptyIndices = IntArrayList()
private var indicesReferences = 0
private fun cowIndices() {
if (indicesReferences != 0) {
nonEmptyIndices = IntArrayList(nonEmptyIndices)
indicesReferences = 0
}
}
private val trackedSlots: Array<ItemStack> = Array(size) { ItemStack.EMPTY } private val trackedSlots: Array<ItemStack> = Array(size) { ItemStack.EMPTY }
private val filters: Array<Item?> = arrayOfNulls(size) private val filters: Array<Item?> = arrayOfNulls(size)
private var filterSynchronizer: WeakReference<IField<MutableMap<Int, Item>>>? = null private var filterSynchronizer: WeakReference<IField<MutableMap<Int, Item>>>? = null
@ -196,7 +209,7 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
Arrays.fill(slots, ItemStack.EMPTY) Arrays.fill(slots, ItemStack.EMPTY)
Arrays.fill(filters, null) Arrays.fill(filters, null)
nonEmptyFlags.clear() nonEmptyFlags.clear()
nonEmptyIndices.clear() nonEmptyIndices = IntArrayList()
filterSynchronizer?.get()?.value?.clear() filterSynchronizer?.get()?.value?.clear()
@ -468,11 +481,13 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
if (slots[slot].isEmpty) { if (slots[slot].isEmpty) {
if (nonEmptyFlags[slot]) { if (nonEmptyFlags[slot]) {
nonEmptyFlags[slot] = false nonEmptyFlags[slot] = false
nonEmptyIndices.removeInt(slot) cowIndices()
nonEmptyIndices.rem(slot)
} }
} else { } else {
if (!nonEmptyFlags[slot]) { if (!nonEmptyFlags[slot]) {
nonEmptyFlags[slot] = true nonEmptyFlags[slot] = true
cowIndices()
nonEmptyIndices.addSorted(slot, IntComparators.NATURAL_COMPARATOR) nonEmptyIndices.addSorted(slot, IntComparators.NATURAL_COMPARATOR)
} }
} }
@ -484,7 +499,7 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
final override fun clearContent() { final override fun clearContent() {
nonEmptyFlags.clear() nonEmptyFlags.clear()
nonEmptyIndices.clear() nonEmptyIndices = IntArrayList()
Arrays.fill(trackedSlots, ItemStack.EMPTY) Arrays.fill(trackedSlots, ItemStack.EMPTY)
@ -497,7 +512,11 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
} }
} }
private inner class Iterator : MutableIterator<ItemStack> { private inner class Iterator : IContainerIterator {
init {
indicesReferences++
}
private val parent = nonEmptyIndices.intIterator() private val parent = nonEmptyIndices.intIterator()
private var lastIndex = -1 private var lastIndex = -1
@ -519,11 +538,37 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I
setItem(lastIndex, ItemStack.EMPTY) setItem(lastIndex, ItemStack.EMPTY)
lastIndex = -1 lastIndex = -1
} }
override fun setChanged() {
if (lastIndex == -1) {
throw NoSuchElementException()
}
setChanged(lastIndex)
}
} }
final override fun iterator(): MutableIterator<ItemStack> { private object EmptyIterator : IContainerIterator {
override fun hasNext(): Boolean {
return false
}
override fun next(): ItemStack {
throw NoSuchElementException()
}
override fun remove() {
throw NoSuchElementException()
}
override fun setChanged() {
throw NoSuchElementException()
}
}
final override fun iterator(): IContainerIterator {
if (isEmpty) { if (isEmpty) {
return ObjectIterators.EMPTY_ITERATOR as MutableIterator<ItemStack> return EmptyIterator
} }
return Iterator() return Iterator()