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

add maxBefore and minAfter #612

Merged
merged 1 commit into from
Jul 22, 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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ lazy val compat = new MultiScalaCrossProject(
sharedSourceDir / "scala-2.11_2.12"
}
},
versionPolicyIntention := Compatibility.BinaryAndSourceCompatible,
versionPolicyIntention := Compatibility.BinaryCompatible,
mimaBinaryIssueFilters ++= {
import com.typesafe.tools.mima.core._
import com.typesafe.tools.mima.core.ProblemFilters._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ private[compat] trait PackageShared {
fact: Sorted[K, V]): SortedExtensionMethods[K, V] =
new SortedExtensionMethods[K, V](fact)

implicit def toSortedMapExtensionMethods[K, V](
fact: collection.SortedMap[K, V]): SortedMapExtensionMethods[K, V] =
new SortedMapExtensionMethods[K, V](fact)

implicit def toSortedSetExtensionMethods[A](
fact: collection.SortedSet[A]): SortedSetExtensionMethods[A] =
new SortedSetExtensionMethods[A](fact)

implicit def toIteratorExtensionMethods[A](self: Iterator[A]): IteratorExtensionMethods[A] =
new IteratorExtensionMethods[A](self)

Expand Down Expand Up @@ -401,6 +409,24 @@ class SortedExtensionMethods[K, T <: Sorted[K, T]](private val fact: Sorted[K, T
def rangeUntil(until: K): T = fact.until(until)
}

class SortedMapExtensionMethods[K, V](
private val self: collection.SortedMap[K, V]
) extends AnyVal {

def minAfter(key: K): Option[(K, V)] = self.from(key).headOption

def maxBefore(key: K): Option[(K, V)] = self.until(key).lastOption
}

class SortedSetExtensionMethods[A](
private val self: collection.SortedSet[A]
) extends AnyVal {

def minAfter(key: A): Option[A] = self.from(key).headOption

def maxBefore(key: A): Option[A] = self.until(key).lastOption
}

class IteratorExtensionMethods[A](private val self: c.Iterator[A]) extends AnyVal {
def sameElements[B >: A](that: c.TraversableOnce[B]): Boolean = {
self.sameElements(that.iterator)
Expand Down
44 changes: 44 additions & 0 deletions compat/src/test/scala/test/scala/collection/SortedTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,48 @@ class SortedTest {
}
*/
}

@Test
def sortedSetMinAfter(): Unit = {
val values = 1 to 10
assertEquals(values.to(collection.SortedSet).minAfter(8), Some(8))
assertEquals(values.to(collection.immutable.SortedSet).minAfter(9), Some(9))
assertEquals(values.to(collection.mutable.SortedSet).minAfter(10), Some(10))

assertEquals(values.to(collection.SortedSet).minAfter(11), None)
assertEquals(values.to(collection.immutable.SortedSet).minAfter(12), None)
assertEquals(values.to(collection.mutable.SortedSet).minAfter(13), None)
}

@Test
def sortedSetMaxBefore(): Unit = {
val values = 1 to 10
assertEquals(values.to(collection.SortedSet).maxBefore(4), Some(3))
assertEquals(values.to(collection.immutable.SortedSet).maxBefore(3), Some(2))
assertEquals(values.to(collection.mutable.SortedSet).maxBefore(2), Some(1))

assertEquals(values.to(collection.SortedSet).maxBefore(1), None)
assertEquals(values.to(collection.immutable.SortedSet).maxBefore(0), None)
assertEquals(values.to(collection.mutable.SortedSet).maxBefore(-1), None)
}

@Test
def sortedMapMinAfter(): Unit = {
val values = (1 to 10).map(x => x -> x.toString)
assertEquals(collection.SortedMap(values: _*).minAfter(9), Some(9 -> "9"))
assertEquals(collection.immutable.SortedMap(values: _*).minAfter(10), Some(10 -> "10"))

assertEquals(collection.SortedMap(values: _*).minAfter(11), None)
assertEquals(collection.immutable.SortedMap(values: _*).minAfter(12), None)
}

@Test
def sortedMapMaxBefore(): Unit = {
val values = (1 to 10).map(x => x -> x.toString)
assertEquals(collection.SortedMap(values: _*).maxBefore(3), Some(2 -> "2"))
assertEquals(collection.immutable.SortedMap(values: _*).maxBefore(2), Some(1 -> "1"))

assertEquals(collection.SortedMap(values: _*).maxBefore(1), None)
assertEquals(collection.immutable.SortedMap(values: _*).maxBefore(0), None)
}
}