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:
Song Minjae
2017-01-22 14:20:24 +09:00
parent 54b52b1b6e
commit 6c94dc9632

View File

@@ -27,11 +27,21 @@ class CircularArray<T>(val size: Int) {
}
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) }
}
else { // queue full
(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])
}
}