@@ -154,6 +154,10 @@ extension ArraySlice: RandomAccessCollection, MutableCollection {
154
154
155
155
/// The position of the first element in a nonempty array.
156
156
///
157
+ /// `ArraySlice` instances are not always indexed from zero. Use `startIndex`
158
+ /// and `endIndex` as the bounds for any element access, instead of `0` and
159
+ /// `count`.
160
+ ///
157
161
/// If the array is empty, `startIndex` is equal to `endIndex`.
158
162
@inlinable
159
163
public var startIndex : Int {
@@ -248,24 +252,25 @@ extension ArraySlice: RandomAccessCollection, MutableCollection {
248
252
/// print(numbers[i])
249
253
/// // Prints "50"
250
254
///
251
- /// The value passed as `n ` must not offset `i` beyond the bounds of the
252
- /// collection.
255
+ /// The value passed as `distance ` must not offset `i` beyond the bounds of
256
+ /// the collection.
253
257
///
254
258
/// - Parameters:
255
259
/// - i: A valid index of the array.
256
- /// - n: The distance to offset `i`.
257
- /// - Returns: An index offset by `n` from the index `i`. If `n` is positive,
258
- /// this is the same value as the result of `n` calls to `index(after:)`.
259
- /// If `n` is negative, this is the same value as the result of `-n` calls
260
- /// to `index(before:)`.
260
+ /// - distance: The distance to offset `i`.
261
+ /// - Returns: An index offset by `distance` from the index `i`. If
262
+ /// `distance` is positive, this is the same value as the result of
263
+ /// `distance` calls to `index(after:)`. If `distance` is negative, this
264
+ /// is the same value as the result of `abs(distance)` calls to
265
+ /// `index(before:)`.
261
266
@inlinable
262
- public func index( _ i: Int , offsetBy n : Int ) -> Int {
267
+ public func index( _ i: Int , offsetBy distance : Int ) -> Int {
263
268
// NOTE: this is a manual specialization of index movement for a Strideable
264
269
// index that is required for Array performance. The optimizer is not
265
270
// capable of creating partial specializations yet.
266
271
// NOTE: Range checks are not performed here, because it is done later by
267
272
// the subscript function.
268
- return i + n
273
+ return i + distance
269
274
}
270
275
271
276
/// Returns an index that is the specified distance from the given index,
@@ -294,33 +299,36 @@ extension ArraySlice: RandomAccessCollection, MutableCollection {
294
299
/// print(j)
295
300
/// // Prints "nil"
296
301
///
297
- /// The value passed as `n ` must not offset `i` beyond the bounds of the
298
- /// collection, unless the index passed as `limit` prevents offsetting
302
+ /// The value passed as `distance ` must not offset `i` beyond the bounds of
303
+ /// the collection, unless the index passed as `limit` prevents offsetting
299
304
/// beyond those bounds.
300
305
///
301
306
/// - Parameters:
302
307
/// - i: A valid index of the array.
303
- /// - n: The distance to offset `i`.
304
- /// - limit: A valid index of the collection to use as a limit. If `n > 0`,
305
- /// `limit` has no effect if it is less than `i`. Likewise, if `n < 0`,
306
- /// `limit` has no effect if it is greater than `i`.
307
- /// - Returns: An index offset by `n` from the index `i`, unless that index
308
- /// would be beyond `limit` in the direction of movement. In that case,
309
- /// the method returns `nil`.
308
+ /// - distance: The distance to offset `i`.
309
+ /// - limit: A valid index of the collection to use as a limit. If
310
+ /// `distance > 0`, `limit` has no effect if it is less than `i`.
311
+ /// Likewise, if `distance < 0`, `limit` has no effect if it is greater
312
+ /// than `i`.
313
+ /// - Returns: An index offset by `distance` from the index `i`, unless that
314
+ /// index would be beyond `limit` in the direction of movement. In that
315
+ /// case, the method returns `nil`.
316
+ ///
317
+ /// - Complexity: O(1)
310
318
@inlinable
311
319
public func index(
312
- _ i: Int , offsetBy n : Int , limitedBy limit: Int
320
+ _ i: Int , offsetBy distance : Int , limitedBy limit: Int
313
321
) -> Int ? {
314
322
// NOTE: this is a manual specialization of index movement for a Strideable
315
323
// index that is required for Array performance. The optimizer is not
316
324
// capable of creating partial specializations yet.
317
325
// NOTE: Range checks are not performed here, because it is done later by
318
326
// the subscript function.
319
327
let l = limit - i
320
- if n > 0 ? l >= 0 && l < n : l <= 0 && n < l {
328
+ if distance > 0 ? l >= 0 && l < distance : l <= 0 && distance < l {
321
329
return nil
322
330
}
323
- return i + n
331
+ return i + distance
324
332
}
325
333
326
334
/// Returns the distance between two indices.
@@ -365,8 +373,9 @@ extension ArraySlice: RandomAccessCollection, MutableCollection {
365
373
/// greater than or equal to `startIndex` and less than `endIndex`.
366
374
///
367
375
/// - Complexity: Reading an element from an array is O(1). Writing is O(1)
368
- /// unless the array's storage is shared with another array, in which case
369
- /// writing is O(*n*), where *n* is the length of the array.
376
+ /// unless the array's storage is shared with another array or uses a
377
+ /// bridged `NSArray` instance as its storage, in which case writing is
378
+ /// O(*n*), where *n* is the length of the array.
370
379
@inlinable
371
380
public subscript( index: Int ) -> Element {
372
381
get {
@@ -931,9 +940,8 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
931
940
///
932
941
/// - Parameter newElement: The element to append to the array.
933
942
///
934
- /// - Complexity: Amortized O(1) over many additions. If the array uses a
935
- /// bridged `NSArray` instance as its storage, the efficiency is
936
- /// unspecified.
943
+ /// - Complexity: O(1) on average, over many calls to `append(_:)` on the
944
+ /// same array.
937
945
@inlinable
938
946
@_semantics ( " array.append_element " )
939
947
public mutating func append( _ newElement: Element ) {
@@ -956,7 +964,9 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
956
964
///
957
965
/// - Parameter newElements: The elements to append to the array.
958
966
///
959
- /// - Complexity: O(*n*), where *n* is the length of the resulting array.
967
+ /// - Complexity: O(*m*) on average, where *m* is the length of
968
+ /// `newElements`, over many calls to `append(contentsOf:)` on the same
969
+ /// array.
960
970
@inlinable
961
971
@_semantics ( " array.append_contentsOf " )
962
972
public mutating func append< S: Sequence > ( contentsOf newElements: S )
@@ -1061,7 +1071,8 @@ extension ArraySlice: RangeReplaceableCollection, ArrayProtocol {
1061
1071
/// `index` must be a valid index of the array or equal to its `endIndex`
1062
1072
/// property.
1063
1073
///
1064
- /// - Complexity: O(*n*), where *n* is the length of the array.
1074
+ /// - Complexity: O(*n*), where *n* is the length of the array. If
1075
+ /// `i == endIndex`, this method is equivalent to `append(_:)`.
1065
1076
@inlinable
1066
1077
public mutating func insert( _ newElement: Element , at i: Int ) {
1067
1078
_checkIndex ( i)
@@ -1320,9 +1331,10 @@ extension ArraySlice {
1320
1331
/// a subrange must be valid indices of the array.
1321
1332
/// - newElements: The new elements to add to the array.
1322
1333
///
1323
- /// - Complexity: O(`subrange.count`) if you are replacing a suffix of the
1324
- /// array with an empty collection; otherwise, O(*n*), where *n* is the
1325
- /// length of the array.
1334
+ /// - Complexity: O(*n* + *m*), where *n* is length of the array and
1335
+ /// *m* is the length of `newElements`. If the call to this method simply
1336
+ /// appends the contents of `newElements` to the array, this method is
1337
+ /// equivalent to `append(contentsOf:)`.
1326
1338
@inlinable
1327
1339
@_semantics ( " array.mutate_unknown " )
1328
1340
public mutating func replaceSubrange< C> (
0 commit comments