@@ -103,6 +103,129 @@ where SubSequence: RandomAccessCollection, Indices: RandomAccessCollection
103
103
104
104
// FIXME(ABI): Associated type inference requires this.
105
105
var endIndex : Index { get }
106
+
107
+ /// Returns the position immediately before the given index.
108
+ ///
109
+ /// - Parameter i: A valid index of the collection. `i` must be greater than
110
+ /// `startIndex`.
111
+ /// - Returns: The index value immediately before `i`.
112
+ func index( before i: Index ) -> Index
113
+
114
+ /// Replaces the given index with its predecessor.
115
+ ///
116
+ /// - Parameter i: A valid index of the collection. `i` must be greater than
117
+ /// `startIndex`.
118
+ func formIndex( before i: inout Index )
119
+
120
+ /// Returns the position immediately after the given index.
121
+ ///
122
+ /// The successor of an index must be well defined. For an index `i` into a
123
+ /// collection `c`, calling `c.index(after: i)` returns the same index every
124
+ /// time.
125
+ ///
126
+ /// - Parameter i: A valid index of the collection. `i` must be less than
127
+ /// `endIndex`.
128
+ /// - Returns: The index value immediately after `i`.
129
+ func index( after i: Index ) -> Index
130
+
131
+ /// Replaces the given index with its successor.
132
+ ///
133
+ /// - Parameter i: A valid index of the collection. `i` must be less than
134
+ /// `endIndex`.
135
+ func formIndex( after i: inout Index )
136
+
137
+ /// Returns an index that is the specified distance from the given index.
138
+ ///
139
+ /// The following example obtains an index advanced four positions from a
140
+ /// string's starting index and then prints the character at that position.
141
+ ///
142
+ /// let s = "Swift"
143
+ /// let i = s.index(s.startIndex, offsetBy: 4)
144
+ /// print(s[i])
145
+ /// // Prints "t"
146
+ ///
147
+ /// The value passed as `distance` must not offset `i` beyond the bounds of
148
+ /// the collection.
149
+ ///
150
+ /// - Parameters:
151
+ /// - i: A valid index of the collection.
152
+ /// - distance: The distance to offset `i`. `distance` must not be negative
153
+ /// unless the collection conforms to the `BidirectionalCollection`
154
+ /// protocol.
155
+ /// - Returns: An index offset by `distance` from the index `i`. If
156
+ /// `distance` is positive, this is the same value as the result of
157
+ /// `distance` calls to `index(after:)`. If `distance` is negative, this
158
+ /// is the same value as the result of `abs(distance)` calls to
159
+ /// `index(before:)`.
160
+ ///
161
+ /// - Complexity: O(1) if the collection conforms to
162
+ /// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the absolute
163
+ /// value of `distance`.
164
+ func index( _ i: Index , offsetBy distance: Int ) -> Index
165
+
166
+ /// Returns an index that is the specified distance from the given index,
167
+ /// unless that distance is beyond a given limiting index.
168
+ ///
169
+ /// The following example obtains an index advanced four positions from a
170
+ /// string's starting index and then prints the character at that position.
171
+ /// The operation doesn't require going beyond the limiting `s.endIndex`
172
+ /// value, so it succeeds.
173
+ ///
174
+ /// let s = "Swift"
175
+ /// if let i = s.index(s.startIndex, offsetBy: 4, limitedBy: s.endIndex) {
176
+ /// print(s[i])
177
+ /// }
178
+ /// // Prints "t"
179
+ ///
180
+ /// The next example attempts to retrieve an index six positions from
181
+ /// `s.startIndex` but fails, because that distance is beyond the index
182
+ /// passed as `limit`.
183
+ ///
184
+ /// let j = s.index(s.startIndex, offsetBy: 6, limitedBy: s.endIndex)
185
+ /// print(j)
186
+ /// // Prints "nil"
187
+ ///
188
+ /// The value passed as `distance` must not offset `i` beyond the bounds of
189
+ /// the collection, unless the index passed as `limit` prevents offsetting
190
+ /// beyond those bounds.
191
+ ///
192
+ /// - Parameters:
193
+ /// - i: A valid index of the collection.
194
+ /// - distance: The distance to offset `i`. `distance` must not be negative
195
+ /// unless the collection conforms to the `BidirectionalCollection`
196
+ /// protocol.
197
+ /// - limit: A valid index of the collection to use as a limit. If
198
+ /// `distance > 0`, a limit that is less than `i` has no effect.
199
+ /// Likewise, if `distance < 0`, a limit that is greater than `i` has no
200
+ /// effect.
201
+ /// - Returns: An index offset by `distance` from the index `i`, unless that
202
+ /// index would be beyond `limit` in the direction of movement. In that
203
+ /// case, the method returns `nil`.
204
+ ///
205
+ /// - Complexity: O(1) if the collection conforms to
206
+ /// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the absolute
207
+ /// value of `distance`.
208
+ func index(
209
+ _ i: Index , offsetBy distance: Int , limitedBy limit: Index
210
+ ) -> Index ?
211
+
212
+ /// Returns the distance between two indices.
213
+ ///
214
+ /// Unless the collection conforms to the `BidirectionalCollection` protocol,
215
+ /// `start` must be less than or equal to `end`.
216
+ ///
217
+ /// - Parameters:
218
+ /// - start: A valid index of the collection.
219
+ /// - end: Another valid index of the collection. If `end` is equal to
220
+ /// `start`, the result is zero.
221
+ /// - Returns: The distance between `start` and `end`. The result can be
222
+ /// negative only if the collection conforms to the
223
+ /// `BidirectionalCollection` protocol.
224
+ ///
225
+ /// - Complexity: O(1) if the collection conforms to
226
+ /// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the
227
+ /// resulting distance.
228
+ func distance( from start: Index , to end: Index ) -> Int
106
229
}
107
230
108
231
// TODO: swift-3-indexing-model - Make sure RandomAccessCollection has
0 commit comments