Skip to content

Commit

Permalink
Merge c1c144f into f066acf
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeiakimov authored Nov 9, 2023
2 parents f066acf + c1c144f commit b261b85
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
52 changes: 19 additions & 33 deletions core/src/main/scala/io/qbeast/core/model/CubeId.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package io.qbeast.core.model
import io.qbeast.core.model.CubeId.{ChildrenIterator, Codec}

import java.nio.ByteBuffer
import java.util.Arrays
import scala.collection.immutable.BitSet
import scala.collection.mutable

Expand Down Expand Up @@ -95,20 +94,6 @@ object CubeId {
containers(point).drop(depth).next()
}

private def trimBitMask(bitMask: Array[Long]): Array[Long] = {
var last = bitMask.length - 1
while (last >= 0 && bitMask(last) == 0) {
last -= 1
}
if (last < bitMask.length - 1) {
val trimmedBitMask = new Array[Long](last + 1)
Array.copy(bitMask, 0, trimmedBitMask, 0, trimmedBitMask.length)
trimmedBitMask
} else {
bitMask
}
}

private class ContainersIterator(point: Point, parent: Option[CubeId])
extends Iterator[CubeId] {

Expand Down Expand Up @@ -270,14 +255,17 @@ case class CubeId(dimensionCount: Int, depth: Int, bitMask: Array[Long])
* is less than, equal to, or greater than the other CubeId.
*/
override def compare(that: CubeId): Int = {
val thisBitset = BitSet.fromBitMaskNoCopy(bitMask)
val thatBitset = BitSet.fromBitMaskNoCopy(that.bitMask)
val commonDepth = math.min(depth, that.depth)
for (depthOffset <- 0.until(commonDepth * dimensionCount)) {
val firstBit = thisBitset.contains(depthOffset)
val secondBit = thatBitset.contains(depthOffset)
if (firstBit != secondBit) {
if (firstBit) {
require(
that.dimensionCount == dimensionCount,
"The two cubes must have the same dimension count.")
val thisBits = BitSet.fromBitMaskNoCopy(bitMask)
val thatBits = BitSet.fromBitMaskNoCopy(that.bitMask)
val end = dimensionCount * math.min(depth, that.depth)
for (i <- (0 until end)) {
val thisBit = thisBits.contains(i)
val thatBit = thatBits.contains(i)
if (thisBit != thatBit) {
if (thisBit) {
return 1
} else {
return -1
Expand All @@ -302,14 +290,13 @@ case class CubeId(dimensionCount: Int, depth: Int, bitMask: Array[Long])
require(
other.dimensionCount == dimensionCount,
"The two cubes must have the same dimension count.")

if (depth > other.depth) {
false
} else {
val end = dimensionCount * depth
val ancestorBitMask = BitSet.fromBitMaskNoCopy(other.bitMask).until(end).toBitMask
Arrays.equals(CubeId.trimBitMask(bitMask), CubeId.trimBitMask(ancestorBitMask))
return false
}
val end = dimensionCount * depth
val bits = BitSet.fromBitMaskNoCopy(bitMask)
val otherBits = BitSet.fromBitMask(other.bitMask).until(end)
bits == otherBits
}

/**
Expand Down Expand Up @@ -402,9 +389,8 @@ case class CubeId(dimensionCount: Int, depth: Int, bitMask: Array[Long])

override def equals(obj: Any): Boolean = obj match {
case other: CubeId =>
dimensionCount == other.dimensionCount && depth == other.depth && Arrays.equals(
CubeId.trimBitMask(bitMask),
CubeId.trimBitMask(other.bitMask))
dimensionCount == other.dimensionCount && depth == other.depth && BitSet.fromBitMaskNoCopy(
bitMask) == BitSet.fromBitMaskNoCopy(other.bitMask)
case _ => false
}

Expand All @@ -413,7 +399,7 @@ case class CubeId(dimensionCount: Int, depth: Int, bitMask: Array[Long])
var result = 1
result = prime * result + dimensionCount
result = prime * result + depth
result = prime * result + Arrays.hashCode(CubeId.trimBitMask(bitMask))
result = prime * result + BitSet.fromBitMaskNoCopy(bitMask).hashCode()
result
}

Expand Down
4 changes: 4 additions & 0 deletions core/src/test/scala/io/qbeast/core/model/CubeIdTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class CubeIdTest extends AnyFlatSpec with Matchers {
val id7 =
CubeId(2, "wQwwwQwwQwwQwwwwwQwQwwwQQwwwwQQwQwwwQwwQwwQwwwwwQwwQQQQQQQQQQQQQ")
id6 == id7 shouldBe true
val id8 =
CubeId(1, 4, Array(9L)).parent.get.parent.get.parent.get
val id9 = CubeId(1, 1, Array(1L))
id8 == id9 shouldBe true
}

it should "implement hashCode correctly" in {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.qbeast.core.model

import io.qbeast.core.transform.{HashTransformation, LinearTransformation, Transformation, Transformer}
import io.qbeast.core.transform.{
HashTransformation,
LinearTransformation,
Transformation,
Transformer
}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand Down

0 comments on commit b261b85

Please sign in to comment.