CircularBuffer
public struct CircularBuffer<Element> : CustomStringConvertible
extension CircularBuffer: Collection, MutableCollection
extension CircularBuffer: RandomAccessCollection
extension CircularBuffer: RangeReplaceableCollection
extension CircularBuffer: ExpressibleByArrayLiteral
extension CircularBuffer: Equatable where Element: Equatable
extension CircularBuffer: Hashable where Element: Hashable
An automatically expanding ring buffer implementation backed by a ContiguousArray
. Even though this implementation
will automatically expand if more elements than initialCapacity
are stored, it’s advantageous to prevent
expansions from happening frequently. Expansions will always force an allocation and a copy to happen.
-
An opaque
CircularBuffer
index.You may get indices offset from other indices by using
CircularBuffer.index(:offsetBy:)
,CircularBuffer.index(before:)
, orCircularBuffer.index(after:)
.Note
Every index is invalidated as soon as you perform a length-changing operating on theCircularBuffer
but remains valid when you replace one item by another using the subscript.Declaration
Swift
public struct Index : Comparable
-
Declaration
Swift
public typealias Element = Element
-
Declaration
Swift
public typealias Indices = DefaultIndices<CircularBuffer<Element>>
-
Undocumented
Declaration
Swift
public typealias RangeType<Bound> = Range<Bound> where Bound : Strideable, Bound.Stride : SignedInteger
-
Declaration
Swift
public typealias SubSequence = CircularBuffer<Element>
-
Returns the position immediately after the given index.
The successor of an index must be well defined. For an index
i
into a collectionc
, callingc.index(after: i)
returns the same index every time.Parameters
i
A valid index of the collection.
i
must be less thanendIndex
.Return Value
The index value immediately after
i
. -
Returns the index before
index
. -
Accesses the element at the specified index.
You can subscript
CircularBuffer
with any valid index other than theCircularBuffer
‘s end index. The end index refers to the position one past the last element of a collection, so it doesn’t correspond with an element.Complexity
O(1)
Parameters
position
The position of the element to access.
position
must be a valid index of the collection that is not equal to theendIndex
property. -
The
CircularBuffer
‘s “past the end” position—that is, the position one greater than the last valid subscript argument.When you need a range that includes the last element of a collection, use the half-open range operator (
..<
) withendIndex
. The..<
operator creates a range that doesn’t include the upper bound, so it’s always safe to use withendIndex
.If the
CircularBuffer
is empty,endIndex
is equal tostartIndex
.Declaration
Swift
@inlinable public var endIndex: Index { get }
-
Returns the distance between two indices.
Unless the collection conforms to the
BidirectionalCollection
protocol,start
must be less than or equal toend
.Complexity
O(1) if the collection conforms to
RandomAccessCollection
; otherwise, O(k), where k is the resulting distance.Declaration
Parameters
start
A valid index of the collection.
end
Another valid index of the collection. If
end
is equal tostart
, the result is zero.Return Value
The distance between
start
andend
. The result can be negative only if the collection conforms to theBidirectionalCollection
protocol.
-
Returns the index offset by
distance
fromindex
.The following example obtains an index advanced four positions from a string’s starting index and then prints the character at that position.
let s = "Swift" let i = s.index(s.startIndex, offsetBy: 4) print(s[i]) // Prints "t"
The value passed as
distance
must not offseti
beyond the bounds of the collection.Complexity
O(1) if the collection conforms to
RandomAccessCollection
; otherwise, O(k), where k is the absolute value ofdistance
.Parameters
i
A valid index of the collection.
distance
The distance to offset
i
.distance
must not be negative unless the collection conforms to theBidirectionalCollection
protocol.Return Value
An index offset by
distance
from the indexi
. Ifdistance
is positive, this is the same value as the result ofdistance
calls toindex(after:)
. Ifdistance
is negative, this is the same value as the result ofabs(distance)
calls toindex(before:)
. -
Declaration
Swift
@inlinable public subscript(bounds: Range<Index>) -> SubSequence { get set }
-
Allocates a buffer that can hold up to
initialCapacity
elements and initialise an empty ring backed by the buffer. When the ring grows to more thaninitialCapacity
elements the buffer will be expanded.Declaration
Swift
@inlinable public init(initialCapacity: Int)
-
Allocates an empty buffer.
Declaration
Swift
@inlinable public init()
-
Append an element to the end of the ring buffer.
Amortized O(1)
Declaration
Swift
@inlinable public mutating func append(_ value: Element)
-
Prepend an element to the front of the ring buffer.
Amortized O(1)
Declaration
Swift
@inlinable public mutating func prepend(_ value: Element)
-
Return element
offset
from first element.O(1)
Declaration
Swift
@inlinable public subscript(offset offset: Int) -> Element { get set }
-
Returns whether the ring is empty.
Declaration
Swift
@inlinable public var isEmpty: Bool { get }
-
Returns the number of element in the ring.
Declaration
Swift
@inlinable public var count: Int { get }
-
The total number of elements that the ring can contain without allocating new storage.
Declaration
Swift
@inlinable public var capacity: Int { get }
-
Removes all members from the circular buffer whist keeping the capacity.
Declaration
Swift
@inlinable public mutating func removeAll(keepingCapacity: Bool = false)
-
Modify the element at
index
.This function exists to provide a method of modifying the element in its underlying backing storage, instead of copying it out, modifying it, and copying it back in. This emulates the behaviour of the
_modify
accessor that is part of the generalized accessors work. That accessor is currently underscored and not safe to use, so this is the next best thing.Note that this function is not guaranteed to be fast. In particular, as it is both generic and accepts a closure it is possible that it will be slower than using the get/modify/set path that occurs with the subscript. If you are interested in using this function for performance you must test and verify that the optimisation applies correctly in your situation.
Declaration
Parameters
index
The index of the object that should be modified. If this index is invalid this function will trap.
modifyFunc
The function to apply to the modified object.
-
Returns a human readable description of the ring.
Declaration
Swift
public var description: String { get }
-
Removes and returns the first element of the
CircularBuffer
.Calling this method may invalidate all saved indices of this
CircularBuffer
. Do not rely on a previously stored index value after altering aCircularBuffer
with any operation that can change its length.Complexity
O(1)
Declaration
Swift
@inlinable public mutating func popFirst() -> Element?
Return Value
The first element of the
CircularBuffer
if theCircularBuffer
is not empty; otherwise,nil
. -
Removes and returns the last element of the
CircularBuffer
.Calling this method may invalidate all saved indices of this
CircularBuffer
. Do not rely on a previously stored index value after altering aCircularBuffer
with any operation that can change its length.Complexity
O(1)
Declaration
Swift
@inlinable public mutating func popLast() -> Element?
Return Value
The last element of the
CircularBuffer
if theCircularBuffer
is not empty; otherwise,nil
. -
Removes the specified number of elements from the end of the
CircularBuffer
.Attempting to remove more elements than exist in the
CircularBuffer
triggers a runtime error.Calling this method may invalidate all saved indices of this
CircularBuffer
. Do not rely on a previously stored index value after altering aCircularBuffer
with any operation that can change its length.Complexity
O(k), where k is the specified number of elements.
Declaration
Swift
@inlinable public mutating func removeLast(_ k: Int)
Parameters
k
The number of elements to remove from the
CircularBuffer
.k
must be greater than or equal to zero and must not exceed the number of elements in theCircularBuffer
. -
Removes the specified number of elements from the beginning of the
CircularBuffer
.Calling this method may invalidate any existing indices for use with this
CircularBuffer
.Complexity
O(k), where k is the specified number of elements.
Declaration
Swift
@inlinable public mutating func removeFirst(_ k: Int)
Parameters
k
The number of elements to remove.
k
must be greater than or equal to zero and must not exceed the number of elements in theCircularBuffer
. -
Removes and returns the first element of the
CircularBuffer
.The
CircularBuffer
must not be empty.Calling this method may invalidate any existing indices for use with this
CircularBuffer
.Complexity
O(1)
Declaration
Swift
@discardableResult @inlinable public mutating func removeFirst() -> Element
Return Value
The removed element.
-
Removes and returns the last element of the
CircularBuffer
.The
CircularBuffer
must not be empty.Calling this method may invalidate all saved indices of this
CircularBuffer
. Do not rely on a previously stored index value after altering theCircularBuffer
with any operation that can change its length.Complexity
O(1)
Declaration
Swift
@discardableResult @inlinable public mutating func removeLast() -> Element
Return Value
The last element of the
CircularBuffer
. -
Replaces the specified subrange of elements with the given
CircularBuffer
.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
Parameters
subrange
The subrange of the collection to replace. The bounds of the range must be valid indices of the
CircularBuffer
.newElements
The new elements to add to the
CircularBuffer
. -
Removes the elements in the specified subrange from the circular buffer.
Declaration
Swift
@inlinable public mutating func removeSubrange(_ bounds: Range<Index>)
Parameters
bounds
The range of the circular buffer to be removed. The bounds of the range must be valid indices of the collection.
-
Removes & returns the item at
position
from the bufferO(1) if the position is
headIdx
ortailIdx
. otherwise O(n) where n is the number of elements betweenposition
andtailIdx
.Declaration
Parameters
position
The index of the item to be removed from the buffer.
-
Prepares the
CircularBuffer
to store the specified number of elements.Declaration
Swift
@inlinable public mutating func reserveCapacity(_ minimumCapacity: Int)
-
Declaration
Swift
public init(arrayLiteral elements: Element...)
-
Declaration
Swift
public static func == (lhs: CircularBuffer, rhs: CircularBuffer) -> Bool
-
Declaration
Swift
public func hash(into hasher: inout Hasher)