Skip to content

Commit

Permalink
Add note about Hasher in README.
Browse files Browse the repository at this point in the history
* Adds a section about another Bloom filter approach, using only a single hashing function with Swift 4.2's `Hasher`.
* Adds links to some more documentation and a blog post implementing the Bloom filter in this way.
* Adds my name as updater.
  • Loading branch information
SpacyRicochet committed Oct 4, 2018
1 parent 4201fb4 commit 849309c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion Bloom Filter/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,29 @@ public func query(_ value: T) -> Bool {

If you're coming from another imperative language, you might notice the unusual syntax in the `exists` assignment. Swift makes use of functional paradigms when it makes code more consise and readable, and in this case `reduce` is a much more consise way to check if all the required bits are `true` than a `for` loop.

*Written for Swift Algorithm Club by Jamil Dhanani. Edited by Matthijs Hollemans.*
## Another approach

Another approach to create different hashes of an element for use in the Bloom filter, is to use the same hash function for every iteration, but combine it with different random numbers. This can help, because finding good hashing functions is hard, but combining them is equally non-trivial.

```
hash("Hello world!") >> hash(987654321) // would flip bit 8
hash("Hello world!") >> hash(123456789) // would flip bit 2
```

Since Swift 4.2, `Hasher` is now included in the Standard library, which is designed to reduce multiple hashes to a single hash in an efficient manner. This makes combining the hashes trivial.

```
private func computeHashes(_ value: T) -> [Int] {
return randomSeeds.map() { seed in
let hasher = Hasher()
hasher.combine(seed)
hasher.combine(value)
let hashValue = hasher.finalize()
return abs(hashValue % array.count)
}
}
```

If you want to learn more about this approach, you can read about the [Hasher documentation](https://developer.apple.com/documentation/swift/hasher) or Soroush Khanlou's [Swift 4.2 Bloom filter](http://khanlou.com/2018/09/bloom-filters/) implementation.

*Written for Swift Algorithm Club by Jamil Dhanani. Edited by Matthijs Hollemans. Updated by Bruno Scheele.*

0 comments on commit 849309c

Please sign in to comment.