forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyFastSequence.swift
205 lines (183 loc) · 5.88 KB
/
TinyFastSequence.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/// A `Sequence` that does not heap allocate, if it only carries a single element
@usableFromInline
struct TinyFastSequence<Element>: Sequence {
@usableFromInline
enum Base {
case none(reserveCapacity: Int)
case one(Element, reserveCapacity: Int)
case two(Element, Element, reserveCapacity: Int)
case n([Element])
}
@usableFromInline
private(set) var base: Base
@inlinable
init() {
self.base = .none(reserveCapacity: 0)
}
@inlinable
init(element: Element) {
self.base = .one(element, reserveCapacity: 1)
}
@inlinable
init(_ collection: some Collection<Element>) {
switch collection.count {
case 0:
self.base = .none(reserveCapacity: 0)
case 1:
self.base = .one(collection.first!, reserveCapacity: 0)
default:
if let collection = collection as? Array<Element> {
self.base = .n(collection)
} else {
self.base = .n(Array(collection))
}
}
}
@inlinable
init(_ max2Sequence: Max2Sequence<Element>) {
switch max2Sequence.count {
case 0:
self.base = .none(reserveCapacity: 0)
case 1:
self.base = .one(max2Sequence.first!, reserveCapacity: 0)
case 2:
self.base = .n(Array(max2Sequence))
default:
fatalError()
}
}
@usableFromInline
var count: Int {
switch self.base {
case .none:
return 0
case .one:
return 1
case .two:
return 2
case .n(let array):
return array.count
}
}
@inlinable
var first: Element? {
switch self.base {
case .none:
return nil
case .one(let element, _):
return element
case .two(let first, _, _):
return first
case .n(let array):
return array.first
}
}
@usableFromInline
var isEmpty: Bool {
switch self.base {
case .none:
return true
case .one, .two, .n:
return false
}
}
@inlinable
mutating func reserveCapacity(_ minimumCapacity: Int) {
switch self.base {
case .none(let reservedCapacity):
self.base = .none(reserveCapacity: Swift.max(reservedCapacity, minimumCapacity))
case .one(let element, let reservedCapacity):
self.base = .one(element, reserveCapacity: Swift.max(reservedCapacity, minimumCapacity))
case .two(let first, let second, let reservedCapacity):
self.base = .two(first, second, reserveCapacity: Swift.max(reservedCapacity, minimumCapacity))
case .n(var array):
self.base = .none(reserveCapacity: 0) // prevent CoW
array.reserveCapacity(minimumCapacity)
self.base = .n(array)
}
}
@inlinable
mutating func append(_ element: Element) {
switch self.base {
case .none(let reserveCapacity):
self.base = .one(element, reserveCapacity: reserveCapacity)
case .one(let first, let reserveCapacity):
self.base = .two(first, element, reserveCapacity: reserveCapacity)
case .two(let first, let second, let reserveCapacity):
var new = [Element]()
new.reserveCapacity(Swift.max(4, reserveCapacity))
new.append(first)
new.append(second)
new.append(element)
self.base = .n(new)
case .n(var existing):
self.base = .none(reserveCapacity: 0) // prevent CoW
existing.append(element)
self.base = .n(existing)
}
}
@inlinable
func makeIterator() -> Iterator {
Iterator(self)
}
@usableFromInline
struct Iterator: IteratorProtocol {
@usableFromInline private(set) var index: Int = 0
@usableFromInline private(set) var backing: TinyFastSequence<Element>
@inlinable
init(_ backing: TinyFastSequence<Element>) {
self.backing = backing
}
@inlinable
mutating func next() -> Element? {
switch self.backing.base {
case .none:
return nil
case .one(let element, _):
if self.index == 0 {
self.index += 1
return element
}
return nil
case .two(let first, let second, _):
defer { self.index += 1 }
switch self.index {
case 0:
return first
case 1:
return second
default:
return nil
}
case .n(let array):
if self.index < array.endIndex {
defer { self.index += 1}
return array[self.index]
}
return nil
}
}
}
}
extension TinyFastSequence: Equatable where Element: Equatable {}
extension TinyFastSequence.Base: Equatable where Element: Equatable {}
extension TinyFastSequence: Hashable where Element: Hashable {}
extension TinyFastSequence.Base: Hashable where Element: Hashable {}
extension TinyFastSequence: Sendable where Element: Sendable {}
extension TinyFastSequence.Base: Sendable where Element: Sendable {}
extension TinyFastSequence: ExpressibleByArrayLiteral {
@inlinable
init(arrayLiteral elements: Element...) {
var iterator = elements.makeIterator()
switch elements.count {
case 0:
self.base = .none(reserveCapacity: 0)
case 1:
self.base = .one(iterator.next()!, reserveCapacity: 0)
case 2:
self.base = .two(iterator.next()!, iterator.next()!, reserveCapacity: 0)
default:
self.base = .n(elements)
}
}
}