Protocolswift 6.0.1Swift
BidirectionalCollection
A collection that supports backward as well as forward traversal.
protocol BidirectionalCollection<Element> : Collection where Self.Indices : BidirectionalCollection, Self.SubSequence : BidirectionalCollection
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
.
Supertypes
protocol Collection<Element>
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
protocol Sequence<Element>
A type that provides sequential, iterated access to its elements.
Requirements
Type members
Instance members
var endIndex: Self.Index
var indices: Self.Indices
The indices that are valid for subscripting the collection, in ascending order.
var startIndex: Self.Index
subscript(Range<Self.Index>
) -> Self.SubSequence Accesses a contiguous subrange of the collection’s elements.
subscript(Self.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.
func index(Self.Index, offsetBy: Int
) -> Self.Index Returns an index that is the specified distance from the given index.
func index(Self.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.
Citizens in Swift
Instance members
var last: Self.Element?
The last element of the collection.
func difference<C>(from: C, by: (C.Element, Self.Element) -> Bool
) -> CollectionDifference<Self.Element> Returns the difference needed to produce this collection’s ordered elements from the given collection, using the given predicate as an equivalence test.
func distance(from: Self.Index, to: Self.Index
) -> Int func dropLast(Int
) -> Self.SubSequence Returns a subsequence containing all but the specified number of final elements.
func formIndex(before: inout Self.Index
) func index(Self.Index, offsetBy: Int
) -> Self.Index func index(Self.Index, offsetBy: Int, limitedBy: Self.Index
) -> Self.Index? func last(where: (Self.Element) throws -> Bool
) rethrows -> Self.Element? Returns the last element of the sequence that satisfies the given predicate.
func lastIndex(where: (Self.Element) throws -> Bool
) rethrows -> Self.Index? Returns the index of the last element in the collection that matches the given predicate.
func reversed(
) -> ReversedCollection<Self> Returns a view presenting the elements of the collection in reverse order.
func suffix(Int
) -> Self.SubSequence Returns a subsequence, up to the given maximum length, containing the final elements of the collection.
Subtypes
protocol RandomAccessCollection<Element>
A collection that supports efficient random-access index traversal.
protocol StringProtocol
A type that can represent a string as a collection of characters.
Citizens in Swift
where Self == Self.SubSequence
Instance members
func popLast(
) -> Self.Element? Removes and returns the last element of the collection.
func removeLast(
) -> Self.Element Removes and returns the last element of the collection.
func removeLast(Int
) Removes the given number of elements from the end of the collection.
Citizens in Swift
where Self.Element:Equatable
Instance members
func difference<C>(from: C
) -> CollectionDifference<Self.Element> Returns the difference needed to produce this collection’s ordered elements from the given collection.
func lastIndex(of: Self.Element
) -> Self.Index? Returns the last index where the specified value appears in the collection.
Citizens in Swift
where Self.Element == String
Instance members
func joined(separator: String
) -> String Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.
Available in _RegexParser
Instance members
Available in _StringProcessing
where Self.Element:Comparable
Instance members
func firstRange<C>(of: C
) -> Range<Self.Index>? Finds and returns the range of the first occurrence of a given collection within this collection.
Available in _StringProcessing
where Self.SubSequence == Substring
Instance members
func contains(some RegexComponent
) -> Bool Returns a Boolean value indicating whether the collection contains the given regex.
func firstMatch<Output>(of: some RegexComponent
) -> Regex<Output>.Match? Returns the first match of the specified regex within the collection.
func firstRange(of: some RegexComponent
) -> Range<Self.Index>? Finds and returns the range of the first occurrence of a given regex within the collection.
func matches<Output>(of: some RegexComponent
) -> [Regex<Output>.Match] Returns a collection containing all matches of the specified regex.
func prefixMatch<R>(of: R
) -> Regex<R.RegexOutput>.Match? Returns a match if this string is matched by the given regex at its start.
func ranges(of: some RegexComponent
) -> [Range<Self.Index>] Finds and returns the ranges of the all occurrences of a given sequence within the collection.
func split(separator: some RegexComponent, maxSplits: Int, omittingEmptySubsequences: Bool
) -> [Self.SubSequence] Returns the longest possible subsequences of the collection, in order, around elements equal to the given separator.
func starts(with: some RegexComponent
) -> Bool Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in the specified regex.
func trimmingPrefix(some RegexComponent
) -> Self.SubSequence Returns a new collection of the same type by removing the initial elements that matches the given regex.
func wholeMatch<R>(of: R
) -> Regex<R.RegexOutput>.Match? Returns a match if this string is matched by the given regex in its entirety.
Available in RegexBuilder
where Self.SubSequence == Substring
Instance members
func contains(() -> some RegexComponent
) -> Bool Returns a Boolean value indicating whether this collection contains a match for the regex, where the regex is created by the given closure.
func firstMatch<Output>(of: () -> some RegexComponent
) -> Regex<Output>.Match? Returns the first match for the regex within this collection, where the regex is created by the given closure.
func firstRange(of: () -> some RegexComponent
) -> Range<Self.Index>? Returns the range of the first match for the regex within this collection, where the regex is created by the given closure.
func matches<Output>(of: () -> some RegexComponent
) -> [Regex<Output>.Match] Returns a collection containing all non-overlapping matches of the regex, created by the given closure.
func prefixMatch<Output>(of: () -> some RegexComponent
) -> Regex<Output>.Match? Matches part of the regex, starting at the beginning, where the regex is created by the given closure.
func ranges(of: () -> some RegexComponent
) -> [Range<Self.Index>] Returns the ranges of the all non-overlapping matches for the regex within this collection, where the regex is created by the given closure.
func split(maxSplits: Int, omittingEmptySubsequences: Bool, separator: () -> some RegexComponent
) -> [Self.SubSequence] Returns the longest possible subsequences of the collection, in order, around subsequence that match the regex created by the given closure.
func starts(with: () -> some RegexComponent
) -> Bool Returns a Boolean value indicating whether the initial elements of this collection are a match for the regex created by the given closure.
func trimmingPrefix(() -> some RegexComponent
) -> Self.SubSequence Returns a subsequence of this collection by removing the elements matching the regex from the start, where the regex is created by the given closure.
func wholeMatch<Output>(of: () -> some RegexComponent
) -> Regex<Output>.Match? Matches a regex in its entirety, where the regex is created by the given closure.
Available in Cxx
Subtypes
Available in FoundationEssentials
Subtypes
Extension in Algorithms
Instance members
func startOfSuffix(while: (Element) throws -> Bool
) rethrows -> Index Returns the inclusive lower bound of the suffix of elements that satisfy the predicate.
func suffix(while: (Element) throws -> Bool
) rethrows -> SubSequence Returns a subsequence containing the elements from the end until
predicate
returnsfalse
and skipping the remaining elements.func trimming(while: (Element) throws -> Bool
) rethrows -> SubSequence Returns a
SubSequence
formed by discarding all elements at the start and end of the collection which satisfy the given predicate.func trimmingSuffix(while: (Element) throws -> Bool
) rethrows -> SubSequence Returns a
SubSequence
formed by discarding all elements at the end of the collection which satisfy the given predicate.
Extension in Algorithms
where Self:RangeReplaceableCollection
Instance members
func trim(while: (Element) throws -> Bool
) rethrows Mutates a
BidirectionalCollection
by discarding all elements at the start and at the end of it which satisfy the given predicate.func trimSuffix(while: (Element) throws -> Bool
) rethrows Mutates a
BidirectionalCollection
by discarding all elements at the end of it which satisfy the given predicate.
Extension in Algorithms
where Self == Self.SubSequence
Instance members
func trim(while: (Element) throws -> Bool
) rethrows Mutates a
BidirectionalCollection
by discarding all elements at the start and at the end of it which satisfy the given predicate.func trimSuffix(while: (Element) throws -> Bool
) rethrows Mutates a
BidirectionalCollection
by discarding all elements at the end of it which satisfy the given predicate.