13
13
@_exported import ModelIO
14
14
import simd
15
15
16
+ extension Array {
17
+ fileprivate init (
18
+ unsafeUninitializedCount count: Int ,
19
+ initializedWith initializer: ( UnsafeMutablePointer < Element > ) -> Void
20
+ ) {
21
+ self . init ( unsafeUninitializedCapacity: count) { buffer, initializedCount in
22
+ initializer ( buffer. baseAddress!)
23
+ initializedCount = count
24
+ }
25
+ }
26
+ }
27
+
16
28
@available ( macOS, introduced: 10.13 )
17
29
@available ( iOS, introduced: 11.0 )
18
30
@available ( tvOS, introduced: 11.0 )
19
31
extension MDLMatrix4x4Array {
20
32
@nonobjc public var float4x4Array : [ float4x4 ] {
21
33
get {
22
34
let count = elementCount
23
- var values = [ float4x4] ( repeating : float4x4 ( ) , count : Int ( count ) )
24
- __getFloat4x4Array ( & values [ 0 ] , maxCount: count)
25
- return values
35
+ return [ float4x4] ( unsafeUninitializedCount : count ) { ptr in
36
+ __getFloat4x4Array ( ptr , maxCount: count)
37
+ }
26
38
}
27
39
set ( array) {
28
40
__setFloat4x4 ( array, count: array. count)
@@ -32,9 +44,9 @@ extension MDLMatrix4x4Array {
32
44
@nonobjc public var double4x4Array : [ double4x4 ] {
33
45
get {
34
46
let count = elementCount
35
- var values = [ double4x4] ( repeating : double4x4 ( ) , count : Int ( count ) )
36
- __getDouble4x4Array ( & values [ 0 ] , maxCount: count)
37
- return values
47
+ return [ double4x4] ( unsafeUninitializedCount : count ) { ptr in
48
+ __getDouble4x4Array ( ptr , maxCount: count)
49
+ }
38
50
}
39
51
set ( array) {
40
52
__setDouble4x4 ( array, count: array. count)
@@ -50,9 +62,9 @@ extension MDLMatrix4x4Array {
50
62
extension MDLAnimatedValue {
51
63
@nonobjc public var times : [ TimeInterval ] {
52
64
get {
53
- var times = [ TimeInterval] ( repeating : 0 , count : Int ( timeSampleCount) )
54
- __getTimes ( & times [ 0 ] , maxCount: timeSampleCount)
55
- return times
65
+ return [ TimeInterval] ( unsafeUninitializedCount : timeSampleCount) { ptr in
66
+ __getTimes ( ptr , maxCount: timeSampleCount)
67
+ }
56
68
}
57
69
}
58
70
}
@@ -70,15 +82,15 @@ extension MDLAnimatedScalarArray {
70
82
}
71
83
72
84
@nonobjc public func floatArray( atTime time: TimeInterval ) -> [ Float ] {
73
- var values = [ Float] ( repeating : 0 , count : Int ( elementCount) )
74
- __getFloat ( & values [ 0 ] , maxCount: elementCount, atTime: time)
75
- return values
85
+ return [ Float] ( unsafeUninitializedCount : elementCount) { ptr in
86
+ __getFloat ( ptr , maxCount: elementCount, atTime: time)
87
+ }
76
88
}
77
89
78
90
@nonobjc public func doubleArray( atTime time: TimeInterval ) -> [ Double ] {
79
- var values = [ Double] ( repeating : 0 , count : Int ( elementCount) )
80
- __getDouble ( & values [ 0 ] , maxCount: elementCount, atTime: time)
81
- return values
91
+ return [ Double] ( unsafeUninitializedCount : elementCount) { ptr in
92
+ __getDouble ( ptr , maxCount: elementCount, atTime: time)
93
+ }
82
94
}
83
95
84
96
@nonobjc public func reset( floatArray array: [ Float ] , atTimes times: [ TimeInterval ] ) {
@@ -92,18 +104,18 @@ extension MDLAnimatedScalarArray {
92
104
@nonobjc public var floatArray : [ Float ] {
93
105
get {
94
106
let count = elementCount * timeSampleCount
95
- var values = [ Float] ( repeating : 0 , count: Int ( count ) )
96
- __getFloat ( & values [ 0 ] , maxCount: count)
97
- return values
107
+ return [ Float] ( unsafeUninitializedCount : count) { ptr in
108
+ __getFloat ( ptr , maxCount: count)
109
+ }
98
110
}
99
111
}
100
112
101
113
@nonobjc public var doubleArray : [ Double ] {
102
114
get {
103
115
let count = elementCount * timeSampleCount
104
- var values = [ Double] ( repeating : 0 , count: Int ( count ) )
105
- __getDouble ( & values [ 0 ] , maxCount: count)
106
- return values
116
+ return [ Double] ( unsafeUninitializedCount : count) { ptr in
117
+ __getDouble ( ptr , maxCount: count)
118
+ }
107
119
}
108
120
}
109
121
}
@@ -121,15 +133,15 @@ extension MDLAnimatedVector3Array {
121
133
}
122
134
123
135
@nonobjc public func float3Array( atTime time: TimeInterval ) -> [ SIMD3 < Float > ] {
124
- var values = [ SIMD3 < Float > ] ( repeating : SIMD3 < Float > ( ) , count : Int ( elementCount ) )
125
- __getFloat3Array ( & values [ 0 ] , maxCount: elementCount, atTime: time)
126
- return values
136
+ return [ SIMD3 < Float > ] ( unsafeUninitializedCount : elementCount ) { ptr in
137
+ __getFloat3Array ( ptr , maxCount: elementCount, atTime: time)
138
+ }
127
139
}
128
140
129
141
@nonobjc public func double3Array( atTime time: TimeInterval ) -> [ SIMD3 < Double > ] {
130
- var values = [ SIMD3 < Double > ] ( repeating : SIMD3 < Double > ( ) , count : Int ( elementCount ) )
131
- __getDouble3Array ( & values [ 0 ] , maxCount: elementCount, atTime: time)
132
- return values
142
+ return [ SIMD3 < Double > ] ( unsafeUninitializedCount : elementCount ) { ptr in
143
+ __getDouble3Array ( ptr , maxCount: elementCount, atTime: time)
144
+ }
133
145
}
134
146
135
147
@nonobjc public func reset( float3Array array: [ SIMD3 < Float > ] , atTimes times: [ TimeInterval ] ) {
@@ -143,18 +155,18 @@ extension MDLAnimatedVector3Array {
143
155
@nonobjc public var float3Array : [ SIMD3 < Float > ] {
144
156
get {
145
157
let count = elementCount * timeSampleCount
146
- var values = [ SIMD3 < Float > ] ( repeating : SIMD3 < Float > ( ) , count : Int ( count ) )
147
- __getFloat3Array ( & values [ 0 ] , maxCount: count)
148
- return values
158
+ return [ SIMD3 < Float > ] ( unsafeUninitializedCount : count ) { ptr in
159
+ __getFloat3Array ( ptr , maxCount: count)
160
+ }
149
161
}
150
162
}
151
163
152
164
@nonobjc public var double3Array : [ SIMD3 < Double > ] {
153
165
get {
154
166
let count = elementCount * timeSampleCount
155
- var values = [ SIMD3 < Double > ] ( repeating : SIMD3 < Double > ( ) , count : Int ( count ) )
156
- __getDouble3Array ( & values [ 0 ] , maxCount: count)
157
- return values
167
+ return [ SIMD3 < Double > ] ( unsafeUninitializedCount : count ) { ptr in
168
+ __getDouble3Array ( ptr , maxCount: count)
169
+ }
158
170
}
159
171
}
160
172
}
@@ -172,15 +184,15 @@ extension MDLAnimatedQuaternionArray {
172
184
}
173
185
174
186
@nonobjc public func floatQuaternionArray( atTime time: TimeInterval ) -> [ simd_quatf ] {
175
- var values = [ simd_quatf] ( repeating : simd_quatf ( ) , count : Int ( elementCount ) )
176
- __getFloat ( & values [ 0 ] , maxCount: elementCount, atTime: time)
177
- return values
187
+ return [ simd_quatf] ( unsafeUninitializedCount : elementCount ) { ptr in
188
+ __getFloat ( ptr , maxCount: elementCount, atTime: time)
189
+ }
178
190
}
179
191
180
192
@nonobjc public func doubleQuaternionArray( atTime time: TimeInterval ) -> [ simd_quatd ] {
181
- var values = [ simd_quatd] ( repeating : simd_quatd ( ) , count : Int ( elementCount ) )
182
- __getDouble ( & values [ 0 ] , maxCount: elementCount, atTime: time)
183
- return values
193
+ return [ simd_quatd] ( unsafeUninitializedCount : elementCount ) { ptr in
194
+ __getDouble ( ptr , maxCount: elementCount, atTime: time)
195
+ }
184
196
}
185
197
186
198
@nonobjc public func reset( floatQuaternionArray array: [ simd_quatf ] , atTimes times: [ TimeInterval ] ) {
@@ -194,18 +206,18 @@ extension MDLAnimatedQuaternionArray {
194
206
@nonobjc public var floatQuaternionArray : [ simd_quatf ] {
195
207
get {
196
208
let count = elementCount * timeSampleCount
197
- var values = [ simd_quatf] ( repeating : simd_quatf ( ) , count : Int ( count ) )
198
- __getFloat ( & values [ 0 ] , maxCount: count)
199
- return values
209
+ return [ simd_quatf] ( unsafeUninitializedCount : count ) { ptr in
210
+ __getFloat ( ptr , maxCount: count)
211
+ }
200
212
}
201
213
}
202
214
203
215
@nonobjc public var doubleQuaternionArray : [ simd_quatd ] {
204
216
get {
205
217
let count = elementCount * timeSampleCount
206
- var values = [ simd_quatd] ( repeating : simd_quatd ( ) , count : Int ( count ) )
207
- __getDouble ( & values [ 0 ] , maxCount: count)
208
- return values
218
+ return [ simd_quatd] ( unsafeUninitializedCount : count ) { ptr in
219
+ __getDouble ( ptr , maxCount: count)
220
+ }
209
221
}
210
222
}
211
223
}
@@ -224,17 +236,17 @@ extension MDLAnimatedScalar {
224
236
225
237
@nonobjc public var floatArray : [ Float ] {
226
238
get {
227
- var values = [ Float] ( repeating : 0 , count : Int ( timeSampleCount) )
228
- __getFloatArray ( & values [ 0 ] , maxCount: timeSampleCount)
229
- return values
239
+ return [ Float] ( unsafeUninitializedCount : timeSampleCount) { ptr in
240
+ __getFloatArray ( ptr , maxCount: timeSampleCount)
241
+ }
230
242
}
231
243
}
232
244
233
245
@nonobjc public var doubleArray : [ Double ] {
234
246
get {
235
- var values = [ Double] ( repeating : 0 , count : Int ( timeSampleCount) )
236
- __getDoubleArray ( & values [ 0 ] , maxCount: timeSampleCount)
237
- return values
247
+ return [ Double] ( unsafeUninitializedCount : timeSampleCount) { ptr in
248
+ __getDoubleArray ( ptr , maxCount: timeSampleCount)
249
+ }
238
250
}
239
251
}
240
252
}
@@ -253,17 +265,17 @@ extension MDLAnimatedVector2 {
253
265
254
266
@nonobjc public var float2Array : [ SIMD2 < Float > ] {
255
267
get {
256
- var values = [ SIMD2 < Float > ] ( repeating : SIMD2 < Float > ( ) , count : Int ( timeSampleCount ) )
257
- __getFloat2Array ( & values [ 0 ] , maxCount: timeSampleCount)
258
- return values
268
+ return [ SIMD2 < Float > ] ( unsafeUninitializedCount : timeSampleCount ) { ptr in
269
+ __getFloat2Array ( ptr , maxCount: timeSampleCount)
270
+ }
259
271
}
260
272
}
261
273
262
274
@nonobjc public var double2Array : [ SIMD2 < Double > ] {
263
275
get {
264
- var values = [ SIMD2 < Double > ] ( repeating : SIMD2 < Double > ( ) , count : Int ( timeSampleCount ) )
265
- __getDouble2Array ( & values [ 0 ] , maxCount: timeSampleCount)
266
- return values
276
+ return [ SIMD2 < Double > ] ( unsafeUninitializedCount : timeSampleCount ) { ptr in
277
+ __getDouble2Array ( ptr , maxCount: timeSampleCount)
278
+ }
267
279
}
268
280
}
269
281
}
@@ -282,17 +294,17 @@ extension MDLAnimatedVector3 {
282
294
283
295
@nonobjc public var float3Array : [ SIMD3 < Float > ] {
284
296
get {
285
- var values = [ SIMD3 < Float > ] ( repeating : SIMD3 < Float > ( ) , count : Int ( timeSampleCount ) )
286
- __getFloat3Array ( & values [ 0 ] , maxCount: timeSampleCount)
287
- return values
297
+ return [ SIMD3 < Float > ] ( unsafeUninitializedCount : timeSampleCount ) { ptr in
298
+ __getFloat3Array ( ptr , maxCount: timeSampleCount)
299
+ }
288
300
}
289
301
}
290
302
291
303
@nonobjc public var double3Array : [ SIMD3 < Double > ] {
292
304
get {
293
- var values = [ SIMD3 < Double > ] ( repeating : SIMD3 < Double > ( ) , count : Int ( timeSampleCount ) )
294
- __getDouble3Array ( & values [ 0 ] , maxCount: timeSampleCount)
295
- return values
305
+ return [ SIMD3 < Double > ] ( unsafeUninitializedCount : timeSampleCount ) { ptr in
306
+ __getDouble3Array ( ptr , maxCount: timeSampleCount)
307
+ }
296
308
}
297
309
}
298
310
}
@@ -311,17 +323,17 @@ extension MDLAnimatedVector4 {
311
323
312
324
@nonobjc public var float4Array : [ SIMD4 < Float > ] {
313
325
get {
314
- var values = [ SIMD4 < Float > ] ( repeating : SIMD4 < Float > ( ) , count : Int ( timeSampleCount ) )
315
- __getFloat4Array ( & values [ 0 ] , maxCount: timeSampleCount)
316
- return values
326
+ return [ SIMD4 < Float > ] ( unsafeUninitializedCount : timeSampleCount ) { ptr in
327
+ __getFloat4Array ( ptr , maxCount: timeSampleCount)
328
+ }
317
329
}
318
330
}
319
331
320
332
@nonobjc public var double4Array : [ SIMD4 < Double > ] {
321
333
get {
322
- var values = [ SIMD4 < Double > ] ( repeating : SIMD4 < Double > ( ) , count : Int ( timeSampleCount ) )
323
- __getDouble4Array ( & values [ 0 ] , maxCount: timeSampleCount)
324
- return values
334
+ return [ SIMD4 < Double > ] ( unsafeUninitializedCount : timeSampleCount ) { ptr in
335
+ __getDouble4Array ( ptr , maxCount: timeSampleCount)
336
+ }
325
337
}
326
338
}
327
339
}
@@ -340,18 +352,17 @@ extension MDLAnimatedMatrix4x4 {
340
352
341
353
@nonobjc public var float4x4Array : [ float4x4 ] {
342
354
get {
343
- var values = [ float4x4] ( repeating : float4x4 ( ) , count : Int ( timeSampleCount ) )
344
- __getFloat4x4Array ( & values [ 0 ] , maxCount: timeSampleCount)
345
- return values
355
+ return [ float4x4] ( unsafeUninitializedCount : timeSampleCount ) { ptr in
356
+ __getFloat4x4Array ( ptr , maxCount: timeSampleCount)
357
+ }
346
358
}
347
359
}
348
360
349
361
@nonobjc public var double4x4Array : [ double4x4 ] {
350
362
get {
351
- var values = [ double4x4] ( repeating : double4x4 ( ) , count : Int ( timeSampleCount ) )
352
- __getDouble4x4Array ( & values [ 0 ] , maxCount: timeSampleCount)
353
- return values
363
+ return [ double4x4] ( unsafeUninitializedCount : timeSampleCount ) { ptr in
364
+ __getDouble4x4Array ( ptr , maxCount: timeSampleCount)
365
+ }
354
366
}
355
367
}
356
368
}
357
-
0 commit comments