File: _UnsafeBitSet%2B_Word.swift.gyb

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (329 lines) | stat: -rw-r--r-- 8,197 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 2024 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
//
//===----------------------------------------------------------------------===//

%{
  from gyb_utils import *
}%
${autogenerated_warning()}


% for modifier in visibility_levels:
${visibility_boilerplate(modifier)}
extension _UnsafeBitSet {
  ${"@frozen" if modifier == "public" else "@frozen @usableFromInline"}
  ${modifier} struct _Word {
    ${"@usableFromInline" if modifier != "public" else ""}
    ${modifier} var value: UInt

    @inlinable
    @inline(__always)
    ${modifier} init(_ value: UInt) {
      self.value = value
    }

    @inlinable
    @inline(__always)
    ${modifier} init(upTo bit: UInt) {
      assert(bit <= _Word.capacity)
      self.init((1 << bit) &- 1)
    }

    @inlinable
    @inline(__always)
    ${modifier} init(from start: UInt, to end: UInt) {
      assert(start <= end && end <= _Word.capacity)
      self = Self(upTo: end).symmetricDifference(Self(upTo: start))
    }
  }
}

extension _UnsafeBitSet._Word: CustomStringConvertible {
  ${"@usableFromInline" if modifier != "public" else "//@usableFromInline" }
  ${modifier} var description: String {
    String(value, radix: 16)
  }
}

extension _UnsafeBitSet._Word {
  /// Returns the `n`th member in `self`.
  ///
  /// - Parameter n: The offset of the element to retrieve. This value is
  ///    decremented by the number of items found in this `self` towards the
  ///    value we're looking for. (If the function returns non-nil, then `n`
  ///    is set to `0` on return.)
  /// - Returns: If this word contains enough members to satisfy the request,
  ///    then this function returns the member found. Otherwise it returns nil.
  @inline(never)
  internal func nthElement(_ n: inout UInt) -> UInt? {
    let c = UInt(bitPattern: count)
    guard n < c else {
      n &-= c
      return nil
    }
    let m = Int(bitPattern: n)
    n = 0
    return value._bit(ranked: m)!
  }
  
  @inline(never)
  internal func nthElementFromEnd(_ n: inout UInt) -> UInt? {
    let c = UInt(bitPattern: count)
    guard n < c else {
      n &-= c
      return nil
    }
    let m = Int(bitPattern: c &- 1 &- n)
    n = 0
    return value._bit(ranked: m)!
  }
}

extension _UnsafeBitSet._Word {
  @inlinable
  @inline(__always)
  ${modifier} static func wordCount(forBitCount count: UInt) -> Int {
    // Note: We perform on UInts to get faster unsigned math (shifts).
    let width = UInt(bitPattern: Self.capacity)
    return Int(bitPattern: (count + width - 1) / width)
  }
}

extension _UnsafeBitSet._Word {
  @inlinable
  @inline(__always)
  ${modifier} static var capacity: Int {
    return UInt.bitWidth
  }

  @inlinable
  @inline(__always)
  ${modifier} var count: Int {
    value.nonzeroBitCount
  }

  @inlinable
  @inline(__always)
  ${modifier} var isEmpty: Bool {
    value == 0
  }

  @inlinable
  @inline(__always)
  ${modifier} var isFull: Bool {
    value == UInt.max
  }

  @inlinable
  @inline(__always)
  ${modifier} func contains(_ bit: UInt) -> Bool {
    assert(bit >= 0 && bit < UInt.bitWidth)
    return value & (1 &<< bit) != 0
  }

  @inlinable
  @inline(__always)
  ${modifier} var firstMember: UInt? {
    value._lastSetBit
  }

  @inlinable
  @inline(__always)
  ${modifier} var lastMember: UInt? {
    value._firstSetBit
  }

  @inlinable
  @inline(__always)
  @discardableResult
  ${modifier} mutating func insert(_ bit: UInt) -> Bool {
    assert(bit < UInt.bitWidth)
    let mask: UInt = 1 &<< bit
    let inserted = value & mask == 0
    value |= mask
    return inserted
  }

  @inlinable
  @inline(__always)
  @discardableResult
  ${modifier} mutating func remove(_ bit: UInt) -> Bool {
    assert(bit < UInt.bitWidth)
    let mask: UInt = 1 &<< bit
    let removed = (value & mask) != 0
    value &= ~mask
    return removed
  }

