AnyRandomAccessCollection

struct AnyRandomAccessCollection

A type-erased wrapper over any collection with indices that support random access traversal.

Inheritance RandomAccessCollection
Associated Types
public typealias Indices = DefaultIndices<AnyRandomAccessCollection<Element>>
public typealias Iterator = AnyIterator<Element>

By default, a collection conforms to the Sequence protocol by supplying IndexingIterator as its associated Iterator type.

public typealias Index = AnyIndex

Valid indices consist of the position of every element and a "past the end" position that's not valid for use as a subscript argument.

public typealias SubSequence = AnyRandomAccessCollection<Element>

This associated type appears as a requirement in the Sequence protocol, but it is restated here with stricter constraints. In a collection, the subsequence should also conform to Collection.

An AnyRandomAccessCollection instance forwards its operations to a base collection having the same Element type, hiding the specifics of the underlying collection.

Initializers

init init(_:) Required

Creates a type-erased collection that wraps the given collection.

  • Parameter base: The collection to wrap.

Complexity: O(1).

Declaration

@inlinable public init<C>(_ base: C) where Element == C.Element, C: RandomAccessCollection
init init(_:) Required

Creates an AnyRandomAccessCollection having the same underlying collection as other.

Complexity: O(1)

Declaration

@inlinable public init(_ other: AnyRandomAccessCollection<Element>)
init init?(_:) Required

Creates an AnyRandomAccessCollection having the same underlying collection as other.

If the underlying collection stored by other does not satisfy RandomAccessCollection, the result is nil.

Complexity: O(1)

Declaration

@inlinable public init?(_ other: AnyCollection<Element>)
init init?(_:) Required

Creates an AnyRandomAccessCollection having the same underlying collection as other.

If the underlying collection stored by other does not satisfy RandomAccessCollection, the result is nil.

Complexity: O(1)

Declaration

@inlinable public init?(_ other: AnyBidirectionalCollection<Element>)

Instance Variables

var count Required

The number of elements.

Complexity: O(1)

Declaration

var count: Int
var endIndex Required

The collection's "past the end" position---that is, the position one greater than the last valid subscript argument.

endIndex is always reachable from startIndex by zero or more applications of index(after:).

Declaration

var endIndex: AnyRandomAccessCollection<Element>.Index
var startIndex Required

The position of the first element in a non-empty collection.

In an empty collection, startIndex == endIndex.

Declaration

var startIndex: AnyRandomAccessCollection<Element>.Index
var underestimatedCount Required

A value less than or equal to the number of elements in the sequence, calculated nondestructively.

The default implementation returns 0. If you provide your own implementation, make sure to compute the value nondestructively.

Complexity: O(1), except if the sequence also conforms to Collection. In this case, see the documentation of Collection.underestimatedCount.

Declaration

var underestimatedCount: Int

Subscripts

subscript subscript(bounds:) Required

Accesses a contiguous subrange of the collection's elements.

The accessed slice uses the same indices for the same elements as the original collection uses. Always use the slice's startIndex property instead of assuming that its indices start at a particular value.

This example demonstrates getting a slice of an array of strings, finding the index of one of the strings in the slice, and then using that index in the original array.

let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
let streetsSlice = streets[2 ..< streets.endIndex]
print(streetsSlice)
// Prints "["Channing", "Douglas", "Evarts"]"

let index = streetsSlice.firstIndex(of: "Evarts")    // 4
print(streets[index!])
// Prints "Evarts"
  • Parameter bounds: A range of the collection's indices. The bounds of the range must be valid indices of the collection.

Complexity: O(1)

Declaration

@inlinable public subscript(bounds: Range<AnyRandomAccessCollection<Element>.Index>) -> AnyRandomAccessCollection<Element>.SubSequence
subscript subscript(position:) Required

Accesses the element indicated by position.

Precondition: position indicates a valid position in self and position != endIndex.

Declaration

@inlinable public subscript(position: AnyRandomAccessCollection<Element>.Index) -> Element

Instance Methods

func distance(from start: AnyRandomAccessCollection<Element>.Index, to end: AnyRandomAccessCollection<Element>.Index) -> Int Required

Returns the distance between two indices.

Unless the collection conforms to the BidirectionalCollection protocol, start must be less than or equal to end.

Complexity: O(1)

Declaration

@inlinable public func distance(from start: AnyRandomAccessCollection<Element>.Index, to end: AnyRandomAccessCollection<Element>.Index) -> Int
func drop(while predicate: (Element) throws -> Bool) rethrows -> AnyRandomAccessCollection<Element> Required

Declaration

@inlinable public func drop(while predicate: (Element) throws -> Bool) rethrows -> AnyRandomAccessCollection<Element>
func dropFirst(_ n: Int = 1) -> AnyRandomAccessCollection<Element> Required

Declaration

@inlinable public func dropFirst(_ n: Int = 1) -> AnyRandomAccessCollection<Element>
func dropLast(_ n: Int = 1) -> AnyRandomAccessCollection<Element> Required

Declaration

@inlinable public func dropLast(_ n: Int = 1) -> AnyRandomAccessCollection<Element>
func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element] Required

Declaration

@inlinable public func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element]
func forEach(_ body: (Element) throws -> Void) rethrows Required

Declaration

@inlinable public func forEach(_ body: (Element) throws -> Void) rethrows
func formIndex(_ i: inout AnyRandomAccessCollection<Element>.Index, offsetBy n: Int) Required

