Instance Method

distance(from:to:)

Returns the distance between two indices.

Declaration

func distance(from start: FlattenSequence<Base>.Index, to end: FlattenSequence<Base>.Index) -> Int
Available when Base conforms to Collection and Base.Element conforms to Collection.

Parameters

start

A valid index of the collection.

end

Another valid index of the collection. If end is equal to start, the result is zero.

Return Value

The distance between start and end. The result can be negative only if the collection conforms to the BidirectionalCollection protocol.

Discussion

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

Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(k), where k is the resulting distance.

Relationships