Skip to content

Commit 41243aa

Browse files
authored
Merge pull request #13 from lrytz/sortedRange
range methods on SortedSet / SortedMap
2 parents d4c8c3b + acf9f39 commit 41243aa

File tree

12 files changed

+79
-19
lines changed

12 files changed

+79
-19
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ unmanagedSourceDirectories in Compile += (
1111
else (sourceDirectory in Compile).value / "scala-2.11_2.12"
1212
)
1313

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

1616
scalaVersion := crossScalaVersions.value.head
1717

src/main/scala-2.11_2.12/collection/compat_impl/package.scala renamed to src/main/scala-2.11_2.12/scala/collection/compat/CompatImpl.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package scala.collection
1+
package scala.collection.compat
22

3-
import scala.collection.generic._
3+
import scala.collection.generic.CanBuildFrom
44
import scala.collection.mutable.Builder
5-
import scala.reflect.ClassTag
5+
import scala.collection.{immutable, mutable}
66

7-
package object compat_impl {
7+
private[compat] object CompatImpl {
88
def simpleCBF[A, C](f: => Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] {
99
def apply(from: Any): Builder[A, C] = apply()
1010
def apply(): Builder[A, C] = f

src/main/scala-2.11_2.12/collection/compat/package.scala renamed to src/main/scala-2.11_2.12/scala/collection/compat/package.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.reflect.ClassTag
66

77
/** The collection compatibility API */
88
package object compat {
9-
import scala.collection.compat_impl._
9+
import CompatImpl._
1010

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

51+
implicit class SortedExtensionMethods[K, T <: Sorted[K, T]](private val fact: Sorted[K, T]) {
52+
def rangeFrom(from: K): T = fact.from(from)
53+
def rangeTo(to: K): T = fact.to(to)
54+
def rangeUntil(until: K): T = fact.until(until)
55+
}
56+
5157
// This really belongs into scala.collection but there's already a package object in scala-library so we can't add to it
5258
type IterableOnce[+X] = TraversableOnce[X]
5359
}

src/test/scala/collection/BuildFromTest.scala renamed to src/test/scala/test/scala/collection/BuildFromTest.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package scala.collection
1+
package test.scala.collection
22

33
import org.junit.Test
44

5-
import scala.collection.mutable.{ArrayBuffer, Builder, ListBuffer}
6-
import scala.collection.immutable.{HashMap, List, TreeMap, TreeSet}
75
import scala.collection.compat._
6+
import scala.collection.immutable.{HashMap, TreeMap, TreeSet}
7+
import scala.collection.mutable.{ArrayBuffer, Builder, ListBuffer}
8+
import scala.collection.{BitSet, BuildFrom, SortedMap, SortedSet, immutable, mutable}
89

910
// Tests copied from the 2.13 scala-library
1011
class BuildFromTest {

src/test/scala/collection/CollectionTest.scala renamed to src/test/scala/test/scala/collection/CollectionTest.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
package collection
1+
package test.scala.collection
22

3-
import java.util
4-
5-
import org.junit.Test
63
import org.junit.Assert._
4+
import org.junit.Test
75

8-
import scala.collection.immutable.BitSet
96
import scala.collection.compat._
7+
import scala.collection.immutable.BitSet
108

119
class CollectionTest {
1210
@Test

src/test/scala/collection/FactoryTest.scala renamed to src/test/scala/test/scala/collection/FactoryTest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package collection
1+
package test.scala.collection
22

33
import org.junit.{Assert, Test}
44

5-
import scala.collection.{BitSet, Factory, Iterable, immutable, mutable}
65
import scala.collection.compat._
6+
import scala.collection.{BitSet, Factory, immutable, mutable}
77

88
class FactoryTest {
99

1010
implicitly[Factory[Char, String]]
1111
implicitly[Factory[Char, Array[Char]]]
12-
implicitly[Factory[Int, BitSet]]
12+
implicitly[Factory[Int, collection.BitSet]]
1313
implicitly[Factory[Int, mutable.BitSet]]
1414
implicitly[Factory[Int, immutable.BitSet]]
1515

src/test/scala/collection/ImmutableArrayTest.scala renamed to src/test/scala/test/scala/collection/ImmutableArrayTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package collection
1+
package test.scala.collection
22

33
import org.junit.{Assert, Test}
44

src/test/scala/collection/StreamTest.scala renamed to src/test/scala/test/scala/collection/StreamTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package collection
1+
package test.scala.collection
22

33
import org.junit.Test
44

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package test.scala.collection.generic
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
import scala.collection.compat._
7+
import scala.collection.{immutable => i, mutable => m}
8+
9+
class SortedTest {
10+
@Test
11+
def immutableRangeOps(): Unit = {
12+
val st = i.SortedSet(-1, 2, 4, 3)
13+
for (x <- List(0, 1, 4, -1, -4)) {
14+
val s1 = st.rangeFrom(x)
15+
assertEquals(s1: i.SortedSet[Int], st.from(x))
16+
val s2 = st.rangeTo(x)
17+
assertEquals(s2: i.SortedSet[Int], st.to(x))
18+
val s3 = st.rangeUntil(x)
19+
assertEquals(s3: i.SortedSet[Int], st.until(x))
20+
}
21+
val mp = i.SortedMap("" -> 0, "ds" -> -3, "-??" -> 13, "Df" -> 33, "d!" -> -32)
22+
for (x <- List("", "-", "@", "aa", "D", "d")) {
23+
val m1 = mp.rangeFrom(x)
24+
assertEquals(m1: i.SortedMap[String, Int], mp.from(x))
25+
val m2 = mp.rangeTo(x)
26+
assertEquals(m2: i.SortedMap[String, Int], mp.to(x))
27+
val m3 = mp.rangeUntil(x)
28+
assertEquals(m3: i.SortedMap[String, Int], mp.until(x))
29+
}
30+
}
31+
32+
@Test
33+
def mutableRangeOps(): Unit = {
34+
val st = m.SortedSet(-1, 2, 4, 3)
35+
for (x <- List(0, 1, 4, -1, -4)) {
36+
val s1 = st.rangeFrom(x)
37+
assertEquals(s1: m.SortedSet[Int], st.from(x))
38+
val s2 = st.rangeTo(x)
39+
assertEquals(s2: m.SortedSet[Int], st.to(x))
40+
val s3 = st.rangeUntil(x)
41+
assertEquals(s3: m.SortedSet[Int], st.until(x))
42+
}
43+
/* 2.11 doesn't have a mutable.SortedMap
44+
val mp = m.SortedMap("" -> 0, "ds" -> -3, "-??" -> 13, "Df" -> 33, "d!" -> -32)
45+
for (x <- List("", "-", "@", "aa", "D", "d")) {
46+
val m1 = mp.rangeFrom(x)
47+
assertEquals(m1: m.SortedMap[String, Int], mp.from(x))
48+
val m2 = mp.rangeTo(x)
49+
assertEquals(m2: m.SortedMap[String, Int], mp.to(x))
50+
val m3 = mp.rangeUntil(x)
51+
assertEquals(m3: m.SortedMap[String, Int], mp.until(x))
52+
}
53+
*/
54+
}
55+
}

0 commit comments

Comments
 (0)