mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
more efficient particles
Former-commit-id: 56dad88ecd715ad6e357e33b903851a47a358dcd Former-commit-id: c85c0b759a447c0461563d98156f59879fa95db2
This commit is contained in:
49
src/net/torvald/terrarum/CircularArray.kt
Normal file
49
src/net/torvald/terrarum/CircularArray.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
|
||||
/**
|
||||
* Notes for particle storage:
|
||||
* Particles does not need to be removed, just let it overwrite as their operation is rather
|
||||
* lightweight. So, just flagDespawn = true if it need to be "deleted" so that it won't update
|
||||
* anymore.
|
||||
*
|
||||
* Created by minjaesong on 2017-01-22.
|
||||
*/
|
||||
class CircularArray<T>(val size: Int) {
|
||||
|
||||
private val buffer: Array<T> = arrayOfNulls<Any>(size) as Array<T>
|
||||
private var tail: Int = 0
|
||||
private var head: Int = 0
|
||||
|
||||
val elemCount: Int
|
||||
get() = if (tail >= head) tail - head else size
|
||||
|
||||
fun add(item: T) {
|
||||
buffer[tail] = item // overwrites oldest item when eligible
|
||||
tail = (tail + 1) % size
|
||||
if (tail == head) {
|
||||
head = (head + 1) % size
|
||||
}
|
||||
}
|
||||
|
||||
fun forEach(action: (T) -> Unit) {
|
||||
if (tail >= head) { // queue not full
|
||||
(head..tail - 1).map { buffer[it] }.forEach { action(it) }
|
||||
}
|
||||
else { // queue full
|
||||
(0..size - 1).map { buffer[(it + head) % size] }.forEach { action(it) }
|
||||
}
|
||||
}
|
||||
|
||||
fun forEachConcurrent(action: (T) -> Unit) {
|
||||
TODO()
|
||||
}
|
||||
|
||||
fun forEachConcurrentWaitFor(action: (T) -> Unit) {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "CircularArray(size=" + buffer.size + ", head=" + head + ", tail=" + tail + ")"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user