CircularArray

public struct CircularArray<T>

A circular array provides most of the features of a standard array such as constant-time random access in addition to amortized constant-time insertion/removal at both ends, instead of just one end. It allows to get or set elements using subscript notation.

Conforms to MutableCollection, ExpressibleByArrayLiteral, Equatable, CustomStringConvertible.

  • Constructs an empty circular array.

    Declaration

    Swift

    public init()
  • Constructs a circular array with a given number of elements, each initialized to the same value.

    Declaration

    Swift

    public init(repeating repeatedValue: T, count: Int)
  • Constructs a circular array from a sequence, such as an array.

    Declaration

    Swift

    public init<S: Sequence>(_ elements: S) where S.Iterator.Element == T
  • Number of elements stored in the circular array.

    Declaration

    Swift

    public var count : Int
  • Returns true if and only if the circular array is empty.

    Declaration

    Swift

    public var isEmpty: Bool
  • The first element, or nil if the circular array is empty.

    Declaration

    Swift

    public var first: T?
  • The last element, or nil if the circular array is empty.

    Declaration

    Swift

    public var last: T?
  • Adds a new item as the first element in an existing circular array.

    Declaration

    Swift

    public mutating func prepend(_ element: T)
  • Adds a new item as the last element in an existing circular array.

    Declaration

    Swift

    public mutating func append(_ element: T)
  • Removes the first element from the circular array and returns it.

    Declaration

    Swift

    public mutating func removeFirst() -> T

    Return Value

    The first element.

  • Removes the last element from the circular array and returns it.

    Declaration

    Swift

    public mutating func removeLast() -> T

    Return Value

    The last element.

  • Inserts an element into the collection at a given index. Use this method to insert a new element anywhere within the range of existing items, or as the last item. The index must be less than or equal to the number of items in the circular array. If you attempt to remove an item at a greater index, you’ll trigger an error.

    Declaration

    Swift

    public mutating func insert(_ element: T, at index: Int)
  • Removes the element at the given index and returns it.

    The index must be less than the number of items in the circular array. If you attempt to remove an item at a greater index, you’ll trigger an error.

    Declaration

    Swift

    public mutating func remove(at index: Int) -> T
  • Removes all the elements from the collection, and by default clears the underlying storage buffer.

    Declaration

    Swift

    public mutating func removeAll(keepingCapacity keep: Bool = false)
  • Provides random access to elements using square bracket notation. The index must be less than the number of items in the circular array. If you attempt to get or set an item at a greater index, you’ll trigger an error.

    Declaration

    Swift

    public subscript(index: Int) -> T
  • Always zero, which is the index of the first element when non-empty.

    Declaration

    Swift

    public var startIndex : Int
  • Always count, which is the successor of the last valid subscript argument.

    Declaration

    Swift

    public var endIndex : Int
  • Returns the position immediately after the given index.

    Parameter

    Parameter i: A valid index of the collection. i must be less than endIndex.

    Returns

    The index value immediately after i.

    Declaration

    Swift

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

    Parameters

    i

    A valid index of the collection. i must be less than endIndex.

    Return Value

    The index value immediately after i.

  • Constructs a circular array using an array literal. let example: CircularArray<Int> = [1,2,3]

    Declaration

    Swift

    public init(arrayLiteral elements: T...)
  • A string containing a suitable textual representation of the circular array.

    Declaration

    Swift

    public var description: String