package link.roadio.riff.player.playback import link.roadio.riff.player.storage.PlaybackQueueOccurrence /** Primitive index for a validated immutable queue; preserves zero and every signed occurrence ID. */ internal class QueueOccurrenceIndex( occurrences: List, ) { private val capacity = Integer.highestOneBit(maxOf(2, Math.multiplyExact(occurrences.size, 2) - 1)) shl 1 private val keys = LongArray(capacity) private val ordinals = IntArray(capacity) { -1 } init { occurrences.forEach { occurrence -> var slot = slot(occurrence.occurrenceId) while (ordinals[slot] != -1) { require(keys[slot] != occurrence.occurrenceId) slot = (slot + 1) and (capacity - 1) } keys[slot] = occurrence.occurrenceId ordinals[slot] = occurrence.ordinal } } private fun slot(key: Long): Int = java.lang.Long.hashCode(key) and (capacity - 1) operator fun get(key: Long): Int? { var slot = slot(key) while (ordinals[slot] != -1) { if (keys[slot] == key) return ordinals[slot] slot = (slot + 1) and (capacity - 1) } return null } }