@@ -482,11 +482,11 @@ public struct ${Self}<Element>
482
482
% end
483
483
}
484
484
485
- /// The array's "past- the- end" position, or one greater than the last valid
485
+ /// The array's "past the end" position, or one greater than the last valid
486
486
/// subscript argument.
487
487
///
488
488
/// When you need a range that includes the last element of an array, use the
489
- /// `..<` operator with `endIndex`. The half-open range operator ( `..<`)
489
+ /// half-open range operator ( `..<`) with `endIndex`. The `..<` operator
490
490
/// creates a range that doesn't include the upper bound, so it's always
491
491
/// safe to use with `endIndex`. For example:
492
492
///
@@ -543,6 +543,32 @@ public struct ${Self}<Element>
543
543
i -= 1
544
544
}
545
545
546
+ /// Returns an index that is the specified distance from the given index.
547
+ ///
548
+ /// The following example obtains an index advanced four positions from an
549
+ /// array's starting index and then prints the element at that position.
550
+ ///
551
+ /// let numbers = [10, 20, 30, 40, 50]
552
+ /// let i = numbers.index(numbers.startIndex, offsetBy: 4)
553
+ /// print(numbers[i])
554
+ /// // Prints "50"
555
+ ///
556
+ /// Advancing an index beyond a collection's ending index or offsetting it
557
+ /// before a collection's starting index may trigger a runtime error. The
558
+ /// value passed as `n` must not result in such an operation.
559
+ ///
560
+ /// - Parameters:
561
+ /// - i: A valid index of the array.
562
+ /// - n: The distance to offset `i`.
563
+ /// - Returns: An index offset by `n` from the index `i`. If `n` is positive,
564
+ /// this is the same value as the result of `n` calls to `index(after:)`.
565
+ /// If `n` is negative, this is the same value as the result of `-n` calls
566
+ /// to `index(before:)`.
567
+ ///
568
+ /// - Precondition:
569
+ /// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
570
+ /// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
571
+ /// - Complexity: O(1)
546
572
@warn_unused_result
547
573
public func index( _ i: Int , offsetBy n: Int ) -> Int {
548
574
// NOTE: this is a manual specialization of index movement for a Strideable
@@ -553,6 +579,47 @@ public struct ${Self}<Element>
553
579
return i + n
554
580
}
555
581
582
+ /// Returns an index that is the specified distance from the given index,
583
+ /// unless that distance is beyond a given limiting index.
584
+ ///
585
+ /// The following example obtains an index advanced four positions from an
586
+ /// array's starting index and then prints the element at that position. The
587
+ /// operation doesn't require going beyond the limiting `numbers.endIndex`
588
+ /// value, so it succeeds.
589
+ ///
590
+ /// let numbers = [10, 20, 30, 40, 50]
591
+ /// let i = numbers.index(numbers.startIndex,
592
+ /// offsetBy: 4,
593
+ /// limitedBy: numbers.endIndex)
594
+ /// print(numbers[i])
595
+ /// // Prints "50"
596
+ ///
597
+ /// The next example attempts to retrieve an index ten positions from
598
+ /// `numbers.startIndex`, but fails, because that distance is beyond the
599
+ /// index passed as `limit`.
600
+ ///
601
+ /// let j = numbers.index(numbers.startIndex,
602
+ /// offsetBy: 10,
603
+ /// limitedBy: numbers.endIndex)
604
+ /// print(j)
605
+ /// // Prints "nil"
606
+ ///
607
+ /// Advancing an index beyond a collection's ending index or offsetting it
608
+ /// before a collection's starting index may trigger a runtime error. The
609
+ /// value passed as `n` must not result in such an operation.
610
+ ///
611
+ /// - Parameters:
612
+ /// - i: A valid index of the array.
613
+ /// - n: The distance to offset `i`.
614
+ /// - limit: A valid index of the collection to use as a limit. If `n > 0`,
615
+ /// `limit` has no effect if it is less than `i`. Likewise, if `n < 0`,
616
+ /// `limit` has no effect if it is greater than `i`.
617
+ /// - Returns: An index offset by `n` from the index `i`, unless that index
618
+ /// would be beyond `limit` in the direction of movement. In that case,
619
+ /// the method returns `nil`.
620
+ ///
621
+ /// - SeeAlso: `index(_:offsetBy:)`, `formIndex(_:offsetBy:limitedBy:)`
622
+ /// - Complexity: O(1)
556
623
@warn_unused_result
557
624
public func index(
558
625
_ i: Int , offsetBy n: Int , limitedBy limit: Int
@@ -569,6 +636,13 @@ public struct ${Self}<Element>
569
636
return i + n
570
637
}
571
638
639
+ /// Returns the distance between two indices.
640
+ ///
641
+ /// - Parameters:
642
+ /// - start: A valid index of the collection.
643
+ /// - end: Another valid index of the collection. If `end` is equal to
644
+ /// `start`, the result is zero.
645
+ /// - Returns: The distance between `start` and `end`.
572
646
@warn_unused_result
573
647
public func distance( from start: Int , to end: Int ) -> Int {
574
648
// NOTE: this is a manual specialization of index movement for a Strideable
@@ -1013,7 +1087,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
1013
1087
/// value.
1014
1088
///
1015
1089
/// Here's an example of creating an array initialized with five strings
1016
- /// containing the letter "Z" .
1090
+ /// containing the letter *Z* .
1017
1091
///
1018
1092
/// let fiveZs = Array(repeating: "Z", count: 5)
1019
1093
/// print(fiveZs)
@@ -1156,16 +1230,17 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
1156
1230
1157
1231
/// Reserves enough space to store the specified number of elements.
1158
1232
///
1159
- /// Use this method to avoid multiple reallocations if you will be adding
1160
- /// a known number of elements to an array . This method ensures that the
1161
- /// array has unique, mutable, contiguous storage, with space allocated for
1162
- /// at least the requested number of elements.
1233
+ /// If you are adding a known number of elements to an array, use this method
1234
+ /// to avoid multiple reallocations . This method ensures that the array has
1235
+ /// unique, mutable, contiguous storage, with space allocated for at least
1236
+ /// the requested number of elements.
1163
1237
///
1164
- /// For performance reasons, the newly allocated storage may be larger
1165
- /// than the requested capacity. Use the array's `capacity` property to
1166
- /// determine the size of the new storage.
1238
+ /// For performance reasons, the newly allocated storage may be larger than
1239
+ /// the requested capacity. Use the array's `capacity` property to determine
1240
+ /// the size of the new storage.
1167
1241
///
1168
1242
/// - Parameter minimumCapacity: The requested number of elements to store.
1243
+ ///
1169
1244
/// - Complexity: O(*n*), where *n* is the count of the array.
1170
1245
@_semantics ( " array.mutate_unknown " )
1171
1246
public mutating func reserveCapacity( _ minimumCapacity: Int) {
@@ -1652,10 +1727,10 @@ extension ${Self} {
1652
1727
/// removed.
1653
1728
///
1654
1729
/// In this example, three elements in the middle of an array of integers are
1655
- /// replaced by the five elements of a `Repeat <Int>` instance.
1730
+ /// replaced by the five elements of a `Repeated <Int>` instance.
1656
1731
///
1657
1732
/// var nums = [10, 20, 30, 40, 50]
1658
- /// nums.replaceSubrange(1...3, with: Repeat(count: 5, repeatedValue: 1) ))
1733
+ /// nums.replaceSubrange(1...3, with: repeatElement(1, count: 5 ))
1659
1734
/// print(nums)
1660
1735
/// // Prints "[10, 1, 1, 1, 1, 1, 50]"
1661
1736
///
0 commit comments