@@ -37,7 +37,29 @@ internal struct _FixedArray${N}<T> {
37
37
)
38
38
39
39
@_versioned // FIXME(sil-serialize-all)
40
- internal static var _arraySize : Int { return ${ N} }
40
+ var _count : Int8
41
+ }
42
+
43
+
44
+ extension _FixedArray ${ N} {
45
+ @_inlineable // FIXME(sil-serialize-all)
46
+ @_versioned // FIXME(sil-serialize-all)
47
+ internal static var capacity : Int {
48
+ @inline ( __always) get { return ${ N} }
49
+ }
50
+
51
+ @_inlineable // FIXME(sil-serialize-all)
52
+ @_versioned // FIXME(sil-serialize-all)
53
+ internal var capacity : Int {
54
+ @inline ( __always) get { return ${ N} }
55
+ }
56
+
57
+ @_inlineable // FIXME(sil-serialize-all)
58
+ @_versioned // FIXME(sil-serialize-all)
59
+ internal var count : Int {
60
+ @inline ( __always) get { return Int ( truncatingIfNeeded: _count) }
61
+ @inline ( __always) set { _count = Int8 ( newValue) }
62
+ }
41
63
}
42
64
43
65
extension _FixedArray ${ N} : Rando mAccessCollection, MutableCollection {
@@ -52,18 +74,17 @@ extension _FixedArray${N} : RandomAccessCollection, MutableCollection {
52
74
@_inlineable // FIXME(sil-serialize-all)
53
75
@_versioned // FIXME(sil-serialize-all)
54
76
internal var endIndex : Index {
55
- return _FixedArray $ { N } . _arraySize
77
+ return count
56
78
}
57
79
58
- @_versioned // FIXME(sil-serialize-all)
59
- internal var count : Int { return _FixedArray ${ N} . _arraySize }
60
-
61
80
@_inlineable // FIXME(sil-serialize-all)
62
81
@_versioned // FIXME(sil-serialize-all)
63
82
internal subscript( i: Index) - > T {
64
83
@_versioned
65
84
@inline ( __always)
66
85
get {
86
+ let count = self . count // for exclusive access
87
+ _sanityCheck ( i >= 0 && i < count)
67
88
var copy = storage
68
89
let res : T = withUnsafeBytes ( of: & copy) {
69
90
( rawPtr : UnsafeRawBufferPointer ) -> T in
@@ -79,16 +100,9 @@ extension _FixedArray${N} : RandomAccessCollection, MutableCollection {
79
100
@_versioned
80
101
@inline ( __always)
81
102
set {
82
- let count = self . count
83
- withUnsafeBytes ( of: & storage) {
84
- ( rawPtr : UnsafeRawBufferPointer ) -> ( ) in
85
- let rawPtr = UnsafeMutableRawBufferPointer ( mutating: rawPtr)
86
- let stride = MemoryLayout< T> . stride
87
- _sanityCheck ( rawPtr. count == ${ N} * stride, " layout mismatch? " )
88
- let bufPtr = UnsafeMutableBufferPointer (
89
- start: rawPtr. baseAddress!. assumingMemoryBound ( to: T . self) ,
90
- count: count)
91
- bufPtr [ i] = newValue
103
+ _sanityCheck ( i >= 0 && i < count)
104
+ self . withUnsafeMutableBufferPointer { buffer in
105
+ buffer [ i] = newValue
92
106
}
93
107
}
94
108
}
@@ -106,22 +120,45 @@ extension _FixedArray${N} : RandomAccessCollection, MutableCollection {
106
120
internal func index( before i: Index ) -> Index {
107
121
return i- 1
108
122
}
123
+ }
109
124
110
- // TODO: Any customization hooks it's profitable to override, e.g. append?
111
-
125
+ extension _FixedArray ${ N} {
126
+ @_inlineable // FIXME(sil-serialize-all)
127
+ @_versioned
128
+ internal mutating func append( _ newElement: T ) {
129
+ _sanityCheck ( count < capacity)
130
+ self [ count] = newElement
131
+ _count += 1
132
+ }
112
133
}
113
134
114
135
extension _FixedArray ${ N} where T : ExpressibleByIntegerLiteral {
115
136
@_inlineable // FIXME(sil-serialize-all)
116
137
@_versioned // FIXME(sil-serialize-all)
117
138
@inline ( __always)
118
- internal init ( allZeros: ( ) ) {
139
+ internal init ( count: Int ) {
140
+ _sanityCheck ( count >= 0 && count <= _FixedArray${ N} . capacity)
119
141
self . storage = (
120
142
% for i in range( 0 , N- 1 ) :
121
143
0 ,
122
144
% end
123
145
0
124
146
)
147
+ self . _count = Int8 ( truncatingIfNeeded: count)
148
+ }
149
+
150
+ @_inlineable // FIXME(sil-serialize-all)
151
+ @_versioned // FIXME(sil-serialize-all)
152
+ @inline ( __always)
153
+ internal init ( ) {
154
+ self . init ( count: 0 )
155
+ }
156
+
157
+ @_inlineable // FIXME(sil-serialize-all)
158
+ @_versioned // FIXME(sil-serialize-all)
159
+ @inline ( __always)
160
+ internal init ( allZeros: ( ) ) {
161
+ self . init ( count: ${ N} )
125
162
}
126
163
}
127
164
@@ -131,8 +168,10 @@ extension _FixedArray${N} {
131
168
internal mutating func withUnsafeMutableBufferPointer< R> (
132
169
_ body: ( UnsafeMutableBufferPointer < Element > ) throws -> R
133
170
) rethrows -> R {
134
- let count = self . count
171
+ let count = self . count // for exclusive access
135
172
return try withUnsafeMutableBytes ( of: & storage) { rawBuffer in
173
+ _sanityCheck ( rawBuffer. count == ${ N} * MemoryLayout< T> . stride,
174
+ " layout mismatch? " )
136
175
let buffer = UnsafeMutableBufferPointer < Element > (
137
176
start: rawBuffer. baseAddress. _unsafelyUnwrappedUnchecked
138
177
. assumingMemoryBound ( to: Element . self) ,
@@ -146,8 +185,10 @@ extension _FixedArray${N} {
146
185
internal mutating func withUnsafeBufferPointer< R> (
147
186
_ body: ( UnsafeBufferPointer < Element > ) throws -> R
148
187
) rethrows -> R {
149
- let count = self . count
188
+ let count = self . count // for exclusive access
150
189
return try withUnsafeBytes ( of: & storage) { rawBuffer in
190
+ _sanityCheck ( rawBuffer. count == ${ N} * MemoryLayout< T> . stride,
191
+ " layout mismatch? " )
151
192
let buffer = UnsafeBufferPointer < Element > (
152
193
start: rawBuffer. baseAddress. _unsafelyUnwrappedUnchecked
153
194
. assumingMemoryBound ( to: Element . self) ,
0 commit comments