Skip to content

Instantly share code, notes, and snippets.

@RinniSwift
Created May 8, 2019 00:18
Show Gist options
  • Save RinniSwift/22886a28ac84e2c55bea7e198ce5dfa3 to your computer and use it in GitHub Desktop.
Save RinniSwift/22886a28ac84e2c55bea7e198ce5dfa3 to your computer and use it in GitHub Desktop.
class CircularBuffer<T> {
// ...
func enqueue(item: T) {
// 1.
if isEmpty() {
elements[bufferMaxSize-1] = item
} else {
// 2.
for index in 0...bufferMaxSize - 2 {
elements[index] = elements[index + 1]
}
// 3.
elements[bufferMaxSize - 1] = item
}
// 4.
if !isFull() {
size += 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment