Skip to content

Commit

Permalink
removing unused comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Aug 31, 2023
1 parent a057767 commit d80091b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
9 changes: 0 additions & 9 deletions avldb/src/main/scala/scorex/db/ByteArrayUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ package scorex.db

object ByteArrayUtils {

import java.util.Comparator

// Java comparator
val BYTE_ARRAY_COMPARATOR: Comparator[Array[Byte]] = (o1: Array[Byte], o2: Array[Byte]) => compare(o1, o2)

// Scala comparator
implicit val ByteArrayOrdering: Ordering[Array[Byte]] =
(o1: Array[Byte], o2: Array[Byte]) => ByteArrayUtils.compare(o1, o2)

def compare(o1: Array[Byte], o2: Array[Byte]): Int = {
val len = Math.min(o1.length, o2.length)
var i = 0
Expand Down
5 changes: 4 additions & 1 deletion avldb/src/test/scala/scorex/db/ByteArrayUtilsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ class ByteArrayUtilsSpec extends AnyPropSpec with ScalaCheckPropertyChecks with

property("compare works properly") {

val effOrdering: Ordering[Array[Byte]] =
(o1: Array[Byte], o2: Array[Byte]) => ByteArrayUtils.compare(o1, o2)

//Simple and inefficient way to order byte arrays, based on
// https://stackoverflow.com/questions/7109943/how-to-define-orderingarraybyte
// but we compare unsigned bytes
val ordering: Ordering[Array[Byte]] = Ordering.by((_: Array[Byte]).toIterable.map(_ & 0xFF))

forAll(nonEmptyBytesGen, nonEmptyBytesGen) { case (bs1, bs2) =>
val efficientOrdering = Seq(bs1, bs2).sorted(ByteArrayUtils.ByteArrayOrdering)
val efficientOrdering = Seq(bs1, bs2).sorted(effOrdering)
val simpleOrdering = Seq(bs1, bs2).sorted(ordering)

efficientOrdering(0).sameElements(simpleOrdering(0)) shouldBe true
Expand Down

0 comments on commit d80091b

Please sign in to comment.