@@ -18,15 +18,15 @@ public struct BitSet {
1818
1919 private let N = 64
2020 public typealias Word = UInt64
21- private (set ) public var words: [Word ]
21+ fileprivate (set ) public var words: [Word ]
2222
2323 public init (size : Int ) {
2424 precondition (size > 0 )
2525 self .size = size
2626
2727 // Round up the count to the next multiple of 64.
2828 let n = (size + (N- 1 )) / N
29- words = . init ( count : n, repeatedValue : 0 )
29+ words = [ Word ]( repeating : 0 , count : n )
3030 }
3131```
3232
@@ -47,7 +47,7 @@ then the `BitSet` allocates an array of three words. Each word has 64 bits and t
4747Most of the operations on `BitSet` take the index of the bit as a parameter, so it's useful to have a way to find which word contains that bit.
4848
4949```swift
50- private func indexOf (i : Int ) -> (Int , Word ) {
50+ private func indexOf (_ i : Int ) -> (Int , Word ) {
5151 precondition (i >= 0 )
5252 precondition (i < size)
5353 let o = i / N
@@ -77,7 +77,7 @@ Note that the mask is always 64 bits because we look at the data one word at a t
7777Now that we know where to find a bit, setting it to 1 is easy:
7878
7979```swift
80- public mutating func set (i : Int ) {
80+ public mutating func set (_ i : Int ) {
8181 let (j, m) = indexOf (i)
8282 words[j] |= m
8383 }
@@ -88,7 +88,7 @@ This looks up the word index and the mask, then performs a bitwise OR between th
8888Clearing the bit -- i.e . changing it to 0 -- is just as easy:
8989
9090```swift
91- public mutating func clear (i : Int ) {
91+ public mutating func clear (_ i : Int ) {
9292 let (j, m) = indexOf (i)
9393 words[j] &= ~ m
9494 }
@@ -99,7 +99,7 @@ Instead of a bitwise OR we now do a bitwise AND with the inverse of the mask. So
9999To see if a bit is set we also use the bitwise AND but without inverting:
100100
101101```swift
102- public func isSet (i : Int ) -> Bool {
102+ public func isSet (_ i: Int ) -> Bool {
103103 let (j, m) = indexOf (i)
104104 return (words[j] & m) != 0
105105 }
@@ -127,15 +127,15 @@ print(bits)
127127This will print the three words that the 140 - bit `BitSet` uses to store everything:
128128
129129```swift
130- 0010000000000000000000000000000000000000000000000000000000000000
131- 0000000000000000000000000000000000010000000000000000000000000000
132- 1000000000000000000000000000000000000000000000000000000000000000
130+ 0010000000000000000000000000000000000000000000000000000000000000
131+ 0000000000000000000000000000000000010000000000000000000000000000
132+ 1000000000000000000000000000000000000000000000000000000000000000
133133```
134134
135135Something else that's fun to do with bits is flipping them. This changes 0 into 1 and 1 into 0 . Here's `flip ()`:
136136
137137```swift
138- public mutating func flip (i : Int ) -> Bool {
138+ public mutating func flip (_ i : Int ) -> Bool {
139139 let (j, m) = indexOf (i)
140140 words[j] ^= m
141141 return (words[j] & m) != 0
@@ -170,17 +170,17 @@ There is also `setAll()` to make all the bits 1. However, this has to deal with
170170First, we copy ones into all the words in our array. The array is now:
171171
172172```swift
173- 1111111111111111111111111111111111111111111111111111111111111111
174- 1111111111111111111111111111111111111111111111111111111111111111
175- 1111111111111111111111111111111111111111111111111111111111111111
173+ 1111111111111111111111111111111111111111111111111111111111111111
174+ 1111111111111111111111111111111111111111111111111111111111111111
175+ 1111111111111111111111111111111111111111111111111111111111111111
176176```
177177
178178But this is incorrect... Since we don't use most of the last word, we should leave those bits at 0 :
179179
180180```swift
181- 1111111111111111111111111111111111111111111111111111111111111111
182- 1111111111111111111111111111111111111111111111111111111111111111
183- 1111111111110000000000000000000000000000000000000000000000000000
181+ 1111111111111111111111111111111111111111111111111111111111111111
182+ 1111111111111111111111111111111111111111111111111111111111111111
183+ 1111111111110000000000000000000000000000000000000000000000000000
184184```
185185
186186Instead of 192 one- bits we now have only 140 one- bits. The fact that the last word may not be completely filled up means that we always have to treat this last word specially.
@@ -189,7 +189,7 @@ Setting those "leftover" bits to 0 is what the `clearUnusedBits()` helper functi
189189
190190This uses some advanced bit manipulation, so pay close attention:
191191
192- ```swift
192+ ```swift
193193 private func lastWordMask () -> Word {
194194 let diff = words.count * N - size // 1
195195 if diff > 0 {
@@ -199,7 +199,7 @@ This uses some advanced bit manipulation, so pay close attention:
199199 return ~ Word ()
200200 }
201201 }
202-
202+
203203 private mutating func clearUnusedBits () {
204204 words[words.count - 1 ] &= lastWordMask () // 4
205205 }
@@ -211,15 +211,15 @@ Here's what it does, step-by-step:
211211
2122122 ) Create a mask that is all 0 's, except the highest bit that's still valid is a 1 . In our example, that would be:
213213
214- 0000000000010000000000000000000000000000000000000000000000000000
214+ 0000000000010000000000000000000000000000000000000000000000000000
215215
2162163 ) Subtract 1 to turn it into:
217217
218- 1111111111100000000000000000000000000000000000000000000000000000
218+ 1111111111100000000000000000000000000000000000000000000000000000
219219
220220and add the high bit back in to get :
221221
222- 1111111111110000000000000000000000000000000000000000000000000000
222+ 1111111111110000000000000000000000000000000000000000000000000000
223223
224224There are now 12 one- bits in this word because `140 - 2 * 64 = 12 `.
225225
@@ -236,7 +236,7 @@ The first one only uses the first 4 bits; the second one uses 8 bits. The first
236236 00100011
237237 -------- OR
238238 10101111
239-
239+
240240That is wrong since two of those 1 - bits aren't supposed to be here. The correct way to do it is :
241241
242242 10000000 unused bits set to 0 first!
0 commit comments