Declaration

@inlinable public func formIndex(_ i: inout AnyRandomAccessCollection<Element>.Index, offsetBy n: Int)
func formIndex(_ i: inout AnyRandomAccessCollection<Element>.Index, offsetBy n: Int, limitedBy limit: AnyRandomAccessCollection<Element>.Index) -> Bool Required

Declaration

@inlinable public func formIndex(_ i: inout AnyRandomAccessCollection<Element>.Index, offsetBy n: Int, limitedBy limit: AnyRandomAccessCollection<Element>.Index) -> Bool
func formIndex(after i: inout AnyRandomAccessCollection<Element>.Index) Required

Replaces the given index with its successor.

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

Declaration

@inlinable public func formIndex(after i: inout AnyRandomAccessCollection<Element>.Index)
func formIndex(before i: inout AnyRandomAccessCollection<Element>.Index) Required

Replaces the given index with its predecessor.

  • Parameter i: A valid index of the collection. i must be greater than startIndex.

Declaration

@inlinable public func formIndex(before i: inout AnyRandomAccessCollection<Element>.Index)
func index(_ i: AnyRandomAccessCollection<Element>.Index, offsetBy n: Int) -> AnyRandomAccessCollection<Element>.Index Required

Returns an index that is the specified distance from the given index.

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 offset i beyond the bounds of the collection.

Complexity: O(1)

Declaration

@inlinable public func index(_ i: AnyRandomAccessCollection<Element>.Index, offsetBy n: Int) -> AnyRandomAccessCollection<Element>.Index
func index(_ i: Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Self.Index? Required

Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.

The following example obtains an index advanced four positions from an array's starting index and then prints the element at that position. The operation doesn't require going beyond the limiting numbers.endIndex value, so it succeeds.

let numbers = [10, 20, 30, 40, 50]
let i = numbers.index(numbers.startIndex, offsetBy: 4)
print(numbers[i])
// Prints "50"

The next example attempts to retrieve an index ten positions from numbers.startIndex, but fails, because that distance is beyond the index passed as limit.

let j = numbers.index(numbers.startIndex,
                      offsetBy: 10,
                      limitedBy: numbers.endIndex)
print(j)
// Prints "nil"

The value passed as distance must not offset i beyond the bounds of the collection, unless the index passed as limit prevents offsetting beyond those bounds.

Complexity: O(1)

Declaration

@inlinable public func index(_ i: Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Self.Index?
func index(_ i: AnyRandomAccessCollection<Element>.Index, offsetBy n: Int, limitedBy limit: AnyRandomAccessCollection<Element>.Index) -> AnyRandomAccessCollection<Element>.Index? Required

Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.

The following example obtains an index advanced four positions from a string's starting index and then prints the character at that position. The operation doesn't require going beyond the limiting s.endIndex value, so it succeeds.

let s = "Swift"
if let i = s.index(s.startIndex, offsetBy: 4, limitedBy: s.endIndex) {
    print(s[i])
}
// Prints "t"

The next example attempts to retrieve an index six positions from s.startIndex but fails, because that distance is beyond the index passed as limit.

let j = s.index(s.startIndex, offsetBy: 6, limitedBy: s.endIndex)
print(j)
// Prints "nil"

The value passed as distance must not offset i beyond the bounds of the collection, unless the index passed as limit prevents offsetting beyond those bounds.

Complexity: O(1)

Declaration

@inlinable public func index(_ i: AnyRandomAccessCollection<Element>.Index, offsetBy n: Int, limitedBy limit: AnyRandomAccessCollection<Element>.Index) -> AnyRandomAccessCollection<Element>.Index?
func index(after i: AnyRandomAccessCollection<Element>.Index) -> AnyRandomAccessCollection<Element>.Index Required

Returns the position immediately after the given index.

The successor of an index must be well defined. For an index i into a collection c, calling c.index(after: i) returns the same index every time.

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

Declaration

@inlinable public func index(after i: AnyRandomAccessCollection<Element>.Index) -> AnyRandomAccessCollection<Element>.Index
func index(before i: AnyRandomAccessCollection<Element>.Index) -> AnyRandomAccessCollection<Element>.Index Required

Returns the position immediately before the given index.

  • Parameter i: A valid index of the collection. i must be greater than startIndex.

Declaration

@inlinable public func index(before i: AnyRandomAccessCollection<Element>.Index) -> AnyRandomAccessCollection<Element>.Index
func makeIterator() -> AnyRandomAccessCollection<Element>.Iterator Required

Returns an iterator over the elements of this collection.

Declaration

@inlinable public func makeIterator() -> AnyRandomAccessCollection<Element>.Iterator
func map(_ transform: (Element) throws -> T) rethrows -> [T] Required

Declaration

@inlinable public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]
func prefix(_ maxLength: Int = 1) -> AnyRandomAccessCollection<Element> Required

Declaration

@inlinable public func prefix(_ maxLength: Int = 1) -> AnyRandomAccessCollection<Element>
func prefix(while predicate: (Element) throws -> Bool) rethrows -> AnyRandomAccessCollection<Element> Required

Declaration

@inlinable public func prefix(while predicate: (Element) throws -> Bool) rethrows -> AnyRandomAccessCollection<Element>
func suffix(_ maxLength: Int) -> AnyRandomAccessCollection<Element> Required

Declaration

@inlinable public func suffix(_ maxLength: Int) -> AnyRandomAccessCollection<Element>