@@ -41,7 +41,7 @@ public struct _DependenceToken {}
41
41
% {
42
42
if True:
43
43
contiguousCaveat = (
44
- ' If no such storage exists, it is first created. ' if Self == 'Array'
44
+ ' If no such storage exists, it is created. ' if Self == 'Array'
45
45
else '')
46
46
47
47
if Self == 'ContiguousArray':
@@ -397,7 +397,7 @@ if True:
397
397
/// Bridging Between Array and NSArray
398
398
/// ==================================
399
399
///
400
- /// When you need to access APIs that expect data in an `NSArray` instance
400
+ /// When you need to access APIs that require data in an `NSArray` instance
401
401
/// instead of `Array`, use the type-cast operator (`as`) to bridge your
402
402
/// instance. For bridging to be possible, the `Element` type of your array
403
403
/// must be a class, an `@objc` protocol (a protocol imported from Objective-C
@@ -406,9 +406,9 @@ if True:
406
406
///
407
407
/// The following example shows how you can bridge an `Array` instance to
408
408
/// `NSArray` to use the `write(to:atomically:)` method. In this example, the
409
- /// `colors` array can be bridged to `NSArray` because its `String` elements
410
- /// bridge to `NSString`. The compiler prevents bridging the `moreColors`
411
- /// array, on the other hand, because its `Element` type is
409
+ /// `colors` array can be bridged to `NSArray` because the `colors` array's
410
+ /// `String` elements bridge to `NSString`. The compiler prevents bridging the
411
+ /// `moreColors` array, on the other hand, because its `Element` type is
412
412
/// `Optional<String>`, which does *not* bridge to a Foundation type.
413
413
///
414
414
/// let colors = ["periwinkle", "rose", "moss"]
@@ -425,14 +425,23 @@ if True:
425
425
/// array's elements are already instances of a class or an `@objc` protocol;
426
426
/// otherwise, it takes O(*n*) time and space.
427
427
///
428
- /// Bridging from `NSArray` to `Array` first calls the `copy(with:)`
428
+ /// When the destination array's element type is a class or an `@objc`
429
+ /// protocol, bridging from `NSArray` to `Array` first calls the `copy(with:)`
429
430
/// (`- copyWithZone:` in Objective-C) method on the array to get an immutable
430
431
/// copy and then performs additional Swift bookkeeping work that takes O(1)
431
432
/// time. For instances of `NSArray` that are already immutable, `copy(with:)`
432
433
/// usually returns the same array in O(1) time; otherwise, the copying
433
- /// performance is unspecified. The instances of `NSArray` and `Array` share
434
- /// storage using the same copy-on-write optimization that is used when two
435
- /// instances of `Array` share storage.
434
+ /// performance is unspecified. If `copy(with:)` returns the same array, the
435
+ /// instances of `NSArray` and `Array` share storage using the same
436
+ /// copy-on-write optimization that is used when two instances of `Array`
437
+ /// share storage.
438
+ ///
439
+ /// When the destination array's element type is a nonclass type that bridges
440
+ /// to a Foundation type, bridging from `NSArray` to `Array` performs a
441
+ /// bridging copy of the elements to contiguous storage in O(*n*) time. For
442
+ /// example, bridging from `NSArray` to `Array<Int>` performs such a copy. No
443
+ /// further bridging is required when accessing elements of the `Array`
444
+ /// instance.
436
445
///
437
446
/// - Note: The `ContiguousArray` and `ArraySlice` types are not bridged;
438
447
/// instances of those types always have a contiguous block of memory as
@@ -1135,24 +1144,31 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
1135
1144
return _getCount ( )
1136
1145
}
1137
1146
1138
- /// The total number of elements that the array can contain using its current
1139
- /// storage.
1147
+ /// The total number of elements that the array can contain without
1148
+ /// allocating new storage.
1140
1149
///
1141
- /// If the array grows larger than its capacity, it discards its current
1142
- /// storage and allocates a larger one.
1150
+ /// Every array reserves a specific amount of memory to hold its contents.
1151
+ /// When you add elements to an array and that array begins to exceed its
1152
+ /// reserved capacity, the array allocates a larger region of memory and
1153
+ /// copies its elements into the new storage. The new storage is a multiple
1154
+ /// of the old storage's size. This exponential growth strategy means that
1155
+ /// appending an element happens in constant time, averaging the performance
1156
+ /// of many append operations. Append operations that trigger reallocation
1157
+ /// have a performance cost, but they occur less and less often as the array
1158
+ /// grows larger.
1143
1159
///
1144
1160
/// The following example creates an array of integers from an array literal,
1145
1161
/// then appends the elements of another collection. Before appending, the
1146
1162
/// array allocates new storage that is large enough store the resulting
1147
1163
/// elements.
1148
1164
///
1149
1165
/// var numbers = [10, 20, 30, 40, 50]
1150
- /// print("Count: \( numbers.count), capacity: \(numbers.capacity)")
1151
- /// // Prints "Count: 5, capacity: 5"
1166
+ /// // numbers.count == 5
1167
+ /// // numbers.capacity == 5
1152
1168
///
1153
1169
/// numbers.append(contentsOf: stride(from: 60, through: 100, by: 10))
1154
- /// print("Count: \( numbers.count), capacity: \(numbers.capacity)")
1155
- /// // Prints "Count: 10, capacity: 12"
1170
+ /// // numbers.count == 10
1171
+ /// // numbers. capacity == 12
1156
1172
public var capacity : Int {
1157
1173
return _getCapacity ( )
1158
1174
}
@@ -1184,13 +1200,19 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
1184
1200
/// unique, mutable, contiguous storage, with space allocated for at least
1185
1201
/// the requested number of elements.
1186
1202
///
1187
- /// For performance reasons, the newly allocated storage may be larger than
1188
- /// the requested capacity. Use the array's `capacity` property to determine
1189
- /// the size of the new storage.
1203
+ % if Self != 'ContiguousArray':
1204
+ /// Calling the `reserveCapacity(_:)` method on an array with bridged storage
1205
+ /// triggers a copy to contiguous storage even if the existing storage
1206
+ /// has room to store `minimumCapacity` elements.
1207
+ ///
1208
+ % end
1209
+ /// For performance reasons, the size of the newly allocated storage might be
1210
+ /// greater than the requested capacity. Use the array's `capacity` property
1211
+ /// to determine the size of the new storage.
1190
1212
///
1191
1213
/// - Parameter minimumCapacity: The requested number of elements to store.
1192
1214
///
1193
- /// - Complexity: O(*n*), where *n* is the count of the array.
1215
+ /// - Complexity: O(*n*), where *n* is the number of elements in the array.
1194
1216
@_semantics ( " array.mutate_unknown " )
1195
1217
public mutating func reserveCapacity( _ minimumCapacity: Int ) {
1196
1218
if _buffer. requestUniqueMutableBackingBuffer (
@@ -1515,7 +1537,6 @@ extension ${Self} {
1515
1537
1516
1538
extension ${ Self} {
1517
1539
/// Calls a closure with a pointer to the array's contiguous storage.
1518
- /// ${contiguousCaveat}
1519
1540
///
1520
1541
/// Often, the optimizer can eliminate bounds checks within an array
1521
1542
/// algorithm, but when that fails, invoking the same algorithm on the
@@ -1534,9 +1555,13 @@ extension ${Self} {
1534
1555
/// }
1535
1556
/// // 'sum' == 9
1536
1557
///
1558
+ /// The pointer passed as an argument to `body` is valid only for the
1559
+ /// lifetime of the closure. Do not escape it from the closure for later
1560
+ /// use.
1561
+ ///
1537
1562
/// - Parameter body: A closure with an `UnsafeBufferPointer` parameter that
1538
- /// points to the contiguous storage for the array. If `body` has a return
1539
- /// value, it is used as the return value for the
1563
+ /// points to the contiguous storage for the array. ${contiguousCaveat} If
1564
+ /// `body` has a return value, it is used as the return value for the
1540
1565
/// `withUnsafeBufferPointer(_:)` method. The pointer argument is valid
1541
1566
/// only for the duration of the closure's execution.
1542
1567
/// - Returns: The return value of the `body` closure parameter, if any.
@@ -1549,13 +1574,13 @@ extension ${Self} {
1549
1574
}
1550
1575
1551
1576
/// Calls the given closure with a pointer to the array's mutable contiguous
1552
- /// storage.${contiguousCaveat}
1577
+ /// storage.
1553
1578
///
1554
1579
/// Often, the optimizer can eliminate bounds checks within an array
1555
1580
/// algorithm, but when that fails, invoking the same algorithm on the
1556
1581
/// buffer pointer passed into your closure lets you trade safety for speed.
1557
1582
///
1558
- /// The following example shows modifying the contents of the
1583
+ /// The following example shows how modifying the contents of the
1559
1584
/// `UnsafeMutableBufferPointer` argument to `body` alters the contents of
1560
1585
/// the array:
1561
1586
///
@@ -1568,16 +1593,21 @@ extension ${Self} {
1568
1593
/// print(numbers)
1569
1594
/// // Prints "[2, 1, 4, 3, 5]"
1570
1595
///
1571
- /// - Warning: Do not rely on anything about `self` (the array that is the
1572
- /// target of this method) during the execution of the `body` closure: It
1573
- /// may not appear to have its correct value. Instead, use only the
1596
+ /// The pointer passed as an argument to `body` is valid only for the
1597
+ /// lifetime of the closure. Do not escape it from the closure for later
1598
+ /// use.
1599
+ ///
1600
+ /// - Warning: Do not rely on anything about the array that is the target of
1601
+ /// this method during execution of the `body` closure; it might not
1602
+ /// appear to have its correct value. Instead, use only the
1574
1603
/// `UnsafeMutableBufferPointer` argument to `body`.
1575
1604
///
1576
1605
/// - Parameter body: A closure with an `UnsafeMutableBufferPointer`
1577
- /// parameter that points to the contiguous storage for the array. If
1578
- /// `body` has a return value, it is used as the return value for the
1579
- /// `withUnsafeMutableBufferPointer(_:)` method. The pointer argument is
1580
- /// valid only for the duration of the closure's execution.
1606
+ /// parameter that points to the contiguous storage for the array.
1607
+ /// ${contiguousCaveat} If `body` has a return value, it is used as the
1608
+ /// return value for the `withUnsafeMutableBufferPointer(_:)` method. The
1609
+ /// pointer argument is valid only for the duration of the closure's
1610
+ /// execution.
1581
1611
/// - Returns: The return value of the `body` closure parameter, if any.
1582
1612
///
1583
1613
/// - SeeAlso: `withUnsafeBufferPointer`, `UnsafeMutableBufferPointer`
@@ -2099,28 +2129,42 @@ public func != <Element : Equatable>(
2099
2129
}
2100
2130
2101
2131
extension ${ Self} {
2102
- /// Calls a closure with a view of the array's underlying bytes of memory as a
2103
- /// Collection of `UInt8`.
2104
- ///
2105
- /// ${contiguousCaveat}
2132
+ /// Calls the given closure with a pointer to the underlying bytes of the
2133
+ /// array's mutable contiguous storage.
2106
2134
///
2107
- /// - Precondition: `Pointee` is a trivial type.
2135
+ /// The array's `Element` type must be a *trivial type*, which can be copied
2136
+ /// with just a bit-for-bit copy without any indirection or
2137
+ /// reference-counting operations. Generally, native Swift types that do not
2138
+ /// contain strong or weak references are trivial, as are imported C structs
2139
+ /// and enums.
2108
2140
///
2109
- /// The following example shows how you copy bytes into an array:
2141
+ /// The following example copies bytes from the `byteValues` array into
2142
+ /// `numbers`, an array of `Int`:
2110
2143
///
2111
- /// var numbers = [Int32](repeating: 0, count: 2)
2112
- /// var byteValues: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]
2113
- /// numbers.withUnsafeMutableBytes { destBytes in
2114
- /// byteValues.withUnsafeBytes { srcBytes in
2115
- /// destBytes.copyBytes(from: srcBytes)
2116
- /// }
2117
- /// }
2144
+ /// var numbers: [Int32] = [0, 0]
2145
+ /// var byteValues: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]
2118
2146
///
2119
- /// - Parameter body: A closure with an `UnsafeRawBufferPointer`
2120
- /// parameter that points to the contiguous storage for the array. If `body`
2121
- /// has a return value, it is used as the return value for the
2122
- /// `withUnsafeMutableBytes(_:)` method. The argument is valid only for the
2123
- /// duration of the closure's execution.
2147
+ /// numbers.withUnsafeMutableBytes { destBytes in
2148
+ /// byteValues.withUnsafeBytes { srcBytes in
2149
+ /// destBytes.copyBytes(from: srcBytes)
2150
+ /// }
2151
+ /// }
2152
+ /// // numbers == [1, 2]
2153
+ ///
2154
+ /// The pointer passed as an argument to `body` is valid only for the
2155
+ /// lifetime of the closure. Do not escape it from the closure for later
2156
+ /// use.
2157
+ ///
2158
+ /// - Warning: Do not rely on anything about the array that is the target of
2159
+ /// this method during execution of the `body` closure; it might not
2160
+ /// appear to have its correct value. Instead, use only the
2161
+ /// `UnsafeMutableRawBufferPointer` argument to `body`.
2162
+ ///
2163
+ /// - Parameter body: A closure with an `UnsafeMutableRawBufferPointer`
2164
+ /// parameter that points to the contiguous storage for the array.
2165
+ /// ${contiguousCaveat} If `body` has a return value, it is used as the
2166
+ /// return value for the `withUnsafeMutableBytes(_:)` method. The argument
2167
+ /// is valid only for the duration of the closure's execution.
2124
2168
/// - Returns: The return value of the `body` closure parameter, if any.
2125
2169
///
2126
2170
/// - SeeAlso: `withUnsafeBytes`, `UnsafeMutableRawBufferPointer`
@@ -2132,30 +2176,33 @@ extension ${Self} {
2132
2176
}
2133
2177
}
2134
2178
2135
- /// Calls a closure with a view of the array's underlying bytes of memory
2136
- /// as a Collection of `UInt8`.
2137
- ///
2138
- /// ${contiguousCaveat}
2179
+ /// Calls the given closure with a pointer to the underlying bytes of the
2180
+ /// array's contiguous storage.
2139
2181
///
2140
- /// - Precondition: `Pointee` is a trivial type.
2182
+ /// The array's `Element` type must be a *trivial type*, which can be copied
2183
+ /// with just a bit-for-bit copy without any indirection or
2184
+ /// reference-counting operations. Generally, native Swift types that do not
2185
+ /// contain strong or weak references are trivial, as are imported C structs
2186
+ /// and enums.
2141
2187
///
2142
- /// The following example shows how you copy the contents of an array into a
2188
+ /// The following example copies the bytes of the `numbers` array into a
2143
2189
/// buffer of `UInt8`:
2144
2190
///
2145
- /// let numbers = [1, 2, 3]
2146
- /// var byteBuffer = [UInt8]()
2147
- /// numbers.withUnsafeBytes {
2148
- /// byteBuffer += $0
2149
- /// }
2191
+ /// var numbers = [1, 2, 3]
2192
+ /// var byteBuffer: [UInt8] = []
2193
+ /// numbers.withUnsafeBytes {
2194
+ /// byteBuffer.append(contentsOf: $0)
2195
+ /// }
2196
+ /// // byteBuffer == [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, ...]
2150
2197
///
2151
2198
/// - Parameter body: A closure with an `UnsafeRawBufferPointer` parameter
2152
- /// that points to the contiguous storage for the array. If `body` has a
2153
- /// return value, it is used as the return value for the
2154
- /// `withUnsafeBytes(_:)` method. The argument is valid only for the
2155
- /// duration of the closure's execution.
2199
+ /// that points to the contiguous storage for the array.
2200
+ /// ${contiguousCaveat} If `body` has a return value, it is used as the
2201
+ /// return value for the `withUnsafeBytes(_:)` method. The argument is
2202
+ /// valid only for the duration of the closure's execution.
2156
2203
/// - Returns: The return value of the `body` closure parameter, if any.
2157
2204
///
2158
- /// - SeeAlso: `withUnsafeBytes `, `UnsafeRawBufferPointer`
2205
+ /// - SeeAlso: `withUnsafeMutableBytes `, `UnsafeRawBufferPointer`
2159
2206
public func withUnsafeBytes< R> (
2160
2207
_ body: ( UnsafeRawBufferPointer ) throws -> R
2161
2208
) rethrows -> R {
0 commit comments