Skip to content
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 bitCount is 0 after intersect or union #40

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bloomfilter.mutable
import java.io._

import bloomfilter.util.Unsafe.unsafe
import scala.annotation.tailrec

@SerialVersionUID(2L)
class UnsafeBitArray(val numberOfBits: Long) extends Serializable {
Expand Down Expand Up @@ -33,6 +34,7 @@ class UnsafeBitArray(val numberOfBits: Long) extends Serializable {
val thatLong = unsafe.getLong(that.ptr + (index >>> 6) * 8L)
val longAtIndex = combiner(thisLong, thatLong)
unsafe.putLong(result.ptr + (index >>> 6) * 8L, longAtIndex)
result.addBitCount(UnsafeBitArray.getSetBit(longAtIndex))
index += 64
}
result
Expand All @@ -50,6 +52,10 @@ class UnsafeBitArray(val numberOfBits: Long) extends Serializable {
combine(that, _ & _)
}

def addBitCount(i: Long): Unit = {
bitCount += i
}

def getBitCount: Long = {
bitCount
}
Expand Down Expand Up @@ -102,4 +108,16 @@ object UnsafeBitArray {
private def readResolve: AnyRef = unsafeBitArray
}

def getSetBit(i: Long): Long = {
@tailrec
def aux(i: Long, cnt: Long): Long = {
if (i == 0) {
cnt
} else {
aux(i & (i -1), cnt + 1)
}
}

aux(i, 0)
}
}