From 63dcfc7d7baa514df4f769f4e71612bd298d6a71 Mon Sep 17 00:00:00 2001 From: Alexandr Nikitin Date: Mon, 6 Feb 2017 17:32:00 +0200 Subject: [PATCH] Byte based UnsafeBitArray --- .../bloomfilter/mutable/UnsafeBitArray.scala | 15 +++++++-------- .../bloomfilter/mutable/UnsafeBitArraySpec.scala | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/bloom-filter/src/main/scala/bloomfilter/mutable/UnsafeBitArray.scala b/bloom-filter/src/main/scala/bloomfilter/mutable/UnsafeBitArray.scala index c2bec90..bbbb092 100644 --- a/bloom-filter/src/main/scala/bloomfilter/mutable/UnsafeBitArray.scala +++ b/bloom-filter/src/main/scala/bloomfilter/mutable/UnsafeBitArray.scala @@ -5,18 +5,17 @@ import java.io.{DataInputStream, DataOutputStream, InputStream, OutputStream} import scala.concurrent.util.Unsafe.{instance => unsafe} class UnsafeBitArray(val numberOfBits: Long) { - private val indices = math.ceil(numberOfBits.toDouble / 64).toLong - private val ptr = unsafe.allocateMemory(8L * indices) - unsafe.setMemory(ptr, 8L * indices, 0.toByte) - + private val indices = math.ceil(numberOfBits.toDouble / 8).toLong + private val ptr = unsafe.allocateMemory(indices) + unsafe.setMemory(ptr, indices, 0.toByte) def get(index: Long): Boolean = { - (unsafe.getLong(ptr + (index >>> 6) * 8L) & (1L << index)) != 0 + (unsafe.getByte(ptr + (index >>> 3)) & (1L << (index & 0x7))) != 0 } def set(index: Long): Unit = { - val offset = ptr + (index >>> 6) * 8L - val long = unsafe.getLong(offset) - unsafe.putLong(offset, long | (1L << index)) + val offset = ptr + (index >>> 3) + val value = unsafe.getByte(offset) + unsafe.putByte(offset, (value | (1L << (index & 0x7))).toByte) } def combine(that: UnsafeBitArray, combiner: (Long, Long) => Long): UnsafeBitArray = { diff --git a/tests/src/test/scala/tests/bloomfilter/mutable/UnsafeBitArraySpec.scala b/tests/src/test/scala/tests/bloomfilter/mutable/UnsafeBitArraySpec.scala index d18ed8b..d46c32a 100644 --- a/tests/src/test/scala/tests/bloomfilter/mutable/UnsafeBitArraySpec.scala +++ b/tests/src/test/scala/tests/bloomfilter/mutable/UnsafeBitArraySpec.scala @@ -37,12 +37,12 @@ class UnsafeBitArraySpec extends Properties("UnsafeBitArray") { def genCommand(state: State): Gen[Command] = for { - i <- Gen.choose[Long](0, state.size) + i <- Gen.choose[Long](0, state.size - 1) } yield commandSequence(SetItem(i), GetItem(i)) case class SetItem(i: Long) extends UnitCommand { def run(sut: Sut): Unit = sut.synchronized(sut.set(i)) - def nextState(state: State) = state + def nextState(state: State): State = state def preCondition(state: State) = true def postCondition(state: State, success: Boolean) = success } @@ -50,7 +50,7 @@ class UnsafeBitArraySpec extends Properties("UnsafeBitArray") { case class GetItem(i: Long) extends SuccessCommand { type Result = Boolean def run(sut: Sut): Boolean = sut.synchronized(sut.get(i)) - def nextState(state: State) = state + def nextState(state: State): State = state def preCondition(state: State) = true def postCondition(state: State, result: Boolean): Prop = result }