Make our circular buffers collections

This commit is contained in:
Cory Benfield committed 2017-11-16 12:56:29 +00:00
1 parent 31cd14eaad
commit 4cf0bbaaf4
4 files changed
+86 -16

No files matched your search

+45 -15
View File
@@ -12,7 +12,15 @@
//
//===----------------------------------------------------------------------===//
public struct CircularBuffer<E>: CustomStringConvertible {
/// AppendableCollection is a protocol partway between Collection and
/// RangeReplaceableCollection. It defines the append method that is present
/// on RangeReplaceableCollection, which makes all RangeReplaceableCollections
/// trivially able to implement this protocol.
public protocol AppendableCollection: Collection {
mutating func append(_ newElement: Self.Iterator.Element)
}
public struct CircularBuffer<E>: CustomStringConvertible, AppendableCollection {
private var buffer: ContiguousArray<E?>
/* the capacity of the underlying buffer */
@@ -100,15 +108,7 @@ public struct CircularBuffer<E>: CustomStringConvertible {
return self.buffer[self.startIdx]
}
}
public var isEmpty: Bool {
return self.bufferLength == 0
}
public var count: Int {
return self.bufferLength
}
private func bufferIndex(ofIndex index: Int) -> Int {
if index < self.ringLength {
return (self.startIdx + index) % self.ringCapacity
@@ -116,7 +116,8 @@ public struct CircularBuffer<E>: CustomStringConvertible {
return self.ringCapacity + index - ringLength
}
}
// MARK: Collection implementation
public subscript(index: Int) -> E {
get {
return self.buffer[self.bufferIndex(ofIndex: index)]!
@@ -125,11 +126,40 @@ public struct CircularBuffer<E>: CustomStringConvertible {
self.buffer[self.bufferIndex(ofIndex: index)] = newValue
}
}
public var indices: CountableRange<Int> {
return 0..<self.ringLength
public subscript(bounds: Range<Int>) -> Slice<CircularBuffer<E>> {
get {
return Slice(base: self, bounds: bounds)
}
}
public var indices: CountableRange<Int> {
return 0..<self.bufferLength
}
public var isEmpty: Bool {
return self.bufferLength == 0
}
public var count: Int {
return self.bufferLength
}
public var startIndex: Int {
return 0
}
public var endIndex: Int {
return self.bufferLength
}
public func index(after: Int) -> Int {
let nextIndex = after + 1
precondition(nextIndex <= endIndex)
return nextIndex
}
// MARK: CustomStringConvertible implementation
public var description: String {
var desc = "[ "
for el in self.buffer.enumerated() {
@@ -141,7 +171,7 @@ public struct CircularBuffer<E>: CustomStringConvertible {
desc += el.1.map { "\($0) " } ?? "_ "
}
desc += "]"
desc += " (bufferCapacity: \(self.bufferCapacity), bufferLength: \(self.bufferLength), ringCapacityacity: \(self.ringCapacity), ringLength: \(self.ringLength))"
desc += " (bufferCapacity: \(self.bufferCapacity), bufferLength: \(self.bufferLength), ringCapacity: \(self.ringCapacity), ringLength: \(self.ringLength))"
return desc
}
}
+9 -1
View File
@@ -17,7 +17,7 @@
/// This object is used extensively within SwiftNIO to handle flushable buffers. It can be used to store buffered
/// writes and mark how far through the buffer the user has flushed, and therefore how far through the buffer is
/// safe to write.
public struct MarkedCircularBuffer<E>: CustomStringConvertible {
public struct MarkedCircularBuffer<E>: CustomStringConvertible, AppendableCollection {
private var buffer: CircularBuffer<E>
private var markedIndex: Int = -1 /* negative: nothing marked */
@@ -71,6 +71,14 @@ public struct MarkedCircularBuffer<E>: CustomStringConvertible {
return self.buffer.indices
}
public var startIndex: Int { return self.buffer.startIndex }
public var endIndex: Int { return self.buffer.startIndex }
public func index(after i: Int) -> Int {
return self.buffer.index(after: i)
}
public var description: String {
return self.buffer.description
}
@@ -30,6 +30,7 @@ extension CircularBufferTests {
("testAddRemoveInALoop", testAddRemoveInALoop),
("testAddAllRemoveAll", testAddAllRemoveAll),
("testHarderExpansion", testHarderExpansion),
("testCollection", testCollection),
]
}
}
+31
View File
@@ -42,41 +42,50 @@ class CircularBufferTests: XCTestCase {
func testHarderExpansion() {
var ring = CircularBuffer<Int>(initialRingCapacity: 3, expandSize: 3)
XCTAssertEqual(ring.indices, 0..<0)
ring.append(1)
XCTAssertEqual(ring.count, 1)
XCTAssertEqual(ring[0], 1)
XCTAssertEqual(ring.indices, 0..<1)
ring.append(2)
XCTAssertEqual(ring.count, 2)
XCTAssertEqual(ring[0], 1)
XCTAssertEqual(ring[1], 2)
XCTAssertEqual(ring.indices, 0..<2)
ring.append(3)
XCTAssertEqual(ring.count, 3)
XCTAssertEqual(ring[0], 1)
XCTAssertEqual(ring[1], 2)
XCTAssertEqual(ring[2], 3)
XCTAssertEqual(ring.indices, 0..<3)
XCTAssertEqual(1, ring.removeFirst())
XCTAssertEqual(ring.count, 2)
XCTAssertEqual(ring[0], 2)
XCTAssertEqual(ring[1], 3)
XCTAssertEqual(ring.indices, 0..<2)
XCTAssertEqual(2, ring.removeFirst())
XCTAssertEqual(ring.count, 1)
XCTAssertEqual(ring[0], 3)
XCTAssertEqual(ring.indices, 0..<1)
ring.append(5)
XCTAssertEqual(ring.count, 2)
XCTAssertEqual(ring[0], 3)
XCTAssertEqual(ring[1], 5)
XCTAssertEqual(ring.indices, 0..<2)
ring.append(6)
XCTAssertEqual(ring.count, 3)
XCTAssertEqual(ring[0], 3)
XCTAssertEqual(ring[1], 5)
XCTAssertEqual(ring[2], 6)
XCTAssertEqual(ring.indices, 0..<3)
ring.append(7)
XCTAssertEqual(ring.count, 4)
@@ -84,5 +93,27 @@ class CircularBufferTests: XCTestCase {
XCTAssertEqual(ring[1], 5)
XCTAssertEqual(ring[2], 6)
XCTAssertEqual(ring[3], 7)
XCTAssertEqual(ring.indices, 0..<4)
}
func testCollection() {
var ring = CircularBuffer<Int>(initialRingCapacity: 4, expandSize: 4)
XCTAssertEqual(ring.indices, 0..<0)
XCTAssertEqual(ring.startIndex, 0)
XCTAssertEqual(ring.endIndex, 0)
for idx in 0..<5 {
ring.append(idx)
}
XCTAssertEqual(ring.indices, 0..<5)
XCTAssertEqual(ring.startIndex, 0)
XCTAssertEqual(ring.endIndex, 5)
XCTAssertEqual(ring.index(after: 1), 2)
let actualValues = [Int](ring)
let expectedValues = [0, 1, 2, 3, 4]
XCTAssertEqual(expectedValues, actualValues)
}
}