|
| 1 | +// |
| 2 | +// CircularBuffer.swift |
| 3 | +// XCombine |
| 4 | +// |
| 5 | +// Created by Serge Bouts on 10/12/19. |
| 6 | +// Copyright © 2019 Serge Bouts. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +/// Circular buffer errors. |
| 12 | +enum CircularBufferError: Error, Equatable { |
| 13 | + case invalidCapacity |
| 14 | + case overflow |
| 15 | + case isEmpty |
| 16 | + case outOfRange |
| 17 | +} |
| 18 | + |
| 19 | +/// A circular buffer implementation. |
| 20 | +/// |
| 21 | +/// See also: [Circular buffer](https://en.wikipedia.org/wiki/Circular_buffer) |
| 22 | +struct CircularBuffer<Element> { |
| 23 | + // MARK: - Properties |
| 24 | + |
| 25 | + private var data: [Element] |
| 26 | + |
| 27 | + private var head: Int = 0 |
| 28 | + |
| 29 | + private let lock = NSLock() |
| 30 | + |
| 31 | + // MARK: - Initialization |
| 32 | + |
| 33 | + /// Creates an instance with the buffer of `capacity` elements size. |
| 34 | + /// |
| 35 | + /// - Parameter capacity: The buffer's capacity. |
| 36 | + /// - Throws: `CircularBufferError.invalidCapacity` if the capacity value is wrong. |
| 37 | + public init(capacity: Int) throws { |
| 38 | + guard capacity > 0 else { throw CircularBufferError.invalidCapacity } |
| 39 | + |
| 40 | + self.capacity = capacity |
| 41 | + self.data = [] |
| 42 | + // `Int.max` capacity value is a special case, for which we don't reserve capacity at all. |
| 43 | + if capacity < Int.max { |
| 44 | + data.reserveCapacity(capacity) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // MARK: - API |
| 49 | + |
| 50 | + /// The buffer's capacity. |
| 51 | + private(set) var capacity: Int |
| 52 | + |
| 53 | + /// The buffer's current size. |
| 54 | + private(set) var count = 0 |
| 55 | + |
| 56 | + /// Returns the index'th element if the index is not out of range; |
| 57 | + /// returns `nil` otherwise. |
| 58 | + subscript(safe index: Int) -> Element? { |
| 59 | + lock.lock() |
| 60 | + defer { lock.unlock() } |
| 61 | + |
| 62 | + guard index >= 0 && index < count else { return nil } |
| 63 | + |
| 64 | + let index = (head + index) % capacity |
| 65 | + |
| 66 | + return data[index] |
| 67 | + } |
| 68 | + |
| 69 | + /// Returns the index'th element if the index is correct; |
| 70 | + /// throws otherwise. |
| 71 | + /// |
| 72 | + /// - Parameter index: The element's index. |
| 73 | + /// - Throws: `CircularBufferError.outOfRange` if the index is out of range. |
| 74 | + /// - Returns: An element if the index is correct. |
| 75 | + func get(at index: Int) throws -> Element { |
| 76 | + guard let result = self[safe: index] else { throw CircularBufferError.outOfRange } |
| 77 | + return result |
| 78 | + } |
| 79 | + |
| 80 | + /// Appends an element at the end of the buffer if the buffer is not full; |
| 81 | + /// throws otherwise. |
| 82 | + /// |
| 83 | + /// - Parameter element: The element to append. |
| 84 | + /// - Throws: `CircularBufferError.overflow` if the buffer if full. |
| 85 | + mutating func append(_ element: Element) throws { |
| 86 | + lock.lock() |
| 87 | + defer { lock.unlock() } |
| 88 | + |
| 89 | + guard !isFull else { throw CircularBufferError.overflow } |
| 90 | + |
| 91 | + if data.count < capacity { |
| 92 | + data.append(element) |
| 93 | + } else { |
| 94 | + data[(head + count) % capacity] = element |
| 95 | + } |
| 96 | + |
| 97 | + count += 1 |
| 98 | + } |
| 99 | + |
| 100 | + /// Removes the first element from the buffer if the buffer is not empty; |
| 101 | + /// throws otherwise. |
| 102 | + /// |
| 103 | + /// - Throws: `CircularBufferError.isEmpty` if the buffer is empty. |
| 104 | + mutating func removeFirst() throws { |
| 105 | + lock.lock() |
| 106 | + defer { lock.unlock() } |
| 107 | + |
| 108 | + guard count > 0 else { throw CircularBufferError.isEmpty } |
| 109 | + |
| 110 | + head = (head + 1) % capacity |
| 111 | + count -= 1 |
| 112 | + } |
| 113 | + |
| 114 | + /// Returns `true` if the buffer if empty; |
| 115 | + /// `false` otherwise. |
| 116 | + var isEmpty: Bool { |
| 117 | + count == 0 |
| 118 | + } |
| 119 | + |
| 120 | + /// Returns `true` if the buffer if full; |
| 121 | + /// `false` otherwise. |
| 122 | + var isFull: Bool { |
| 123 | + assert(count <= capacity) |
| 124 | + return count == capacity |
| 125 | + } |
| 126 | + |
| 127 | + /// Returns the number of elements, that can yet be appended. |
| 128 | + var freeSpace: Int { |
| 129 | + return capacity - count |
| 130 | + } |
| 131 | +} |
0 commit comments