CircularBuffer

public struct CircularBuffer<E> : CustomStringConvertible, AppendableCollection

An automatically expanding ring buffer implementation backed by a ContiguousArray. Even though this implementation will automatically expand if more elements than initialRingCapacity are stored, it’s advantageous to prevent expansions from happening frequently. Expansions will always force an allocation and a copy to happen.

  • Allocates a buffer that can hold up to initialRingCapacity elements and initialise an empty ring backed by the buffer. When the ring grows to more than initialRingCapacity elements the buffer will be expanded.

    Declaration

    Swift

    public init(initialRingCapacity: Int)
  • Allocates an empty buffer.

    Declaration

    Swift

    public init()
  • Append an element to the end of the ring buffer.

    Amortized O(1)

    Declaration

    Swift

    public mutating func append(_ value: E)
  • Prepend an element to the front of the ring buffer.

    Amortized O(1)

    Declaration

    Swift

    public mutating func prepend(_ value: E)
  • Return element index of the ring.

    O(1)

    Declaration

    Swift

    public subscript(index: Int) -> E { get set }
  • Return all valid indices of the ring.

    Declaration

    Swift

    public var indices: RangeType<Int> { get }
  • Returns whether the ring is empty.

    Declaration

    Swift

    public var isEmpty: Bool { get }
  • Returns the number of element in the ring.

    Declaration

    Swift

    public var count: Int { get }
  • The total number of elements that the ring can contain without allocating new storage.

    Declaration

    Swift

    public var capacity: Int { get }
  • Returns the index of the first element of the ring.

    Declaration

    Swift

    public var startIndex: Int { get }
  • Returns the ring’s past the end position – that is, the position one greater than the last valid subscript argument.

    Declaration

    Swift

    public var endIndex: Int { get }
  • Returns the next index after index.

    Declaration

    Swift

    public func index(after: Int) -> Int
  • Returns the index before index.

    Declaration

    Swift

    public func index(before: Int) -> Int
  • Removes all members from the circular buffer whist keeping the capacity.

    Declaration

    Swift

    public mutating func removeAll(keepingCapacity: Bool = false)
  • Returns a human readable description of the ring.

    Declaration

    Swift

    public var description: String { get }
  • Replaces the specified subrange of elements with the given collection.

    O(n) where n is the length of the new elements collection if the subrange equals to n

    O(m) where m is the combined length of the collection and newElements

    Declaration

    Swift

    public mutating func replaceSubrange<C>(_ subrange: Range<Index>, with newElements: C) where E == C.Element, C : Collection

    Parameters

    subrange

    The subrange of the collection to replace. The bounds of the range must be valid indices of the collection.

    newElements

    The new elements to add to the collection.

  • Removes the elements in the specified subrange from the circular buffer.

    Declaration

    Swift

    public mutating func removeSubrange(_ bounds: Range<Int>)

    Parameters

    bounds

    The range of the circular buffer to be removed. The bounds of the range must be valid indices of the collection.

  • Removes the given number of elements from the end of the collection.

    Declaration

    Swift

    public mutating func removeLast(_ n: Int)

    Parameters

    n

    The number of elements to remove from the tail of the buffer.

  • Removes & returns the item at position from the buffer

    O(1) if the position is headIdx or tailIdx. otherwise O(n) where n is the number of elements between position and tailIdx.

    Declaration

    Swift

    @discardableResult
    public mutating func remove(at position: Int) -> E

    Parameters

    position

    The index of the item to be removed from the buffer.