summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid MacIver <david.maciver@gmail.com>2009-06-01 11:05:39 +0000
committerDavid MacIver <david.maciver@gmail.com>2009-06-01 11:05:39 +0000
commitcdda313b4010409537c61f6d2a92cf6957e2dd2c (patch)
tree3661b9846310103becbc4b75e67bcfe3c76a8c76
parent3f8de98f0b0ec0e10864b37016c2318439ce7898 (diff)
downloadscala-cdda313b4010409537c61f6d2a92cf6957e2dd2c.tar.gz
scala-cdda313b4010409537c61f6d2a92cf6957e2dd2c.tar.bz2
scala-cdda313b4010409537c61f6d2a92cf6957e2dd2c.zip
Fix and test for bug 2029.
-rw-r--r--src/library/scala/Ordering.scala6
-rw-r--r--src/library/scala/PartialOrdering.scala7
-rw-r--r--src/library/scala/collection/generic/Ranged.scala61
-rw-r--r--src/library/scala/collection/generic/Sorted.scala47
-rw-r--r--src/library/scala/collection/generic/SortedMapTemplate.scala2
-rw-r--r--src/library/scala/collection/generic/SortedSetTemplate.scala5
-rw-r--r--test/files/run/bug2029.check3
-rw-r--r--test/files/run/bug2029.scala16
8 files changed, 83 insertions, 64 deletions
diff --git a/src/library/scala/Ordering.scala b/src/library/scala/Ordering.scala
index e9af2b8a94..c772e0d3fe 100644
--- a/src/library/scala/Ordering.scala
+++ b/src/library/scala/Ordering.scala
@@ -39,6 +39,7 @@ package scala
*/
trait Ordering[T] extends PartialOrdering[T] {
+ outer =>
/** Returns a negative integer iff <code>x</code> comes before
* <code>y</code> in the ordering, returns 0 iff <code>x</code>
* is the same in the ordering as <code>y</code>, and returns a
@@ -78,6 +79,11 @@ trait Ordering[T] extends PartialOrdering[T] {
/** Returns the argument which comes earlier in the ordering. */
def min(x: T, y: T): T = if (lteq(x, y)) x else y
+ override def reverse : Ordering[T] = new Ordering[T]{
+ override def reverse = outer;
+ def compare(x : T, y : T) = outer.compare(y, x);
+ }
+
class Ops(lhs: T) {
def <(rhs: T) = lt(lhs, rhs)
def <=(rhs: T) = lteq(lhs, rhs)
diff --git a/src/library/scala/PartialOrdering.scala b/src/library/scala/PartialOrdering.scala
index e337722a49..d28f03d39e 100644
--- a/src/library/scala/PartialOrdering.scala
+++ b/src/library/scala/PartialOrdering.scala
@@ -35,6 +35,7 @@ package scala
*/
trait PartialOrdering[T] extends Equiv[T] {
+ outer =>
/** Returns <code>true</code> iff <code>x</code> comes before
* <code>y</code> in the ordering.
*/
@@ -59,4 +60,10 @@ trait PartialOrdering[T] extends Equiv[T] {
* <code>y</code> in the ordering.
*/
def equiv(x: T, y: T): Boolean = lteq(x,y) && lteq(y,x)
+
+
+ def reverse : PartialOrdering[T] = new PartialOrdering[T]{
+ override def reverse = outer;
+ def lteq(x : T, y : T) = outer.lteq(y, x);
+ }
}
diff --git a/src/library/scala/collection/generic/Ranged.scala b/src/library/scala/collection/generic/Ranged.scala
deleted file mode 100644
index c0610ad893..0000000000
--- a/src/library/scala/collection/generic/Ranged.scala
+++ /dev/null
@@ -1,61 +0,0 @@
-/* __ *\
-** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2006-2009, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
-** /____/\___/_/ |_/____/_/ | | **
-** |/ **
-\* */
-
-// $Id: Ranged.scala 17537 2009-04-20 18:37:37Z odersky $
-
-package scala.collection.generic
-
-/** Any collection (including maps) whose keys (or elements) are ordered.
- *
- * @author martin Odersky
- * @version 2.8
- */
-trait Ranged[K, +This <: Ranged[K, This]] {
-
- /** Returns the first key of the collection. */
- def firstKey: K
-
- /** Returns the last key of the collection. */
- def lastKey: K
-
- /** Comparison function that orders keys. */
- def compare(k0: K, k1: K): Int
-
- /** Creates a ranged projection of this collection. Any mutations in the
- * ranged projection will update this collection and vice versa. Note: keys
- * are not garuanteed to be consistent between this collection and the projection.
- * This is the case for buffers where indexing is relative to the projection.
- *
- * @param from The lower-bound (inclusive) of the ranged projection.
- * <code>None</code> if there is no lower bound.
- * @param until The upper-bound (exclusive) of the ranged projection.
- * <code>None</code> if there is no upper bound.
- */
- def rangeImpl(from: Option[K], until: Option[K]): This
-
- /** Creates a ranged projection of this collection with no upper-bound.
- *
- * @param from The lower-bound (inclusive) of the ranged projection.
- */
- def from(from: K): This = rangeImpl(Some(from), None)
-
- /** Creates a ranged projection of this collection with no lower-bound.
- *
- * @param until The upper-bound (exclusive) of the ranged projection.
- */
- def until(until: K): This = rangeImpl(None, Some(until))
-
- /** Creates a ranged projection of this collection with both a lower-bound
- * and an upper-bound.
- *
- * @param from The upper-bound (exclusive) of the ranged projection.
- * @param until ...
- * @return ...
- */
- def range(from: K, until: K): This = rangeImpl(Some(from), Some(until))
-}
diff --git a/src/library/scala/collection/generic/Sorted.scala b/src/library/scala/collection/generic/Sorted.scala
index a8e45cc3a4..5e9c8b070d 100644
--- a/src/library/scala/collection/generic/Sorted.scala
+++ b/src/library/scala/collection/generic/Sorted.scala
@@ -14,7 +14,8 @@ package scala.collection.generic
*
* @author Sean McDirmid
*/
-trait Sorted[K, +This <: Sorted[K, This]] extends Ranged[K, This] {
+trait Sorted[K, +This <: Sorted[K, This]]{
+ def ordering : Ordering[K];
/** The current collection */
protected def thisCollection: This
@@ -22,6 +23,50 @@ trait Sorted[K, +This <: Sorted[K, This]] extends Ranged[K, This] {
/** return as a projection the set of keys in this collection */
def keySet: SortedSet[K]
+
+ /** Returns the first key of the collection. */
+ def firstKey: K
+
+ /** Returns the last key of the collection. */
+ def lastKey: K
+
+ /** Comparison function that orders keys. */
+ def compare(k0: K, k1: K): Int = ordering.compare(k0, k1);
+
+ /** Creates a ranged projection of this collection. Any mutations in the
+ * ranged projection will update this collection and vice versa. Note: keys
+ * are not garuanteed to be consistent between this collection and the projection.
+ * This is the case for buffers where indexing is relative to the projection.
+ *
+ * @param from The lower-bound (inclusive) of the ranged projection.
+ * <code>None</code> if there is no lower bound.
+ * @param until The upper-bound (exclusive) of the ranged projection.
+ * <code>None</code> if there is no upper bound.
+ */
+ def rangeImpl(from: Option[K], until: Option[K]): This
+
+ /** Creates a ranged projection of this collection with no upper-bound.
+ *
+ * @param from The lower-bound (inclusive) of the ranged projection.
+ */
+ def from(from: K): This = rangeImpl(Some(from), None)
+
+ /** Creates a ranged projection of this collection with no lower-bound.
+ *
+ * @param until The upper-bound (exclusive) of the ranged projection.
+ */
+ def until(until: K): This = rangeImpl(None, Some(until))
+
+ /** Creates a ranged projection of this collection with both a lower-bound
+ * and an upper-bound.
+ *
+ * @param from The upper-bound (exclusive) of the ranged projection.
+ * @param until ...
+ * @return ...
+ */
+ def range(from: K, until: K): This = rangeImpl(Some(from), Some(until))
+
+
/** Create a range projection of this collection with no lower-bound.
* @param to The upper-bound (inclusive) of the ranged projection.
*/
diff --git a/src/library/scala/collection/generic/SortedMapTemplate.scala b/src/library/scala/collection/generic/SortedMapTemplate.scala
index 90c81e8484..23b1f13dc4 100644
--- a/src/library/scala/collection/generic/SortedMapTemplate.scala
+++ b/src/library/scala/collection/generic/SortedMapTemplate.scala
@@ -29,7 +29,7 @@ self =>
def rangeImpl(from : Option[A], until : Option[A]) : This
protected class DefaultKeySet extends super.DefaultKeySet with SortedSet[A] {
- def compare(k0: A, k1: A) = self.thisCollection.compare(k0, k1)
+ def ordering = self.ordering;
/** We can't give an implementation of +/- here because we do not have a generic sorted set implementation
*/
override def + (elem: A): SortedSet[A] = throw new UnsupportedOperationException("keySet.+")
diff --git a/src/library/scala/collection/generic/SortedSetTemplate.scala b/src/library/scala/collection/generic/SortedSetTemplate.scala
index 189131173b..4c1b5a9721 100644
--- a/src/library/scala/collection/generic/SortedSetTemplate.scala
+++ b/src/library/scala/collection/generic/SortedSetTemplate.scala
@@ -32,7 +32,10 @@ self =>
override def range(from: A, until: A): This = rangeImpl(Some(from), Some(until))
override def subsetOf(that: Set[A]): Boolean = that match {
- case that: SortedSet[_] => that.hasAll(this.iterator)
+ // TODO: It may actually be pretty rare that the guard here ever
+ // passes. Is this really worth keeping? If it is, we should add
+ // more sensible implementations of == to Ordering.
+ case that: SortedSet[_] if that.ordering == ordering => that.hasAll(this.iterator)
case that => super.subsetOf(that)
}
}
diff --git a/test/files/run/bug2029.check b/test/files/run/bug2029.check
new file mode 100644
index 0000000000..57b610ccc5
--- /dev/null
+++ b/test/files/run/bug2029.check
@@ -0,0 +1,3 @@
+1,2,3,4,5
+4,3,2
+true
diff --git a/test/files/run/bug2029.scala b/test/files/run/bug2029.scala
new file mode 100644
index 0000000000..32b04f0b47
--- /dev/null
+++ b/test/files/run/bug2029.scala
@@ -0,0 +1,16 @@
+object Test{
+ def main(args : Array[String]){
+ import scala.collection.immutable.TreeSet;
+
+ val mainSet = TreeSet(1 to 5 :_*)
+
+ var compareCalled = false;
+ val smallerSet = TreeSet(2 to 4 :_*)(Ordering[Int].reverse)
+
+ println(mainSet.mkString(","))
+ println(smallerSet.mkString(","))
+ println(smallerSet.subsetOf(mainSet));
+ }
+
+
+}