BidirectionalCollection
A collection that supports backward as well as forward traversal.
Declaration
protocol BidirectionalCollection<Element> : Collection where Self.Indices : BidirectionalCollection, Self.SubSequence : BidirectionalCollectionOverview
Bidirectional collections offer traversal backward from any valid index, not including a collection’s startIndex. Bidirectional collections can therefore offer additional operations, such as a last property that provides efficient access to the last element and a reversed() method that presents the elements in reverse order. In addition, bidirectional collections have more efficient implementations of some sequence and collection methods, such as suffix(_:).
Conforming to the BidirectionalCollection Protocol
To add BidirectionalProtocol conformance to your custom types, implement the index(before:) method in addition to the requirements of the Collection protocol.
Indices that are moved forward and backward in a bidirectional collection move by the same amount in each direction. That is, for any valid index i into a bidirectional collection c:
If
i >= c.startIndex && i < c.endIndex, thenc.index(before: c.index(after: i)) == i.If
i > c.startIndex && i <= c.endIndex, thenc.index(after: c.index(before: i)) == i.
Valid indices are exactly those indices that are reachable from the collection’s startIndex by repeated applications of index(after:), up to, and including, the endIndex.
Topics
Associated Types
Instance Properties
Instance Methods
contains(_:)contains(_:)difference(from:)difference(from:by:)distance(from:to:)dropLast(_:)firstMatch(of:)firstMatch(of:)firstRange(of:)firstRange(of:)firstRange(of:)formIndex(after:)formIndex(before:)index(_:offsetBy:)index(_:offsetBy:limitedBy:)index(after:)index(before:)joined(separator:)last(where:)lastIndex(of:)lastIndex(where:)matches(of:)matches(of:)popLast()prefixMatch(of:)prefixMatch(of:)ranges(of:)ranges(of:)removeLast()removeLast(_:)reversed()split(maxSplits:omittingEmptySubsequences:separator:)split(separator:maxSplits:omittingEmptySubsequences:)starts(with:)starts(with:)suffix(_:)trimmingPrefix(_:)trimmingPrefix(_:)wholeMatch(of:)wholeMatch(of:)