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

#228 Implementation of equals/hashCode of CubeId is fixed #231

Merged
Show file tree
Hide file tree
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
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
11 changes: 11 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 Expand Up @@ -153,6 +157,13 @@ class CubeIdTest extends AnyFlatSpec with Matchers {
id4.nextSibling shouldBe None
}

it should "implement children iterator throwing NoSuchElementException after last child" in {
val children = CubeId.root(1).children
children.next() shouldBe CubeId.root(1).firstChild
children.next() shouldBe CubeId.root(1).firstChild.nextSibling.get
assertThrows[NoSuchElementException](children.next())
}

it should "return a correct container with specified depth" in {
val point = Point(0.66, 0.83)
val id = CubeId.container(point, 2)
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
Loading