-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #24: can the add() method return a boolean indicating a 'fresh' insert #27
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,17 +12,27 @@ class BloomFilter[T] private (val numberOfBits: Long, val numberOfHashes: Int, p | |
this(numberOfBits, numberOfHashes, new UnsafeBitArray(numberOfBits)) | ||
} | ||
|
||
/** Add an item in the Bloom filter | ||
* | ||
* @param x the T item to add | ||
* @return true if the item was not in the filter before, false otherwise | ||
*/ | ||
def add(x: T): Unit = { | ||
val hash = canGenerateHash.generateHash(x) | ||
val hash1 = hash >>> 32 | ||
val hash2 = (hash << 32) >> 32 | ||
var was_defined = true | ||
|
||
var i = 0 | ||
while (i < numberOfHashes) { | ||
val computedHash = hash1 + i * hash2 | ||
if (!bits.get((computedHash & Long.MaxValue) % numberOfBits)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an additional read op that I really want to avoid. It can be done inside UnsafeBitArray but still. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not for the separate method, def addAndGet(x: T): Boolean = {
val was_set = mightContain(x)
add(x)
!was_set
} or C&P? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the best way is a separate method in
|
||
was_defined = false | ||
bits.set((computedHash & Long.MaxValue) % numberOfBits) | ||
i += 1 | ||
} | ||
|
||
!was_defined | ||
} | ||
|
||
def union(that: BloomFilter[T]): BloomFilter[T] = { | ||
|
@@ -41,6 +51,7 @@ class BloomFilter[T] private (val numberOfBits: Long, val numberOfHashes: Int, p | |
val hash = canGenerateHash.generateHash(x) | ||
val hash1 = hash >>> 32 | ||
val hash2 = (hash << 32) >> 32 | ||
|
||
var i = 0 | ||
while (i < numberOfHashes) { | ||
val computedHash = hash1 + i * hash2 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still return
Unit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops