- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathFixedArray.swift
152 lines (135 loc) · 4.26 KB
/
FixedArray.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//===--- FixedArray.swift -------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A helper struct to provide fixed-sized array like functionality
//
//===----------------------------------------------------------------------===//
internalstruct_FixedArray16<T>{
// ABI TODO: This makes assumptions about tuple layout in the ABI, namely that
// they are laid out contiguously and individually addressable (i.e. strided).
//
internalvarstorage:(
// A 16-wide tuple of type T
T,T,T,T,T,T,T,T,
T,T,T,T,T,T,T,T
)
var_count:Int8
}
extension_FixedArray16{
internalstaticvarcapacity:Int{
@inline(__always)get{return16}
}
internalvarcapacity:Int{
@inline(__always)get{return16}
}
internalvarcount:Int{
@inline(__always)get{returnInt(truncatingIfNeeded: _count)}
@inline(__always)set{ _count =Int8(newValue)}
}
}
extension_FixedArray16:RandomAccessCollection,MutableCollection{
internaltypealiasIndex=Int
internalvarstartIndex:Index{
return0
}
internalvarendIndex:Index{
return count
}
internal subscript(i:Index)->T{
@inline(__always)
get{
letcount=self.count // for exclusive access
_internalInvariant(i >=0 && i < count)
letres:T=withUnsafeBytes(of: storage){
(rawPtr:UnsafeRawBufferPointer)->Tin
letstride= MemoryLayout<T>.stride
_internalInvariant(rawPtr.count ==16*stride,"layout mismatch?")
letbufPtr=UnsafeBufferPointer(
start: rawPtr.baseAddress!.assumingMemoryBound(to:T.self),
count: count)
returnbufPtr[_unchecked: i]
}
return res
}
@inline(__always)
set{
_internalInvariant(i >=0 && i < count)
self.withUnsafeMutableBufferPointer{ buffer in
buffer[_unchecked: i]= newValue
}
}
}
@inline(__always)
internalfunc index(after i:Index)->Index{
return i+1
}
@inline(__always)
internalfunc index(before i:Index)->Index{
return i-1
}
}
extension_FixedArray16{
internalmutatingfunc append(_ newElement:T){
_internalInvariant(count < capacity)
_count +=1
self[count-1]= newElement
}
}
extension_FixedArray16where T:ExpressibleByIntegerLiteral{
@inline(__always)
internalinit(count:Int){
_internalInvariant(count >=0 && count <= _FixedArray16.capacity)
self.storage =(
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
)
self._count =Int8(truncatingIfNeeded: count)
}
@inline(__always)
internalinit(){
self.init(count:0)
}
@inline(__always)
internalinit(allZeros:()){
self.init(count:16)
}
}
extension_FixedArray16{
internalmutatingfunc withUnsafeMutableBufferPointer<R>(
_ body:(UnsafeMutableBufferPointer<Element>)throws->R
)rethrows->R{
letcount=self.count // for exclusive access
returntrywithUnsafeMutableBytes(of:&storage){ rawBuffer in
_internalInvariant(rawBuffer.count ==16*MemoryLayout<T>.stride,
"layout mismatch?")
letbuffer=UnsafeMutableBufferPointer<Element>(
start: rawBuffer.baseAddress._unsafelyUnwrappedUnchecked
.assumingMemoryBound(to:Element.self),
count: count)
returntrybody(buffer)
}
}
internalmutatingfunc withUnsafeBufferPointer<R>(
_ body:(UnsafeBufferPointer<Element>)throws->R
)rethrows->R{
letcount=self.count // for exclusive access
returntrywithUnsafeBytes(of:&storage){ rawBuffer in
_internalInvariant(rawBuffer.count ==16*MemoryLayout<T>.stride,
"layout mismatch?")
letbuffer=UnsafeBufferPointer<Element>(
start: rawBuffer.baseAddress._unsafelyUnwrappedUnchecked
.assumingMemoryBound(to:Element.self),
count: count)
returntrybody(buffer)
}
}
}