Skip to content

Commit

Permalink
Backport #3126 Remove Order constraint from Hash instance for Sor…
Browse files Browse the repository at this point in the history
…tedSet (#3339)

* backported #3126 removed `Order` constraint from `Hash` instance for `SortedSet`
  • Loading branch information
gagandeepkalra authored Mar 10, 2020
1 parent 29efba5 commit 8227961
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions core/src/main/scala/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ trait AllInstancesBinCompat7
with VectorInstancesBinCompat1
with EitherInstancesBinCompat0
with StreamInstancesBinCompat1
with SortedSetInstancesBinCompat2
6 changes: 5 additions & 1 deletion core/src/main/scala/cats/instances/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ package object instances {
with SortedMapInstancesBinCompat0
with SortedMapInstancesBinCompat1
with SortedMapInstancesBinCompat2
object sortedSet extends SortedSetInstances with SortedSetInstancesBinCompat0 with SortedSetInstancesBinCompat1
object sortedSet
extends SortedSetInstances
with SortedSetInstancesBinCompat0
with SortedSetInstancesBinCompat1
with SortedSetInstancesBinCompat2
object stream extends StreamInstances with StreamInstancesBinCompat0 with StreamInstancesBinCompat1
object string extends StringInstances
object try_ extends TryInstances
Expand Down
18 changes: 13 additions & 5 deletions core/src/main/scala/cats/instances/sortedSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package cats
package instances

import cats.kernel.{BoundedSemilattice, Hash, Order}
import scala.collection.immutable.SortedSet

import scala.annotation.tailrec
import scala.collection.immutable.SortedSet

trait SortedSetInstances extends SortedSetInstances1 {

Expand Down Expand Up @@ -73,7 +74,7 @@ trait SortedSetInstances extends SortedSetInstances1 {
private[instances] trait SortedSetInstances1 {
@deprecated("Use cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet", "2.0.0-RC2")
private[instances] def catsKernelStdHashForSortedSet[A: Order: Hash]: Hash[SortedSet[A]] =
cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet[A]
cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet[A](Hash[A])

@deprecated("Use cats.kernel.instances.sortedSet.catsKernelStdSemilatticeForSortedSet", "2.0.0-RC2")
def catsKernelStdSemilatticeForSortedSet[A: Order]: BoundedSemilattice[SortedSet[A]] =
Expand All @@ -93,8 +94,9 @@ private[instances] trait SortedSetInstancesBinCompat0 {

private[instances] trait SortedSetInstancesBinCompat1 extends LowPrioritySortedSetInstancesBinCompat1 {
// TODO: Remove when this is no longer necessary for binary compatibility.
implicit override def catsKernelStdHashForSortedSet[A: Order: Hash]: Hash[SortedSet[A]] =
cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet[A]
@deprecated("Use cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet", "2.0.0-RC2")
override def catsKernelStdHashForSortedSet[A: Order: Hash]: Hash[SortedSet[A]] =
cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet(Hash[A])
}

private[instances] trait LowPrioritySortedSetInstancesBinCompat1
Expand All @@ -103,8 +105,14 @@ private[instances] trait LowPrioritySortedSetInstancesBinCompat1
implicit override def catsKernelStdOrderForSortedSet[A: Order]: Order[SortedSet[A]] =
cats.kernel.instances.sortedSet.catsKernelStdOrderForSortedSet[A]

@deprecated("Use cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet", "2.0.0-RC2")
override def catsKernelStdHashForSortedSet[A: Order: Hash]: Hash[SortedSet[A]] =
cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet[A]
cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet(Hash[A])
}

private[instances] trait SortedSetInstancesBinCompat2 extends SortedSetInstancesBinCompat1 {
implicit def catsKernelStdHashForSortedSet[A: Hash]: Hash[SortedSet[A]] =
cats.kernel.instances.sortedSet.catsKernelStdHashForSortedSet[A](Hash[A])
}

@deprecated("Use cats.kernel.instances.SortedSetHash", "2.0.0-RC2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ trait AllInstances
private[instances] trait AllInstancesBinCompat0 extends FiniteDurationInstances

private[instances] trait AllInstancesBinCompat1 extends SortedMapInstances with SortedSetInstances

private[instances] trait AllInstancesBinCompat2 extends SortedSetInstances2
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cats.kernel
package instances

import cats.kernel.{BoundedSemilattice, Hash, Order}
import scala.collection.immutable.SortedSet

trait SortedSetInstances extends SortedSetInstances1 {
implicit def catsKernelStdHashForSortedSet[A: Order: Hash]: Hash[SortedSet[A]] =
@deprecated("Use catsKernelStdHashForSortedSet without Order", "2.1.0")
def catsKernelStdHashForSortedSet[A: Order: Hash]: Hash[SortedSet[A]] =
new SortedSetHash[A]
}

Expand All @@ -17,6 +17,11 @@ private[instances] trait SortedSetInstances1 {
new SortedSetSemilattice[A]
}

private[instances] trait SortedSetInstances2 extends SortedSetInstances1 {
implicit def catsKernelStdHashForSortedSet[A: Hash]: Hash[SortedSet[A]] =
new SortedSetHash[A]
}

class SortedSetOrder[A: Order] extends Order[SortedSet[A]] {
def compare(a1: SortedSet[A], a2: SortedSet[A]): Int =
cats.kernel.instances.int.catsKernelStdOrderForInt.compare(a1.size, a2.size) match {
Expand All @@ -28,9 +33,12 @@ class SortedSetOrder[A: Order] extends Order[SortedSet[A]] {
StaticMethods.iteratorEq(s1.iterator, s2.iterator)
}

class SortedSetHash[A: Order: Hash] extends Hash[SortedSet[A]] {
class SortedSetHash[A](implicit hashA: Hash[A]) extends Hash[SortedSet[A]] {
import scala.util.hashing.MurmurHash3._

@deprecated("Use the constructor _without_ Order instead, since Order is not required", "2.1.0")
private[instances] def this(o: Order[A], h: Hash[A]) = this()(h)

// adapted from [[scala.util.hashing.MurmurHash3]],
// but modified standard `Any#hashCode` to `ev.hash`.
def hash(xs: SortedSet[A]): Int = {
Expand All @@ -50,7 +58,7 @@ class SortedSetHash[A: Order: Hash] extends Hash[SortedSet[A]] {
finalizeHash(h, n)
}
override def eqv(s1: SortedSet[A], s2: SortedSet[A]): Boolean =
StaticMethods.iteratorEq(s1.iterator, s2.iterator)(Order[A])
StaticMethods.iteratorEq(s1.iterator, s2.iterator)(Eq[A])
}

class SortedSetSemilattice[A: Order] extends BoundedSemilattice[SortedSet[A]] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package cats.kernel
package instances

package object all extends AllInstances with AllInstancesBinCompat0 with AllInstancesBinCompat1
package object all
extends AllInstances
with AllInstancesBinCompat0
with AllInstancesBinCompat1
with AllInstancesBinCompat2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cats.kernel
package instances

package object sortedSet extends SortedSetInstances
package object sortedSet extends SortedSetInstances with SortedSetInstances2

0 comments on commit 8227961

Please sign in to comment.