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 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 @@ -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
@@ -1,10 +1,11 @@
package scala.collection
package test.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, TreeMap, TreeSet}
import scala.collection.mutable.{ArrayBuffer, Builder, ListBuffer}
import scala.collection.{BitSet, BuildFrom, SortedMap, SortedSet, immutable, mutable}

// 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 test.scala.collection

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

class CollectionTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package collection
package test.scala.collection

import org.junit.{Assert, Test}

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

class FactoryTest {

implicitly[Factory[Char, String]]
implicitly[Factory[Char, Array[Char]]]
implicitly[Factory[Int, BitSet]]
implicitly[Factory[Int, collection.BitSet]]
implicitly[Factory[Int, mutable.BitSet]]
implicitly[Factory[Int, immutable.BitSet]]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package collection
package test.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 test.scala.collection

import org.junit.Test

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

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

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))
}
*/
}
}