  @inlinable
  @inline(__always)
  ${modifier} mutating func update(_ bit: UInt, to value: Bool) {
    assert(bit < UInt.bitWidth)
    let mask: UInt = 1 &<< bit
    if value {
      self.value |= mask
    } else {
      self.value &= ~mask
    }
  }
}

extension _UnsafeBitSet._Word {
  @inlinable
  @inline(__always)
  internal mutating func insertAll(upTo bit: UInt) {
    assert(bit >= 0 && bit < Self.capacity)
    let mask: UInt = (1 as UInt &<< bit) &- 1
    value |= mask
  }

  @inlinable
  @inline(__always)
  internal mutating func removeAll(upTo bit: UInt) {
    assert(bit >= 0 && bit < Self.capacity)
    let mask = UInt.max &<< bit
    value &= mask
  }

  @inlinable
  @inline(__always)
  internal mutating func removeAll(through bit: UInt) {
    assert(bit >= 0 && bit < Self.capacity)
    var mask = UInt.max &<< bit
    mask &= mask &- 1       // Clear lowest nonzero bit.
    value &= mask
  }

  @inlinable
  @inline(__always)
  internal mutating func removeAll(from bit: UInt) {
    assert(bit >= 0 && bit < Self.capacity)
    let mask: UInt = (1 as UInt &<< bit) &- 1
    value &= mask
  }
}

extension _UnsafeBitSet._Word {
  @inlinable
  @inline(__always)
  ${modifier} static var empty: Self {
    Self(0)
  }

  @inline(__always)
  ${modifier} static var allBits: Self {
    Self(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 @inline(__always)
  ${modifier} var underestimatedCount: Int {
    count
  }

  /// Return the index of the lowest set bit in this word,
  /// and also destructively clear it.
  @inlinable
  ${modifier} mutating func next() -> UInt? {
    guard value != 0 else { return nil }
    let bit = UInt(truncatingIfNeeded: value.trailingZeroBitCount)
    value &= value &- 1       // Clear lowest nonzero bit.
    return bit
  }
}

extension _UnsafeBitSet._Word: Equatable {
  @inlinable
  ${modifier} static func ==(left: Self, right: Self) -> Bool {
    left.value == right.value
  }
}

extension _UnsafeBitSet._Word: Hashable {
  @inlinable
  ${modifier} func hash(into hasher: inout Hasher) {
    hasher.combine(value)
  }
}

extension _UnsafeBitSet._Word {
  @inlinable @inline(__always)
  ${modifier} func complement() -> Self {
    Self(~self.value)
  }

  @inlinable @inline(__always)
  ${modifier} mutating func formComplement() {
    self.value = ~self.value
  }

  @inlinable @inline(__always)
  ${modifier} func union(_ other: Self) -> Self {
    Self(self.value | other.value)
  }

  @inlinable @inline(__always)
  ${modifier} mutating func formUnion(_ other: Self) {
    self.value |= other.value
  }

  @inlinable @inline(__always)
  ${modifier} func intersection(_ other: Self) -> Self {
    Self(self.value & other.value)
  }

  @inlinable @inline(__always)
  ${modifier} mutating func formIntersection(_ other: Self) {
    self.value &= other.value
  }

  @inlinable @inline(__always)
  ${modifier} func symmetricDifference(_ other: Self) -> Self {
    Self(self.value ^ other.value)
  }

  @inlinable @inline(__always)
  ${modifier} mutating func formSymmetricDifference(_ other: Self) {
    self.value ^= other.value
  }

  @inlinable @inline(__always)
  ${modifier} func subtracting(_ other: Self) -> Self {
    Self(self.value & ~other.value)
  }

  @inlinable @inline(__always)
  ${modifier} mutating func subtract(_ other: Self) {
    self.value &= ~other.value
  }
}

extension _UnsafeBitSet._Word {
  @inlinable
  @inline(__always)
  ${modifier} func shiftedDown(by shift: UInt) -> Self {
    assert(shift >= 0 && shift < Self.capacity)
    return Self(self.value &>> shift)
  }

  @inlinable
  @inline(__always)
  ${modifier} func shiftedUp(by shift: UInt) -> Self {
    assert(shift >= 0 && shift < Self.capacity)
    return Self(self.value &<< shift)
  }
}
% end
${visibility_boilerplate("end")}