A collection that supports efficient random-access index traversal.
protocol RandomAccessCollection<Element> : BidirectionalCollection where Self.Indices : RandomAccessCollection, Self.SubSequence : RandomAccessCollection
Random-access collections can move indices any distance and measure the distance between indices in O(1) time. Therefore, the fundamental difference between random-access and bidirectional collections is that operations that depend on index movement or distance measurement offer significantly improved efficiency. For example, a random-access collection’s
count property is calculated in O(1) instead of requiring iteration of an entire collection.
Conforming to the RandomAccessCollection Protocol
RandomAccessCollection protocol adds further constraints on the associated
SubSequence types, but otherwise imposes no additional requirements over the
BidirectionalCollection protocol. However, in order to meet the complexity guarantees of a random-access collection, either the index for your custom type must conform to the
Strideable protocol or you must implement the
distance(from:to:) methods with O(1) efficiency.
A collection that supports backward as well as forward traversal.
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
A type that provides sequential, iterated access to its elements.
var endIndex: Self.Index
var indices: Self.Indices
The indices that are valid for subscripting the collection, in ascending order.
var startIndex: Self.Index
<Self.Index>) -> Self.SubSequence
Accesses a contiguous subrange of the collection’s elements.
.Index) -> Self.Element
func distance(from: Self.Index, to: Self.Index
) -> Int
Returns the distance between two indices.
func formIndex(after: inout Self.Index
Replaces the given index with its successor.
func formIndex(before: inout Self.Index
Replaces the given index with its predecessor.
.Index, offsetBy: Int) -> Self.Index
Returns an index that is the specified distance from the given index.
.Index, offsetBy: Int, limitedBy: Self.Index) -> Self.Index?
Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
func index(after: Self.Index
) -> Self.Index
Returns the position immediately after the given index.
func index(before: Self.Index
) -> Self.Index
Returns the position immediately before the given index.