|
12 | 12 |
|
13 | 13 | /// A fixed-size array. |
14 | 14 | /// |
15 | | -/// The `InlineArray` type is a specialized array that stores its elements |
16 | | -/// contiguously inline, rather than allocating an out-of-line region of memory |
17 | | -/// with copy-on-write optimization. |
| 15 | +/// An `InlineArray` is a specialized container that doesn't use a separate |
| 16 | +/// memory allocation just to store its elements. When a value is copied, all of |
| 17 | +/// its elements are copied eagerly, like those of a tuple. Use an `InlineArray` |
| 18 | +/// when you have a fixed number of elements and need to avoid a separate heap |
| 19 | +/// allocation. |
18 | 20 | /// |
19 | | -/// Memory Layout |
20 | | -/// ------------- |
| 21 | +/// Initializing a Value |
| 22 | +/// -------------------- |
| 23 | +/// |
| 24 | +/// When initializing a new `InlineArray` value, you must initialize all of its |
| 25 | +/// elements. You can use an array literal just as with `Array`, rely on type |
| 26 | +/// inference for the `count` and `Element` type, and spell the type with the |
| 27 | +/// shorthand `[count of Element]`. |
| 28 | +/// |
| 29 | +/// let a: InlineArray<3, Int> = [1, 2, 3] |
| 30 | +/// let b: InlineArray<_, Int> = [1, 2, 3] |
| 31 | +/// let c: InlineArray<3, _> = [1, 2, 3] |
| 32 | +/// let d: InlineArray = [1, 2, 3] |
| 33 | +/// |
| 34 | +/// let e: [3 of Int] = [1, 2, 3] |
| 35 | +/// let f: [_ of Int] = [1, 2, 3] |
| 36 | +/// let g: [3 of _] = [1, 2, 3] |
| 37 | +/// let h: [_ of _] = [1, 2, 3] |
| 38 | +/// |
| 39 | +/// You can also use one of the type's initializers to create a new value. |
| 40 | +/// |
| 41 | +/// Accessing Elements |
| 42 | +/// ------------------ |
| 43 | +/// |
| 44 | +/// Just as with `Array`, you can read and modify an element in an `InlineArray` |
| 45 | +/// using a subscript. Unless you use the memory-unsafe `unchecked` subscript, |
| 46 | +/// any index you provide is subject to bounds checking; an invalid index |
| 47 | +/// triggers a runtime error in your program. |
21 | 48 | /// |
22 | | -/// An *empty* array's size is zero. Its stride and alignment are one byte. |
| 49 | +/// var values: [3 of Double] = [1, 1.5, 2] |
| 50 | +/// print(values[0]) // Prints "1.0" |
| 51 | +/// values[1] -= 0.25 |
| 52 | +/// print(values[1]) // Prints "1.25" |
| 53 | +/// values[3] = 42.0 // Fatal error: Index out of bounds |
23 | 54 | /// |
24 | | -/// A *nonempty* array's size and stride are equal to the element's stride |
25 | | -/// multiplied by the number of elements. Its alignment is equal to the |
26 | | -/// element's alignment. |
| 55 | +/// You can use the `indices` property to iterate over all elements in order. |
| 56 | +/// |
| 57 | +/// for index in values.indices { |
| 58 | +/// print(values[index]) |
| 59 | +/// } |
| 60 | +/// |
| 61 | +/// Working with Noncopyable Elements |
| 62 | +/// --------------------------------- |
| 63 | +/// |
| 64 | +/// An `InlineArray` can store elements of potentially noncopyable type. When |
| 65 | +/// `Element` isn't copyable, the `InlineArray` itself also isn't copyable. You |
| 66 | +/// must then explicitly move or consume the value if you want to transfer |
| 67 | +/// ownership. |
| 68 | +/// |
| 69 | +/// Memory Layout |
| 70 | +/// ------------- |
27 | 71 | /// |
28 | | -/// MemoryLayout<InlineArray<3, UInt16>>.size //-> 6 |
29 | | -/// MemoryLayout<InlineArray<3, UInt16>>.stride //-> 6 |
30 | | -/// MemoryLayout<InlineArray<3, UInt16>>.alignment //-> 2 |
| 72 | +/// An `InlineArray` stores its elements contiguously. If an `InlineArray` is a |
| 73 | +/// stored property of a class, then it's allocated on the heap along with the |
| 74 | +/// other stored properties of the class. Otherwise, in general, an |
| 75 | +/// `InlineArray` is allocated on the stack. |
31 | 76 | /// |
32 | | -/// Literal Initialization |
33 | | -/// ---------------------- |
| 77 | +/// A *non-empty* `InlineArray`'s size and stride are both found by multiplying |
| 78 | +/// the `count` of elements by the `Element`'s stride. Its alignment is equal to |
| 79 | +/// the `Element`'s alignment. |
34 | 80 | /// |
35 | | -/// Array literal syntax can be used to initialize an `InlineArray` instance. |
36 | | -/// A stack-allocated array will do in-place initialization of each element. |
37 | | -/// The `count` and/or `Element` can be inferred from the array literal. |
| 81 | +/// struct Record { |
| 82 | +/// let x: UInt32 |
| 83 | +/// let y: Bool |
| 84 | +/// } |
| 85 | +/// MemoryLayout<Record>.size // 5 |
| 86 | +/// MemoryLayout<Record>.stride // 8 |
| 87 | +/// MemoryLayout<Record>.alignment // 4 |
| 88 | +/// MemoryLayout<[2 of Record]>.size // 16 |
| 89 | +/// MemoryLayout<[2 of Record]>.stride // 16 |
| 90 | +/// MemoryLayout<[2 of Record]>.alignment // 4 |
| 91 | +/// MemoryLayout<(Record, Record)>.size // 13 |
| 92 | +/// MemoryLayout<(Record, Record)>.stride // 16 |
| 93 | +/// MemoryLayout<(Record, Record)>.alignment // 4 |
38 | 94 | /// |
39 | | -/// let a: InlineArray<4, Int> = [1, 2, 4, 8] |
40 | | -/// let b: InlineArray<_, Int> = [1, 2, 4, 8] |
41 | | -/// let c: InlineArray<4, _> = [1, 2, 4, 8] |
42 | | -/// let d: InlineArray = [1, 2, 4, 8] |
| 95 | +/// An *empty* `InlineArray`'s size is zero. Its stride and alignment are both |
| 96 | +/// one byte. |
43 | 97 | @available(SwiftStdlib 6.2, *) |
44 | 98 | @frozen |
45 | 99 | @safe |
@@ -203,7 +257,7 @@ extension InlineArray where Element: ~Copyable { |
203 | 257 | /// count of the array, to initialize every element by passing the closure |
204 | 258 | /// the index of the current element being initialized. |
205 | 259 | /// |
206 | | - /// InlineArray<4, Int> { 1 << $0 } //-> [1, 2, 4, 8] |
| 260 | + /// InlineArray<4, Int> { $0 * 2 } // [0, 2, 4, 6] |
207 | 261 | /// |
208 | 262 | /// The closure is allowed to throw an error at any point during |
209 | 263 | /// initialization at which point the array will stop initialization, |
@@ -242,7 +296,7 @@ extension InlineArray where Element: ~Copyable { |
242 | 296 | /// count of the array, to initialize every element by passing the closure an |
243 | 297 | /// immutable borrow reference to the preceding element. |
244 | 298 | /// |
245 | | - /// InlineArray<4, Int>(first: 1) { $0 << 1 } //-> [1, 2, 4, 8] |
| 299 | + /// InlineArray<4, Int>(first: 1) { $0 * 2 } // [1, 2, 4, 8] |
246 | 300 | /// |
247 | 301 | /// The closure is allowed to throw an error at any point during |
248 | 302 | /// initialization at which point the array will stop initialization, |
|
0 commit comments