Skip to content

range methods on SortedSet / SortedMap #13

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

Merged
merged 6 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -11,7 +11,7 @@ unmanagedSourceDirectories in Compile += (
else (sourceDirectory in Compile).value / "scala-2.11_2.12"
)

crossScalaVersions := Seq("2.12.5", "2.13.0-M4-pre-20d3c21", "2.11.12")
crossScalaVersions := Seq("2.12.5", "2.13.0-pre-b11db01", "2.11.12")

scalaVersion := crossScalaVersions.value.head

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package scala.collection
package scala.collection.compat

import scala.collection.generic._
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.Builder
import scala.reflect.ClassTag
import scala.collection.{immutable, mutable}

package object compat_impl {
private[compat] object CompatImpl {
def simpleCBF[A, C](f: => Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] {
def apply(from: Any): Builder[A, C] = apply()
def apply(): Builder[A, C] = f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.reflect.ClassTag

/** The collection compatibility API */
package object compat {
import scala.collection.compat_impl._
import CompatImpl._

implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]](fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] =
simpleCBF(fact.newBuilder[A])
Expand Down Expand Up @@ -48,6 +48,12 @@ package object compat {
def lazyAppendAll(as: => TraversableOnce[A]): Stream[A] = stream.append(as)
}

implicit class SortedExtensionMethods[K, T <: Sorted[K, T]](private val fact: Sorted[K, T]) {
def rangeFrom(from: K): T = fact.from(from)
def rangeTo(to: K): T = fact.to(to)
def rangeUntil(until: K): T = fact.until(until)
}

// This really belongs into scala.collection but there's already a package object in scala-library so we can't add to it
type IterableOnce[+X] = TraversableOnce[X]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package scala.collection

import org.junit.Test

import scala.collection.mutable.{ArrayBuffer, Builder, ListBuffer}
import scala.collection.immutable.{HashMap, List, TreeMap, TreeSet}
import scala.collection.compat._
import scala.collection.immutable.{HashMap, List, TreeMap, TreeSet}
import scala.collection.mutable.{ArrayBuffer, Builder, ListBuffer}

// Tests copied from the 2.13 scala-library
class BuildFromTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package collection
package scala.collection
Copy link
Contributor

Choose a reason for hiding this comment

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

Initially the tests were in the “empty” package. I moved them to the collection package just to not be in the empty package. However I’d rather not use the scala.collection package because it implicitly imports everything that is in that package, whereas user code doesn’t do that usually. I think we should put the tests in a dummy package just to reproduce the same context as in user code. (And that would solve the issue with BitSet shadowing)

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I'll move them. What do you think about test.scala.collection....? Having them in _root_.collection looked like an oversight.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK!

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be good now.


import java.util

import org.junit.Test
import org.junit.Assert._
import org.junit.Test

import scala.collection.immutable.BitSet
import scala.collection.compat._
import scala.collection.immutable.{BitSet => BS} // importing BitSet doesn't work, scala.collection.BitSet shadows it

class CollectionTest {
@Test
Expand All @@ -20,9 +18,9 @@ class CollectionTest {
val aT: Array[Int] = a
assertEquals(Vector(1,2,3), a.toVector)

val b = xs.to(BitSet) // we can fake a type constructor for the 2 standard BitSet types
val b = xs.to(BS) // we can fake a type constructor for the 2 standard BitSet types
val bT: BitSet = b
assertEquals(BitSet(1,2,3), b)
assertEquals(BS(1,2,3), b)

val ys = List(1 -> "a", 2 -> "b")
val m = ys.to(Map)
Expand All @@ -39,9 +37,9 @@ class CollectionTest {
val vT: Vector[Int] = v
assertEquals(Vector(1,2,3), v)

val b = BitSet.fromSpecific(xs)
val bT: BitSet = b
assertEquals(BitSet(1,2,3), b)
val b = BS.fromSpecific(xs)
val bT: BS = b
assertEquals(BS(1,2,3), b)

val ys = List(1 -> "a", 2 -> "b")
val m = Map.from(ys)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package collection
package scala.collection

import org.junit.{Assert, Test}

import scala.collection.{BitSet, Factory, Iterable, immutable, mutable}
import scala.collection.compat._

class FactoryTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package collection
package scala.collection

import org.junit.{Assert, Test}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package collection
package scala.collection

import org.junit.Test

Expand Down
55 changes: 55 additions & 0 deletions src/test/scala/scala/collection/generic/SortedTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package scala.collection.generic

import org.junit.Test
import org.junit.Assert._

import scala.collection.compat._
import scala.collection.{immutable => i, mutable => m}

class SortedTest {
@Test
def immutableRangeOps(): Unit = {
val st = i.SortedSet(-1, 2, 4, 3)
for (x <- List(0, 1, 4, -1, -4)) {
val s1 = st.rangeFrom(x)
assertEquals(s1: i.SortedSet[Int], st.from(x))
val s2 = st.rangeTo(x)
assertEquals(s2: i.SortedSet[Int], st.to(x))
val s3 = st.rangeUntil(x)
assertEquals(s3: i.SortedSet[Int], st.until(x))
}
val mp = i.SortedMap("" -> 0, "ds" -> -3, "-??" -> 13, "Df" -> 33, "d!" -> -32)
for (x <- List("", "-", "@", "aa", "D", "d")) {
val m1 = mp.rangeFrom(x)
assertEquals(m1: i.SortedMap[String, Int], mp.from(x))
val m2 = mp.rangeTo(x)
assertEquals(m2: i.SortedMap[String, Int], mp.to(x))
val m3 = mp.rangeUntil(x)
assertEquals(m3: i.SortedMap[String, Int], mp.until(x))
}
}

@Test
def mutableRangeOps(): Unit = {
val st = m.SortedSet(-1, 2, 4, 3)
for (x <- List(0, 1, 4, -1, -4)) {
val s1 = st.rangeFrom(x)
assertEquals(s1: m.SortedSet[Int], st.from(x))
val s2 = st.rangeTo(x)
assertEquals(s2: m.SortedSet[Int], st.to(x))
val s3 = st.rangeUntil(x)
assertEquals(s3: m.SortedSet[Int], st.until(x))
}
/* 2.11 doesn't have a mutable.SortedMap
val mp = m.SortedMap("" -> 0, "ds" -> -3, "-??" -> 13, "Df" -> 33, "d!" -> -32)
for (x <- List("", "-", "@", "aa", "D", "d")) {
val m1 = mp.rangeFrom(x)
assertEquals(m1: m.SortedMap[String, Int], mp.from(x))
val m2 = mp.rangeTo(x)
assertEquals(m2: m.SortedMap[String, Int], mp.to(x))
val m3 = mp.rangeUntil(x)
assertEquals(m3: m.SortedMap[String, Int], mp.until(x))
}
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think of using TreeMap instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

There's no mutable.TreeMap in 2.11..

}
}