-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathBitset.swift
339 lines (299 loc) · 8.34 KB
/
Bitset.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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 simple bitmap of a fixed number of bits, implementing a sorted set of
/// small nonnegative Int values.
///
/// Because `_UnsafeBitset` implements a flat bit vector, it isn't suitable for
/// holding arbitrarily large integers. The maximal element a bitset can store
/// is fixed at its initialization.
@frozen
@usableFromInline // @testable
internal struct _UnsafeBitset {
@usableFromInline
internal let words: UnsafeMutablePointer<Word>
@usableFromInline
internal let wordCount: Int
@inlinable
@inline(__always)
internal init(words: UnsafeMutablePointer<Word>, wordCount: Int) {
self.words = words
self.wordCount = wordCount
}
}
extension _UnsafeBitset {
@inlinable
@inline(__always)
internal static func word(for element: Int) -> Int {
_internalInvariant(element >= 0)
// Note: We perform on UInts to get faster unsigned math (shifts).
let element = UInt(bitPattern: element)
let capacity = UInt(bitPattern: Word.capacity)
return Int(bitPattern: element / capacity)
}
@inlinable
@inline(__always)
internal static func bit(for element: Int) -> Int {
_internalInvariant(element >= 0)
// Note: We perform on UInts to get faster unsigned math (masking).
let element = UInt(bitPattern: element)
let capacity = UInt(bitPattern: Word.capacity)
return Int(bitPattern: element % capacity)
}
@inlinable
@inline(__always)
internal static func split(_ element: Int) -> (word: Int, bit: Int) {
return (word(for: element), bit(for: element))
}
@inlinable
@inline(__always)
internal static func join(word: Int, bit: Int) -> Int {
_internalInvariant(bit >= 0 && bit < Word.capacity)
return word &* Word.capacity &+ bit
}
}
extension _UnsafeBitset {
@inlinable
@inline(__always)
internal static func wordCount(forCapacity capacity: Int) -> Int {
return word(for: capacity &+ Word.capacity &- 1)
}
@inlinable
internal var capacity: Int {
@inline(__always)
get {
return wordCount &* Word.capacity
}
}
@inlinable
@inline(__always)
internal func isValid(_ element: Int) -> Bool {
return element >= 0 && element <= capacity
}
@inlinable
@inline(__always)
internal func uncheckedContains(_ element: Int) -> Bool {
_internalInvariant(isValid(element))
let (word, bit) = _UnsafeBitset.split(element)
return words[word].uncheckedContains(bit)
}
@inlinable
@inline(__always)
@discardableResult
internal func uncheckedInsert(_ element: Int) -> Bool {
_internalInvariant(isValid(element))
let (word, bit) = _UnsafeBitset.split(element)
return words[word].uncheckedInsert(bit)
}
@inlinable
@inline(__always)
@discardableResult
internal func uncheckedRemove(_ element: Int) -> Bool {
_internalInvariant(isValid(element))
let (word, bit) = _UnsafeBitset.split(element)
return words[word].uncheckedRemove(bit)
}
@inlinable
@inline(__always)
internal func clear() {
words.assign(repeating: .empty, count: wordCount)
}
}
extension _UnsafeBitset: Sequence {
@usableFromInline
internal typealias Element = Int
@inlinable
internal var count: Int {
var count = 0
for w in 0 ..< wordCount {
count += words[w].count
}
return count
}
@inlinable
internal var underestimatedCount: Int {
return count
}
@inlinable
func makeIterator() -> Iterator {
return Iterator(self)
}
@usableFromInline
@frozen
internal struct Iterator: IteratorProtocol {
@usableFromInline
internal let bitset: _UnsafeBitset
@usableFromInline
internal var index: Int
@usableFromInline
internal var word: Word
@inlinable
internal init(_ bitset: _UnsafeBitset) {
self.bitset = bitset
self.index = 0
self.word = bitset.wordCount > 0 ? bitset.words[0] : .empty
}
@inlinable
internal mutating func next() -> Int? {
if let bit = word.next() {
return _UnsafeBitset.join(word: index, bit: bit)
}
while (index + 1) < bitset.wordCount {
index += 1
word = bitset.words[index]
if let bit = word.next() {
return _UnsafeBitset.join(word: index, bit: bit)
}
}
return nil
}
}
}
////////////////////////////////////////////////////////////////////////////////
extension _UnsafeBitset {
@frozen
@usableFromInline
internal struct Word {
@usableFromInline
internal var value: UInt
@inlinable
internal init(_ value: UInt) {
self.value = value
}
}
}
extension _UnsafeBitset.Word {
@inlinable
internal static var capacity: Int {
@inline(__always)
get {
return UInt.bitWidth
}
}
@inlinable
@inline(__always)
internal func uncheckedContains(_ bit: Int) -> Bool {
_internalInvariant(bit >= 0 && bit < UInt.bitWidth)
return value & (1 &<< bit) != 0
}
@inlinable
@inline(__always)
@discardableResult
internal mutating func uncheckedInsert(_ bit: Int) -> Bool {
_internalInvariant(bit >= 0 && bit < UInt.bitWidth)
let mask: UInt = 1 &<< bit
let inserted = value & mask == 0
value |= mask
return inserted
}
@inlinable
@inline(__always)
@discardableResult
internal mutating func uncheckedRemove(_ bit: Int) -> Bool {
_internalInvariant(bit >= 0 && bit < UInt.bitWidth)
let mask: UInt = 1 &<< bit
let removed = value & mask != 0
value &= ~mask
return removed
}
}
extension _UnsafeBitset.Word {
@inlinable
var minimum: Int? {
@inline(__always)
get {
guard value != 0 else { return nil }
return value.trailingZeroBitCount
}
}
@inlinable
var maximum: Int? {
@inline(__always)
get {
guard value != 0 else { return nil }
return _UnsafeBitset.Word.capacity &- 1 &- value.leadingZeroBitCount
}
}
@inlinable
var complement: _UnsafeBitset.Word {
@inline(__always)
get {
return _UnsafeBitset.Word(~value)
}
}
@inlinable
@inline(__always)
internal func subtracting(elementsBelow bit: Int) -> _UnsafeBitset.Word {
_internalInvariant(bit >= 0 && bit < _UnsafeBitset.Word.capacity)
let mask = UInt.max &<< bit
return _UnsafeBitset.Word(value & mask)
}
@inlinable
@inline(__always)
internal func intersecting(elementsBelow bit: Int) -> _UnsafeBitset.Word {
_internalInvariant(bit >= 0 && bit < _UnsafeBitset.Word.capacity)
let mask: UInt = (1 as UInt &<< bit) &- 1
return _UnsafeBitset.Word(value & mask)
}
@inlinable
@inline(__always)
internal func intersecting(elementsAbove bit: Int) -> _UnsafeBitset.Word {
_internalInvariant(bit >= 0 && bit < _UnsafeBitset.Word.capacity)
let mask = (UInt.max &<< bit) &<< 1
return _UnsafeBitset.Word(value & mask)
}
}
extension _UnsafeBitset.Word {
@inlinable
internal static var empty: _UnsafeBitset.Word {
@inline(__always)
get {
return _UnsafeBitset.Word(0)
}
}
@inlinable
internal static var allBits: _UnsafeBitset.Word {
@inline(__always)
get {
return _UnsafeBitset.Word(UInt.max)
}
}
}
// Word implements Sequence by using a copy of itself as its Iterator.
// Iteration with `next()` destroys the word's value; however, this won't cause
// problems in normal use, because `next()` is usually called on a separate
// iterator, not the original word.
extension _UnsafeBitset.Word: Sequence, IteratorProtocol {
@inlinable
internal var count: Int {
return value.nonzeroBitCount
}
@inlinable
internal var underestimatedCount: Int {
return count
}
@inlinable
internal var isEmpty: Bool {
@inline(__always)
get {
return value == 0
}
}
/// Return the index of the lowest set bit in this word,
/// and also destructively clear it.
@inlinable
internal mutating func next() -> Int? {
guard value != 0 else { return nil }
let bit = value.trailingZeroBitCount
value &= value &- 1 // Clear lowest nonzero bit.
return bit
}
}