mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-06 08:38:30 +09:00
iterative circular array for slightly better performance (test might be wrong tho)
Former-commit-id: 95c4e576efca6e12af68c184ed5effa4b115d8c4 Former-commit-id: bbd74994f90f582d0643ebbbca58f40d097e2f82
This commit is contained in:
@@ -27,11 +27,21 @@ class CircularArray<T>(val size: Int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun forEach(action: (T) -> Unit) {
|
fun forEach(action: (T) -> Unit) {
|
||||||
if (tail >= head) { // queue not full
|
/*if (tail >= head) { // queue not full
|
||||||
(head..tail - 1).map { buffer[it] }.forEach { action(it) }
|
(head..tail - 1).map { buffer[it] }.forEach { action(it) }
|
||||||
}
|
}
|
||||||
else { // queue full
|
else { // queue full
|
||||||
(0..size - 1).map { buffer[(it + head) % size] }.forEach { action(it) }
|
(0..size - 1).map { buffer[(it + head) % size] }.forEach { action(it) }
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// has slightly better iteration performance than lambda
|
||||||
|
if (tail >= head) {
|
||||||
|
for (i in head..tail - 1)
|
||||||
|
action(buffer[i])
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (i in 0..size - 1)
|
||||||
|
action(buffer[(i + head) % size])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user