@@ -619,24 +619,25 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
619
619
/// print(numbers[i])
620
620
/// // Prints "50"
621
621
///
622
- /// The value passed as `n ` must not offset `i` beyond the bounds of the
623
- /// collection.
622
+ /// The value passed as `distance ` must not offset `i` beyond the bounds of
623
+ /// the collection.
624
624
///
625
625
/// - Parameters:
626
626
/// - i: A valid index of the array.
627
- /// - n: The distance to offset `i`.
628
- /// - Returns: An index offset by `n` from the index `i`. If `n` is positive,
629
- /// this is the same value as the result of `n` calls to `index(after:)`.
630
- /// If `n` is negative, this is the same value as the result of `-n` calls
631
- /// to `index(before:)`.
632
- @inlinable
633
- public func index( _ i: Int , offsetBy n: Int ) -> Int {
627
+ /// - distance: The distance to offset `i`.
628
+ /// - Returns: An index offset by `distance` from the index `i`. If
629
+ /// `distance` is positive, this is the same value as the result of
630
+ /// `distance` calls to `index(after:)`. If `distance` is negative, this
631
+ /// is the same value as the result of `abs(distance)` calls to
632
+ /// `index(before:)`.
633
+ @inlinable
634
+ public func index( _ i: Int , offsetBy distance: Int ) -> Int {
634
635
// NOTE: this is a manual specialization of index movement for a Strideable
635
636
// index that is required for Array performance. The optimizer is not
636
637
// capable of creating partial specializations yet.
637
638
// NOTE: Range checks are not performed here, because it is done later by
638
639
// the subscript function.
639
- return i + n
640
+ return i + distance
640
641
}
641
642
642
643
/// Returns an index that is the specified distance from the given index,
@@ -665,33 +666,36 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
665
666
/// print(j)
666
667
/// // Prints "nil"
667
668
///
668
- /// The value passed as `n ` must not offset `i` beyond the bounds of the
669
- /// collection, unless the index passed as `limit` prevents offsetting
669
+ /// The value passed as `distance ` must not offset `i` beyond the bounds of
670
+ /// the collection, unless the index passed as `limit` prevents offsetting
670
671
/// beyond those bounds.
671
672
///
672
673
/// - Parameters:
673
674
/// - i: A valid index of the array.
674
- /// - n: The distance to offset `i`.
675
- /// - limit: A valid index of the collection to use as a limit. If `n > 0`,
676
- /// `limit` has no effect if it is less than `i`. Likewise, if `n < 0`,
677
- /// `limit` has no effect if it is greater than `i`.
678
- /// - Returns: An index offset by `n` from the index `i`, unless that index
679
- /// would be beyond `limit` in the direction of movement. In that case,
680
- /// the method returns `nil`.
675
+ /// - distance: The distance to offset `i`.
676
+ /// - limit: A valid index of the collection to use as a limit. If
677
+ /// `distance > 0`, `limit` has no effect if it is less than `i`.
678
+ /// Likewise, if `distance < 0`, `limit` has no effect if it is greater
679
+ /// than `i`.
680
+ /// - Returns: An index offset by `distance` from the index `i`, unless that
681
+ /// index would be beyond `limit` in the direction of movement. In that
682
+ /// case, the method returns `nil`.
683
+ ///
684
+ /// - Complexity: O(1)
681
685
@inlinable
682
686
public func index(
683
- _ i: Int , offsetBy n : Int , limitedBy limit: Int
687
+ _ i: Int , offsetBy distance : Int , limitedBy limit: Int
684
688
) -> Int ? {
685
689
// NOTE: this is a manual specialization of index movement for a Strideable
686
690
// index that is required for Array performance. The optimizer is not
687
691
// capable of creating partial specializations yet.
688
692
// NOTE: Range checks are not performed here, because it is done later by
689
693
// the subscript function.
690
694
let l = limit - i
691
- if n > 0 ? l >= 0 && l < n : l <= 0 && n < l {
695
+ if distance > 0 ? l >= 0 && l < distance : l <= 0 && distance < l {
692
696
return nil
693
697
}
694
- return i + n
698
+ return i + distance
695
699
}
696
700
697
701
/// Returns the distance between two indices.
@@ -735,12 +739,15 @@ extension ${Self}: RandomAccessCollection, MutableCollection {
735
739
/// - Parameter index: The position of the element to access. `index` must be
736
740
/// greater than or equal to `startIndex` and less than `endIndex`.
737
741
///
742
+ % if Self == 'ContiguousArray':
738
743
/// - Complexity: Reading an element from an array is O(1). Writing is O(1)
739
744
/// unless the array's storage is shared with another array, in which case
740
745
/// writing is O(*n*), where *n* is the length of the array.
741
- % if Self == 'Array':
742
- /// If the array uses a bridged `NSArray` instance as its storage, the
743
- /// efficiency is unspecified.
746
+ % else :
747
+ /// - Complexity: Reading an element from an array is O(1). Writing is O(1)
748
+ /// unless the array's storage is shared with another array or uses a
749
+ /// bridged `NSArray` instance as its storage, in which case writing is
750
+ /// O(*n*), where *n* is the length of the array.
744
751
% end
745
752
@inlinable
746
753
public subscript( index: Int) - > Element {
@@ -1416,9 +1423,8 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
1416
1423
///
1417
1424
/// - Parameter newElement: The element to append to the array.
1418
1425
///
1419
- /// - Complexity: Amortized O(1) over many additions. If the array uses a
1420
- /// bridged `NSArray` instance as its storage, the efficiency is
1421
- /// unspecified.
1426
+ /// - Complexity: O(1) on average, over many calls to `append(_:)` on the
1427
+ /// same array.
1422
1428
@inlinable
1423
1429
@_semantics( " array.append_element " )
1424
1430
public mutating func append( _ newElement: Element) {
@@ -1441,7 +1447,9 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
1441
1447
///
1442
1448
/// - Parameter newElements: The elements to append to the array.
1443
1449
///
1444
- /// - Complexity: O(*n*), where *n* is the length of the resulting array.
1450
+ /// - Complexity: O(*m*) on average, where *m* is the length of
1451
+ /// `newElements`, over many calls to `append(contentsOf:)` on the same
1452
+ /// array.
1445
1453
@inlinable
1446
1454
@_semantics( " array.append_contentsOf " )
1447
1455
public mutating func append< S : Sequence> ( contentsOf newElements: S)
@@ -1588,7 +1596,8 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
1588
1596
/// `index` must be a valid index of the array or equal to its `endIndex`
1589
1597
/// property.
1590
1598
///
1591
- /// - Complexity: O(*n*), where *n* is the length of the array.
1599
+ /// - Complexity: O(*n*), where *n* is the length of the array. If
1600
+ /// `i == endIndex`, this method is equivalent to `append(_:)`.
1592
1601
@inlinable
1593
1602
public mutating func insert( _ newElement: Element, at i: Int) {
1594
1603
_checkIndex ( i)
@@ -1943,9 +1952,10 @@ extension ${Self} {
1943
1952
/// a subrange must be valid indices of the array.
1944
1953
/// - newElements: The new elements to add to the array.
1945
1954
///
1946
- /// - Complexity: O(`subrange.count`) if you are replacing a suffix of the
1947
- /// array with an empty collection; otherwise, O(*n*), where *n* is the
1948
- /// length of the array.
1955
+ /// - Complexity: O(*n* + *m*), where *n* is length of the array and
1956
+ /// *m* is the length of `newElements`. If the call to this method simply
1957
+ /// appends the contents of `newElements` to the array, this method is
1958
+ /// equivalent to `append(contentsOf:)`.
1949
1959
@inlinable
1950
1960
@_semantics ( " array.mutate_unknown " )
1951
1961
public mutating func replaceSubrange< C> (
0 commit comments