Skip to content

Commit 1911744

Browse files
committed
Adds Bloom Filter and Bit Set
1 parent 128e6a1 commit 1911744

File tree

6 files changed

+105
-10
lines changed

6 files changed

+105
-10
lines changed

SwiftAlgorithmClub/BitSet.playground/Sources/BitSet.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,7 @@ public func == (lhs: BitSet, rhs: BitSet) -> Bool {
147147

148148
// MARK: - Hashing
149149

150-
extension BitSet: Hashable {
151-
/* Based on the hashing code from Java's BitSet. */
152-
public var hashValue: Int {
153-
var h = Word(1234)
154-
for i in stride(from: words.count, to: 0, by: -1) {
155-
h ^= words[i - 1] &* Word(i)
156-
}
157-
return Int((h >> 32) ^ h)
158-
}
159-
}
150+
extension BitSet: Hashable {}
160151

161152
// MARK: - Bitwise operations
162153

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//: Playground - noun: a place where people can play
2+
3+
public class BloomFilter<T> {
4+
fileprivate var array: [Bool]
5+
private var hashFunctions: [(T) -> Int]
6+
7+
public init(size: Int = 1024, hashFunctions: [(T) -> Int]) {
8+
self.array = [Bool](repeating: false, count: size)
9+
self.hashFunctions = hashFunctions
10+
}
11+
12+
private func computeHashes(_ value: T) -> [Int] {
13+
return hashFunctions.map { hashFunc in abs(hashFunc(value) % array.count) }
14+
}
15+
16+
public func insert(_ element: T) {
17+
for hashValue in computeHashes(element) {
18+
array[hashValue] = true
19+
}
20+
}
21+
22+
public func insert(_ values: [T]) {
23+
for value in values {
24+
insert(value)
25+
}
26+
}
27+
28+
public func query(_ value: T) -> Bool {
29+
let hashValues = computeHashes(value)
30+
31+
// Map hashes to indices in the Bloom Filter
32+
let results = hashValues.map { hashValue in array[hashValue] }
33+
34+
// All values must be 'true' for the query to return true
35+
36+
// This does NOT imply that the value is in the Bloom filter,
37+
// only that it may be. If the query returns false, however,
38+
// you can be certain that the value was not added.
39+
40+
let exists = results.allSatisfy { $0 }
41+
return exists
42+
}
43+
44+
public func isEmpty() -> Bool {
45+
// As soon as the reduction hits a 'true' value, the && condition will fail.
46+
array.first { !$0 } ?? true
47+
}
48+
}
49+
50+
/* Two hash functions, adapted from http://www.cse.yorku.ca/~oz/hash.html */
51+
52+
func djb2(x: String) -> Int {
53+
var hash = 5381
54+
for char in x {
55+
hash = ((hash << 5) &+ hash) &+ char.hashValue
56+
}
57+
return Int(hash)
58+
}
59+
60+
func sdbm(x: String) -> Int {
61+
var hash = 0
62+
for char in x {
63+
hash = char.hashValue &+ (hash << 6) &+ (hash << 16) &- hash
64+
}
65+
return Int(hash)
66+
}
67+
68+
/* A simple test */
69+
70+
let bloom = BloomFilter<String>(size: 17, hashFunctions: [djb2, sdbm])
71+
72+
bloom.insert("Hello world!")
73+
print(bloom.array)
74+
75+
bloom.query("Hello world!") // true
76+
bloom.query("Hello WORLD") // false
77+
78+
bloom.insert("Bloom Filterz")
79+
print(bloom.array)
80+
81+
bloom.query("Bloom Filterz") // true
82+
bloom.query("Hello WORLD") // true
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

SwiftAlgorithmClub/BloomFilter.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

SwiftAlgorithmClub/SwiftAlgorithmClub.xcworkspace/contents.xcworkspacedata

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)