-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularBuffer.kt
46 lines (38 loc) · 1.03 KB
/
CircularBuffer.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class CircularBuffer<T>(private val size: Int) {
private var elements: Array<Any?> = arrayOfNulls(size)
private var head: Int = 0
private var tail: Int = 0
private var count: Int = 0
val isFull: Boolean
get() = count == size
val isEmpty: Boolean
get() = count == 0
fun add(element: T) {
elements[tail] = element
tail = (tail + 1) % size
if (isFull) {
head = (head + 1) % size
} else {
count++
}
}
@Suppress("UNCHECKED_CAST")
fun remove(): T? {
if (isEmpty) return null
val element = elements[head]
head = (head + 1) % size
count--
return element as T
}
@Suppress("UNCHECKED_CAST")
fun toList(): List<T> {
return (0 until count).map { index ->
elements[(head + index) % size] as T
}
}
fun first(): Any? {
if (elements.isEmpty())
throw NoSuchElementException("CircularBuffer is empty.")
return elements[0]
}
}