Created
May 8, 2019 00:18
-
-
Save RinniSwift/22886a28ac84e2c55bea7e198ce5dfa3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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