Commit 429d5aec authored by David Owens II's avatar David Owens II

Add a ring buffer that may be used in some of the parser internals.

parent e3ba4782
......@@ -7,15 +7,84 @@
import Foundation
class SizedQueue<ElementType> {
var items: [ElementType] = []
let maximumNumberOfItems: Int
/**
* A fixed size, continuous buffer.
*/
class RingBuffer<ElementType> {
let capacity: Int
var buffer: [ElementType?]
private var lastIndex: Int = 0
private var start: Int = 0
private var end: Int = 0
init(maximumNumberOfItems: Int = 10) {
self.maximumNumberOfItems = maximumNumberOfItems
init(capacity: Int) {
precondition(capacity > 1, "The capacity of the buffer must be greater than one.")
self.capacity = capacity
buffer = [ElementType?](count: capacity, repeatedValue: nil)
}
/**
* Inserts an element into the buffer. When the capacity has been met,
* the oldest item is overwritten.
*/
func insert(element: ElementType) {
buffer[end] = element
if start == 0 && end == 0 {
end = 1
}
else {
end = (end + 1) % capacity
if end == start {
start += 1
}
}
}
/**
* Removes the oldest item in the buffer.
*/
func remove() -> ElementType? {
let element = buffer[start]
buffer[start] = nil
if start != end {
start = (start + 1) % capacity
}
return element
}
/**
* Retrieves an item from the buffer at the given index.
*
* @param index The index within the buffer.
*/
subscript(index: Int) -> ElementType? {
precondition(index >= 0, "The index must be greater than or equal to 0.")
precondition(index < capacity, "The index must be less than the capacity.")
return buffer[index]
}
}
/**
* A implementation of a queue that limits the number of items stored within
* itself. New items added after the capacity is filled overwrite the last
* item.
*/
// struct CircularQueue<ElementType> {
// private class _QueueData {
// let capacity: Int
// var items: [ElementType] = []
// }
}
\ No newline at end of file
// private let _data = _QueueData()
// private var lastIndex: Int = 0
// init(capacity: Int = 10) {
// self.capacity = capacity
// }
// }
// © 2016 Anarchy Tools Contributors.
// This file is part of atbuild. It is subject to the license terms in the LICENSE
// file found in the top level of this distribution
// No part of atbuild, including this file, may be copied, modified,
// propagated, or distributed except according to the terms contained
// in the LICENSE file.
extension String: ErrorType {}
enum test {
static func assert(condition: Bool, functionName: String = __FUNCTION__) throws {
print(" \(functionName): \(condition ? "PASSED" : "**FAILED**")")
if !condition { throw "clj.tests.failed" }
}
}
class RingBufferTests: Test {
required init() {}
func runTests() {
print("Tests for \(__FILE__)")
let tests = [
testBasicInit,
testInsertion,
testRemove
]
for test in tests {
let _ = try? test()
}
}
func testBasicInit() throws {
let buffer = RingBuffer<Int>(capacity: 10)
try test.assert(buffer.capacity == 10)
}
func testInsertion() throws {
let buffer = RingBuffer<Int>(capacity: 3)
try test.assert(buffer.capacity == 3)
buffer.insert(100)
try test.assert(buffer[0] == 100)
buffer.insert(101)
try test.assert(buffer[1] == 101)
buffer.insert(102)
try test.assert(buffer[2] == 102)
buffer.insert(103)
try test.assert(buffer[0] == 103)
}
func testRemove() throws {
let buffer = RingBuffer<Int>(capacity: 3)
try test.assert(buffer.capacity == 3)
buffer.insert(100)
try test.assert(buffer[0] == 100)
try test.assert(buffer.remove() == 100)
try test.assert(buffer[0] == nil)
try test.assert(buffer[1] == nil)
try test.assert(buffer[2] == nil)
buffer.insert(100)
buffer.insert(101)
try test.assert(buffer[0] == nil)
try test.assert(buffer[1] == 100)
try test.assert(buffer[2] == 101)
try test.assert(buffer.remove() == 100)
try test.assert(buffer.remove() == 101)
try test.assert(buffer.remove() == nil)
try test.assert(buffer.remove() == nil)
buffer.insert(100)
buffer.insert(101)
try test.assert(buffer[0] == 100)
try test.assert(buffer[1] == 101)
try test.assert(buffer[2] == nil)
}
}
\ No newline at end of file
// © 2016 Anarchy Tools Contributors.
// This file is part of atbuild. It is subject to the license terms in the LICENSE
// file found in the top level of this distribution
// No part of atbuild, including this file, may be copied, modified,
// propagated, or distributed except according to the terms contained
// in the LICENSE file.
enum test {
static func assert(condition: Bool, functionName: String = __FUNCTION__) {
print(" \(functionName): \(condition ? "PASSED" : "**FAILED**")")
}
}
class SizedQueueTests {
required init() {}
func runTests() {
print("Tests for \(__FILE__)")
testBasicInit()
}
func testBasicInit() {
let queue = SizedQueue<String>()
test.assert(10 == queue.maximumNumberOfItems)
}
}
\ No newline at end of file
......@@ -10,10 +10,14 @@ protocol Test {
func runTests()
}
let tests = [
SizedQueueTests()
print()
let tests: [Test] = [
RingBufferTests()
]
for test in tests {
test.runTests()
}
print()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment