CircularArray

public struct CircularArray<Element>
extension CircularArray: RandomAccessCollectionWrapping
extension CircularArray: BidirectionalCollection
extension CircularArray: RangeReplaceableCollection
extension CircularArray: ExpressibleByArrayLiteral
extension CircularArray: Equatable where Element: Equatable

Array-like structure that allows retrieval of elements at indices outside of the bounds of the internal storage.

Example Usage

let loop: CircularArray = [0,1,2,3,4,5]
loop[circular: 6] // => 0
loop[from: 2, through: 7] // => [2,3,4,5,0,1]

Initializers

  • Creates a CircularArray with an sequence.

    Declaration

    Swift

    public init<S>(_ storage: S) where Element == S.Element, S : Sequence

Instance Methods

  • Declaration

    Swift

    public subscript(circular index: Int) -> Element { get }

    Return Value

    Element at the given logical index.

  • Note

    If the real start index is greater than the real end index (for the given logical indices), the resultant array will loop around the back, to the front of the internal storage.

    Declaration

    Swift

    public subscript(from start: Int, through end: Int) -> [Element] { get }

    Return Value

    Array of elements from (and including) the given logical start index through (and including) the given logical end index.

  • Note

    If the real start index is greater than the real end index (for the given logical indices), the resultant array will loop around the back, to the front of the internal storage.

    Declaration

    Swift

    public subscript(after start: Int, upTo end: Int) -> [Element] { get }

    Return Value

    Array of elements after (not including) the given logical start index up to (not including) the given logical end index.

  • Declaration

    Swift

    public func sorted(by areInIncreasingOrder: (Element, Element) -> Bool) -> CircularArray

    Return Value

    A sorted copy of CircularArray.

RandomAccessCollectionWrapping

  • Declaration

    Swift

    public var base: [Element] { get }

    Return Value

    The underlying, non-circular contiguous storage of elements.

BidirectionalCollection

  • Start index.

    Declaration

    Swift

    public var startIndex: Int { get }
  • End index.

    Declaration

    Swift

    public var endIndex: Int { get }
  • Element at index.

    Declaration

    Swift

    public subscript(index: Int) -> Element { get }
  • Index after given i.

    Declaration

    Swift

    public func index(after i: Int) -> Int
  • Index before given i.

    Declaration

    Swift

    public func index(before i: Int) -> Int

RangeReplaceableCollection

  • Replaces the specified subrange of elements with the given collection.

    This method has the effect of removing the specified range of elements from the collection and inserting the new elements at the same location. The number of new elements need not match the number of elements being removed.

    In this example, three elements in the middle of an array of integers are replaced by the five elements of a Repeated<Int> instance.

     var nums = [10, 20, 30, 40, 50]
     nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
     print(nums)
     // Prints "[10, 1, 1, 1, 1, 1, 50]"
    

    If you pass a zero-length range as the subrange parameter, this method inserts the elements of newElements at subrange.startIndex. Calling the insert(contentsOf:at:) method instead is preferred.

    Likewise, if you pass a zero-length collection as the newElements parameter, this method removes the elements in the given subrange without replacement. Calling the removeSubrange(_:) method instead is preferred.

    Calling this method may invalidate any existing indices for use with this collection.

    Complexity

    O(m), where m is the combined length of the collection and newElements. If the call to replaceSubrange simply appends the contents of newElements to the collection, the complexity is O(n), where n is the length of newElements.

    Declaration

    Swift

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

    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.

  • Creates an empty CircularArray.

    Declaration

    Swift

    public init()

ExpressibleByArrayLiteral

  • Creates a CircularArray with an array literal.

    Declaration

    Swift

    public init(arrayLiteral elements: Element...)