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

keep fixing warnings in 'core' #571

Merged
merged 7 commits into from
Jan 15, 2023
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
9 changes: 5 additions & 4 deletions core/src/main/scala/cats/collections/HashMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@

package cats.collections

import cats.kernel.compat.scalaVersionSpecific._

import cats.Always
import cats.CommutativeApplicative
import cats.Eval
import cats.Foldable
import cats.Semigroup
import cats.Show
import cats.UnorderedTraverse
import cats.data.NonEmptyVector
import cats.kernel.CommutativeMonoid
import cats.kernel.CommutativeSemigroup
import cats.kernel.Monoid
import cats.kernel.Eq
import cats.kernel.Hash
import cats.kernel.Monoid
import cats.kernel.compat.scalaVersionSpecific._
import cats.syntax.eq._

import java.util.Arrays

import HashMap.improve
import HashMap.WrappedHashMap
import cats.data.NonEmptyVector

/**
* An immutable hash map using [[cats.kernel.Hash]] for hashing.
Expand All @@ -76,6 +76,7 @@ import cats.data.NonEmptyVector
* @param hashKey
* the [[cats.kernel.Hash]] instance used for hashing keys.
*/
@suppressUnusedImportWarningForScalaVersionSpecific
final class HashMap[K, +V] private[collections] (private[collections] val rootNode: HashMap.Node[K, V])(implicit
val hashKey: Hash[K]
) extends compat.HashMapCompat[K, V] {
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/scala/cats/collections/HashSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import HashSet.WrappedHashSet
* @param hash
* the [[cats.kernel.Hash]] instance used for hashing values.
*/
@suppressUnusedImportWarningForScalaVersionSpecific
final class HashSet[A] private (private val rootNode: HashSet.Node[A])(implicit val hash: Hash[A])
extends compat.HashSetCompat[A] {

Expand Down Expand Up @@ -1134,12 +1135,16 @@ object HashSet extends compat.HashSetCompatCompanion {
implicit def catsCollectionsShowForHashSet[A](implicit A: Show[A]): Show[HashSet[A]] =
Show.show[HashSet[A]](_.show)

implicit def catsCollectionsHashForHashSet[A](implicit A: Hash[A]): Hash[HashSet[A]] =
implicit def catsCollectionsHashForHashSet[A]: Hash[HashSet[A]] =
new Hash[HashSet[A]] {
def hash(hs: HashSet[A]): Int = hs.hashCode
def eqv(x: HashSet[A], y: HashSet[A]): Boolean = x === y
}

@deprecated("use unconstrained version instead", "0.9.6")
def catsCollectionsHashForHashSet[A](A: Hash[A]): Hash[HashSet[A]] =
catsCollectionsHashForHashSet

implicit def catsCollectionsDistributiveLatticeForHashSet[A](implicit A: Hash[A]): DistributiveLattice[HashSet[A]] =
new DistributiveLattice[HashSet[A]] {
private val joinMonoid = new HashSetUnionMonoid[A]
Expand Down
13 changes: 8 additions & 5 deletions core/src/main/scala/cats/collections/Heap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,7 @@ object Heap {
override def unbalanced: Boolean = size < (1L << height) - 1L
}

private[collections] case object Leaf extends Heap[Nothing] {
def apply[A](): Heap[A] = this.asInstanceOf[Heap[A]]

def unapply[A](heap: Heap[A]): Boolean = heap.isEmpty

final private[collections] case class Leaf[A] private () extends Heap[A] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much the same fix as it was in Diet.scala for the "match may not be exhaustive" warnings.

override def size: Long = 0L

override def height: Int = 0
Expand All @@ -325,6 +321,13 @@ object Heap {
override def minimumOption: Option[Nothing] = None
}

private[collections] object Leaf {
// Cached singleton instance for Leaf.
private val instance = new Leaf

def apply[A](): Leaf[A] = instance.asInstanceOf[Leaf[A]]
}

private[collections] def bubbleUp[A](x: A, l: Heap[A], r: Heap[A])(implicit order: Order[A]): Heap[A] = (l, r) match {
case (Branch(y, lt, rt), _) if order.gt(x, y) => Heap(y, Heap(x, lt, rt), r)
case (_, Branch(z, lt, rt)) if order.gt(x, z) => Heap(z, l, Heap(x, lt, rt))
Expand Down
23 changes: 13 additions & 10 deletions core/src/main/scala/cats/collections/PairingHeap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -308,24 +308,27 @@ object PairingHeap {
}
}

final private case object Leaf extends PairingHeap[Nothing] {
def apply[A](): PairingHeap[A] = this.asInstanceOf[PairingHeap[A]]

def unapply[A](heap: PairingHeap[A]): Boolean = heap.isEmpty

override def subtrees: List[PairingHeap[Nothing]] = Nil
final private case class Leaf[A] private () extends PairingHeap[A] {
override def subtrees: List[PairingHeap[A]] = Nil

override def size: Long = 0L

override def isEmpty: Boolean = true

override def minimumOption: Option[Nothing] = None
override def minimumOption: Option[A] = None

override def exists(fn: A => Boolean): Boolean = false

override def exists(fn: Nothing => Boolean): Boolean = false
override def forall(fn: A => Boolean): Boolean = true

override def combine(that: PairingHeap[A])(implicit order: Order[A]): PairingHeap[A] = that
}

override def forall(fn: Nothing => Boolean): Boolean = true
private object Leaf {
// Cached singleton instance for Leaf.
private val instance = new Leaf

override def combine(that: PairingHeap[Nothing])(implicit order: Order[Nothing]): PairingHeap[Nothing] = that
def apply[A](): Leaf[A] = instance.asInstanceOf[Leaf[A]]
}

implicit def toShowable[A](implicit s: Show[A], order: Order[A]): Show[PairingHeap[A]] = new Show[PairingHeap[A]] {
Expand Down
23 changes: 14 additions & 9 deletions core/src/main/scala/cats/collections/Range.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

package cats.collections

import cats.{Eq, Order, Show}
import cats.Eq
import cats.Order
import cats.Show

/**
* Represent an inclusive range [x, y] that can be generated by using discrete operations
*/
final case class Range[A](val start: A, val end: A) {
final case class Range[A](start: A, end: A) {

/**
* Subtract a Range from this range. The result will be 0, 1 or 2 ranges
Expand Down Expand Up @@ -77,13 +79,12 @@ final case class Range[A](val start: A, val end: A) {
* the start's successor.
*/
def toIterator(implicit discrete: Discrete[A], order: Order[A]): Iterator[A] = {
val next: A => A = a => if (order.lteqv(a, end)) discrete.succ(a) else discrete.pred(a)
val next: A => A = if (order.lt(start, end)) discrete.succ(_) else discrete.pred(_)

Stream
Iterator
.iterate(start)(next)
.takeWhile(concurrent => !order.eqv(concurrent, end))
.append(Seq(end))
.iterator
.takeWhile(order.neqv(_, end))
.++(Iterator.single(end))
}

/**
Expand All @@ -103,14 +104,18 @@ final case class Range[A](val start: A, val end: A) {
def contains(x: A)(implicit A: Order[A]): Boolean = A.gteqv(x, start) && A.lteqv(x, end)

/**
* Apply function f to each element in range [star, end]
* Apply function f to each element in range `[star, end]`. Does nothing if `end` precedes `start`.
*/
def foreach(f: A => Unit)(implicit discrete: Discrete[A], order: Order[A]): Unit = {
var i = start
while (order.lteqv(i, end)) {
// Visit values in range [start, end), i.e. excluding `end`.
while (order.lt(i, end)) {
f(i)
i = discrete.succ(i)
}
// Visit the last (or the only) value, if any.
if (order.eqv(i, end))
f(i)
Comment on lines +111 to +118
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation was resulting to infinite loops in cases like

Range(123.toByte, Byte.MaxValue).foreach(_ => ())

I.e. due to integer overflows that could not be caught previously.

}

def map[B](f: A => B): Range[B] = Range[B](f(start), f(end))
Expand Down
5 changes: 4 additions & 1 deletion mima.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ import com.typesafe.tools.mima.core._
ThisBuild / mimaBinaryIssueFilters ++= Seq(
// Methods and other things defined as `private [collections]` and therefore
// not supposed to be referenced externally.
exclude[IncompatibleMethTypeProblem]("cats.collections.Diet.splitMax")
exclude[IncompatibleMethTypeProblem]("cats.collections.Diet.splitMax"),
// PairingHeap#Leaf has always been private so it's unclear why Mima gets triggered on its changes.
exclude[MissingTypesProblem]("cats.collections.PairingHeap$Leaf$"),
exclude[Problem]("cats.collections.PairingHeap#Leaf.*")
)
Loading