Skip to content

Commit e26bd64

Browse files
committed
Merge pull request #2586 from natecook1000/nc-revise-indices
[stdlib] Revise documentation for new indexing model
2 parents 843355c + 982e3d0 commit e26bd64

24 files changed

+2288
-1316
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ extension _ArrayBuffer {
200200
}
201201

202202
/// Copy the elements in `bounds` from this buffer into uninitialized
203-
/// memory starting at `target`. Return a pointer past-the-end of the
203+
/// memory starting at `target`. Return a pointer "past the end" of the
204204
/// just-initialized memory.
205205
@discardableResult
206206
public func _copyContents(

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public protocol _ArrayBufferProtocol
2727
init(_ buffer: _ContiguousArrayBuffer<Element>, shiftedToStartIndex: Int)
2828

2929
/// Copy the elements in `bounds` from this buffer into uninitialized
30-
/// memory starting at `target`. Return a pointer past-the-end of the
30+
/// memory starting at `target`. Return a pointer "past the end" of the
3131
/// just-initialized memory.
3232
@discardableResult
3333
func _copyContents(

stdlib/public/core/Arrays.swift.gyb

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,11 @@ public struct ${Self}<Element>
482482
%end
483483
}
484484

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
486486
/// subscript argument.
487487
///
488488
/// 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
490490
/// creates a range that doesn't include the upper bound, so it's always
491491
/// safe to use with `endIndex`. For example:
492492
///
@@ -543,6 +543,32 @@ public struct ${Self}<Element>
543543
i -= 1
544544
}
545545

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)
546572
@warn_unused_result
547573
public func index(_ i: Int, offsetBy n: Int) -> Int {
548574
// NOTE: this is a manual specialization of index movement for a Strideable
@@ -553,6 +579,47 @@ public struct ${Self}<Element>
553579
return i + n
554580
}
555581

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)
556623
@warn_unused_result
557624
public func index(
558625
_ i: Int, offsetBy n: Int, limitedBy limit: Int
@@ -569,6 +636,13 @@ public struct ${Self}<Element>
569636
return i + n
570637
}
571638

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`.
572646
@warn_unused_result
573647
public func distance(from start: Int, to end: Int) -> Int {
574648
// NOTE: this is a manual specialization of index movement for a Strideable
@@ -1013,7 +1087,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
10131087
/// value.
10141088
///
10151089
/// Here's an example of creating an array initialized with five strings
1016-
/// containing the letter "Z".
1090+
/// containing the letter *Z*.
10171091
///
10181092
/// let fiveZs = Array(repeating: "Z", count: 5)
10191093
/// print(fiveZs)
@@ -1156,16 +1230,17 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
11561230

11571231
/// Reserves enough space to store the specified number of elements.
11581232
///
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.
11631237
///
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.
11671241
///
11681242
/// - Parameter minimumCapacity: The requested number of elements to store.
1243+
///
11691244
/// - Complexity: O(*n*), where *n* is the count of the array.
11701245
@_semantics("array.mutate_unknown")
11711246
public mutating func reserveCapacity(_ minimumCapacity: Int) {
@@ -1652,10 +1727,10 @@ extension ${Self} {
16521727
/// removed.
16531728
///
16541729
/// 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.
16561731
///
16571732
/// 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))
16591734
/// print(nums)
16601735
/// // Prints "[10, 1, 1, 1, 1, 1, 50]"
16611736
///

0 commit comments

Comments
 (0)