Skip to content

Commit

Permalink
add maxBefore and minAfter
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Jul 8, 2023
1 parent 65cf59e commit bed5a3b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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)
}
}

0 comments on commit bed5a3b

Please sign in to comment.