protocol RandomAccessIndexType
Inheritance |
BidirectionalIndexType, Comparable, Equatable, ForwardIndexType, Strideable, _Incrementable, _RandomAccessAmbiguity
View Protocol Hierarchy →
|
---|---|
Associated Types | |
Import | import Swift |
Instance Methods
A strict total order
over instances of Self
.
Declaration
func <(lhs: Self, rhs: Self) -> Bool
Declared In
Comparable
Returns true
if lhs
is equal to rhs
.
Equality implies substitutability. When x == y
, x
and
y
are interchangeable in any code that only depends on their
values.
Class instance identity as distinguished by triple-equals ===
is notably not part of an instance's value. Exposing other
non-value aspects of Equatable
types is discouraged, and any
that are exposed should be explicitly pointed out in
documentation.
Equality is an equivalence relation
x == x
istrue
x == y
impliesy == x
x == y
andy == z
impliesx == z
Inequality is the inverse of equality, i.e. !(x == y)
iff
x != y
.
Declaration
func ==(lhs: Self, rhs: Self) -> Bool
Declared In
Equatable
Declaration
func advancedBy(n: Self.Distance) -> Self
Declared In
RandomAccessIndexType
, ForwardIndexType
, Strideable
Declaration
func advancedBy(n: Self.Distance, limit: Self) -> Self
Declared In
RandomAccessIndexType
, ForwardIndexType
Declaration
func distanceTo(other: Self) -> Self.Distance
Declared In
RandomAccessIndexType
, ForwardIndexType
, Strideable
Returns the previous consecutive value in a discrete sequence.
If self
has a well-defined successor,
self.successor().predecessor() == self
. If self
has a
well-defined predecessor, self.predecessor().successor() ==
self
.
Requires: self
has a well-defined predecessor.
Declaration
func predecessor() -> Self
Declared In
BidirectionalIndexType
Returns the next consecutive value in a discrete sequence of
Self
values.
Requires: self
has a well-defined successor.
Declaration
func successor() -> Self
Declared In
_Incrementable
Default Implementations
Returns the result of advancing self
by n
positions.
Returns:
- If n > 0
, the result of applying successor
to self
n
times.
- If n < 0
, the result of applying predecessor
to self
-n
times.
- Otherwise, self
.
Requires: n >= 0
if only conforming to ForwardIndexType
Complexity:
- O(1) if conforming to RandomAccessIndexType
- O(abs(n)
) otherwise
Declaration
func advancedBy(n: Self.Distance) -> Self
Declared In
BidirectionalIndexType
, _RandomAccessAmbiguity
, ForwardIndexType
Declaration
func advancedBy(n: Self.Distance, limit: Self) -> Self
Declared In
RandomAccessIndexType
, BidirectionalIndexType
, ForwardIndexType
Measure the distance between self
and end
.
Requires:
- start
and end
are part of the same sequence when conforming to
RandomAccessSequenceType
.
- end
is reachable from self
by incrementation otherwise.
Complexity:
- O(1) if conforming to RandomAccessIndexType
- O(n
) otherwise, where n
is the function's result.
Declaration
func distanceTo(end: Self) -> Self.Distance
Declared In
BidirectionalIndexType
, ForwardIndexType
Returns the sequence of values (self
, self + stride
, self +
stride + stride
, ... last) where last is the last value in
the progression less than or equal to end
.
Note: There is no guarantee that end
is an element of the sequence.
Declaration
func stride(through end: Self, by stride: Self.Stride) -> StrideThrough<Self>
Declared In
Strideable
Returns the sequence of values (self
, self + stride
, self +
stride + stride
, ... last) where last is the last value in
the progression that is less than end
.
Declaration
func stride(to end: Self, by stride: Self.Stride) -> StrideTo<Self>
Declared In
Strideable
An index that can be offset by an arbitrary number of positions, and can measure the distance to any reachable value, in O(1).