Skip to content

Commit 891a700

Browse files
committed
[Swift 4] Update Bit Set
1 parent 1e026d7 commit 891a700

File tree

4 files changed

+45
-45
lines changed

4 files changed

+45
-45
lines changed

Bit Set/BitSet.playground/Contents.swift

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//: Playground - noun: a place where people can play
22

3+
// last checked with Xcode 9.0b4
4+
#if swift(>=4.0)
5+
print("Hello, Swift 4!")
6+
#endif
7+
38
// Create a bit set that stores 140 bits
49
var bits = BitSet(size: 140)
510

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public struct BitSet {
4141
// Set the highest bit that's still valid.
4242
let mask = 1 << Word(63 - diff)
4343
// Subtract 1 to turn it into a mask, and add the high bit back in.
44-
return mask | (mask - 1)
44+
return (Word)(mask | (mask - 1))
4545
} else {
4646
return allOnes
4747
}
@@ -167,7 +167,7 @@ extension BitSet: Hashable {
167167

168168
// MARK: - Bitwise operations
169169

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

Bit Set/BitSet.playground/timeline.xctimeline

-6
This file was deleted.

Bit Set/BitSet.swift

+38-37
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
/*
2-
A fixed-size sequence of n bits. Bits have indices 0 to n-1.
3-
*/
2+
A fixed-size sequence of n bits. Bits have indices 0 to n-1.
3+
*/
44
public struct BitSet {
55
/* How many bits this object can hold. */
66
private(set) public var size: Int
7-
7+
88
/*
9-
We store the bits in a list of unsigned 64-bit integers.
10-
The first entry, `words[0]`, is the least significant word.
11-
*/
9+
We store the bits in a list of unsigned 64-bit integers.
10+
The first entry, `words[0]`, is the least significant word.
11+
*/
1212
private let N = 64
1313
public typealias Word = UInt64
1414
fileprivate(set) public var words: [Word]
15-
15+
1616
private let allOnes = ~Word()
17-
17+
1818
/* Creates a bit set that can hold `size` bits. All bits are initially 0. */
1919
public init(size: Int) {
2020
precondition(size > 0)
2121
self.size = size
22-
22+
2323
// Round up the count to the next multiple of 64.
2424
let n = (size + (N-1)) / N
2525
words = [Word](repeating: 0, count: n)
2626
}
27-
27+
2828
/* Converts a bit index into an array index and a mask inside the word. */
2929
private func indexOf(_ i: Int) -> (Int, Word) {
3030
precondition(i >= 0)
@@ -33,79 +33,79 @@ public struct BitSet {
3333
let m = Word(i - o*N)
3434
return (o, 1 << m)
3535
}
36-
36+
3737
/* Returns a mask that has 1s for all bits that are in the last word. */
3838
private func lastWordMask() -> Word {
3939
let diff = words.count*N - size
4040
if diff > 0 {
4141
// Set the highest bit that's still valid.
4242
let mask = 1 << Word(63 - diff)
4343
// Subtract 1 to turn it into a mask, and add the high bit back in.
44-
return mask | (mask - 1)
44+
return (Word)(mask | (mask - 1))
4545
} else {
4646
return allOnes
4747
}
4848
}
49-
49+
5050
/*
51-
If the size is not a multiple of N, then we have to clear out the bits
52-
that we're not using, or bitwise operations between two differently sized
53-
BitSets will go wrong.
54-
*/
51+
If the size is not a multiple of N, then we have to clear out the bits
52+
that we're not using, or bitwise operations between two differently sized
53+
BitSets will go wrong.
54+
*/
5555
fileprivate mutating func clearUnusedBits() {
5656
words[words.count - 1] &= lastWordMask()
5757
}
58-
58+
5959
/* So you can write bitset[99] = ... */
6060
public subscript(i: Int) -> Bool {
6161
get { return isSet(i) }
6262
set { if newValue { set(i) } else { clear(i) } }
6363
}
64-
64+
6565
/* Sets the bit at the specified index to 1. */
6666
public mutating func set(_ i: Int) {
6767
let (j, m) = indexOf(i)
6868
words[j] |= m
6969
}
70-
70+
7171
/* Sets all the bits to 1. */
7272
public mutating func setAll() {
7373
for i in 0..<words.count {
7474
words[i] = allOnes
7575
}
7676
clearUnusedBits()
7777
}
78-
78+
7979
/* Sets the bit at the specified index to 0. */
8080
public mutating func clear(_ i: Int) {
8181
let (j, m) = indexOf(i)
8282
words[j] &= ~m
8383
}
84-
84+
8585
/* Sets all the bits to 0. */
8686
public mutating func clearAll() {
8787
for i in 0..<words.count {
8888
words[i] = 0
8989
}
9090
}
91-
91+
9292
/* Changes 0 into 1 and 1 into 0. Returns the new value of the bit. */
9393
public mutating func flip(_ i: Int) -> Bool {
9494
let (j, m) = indexOf(i)
9595
words[j] ^= m
9696
return (words[j] & m) != 0
9797
}
98-
98+
9999
/* Determines whether the bit at the specific index is 1 (true) or 0 (false). */
100100
public func isSet(_ i: Int) -> Bool {
101101
let (j, m) = indexOf(i)
102102
return (words[j] & m) != 0
103103
}
104-
104+
105105
/*
106-
Returns the number of bits that are 1. Time complexity is O(s) where s is
107-
the number of 1-bits.
108-
*/
106+
Returns the number of bits that are 1. Time complexity is O(s) where s is
107+
the number of 1-bits.
108+
*/
109109
public var cardinality: Int {
110110
var count = 0
111111
for var x in words {
@@ -117,23 +117,23 @@ public struct BitSet {
117117
}
118118
return count
119119
}
120-
120+
121121
/* Checks if all the bits are set. */
122122
public func all1() -> Bool {
123123
for i in 0..<words.count - 1 {
124124
if words[i] != allOnes { return false }
125125
}
126126
return words[words.count - 1] == lastWordMask()
127127
}
128-
128+
129129
/* Checks if any of the bits are set. */
130130
public func any1() -> Bool {
131131
for x in words {
132132
if x != 0 { return true }
133133
}
134134
return false
135135
}
136-
136+
137137
/* Checks if none of the bits are set. */
138138
public func all0() -> Bool {
139139
for x in words {
@@ -167,7 +167,7 @@ extension BitSet: Hashable {
167167

168168
// MARK: - Bitwise operations
169169

170-
extension BitSet: BitwiseOperations {
170+
extension BitSet {
171171
public static var allZeros: BitSet {
172172
return BitSet(size: 64)
173173
}
@@ -178,11 +178,11 @@ private func copyLargest(_ lhs: BitSet, _ rhs: BitSet) -> BitSet {
178178
}
179179

180180
/*
181-
Note: In all of these bitwise operations, lhs and rhs are allowed to have a
182-
different number of bits. The new BitSet always has the larger size.
183-
The extra bits that get added to the smaller BitSet are considered to be 0.
184-
That will strip off the higher bits from the larger BitSet when doing &.
185-
*/
181+
Note: In all of these bitwise operations, lhs and rhs are allowed to have a
182+
different number of bits. The new BitSet always has the larger size.
183+
The extra bits that get added to the smaller BitSet are considered to be 0.
184+
That will strip off the higher bits from the larger BitSet when doing &.
185+
*/
186186

187187
public func & (lhs: BitSet, rhs: BitSet) -> BitSet {
188188
let m = max(lhs.size, rhs.size)
@@ -245,3 +245,4 @@ extension BitSet: CustomStringConvertible {
245245
return s
246246
}
247247
}
248+

0 commit comments

Comments
 (0)