@@ -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
@@ -1138,21 +1147,21 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
1138
1147
/// The total number of elements that the array can contain using its current
1139
1148
/// storage.
1140
1149
///
1141
- /// If the array grows larger than its capacity, it discards its current
1142
- /// storage and allocates a larger one.
1150
+ /// If you add more elements to an array than it can hold with its current
1151
+ /// capacity, it discards its current storage and allocates a larger one.
1143
1152
///
1144
1153
/// The following example creates an array of integers from an array literal,
1145
1154
/// then appends the elements of another collection. Before appending, the
1146
1155
/// array allocates new storage that is large enough store the resulting
1147
1156
/// elements.
1148
1157
///
1149
1158
/// var numbers = [10, 20, 30, 40, 50]
1150
- /// print("Count: \( numbers.count), capacity: \(numbers.capacity)")
1151
- /// // Prints "Count: 5, capacity: 5"
1159
+ /// // numbers.count == 5
1160
+ /// // numbers.capacity == 5
1152
1161
///
1153
1162
/// numbers.append(contentsOf: stride(from: 60, through: 100, by: 10))
1154
- /// print("Count: \( numbers.count), capacity: \(numbers.capacity)")
1155
- /// // Prints "Count: 10, capacity: 12"
1163
+ /// // numbers.count == 10
1164
+ /// // numbers. capacity == 12
1156
1165
public var capacity : Int {
1157
1166
return _getCapacity ( )
1158
1167
}
@@ -1184,13 +1193,19 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
1184
1193
/// unique, mutable, contiguous storage, with space allocated for at least
1185
1194
/// the requested number of elements.
1186
1195
///
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.
1196
+ % if Self != 'ContiguousArray':
1197
+ /// Calling the `reserveCapacity(_:)` method on an array with bridged storage
1198
+ /// triggers a copy to contiguous storage even if the existing storage
1199
+ /// has room to store `minimumCapacity` elements.
1200
+ ///
1201
+ % end
1202
+ /// For performance reasons, the size of the newly allocated storage might be
1203
+ /// greater than the requested capacity. Use the array's `capacity` property
1204
+ /// to determine the size of the new storage.
1190
1205
///
1191
1206
/// - Parameter minimumCapacity: The requested number of elements to store.
1192
1207
///
1193
- /// - Complexity: O(*n*), where *n* is the count of the array.
1208
+ /// - Complexity: O(*n*), where *n* is the number of elements in the array.
1194
1209
@_semantics ( " array.mutate_unknown " )
1195
1210
public mutating func reserveCapacity( _ minimumCapacity: Int ) {
1196
1211
if _buffer. requestUniqueMutableBackingBuffer (
@@ -1515,7 +1530,6 @@ extension ${Self} {
1515
1530
1516
1531
extension ${ Self} {
1517
1532
/// Calls a closure with a pointer to the array's contiguous storage.
1518
- /// ${contiguousCaveat}
1519
1533
///
1520
1534
/// Often, the optimizer can eliminate bounds checks within an array
1521
1535
/// algorithm, but when that fails, invoking the same algorithm on the
@@ -1534,9 +1548,13 @@ extension ${Self} {
1534
1548
/// }
1535
1549
/// // 'sum' == 9
1536
1550
///
1551
+ /// The pointer passed as an argument to `body` is valid only for the
1552
+ /// lifetime of the closure. Do not escape it from the closure for later
1553
+ /// use.
1554
+ ///
1537
1555
/// - 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
1556
+ /// points to the contiguous storage for the array. ${contiguousCaveat} If
1557
+ /// `body` has a return value, it is used as the return value for the
1540
1558
/// `withUnsafeBufferPointer(_:)` method. The pointer argument is valid
1541
1559
/// only for the duration of the closure's execution.
1542
1560
/// - Returns: The return value of the `body` closure parameter, if any.
@@ -1549,13 +1567,13 @@ extension ${Self} {
1549
1567
}
1550
1568
1551
1569
/// Calls the given closure with a pointer to the array's mutable contiguous
1552
- /// storage.${contiguousCaveat}
1570
+ /// storage.
1553
1571
///
1554
1572
/// Often, the optimizer can eliminate bounds checks within an array
1555
1573
/// algorithm, but when that fails, invoking the same algorithm on the
1556
1574
/// buffer pointer passed into your closure lets you trade safety for speed.
1557
1575
///
1558
- /// The following example shows modifying the contents of the
1576
+ /// The following example shows how modifying the contents of the
1559
1577
/// `UnsafeMutableBufferPointer` argument to `body` alters the contents of
1560
1578
/// the array:
1561
1579
///
@@ -1568,16 +1586,21 @@ extension ${Self} {
1568
1586
/// print(numbers)
1569
1587
/// // Prints "[2, 1, 4, 3, 5]"
1570
1588
///
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
1589
+ /// The pointer passed as an argument to `body` is valid only for the
1590
+ /// lifetime of the closure. Do not escape it from the closure for later
1591
+ /// use.
1592
+ ///
1593
+ /// - Warning: Do not rely on anything about the array that is the target of
1594
+ /// this method during execution of the `body` closure; it might not
1595
+ /// appear to have its correct value. Instead, use only the
1574
1596
/// `UnsafeMutableBufferPointer` argument to `body`.
1575
1597
///
1576
1598
/// - 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.
1599
+ /// parameter that points to the contiguous storage for the array.
1600
+ /// ${contiguousCaveat} If `body` has a return value, it is used as the
1601
+ /// return value for the `withUnsafeMutableBufferPointer(_:)` method. The
1602
+ /// pointer argument is valid only for the duration of the closure's
1603
+ /// execution.
1581
1604
/// - Returns: The return value of the `body` closure parameter, if any.
1582
1605
///
1583
1606
/// - SeeAlso: `withUnsafeBufferPointer`, `UnsafeMutableBufferPointer`
@@ -2099,28 +2122,42 @@ public func != <Element : Equatable>(
2099
2122
}
2100
2123
2101
2124
extension ${ Self} {
2102
- /// Calls a closure with a view of the array's underlying bytes of memory as a
2103
- /// Collection of `UInt8` .
2125
+ /// Calls the given closure with a pointer to the underlying bytes of the
2126
+ /// array's mutable contiguous storage .
2104
2127
///
2105
- /// ${contiguousCaveat}
2128
+ /// The array's `Element` type must be a *trivial type*, which can be copied
2129
+ /// with just a bit-for-bit copy without any indirection or
2130
+ /// reference-counting operations. Generally, native Swift types that do not
2131
+ /// contain strong or weak references are trivial, as are imported C structs
2132
+ /// and enums.
2106
2133
///
2107
- /// - Precondition: `Pointee` is a trivial type.
2134
+ /// The following example copies bytes from the `byteValues` array into
2135
+ /// `numbers`, an array of `Int`:
2108
2136
///
2109
- /// The following example shows how you copy bytes into an array:
2137
+ /// var numbers: [Int32] = [0, 0]
2138
+ /// var byteValues: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]
2110
2139
///
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
- /// }
2118
- ///
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.
2140
+ /// numbers.withUnsafeMutableBytes { destBytes in
2141
+ /// byteValues.withUnsafeBytes { srcBytes in
2142
+ /// destBytes.copyBytes(from: srcBytes)
2143
+ /// }
2144
+ /// }
2145
+ /// // numbers == [1, 2]
2146
+ ///
2147
+ /// The pointer passed as an argument to `body` is valid only for the
2148
+ /// lifetime of the closure. Do not escape it from the closure for later
2149
+ /// use.
2150
+ ///
2151
+ /// - Warning: Do not rely on anything about the array that is the target of
2152
+ /// this method during execution of the `body` closure; it might not
2153
+ /// appear to have its correct value. Instead, use only the
2154
+ /// `UnsafeMutableRawBufferPointer` argument to `body`.
2155
+ ///
2156
+ /// - Parameter body: A closure with an `UnsafeMutableRawBufferPointer`
2157
+ /// parameter that points to the contiguous storage for the array.
2158
+ /// ${contiguousCaveat} If `body` has a return value, it is used as the
2159
+ /// return value for the `withUnsafeMutableBytes(_:)` method. The argument
2160
+ /// is valid only for the duration of the closure's execution.
2124
2161
/// - Returns: The return value of the `body` closure parameter, if any.
2125
2162
///
2126
2163
/// - SeeAlso: `withUnsafeBytes`, `UnsafeMutableRawBufferPointer`
@@ -2132,27 +2169,24 @@ extension ${Self} {
2132
2169
}
2133
2170
}
2134
2171
2135
- /// Calls a closure with a view of the array's underlying bytes of memory
2136
- /// as a Collection of `UInt8` .
2172
+ /// Calls the given closure with a pointer to the underlying bytes of the
2173
+ /// array's contiguous storage .
2137
2174
///
2138
- /// ${contiguousCaveat}
2139
- ///
2140
- /// - Precondition: `Pointee` is a trivial type.
2141
- ///
2142
- /// The following example shows how you copy the contents of an array into a
2175
+ /// The following example copies the bytes of the `numbers` array into a
2143
2176
/// buffer of `UInt8`:
2144
2177
///
2145
- /// let numbers = [1, 2, 3]
2146
- /// var byteBuffer = [UInt8]()
2147
- /// numbers.withUnsafeBytes {
2148
- /// byteBuffer += $0
2149
- /// }
2178
+ /// var numbers = [1, 2, 3]
2179
+ /// var byteBuffer: [UInt8] = []
2180
+ /// numbers.withUnsafeBytes {
2181
+ /// byteBuffer.append(contentsOf: $0)
2182
+ /// }
2183
+ /// // byteBuffer == [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, ...]
2150
2184
///
2151
2185
/// - 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.
2186
+ /// that points to the contiguous storage for the array.
2187
+ /// ${contiguousCaveat} If `body` has a return value, it is used as the
2188
+ /// return value for the `withUnsafeBytes(_:)` method. The argument is
2189
+ /// valid only for the duration of the closure's execution.
2156
2190
/// - Returns: The return value of the `body` closure parameter, if any.
2157
2191
///
2158
2192
/// - SeeAlso: `withUnsafeBytes`, `UnsafeRawBufferPointer`
0 commit comments