diff --git a/Sources/NIO/CircularBuffer.swift b/Sources/NIO/CircularBuffer.swift index 4b357e6a0..e2af1ee86 100644 --- a/Sources/NIO/CircularBuffer.swift +++ b/Sources/NIO/CircularBuffer.swift @@ -12,7 +12,15 @@ // //===----------------------------------------------------------------------===// -public struct CircularBuffer: 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: CustomStringConvertible, AppendableCollection { private var buffer: ContiguousArray /* the capacity of the underlying buffer */ @@ -100,15 +108,7 @@ public struct CircularBuffer: 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: 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: CustomStringConvertible { self.buffer[self.bufferIndex(ofIndex: index)] = newValue } } - - public var indices: CountableRange { - return 0..) -> Slice> { + get { + return Slice(base: self, bounds: bounds) + } } + public var indices: CountableRange { + return 0.. 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: 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 } } diff --git a/Sources/NIO/MarkedCircularBuffer.swift b/Sources/NIO/MarkedCircularBuffer.swift index 2158c1dbc..d8620fb01 100644 --- a/Sources/NIO/MarkedCircularBuffer.swift +++ b/Sources/NIO/MarkedCircularBuffer.swift @@ -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: CustomStringConvertible { +public struct MarkedCircularBuffer: CustomStringConvertible, AppendableCollection { private var buffer: CircularBuffer private var markedIndex: Int = -1 /* negative: nothing marked */ @@ -71,6 +71,14 @@ public struct MarkedCircularBuffer: 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 } diff --git a/Tests/NIOTests/CircularBufferTests+XCTest.swift b/Tests/NIOTests/CircularBufferTests+XCTest.swift index 14881a676..d580db24b 100644 --- a/Tests/NIOTests/CircularBufferTests+XCTest.swift +++ b/Tests/NIOTests/CircularBufferTests+XCTest.swift @@ -30,6 +30,7 @@ extension CircularBufferTests { ("testAddRemoveInALoop", testAddRemoveInALoop), ("testAddAllRemoveAll", testAddAllRemoveAll), ("testHarderExpansion", testHarderExpansion), + ("testCollection", testCollection), ] } } diff --git a/Tests/NIOTests/CircularBufferTests.swift b/Tests/NIOTests/CircularBufferTests.swift index 72926d2bd..ec669b86d 100644 --- a/Tests/NIOTests/CircularBufferTests.swift +++ b/Tests/NIOTests/CircularBufferTests.swift @@ -42,41 +42,50 @@ class CircularBufferTests: XCTestCase { func testHarderExpansion() { var ring = CircularBuffer(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(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) } }