From 4e787be632907667ab89f6ea4eea33901450e848 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 8 Mar 2011 16:32:16 +0000 Subject: Moved SeqDerived into an Ordering.Implicits obj... Moved SeqDerived into an Ordering.Implicits object. Closes #3152 (only mopping up), no review. --- src/library/scala/math/Ordering.scala | 42 ++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/library/scala/math/Ordering.scala b/src/library/scala/math/Ordering.scala index 25c146e183..b921183f52 100644 --- a/src/library/scala/math/Ordering.scala +++ b/src/library/scala/math/Ordering.scala @@ -107,6 +107,26 @@ trait Ordering[T] extends Comparator[T] with PartialOrdering[T] with Serializabl implicit def mkOrderingOps(lhs: T): Ops = new Ops(lhs) } +trait ExtraOrderingImplicits { + /** Not in the standard scope due to the potential for divergence: + * For instance implicitly[Ordering[Any]] diverges in its presence. + */ + implicit def SeqDerived[CC[X] <: collection.Seq[X], T](implicit ord: Ordering[T]): Ordering[CC[T]] = + new Ordering[CC[T]] { + def compare(x: CC[T], y: CC[T]): Int = { + val xe = x.iterator + val ye = y.iterator + + while (xe.hasNext && ye.hasNext) { + val res = ord.compare(xe.next, ye.next) + if (res != 0) return res + } + + Ordering.Boolean.compare(xe.hasNext, ye.hasNext) + } + } +} + trait LowPriorityOrderingImplicits { /** This would conflict with all the nice implicit Orderings * available, but thanks to the magic of prioritized implicits @@ -125,6 +145,13 @@ trait LowPriorityOrderingImplicits { object Ordering extends LowPriorityOrderingImplicits { def apply[T](implicit ord: Ordering[T]) = ord + /** An object for implicits which for one reason or another we + * aren't ready to put in the default scope. + */ + object Implicits extends ExtraOrderingImplicits { + + } + def fromLessThan[T](cmp: (T, T) => Boolean): Ordering[T] = new Ordering[T] { def compare(x: T, y: T) = if (cmp(x, y)) -1 else if (cmp(y, x)) 1 else 0 // overrides to avoid multiple comparisons @@ -234,21 +261,6 @@ object Ordering extends LowPriorityOrderingImplicits { } } - implicit def SeqDerived[CC[X] <: collection.Seq[X], T](implicit ord: Ordering[T]): Ordering[CC[T]] = - new Ordering[CC[T]] { - def compare(x: CC[T], y: CC[T]): Int = { - val xe = x.iterator - val ye = y.iterator - - while (xe.hasNext && ye.hasNext) { - val res = ord.compare(xe.next, ye.next) - if (res != 0) return res - } - - Boolean.compare(xe.hasNext, ye.hasNext) - } - } - implicit def Tuple2[T1, T2](implicit ord1: Ordering[T1], ord2: Ordering[T2]): Ordering[(T1, T2)] = new Ordering[(T1, T2)]{ def compare(x: (T1, T2), y: (T1, T2)): Int = { -- cgit v1.2.3