Skip to content

Commit 8680a97

Browse files
author
Chris Pilcher
committed
Updating to Swift3
1 parent 21b05b1 commit 8680a97

File tree

3 files changed

+44
-44
lines changed

3 files changed

+44
-44
lines changed

Bit Set/BitSet.playground/Sources/BitSet.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct BitSet {
1111
*/
1212
private let N = 64
1313
public typealias Word = UInt64
14-
private(set) public var words: [Word]
14+
fileprivate(set) public var words: [Word]
1515

1616
private let allOnes = ~Word()
1717

@@ -22,11 +22,11 @@ public struct BitSet {
2222

2323
// Round up the count to the next multiple of 64.
2424
let n = (size + (N-1)) / N
25-
words = .init(count: n, repeatedValue: 0)
25+
words = [Word](repeating: 0, count: n)
2626
}
2727

2828
/* Converts a bit index into an array index and a mask inside the word. */
29-
private func indexOf(i: Int) -> (Int, Word) {
29+
private func indexOf(_ i: Int) -> (Int, Word) {
3030
precondition(i >= 0)
3131
precondition(i < size)
3232
let o = i / N
@@ -52,7 +52,7 @@ public struct BitSet {
5252
that we're not using, or bitwise operations between two differently sized
5353
BitSets will go wrong.
5454
*/
55-
private mutating func clearUnusedBits() {
55+
fileprivate mutating func clearUnusedBits() {
5656
words[words.count - 1] &= lastWordMask()
5757
}
5858

@@ -63,7 +63,7 @@ public struct BitSet {
6363
}
6464

6565
/* Sets the bit at the specified index to 1. */
66-
public mutating func set(i: Int) {
66+
public mutating func set(_ i: Int) {
6767
let (j, m) = indexOf(i)
6868
words[j] |= m
6969
}
@@ -77,7 +77,7 @@ public struct BitSet {
7777
}
7878

7979
/* Sets the bit at the specified index to 0. */
80-
public mutating func clear(i: Int) {
80+
public mutating func clear(_ i: Int) {
8181
let (j, m) = indexOf(i)
8282
words[j] &= ~m
8383
}
@@ -90,14 +90,14 @@ public struct BitSet {
9090
}
9191

9292
/* Changes 0 into 1 and 1 into 0. Returns the new value of the bit. */
93-
public mutating func flip(i: Int) -> Bool {
93+
public mutating func flip(_ i: Int) -> Bool {
9494
let (j, m) = indexOf(i)
9595
words[j] ^= m
9696
return (words[j] & m) != 0
9797
}
9898

9999
/* Determines whether the bit at the specific index is 1 (true) or 0 (false). */
100-
public func isSet(i: Int) -> Bool {
100+
public func isSet(_ i: Int) -> Bool {
101101
let (j, m) = indexOf(i)
102102
return (words[j] & m) != 0
103103
}
@@ -158,7 +158,7 @@ extension BitSet: Hashable {
158158
/* Based on the hashing code from Java's BitSet. */
159159
public var hashValue: Int {
160160
var h = Word(1234)
161-
for i in words.count.stride(to: 0, by: -1) {
161+
for i in stride(from: words.count, to: 0, by: -1) {
162162
h ^= words[i - 1] &* Word(i)
163163
}
164164
return Int((h >> 32) ^ h)
@@ -167,13 +167,13 @@ extension BitSet: Hashable {
167167

168168
// MARK: - Bitwise operations
169169

170-
extension BitSet: BitwiseOperationsType {
170+
extension BitSet: BitwiseOperations {
171171
public static var allZeros: BitSet {
172172
return BitSet(size: 64)
173173
}
174174
}
175175

176-
private func copyLargest(lhs: BitSet, _ rhs: BitSet) -> BitSet {
176+
private func copyLargest(_ lhs: BitSet, _ rhs: BitSet) -> BitSet {
177177
return (lhs.words.count > rhs.words.count) ? lhs : rhs
178178
}
179179

Bit Set/BitSet.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct BitSet {
1111
*/
1212
private let N = 64
1313
public typealias Word = UInt64
14-
private(set) public var words: [Word]
14+
fileprivate(set) public var words: [Word]
1515

1616
private let allOnes = ~Word()
1717

@@ -22,11 +22,11 @@ public struct BitSet {
2222

2323
// Round up the count to the next multiple of 64.
2424
let n = (size + (N-1)) / N
25-
words = .init(count: n, repeatedValue: 0)
25+
words = [Word](repeating: 0, count: n)
2626
}
2727

2828
/* Converts a bit index into an array index and a mask inside the word. */
29-
private func indexOf(i: Int) -> (Int, Word) {
29+
private func indexOf(_ i: Int) -> (Int, Word) {
3030
precondition(i >= 0)
3131
precondition(i < size)
3232
let o = i / N
@@ -52,7 +52,7 @@ public struct BitSet {
5252
that we're not using, or bitwise operations between two differently sized
5353
BitSets will go wrong.
5454
*/
55-
private mutating func clearUnusedBits() {
55+
fileprivate mutating func clearUnusedBits() {
5656
words[words.count - 1] &= lastWordMask()
5757
}
5858

@@ -63,7 +63,7 @@ public struct BitSet {
6363
}
6464

6565
/* Sets the bit at the specified index to 1. */
66-
public mutating func set(i: Int) {
66+
public mutating func set(_ i: Int) {
6767
let (j, m) = indexOf(i)
6868
words[j] |= m
6969
}
@@ -77,7 +77,7 @@ public struct BitSet {
7777
}
7878

7979
/* Sets the bit at the specified index to 0. */
80-
public mutating func clear(i: Int) {
80+
public mutating func clear(_ i: Int) {
8181
let (j, m) = indexOf(i)
8282
words[j] &= ~m
8383
}
@@ -90,14 +90,14 @@ public struct BitSet {
9090
}
9191

9292
/* Changes 0 into 1 and 1 into 0. Returns the new value of the bit. */
93-
public mutating func flip(i: Int) -> Bool {
93+
public mutating func flip(_ i: Int) -> Bool {
9494
let (j, m) = indexOf(i)
9595
words[j] ^= m
9696
return (words[j] & m) != 0
9797
}
9898

9999
/* Determines whether the bit at the specific index is 1 (true) or 0 (false). */
100-
public func isSet(i: Int) -> Bool {
100+
public func isSet(_ i: Int) -> Bool {
101101
let (j, m) = indexOf(i)
102102
return (words[j] & m) != 0
103103
}
@@ -158,7 +158,7 @@ extension BitSet: Hashable {
158158
/* Based on the hashing code from Java's BitSet. */
159159
public var hashValue: Int {
160160
var h = Word(1234)
161-
for i in words.count.stride(to: 0, by: -1) {
161+
for i in stride(from: words.count, to: 0, by: -1) {
162162
h ^= words[i - 1] &* Word(i)
163163
}
164164
return Int((h >> 32) ^ h)
@@ -167,13 +167,13 @@ extension BitSet: Hashable {
167167

168168
// MARK: - Bitwise operations
169169

170-
extension BitSet: BitwiseOperationsType {
170+
extension BitSet: BitwiseOperations {
171171
public static var allZeros: BitSet {
172172
return BitSet(size: 64)
173173
}
174174
}
175175

176-
private func copyLargest(lhs: BitSet, _ rhs: BitSet) -> BitSet {
176+
private func copyLargest(_ lhs: BitSet, _ rhs: BitSet) -> BitSet {
177177
return (lhs.words.count > rhs.words.count) ? lhs : rhs
178178
}
179179

Bit Set/README.markdown

+22-22
Original file line numberDiff line numberDiff line change
@@ -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
4747
Most 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
7777
Now 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
8888
Clearing 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
9999
To 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)
127127
This 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

135135
Something 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
170170
First, 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

178178
But 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

186186
Instead 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

190190
This 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

212212
2) 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

216216
3) Subtract 1 to turn it into:
217217

218-
1111111111100000000000000000000000000000000000000000000000000000
218+
1111111111100000000000000000000000000000000000000000000000000000
219219

220220
and add the high bit back in to get:
221221

222-
1111111111110000000000000000000000000000000000000000000000000000
222+
1111111111110000000000000000000000000000000000000000000000000000
223223

224224
There 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+
240240
That 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

Comments
 (0)