From cdda313b4010409537c61f6d2a92cf6957e2dd2c Mon Sep 17 00:00:00 2001 From: David MacIver Date: Mon, 1 Jun 2009 11:05:39 +0000 Subject: Fix and test for bug 2029. --- src/library/scala/Ordering.scala | 6 +++ src/library/scala/PartialOrdering.scala | 7 +++ src/library/scala/collection/generic/Ranged.scala | 61 ---------------------- src/library/scala/collection/generic/Sorted.scala | 47 ++++++++++++++++- .../collection/generic/SortedMapTemplate.scala | 2 +- .../collection/generic/SortedSetTemplate.scala | 5 +- 6 files changed, 64 insertions(+), 64 deletions(-) delete mode 100644 src/library/scala/collection/generic/Ranged.scala (limited to 'src') 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 x comes before * y in the ordering, returns 0 iff x * is the same in the ordering as y, 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 true iff x comes before * y in the ordering. */ @@ -59,4 +60,10 @@ trait PartialOrdering[T] extends Equiv[T] { * y 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. - * None if there is no lower bound. - * @param until The upper-bound (exclusive) of the ranged projection. - * None 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. + * None if there is no lower bound. + * @param until The upper-bound (exclusive) of the ranged projection. + * None 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) } } -- cgit v1.2.3