diff options
author | Paul Phillips <paulp@improving.org> | 2009-08-28 12:39:50 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-08-28 12:39:50 +0000 |
commit | d582588b6d6837099d0b391ff5fc70700dc05dce (patch) | |
tree | 918258703460f2f90cfe37c3b2abedf445810d4a | |
parent | ce362ef76f84ff1198e0e99d08f82b788d24ddf1 (diff) | |
download | scala-d582588b6d6837099d0b391ff5fc70700dc05dce.tar.gz scala-d582588b6d6837099d0b391ff5fc70700dc05dce.tar.bz2 scala-d582588b6d6837099d0b391ff5fc70700dc05dce.zip |
A bunch of cleanups around Ordered & Ordering, ...
A bunch of cleanups around Ordered & Ordering, and provided
PartialOrdering with the "partial" part it had never been given.
-rw-r--r-- | src/compiler/scala/tools/nsc/doc/DocUtil.scala | 55 | ||||
-rw-r--r-- | src/library/scala/Ordering.scala | 4 | ||||
-rw-r--r-- | src/library/scala/PartialOrdering.scala | 16 | ||||
-rw-r--r-- | src/library/scala/collection/Iterable.scala | 22 |
4 files changed, 36 insertions, 61 deletions
diff --git a/src/compiler/scala/tools/nsc/doc/DocUtil.scala b/src/compiler/scala/tools/nsc/doc/DocUtil.scala index b3b8558865..dbb6f94386 100644 --- a/src/compiler/scala/tools/nsc/doc/DocUtil.scala +++ b/src/compiler/scala/tools/nsc/doc/DocUtil.scala @@ -13,11 +13,8 @@ import org.xml.sax.InputSource import scala.collection.immutable.{ListMap, TreeSet} import scala.xml._ -object DocUtil { - - //def dquote(str: String): NodeSeq = - // DQUOTE :: Text(str) :: DQUOTE :: Nil - +object DocUtil +{ def load(str: String): NodeSeq = if ((str == null) || (str.length == 0)) NodeSeq.Empty @@ -28,13 +25,6 @@ object DocUtil { XML.load(new StringReader(xmlSrc)) } - //object DQUOTE extends SpecialNode { - // def toString(sb: StringBuffer) = { - // sb.append("\""); sb - // } - // def label = "#PCDATA" - //} - def br(nodes: NodeSeq): NodeSeq = nodes ++ (<br/>) def hr(nodes: NodeSeq): NodeSeq = nodes ++ (<hr/>) @@ -83,40 +73,29 @@ object DocUtil { def div0(title: String): NodeSeq = (<div class="doctitle-larger">{Text(title)}</div>); - def merge[T](ts0: TreeSet[T], ts1: TreeSet[T]): TreeSet[T] = { - var ts = ts0 - for (t <- ts1.toList) ts += t - ts - } + def merge[T](ts0: TreeSet[T], ts1: TreeSet[T]): TreeSet[T] = ts0 ++ ts1 - def merge[T,S <: Ordered[S]](ts0: ListMap[T,TreeSet[S]], - ts1: ListMap[T,TreeSet[S]]): ListMap[T,TreeSet[S]] = { - var ts = ts0 - for (t <- ts1.iterator) { - if (!ts.contains(t._1)) - ts = ts.updated(t._1, TreeSet.empty[S](Ordering.ordered[S])); - ts = ts.updated(t._1, merge(ts(t._1), t._2)) + def merge[T,S](ts0: ListMap[T,TreeSet[S]], ts1: ListMap[T,TreeSet[S]]): ListMap[T,TreeSet[S]] = { + (ts1 foldLeft ts0) { case (xs, (k, v)) => + if (xs contains k) xs.updated(k, xs(k) ++ v) + else xs.updated(k, v) } - ts } + implicit def coerceIterable[T](list : Iterable[T]) = NodeWrapper(list.iterator) implicit def coerceIterator[T](list : Iterator[T]) = NodeWrapper(list) - case class NodeWrapper[T](list : Iterator[T]) { + case class NodeWrapper[T](list: Iterator[T]) { + def interleave(xs: Seq[NodeSeq], sep: NodeSeq): NodeSeq = + if (xs.isEmpty) NodeSeq.Empty + else if (xs.size == 1) xs.head + else xs.head ++ sep ++ interleave(xs.tail, sep) - def mkXML(begin: NodeSeq, separator: NodeSeq, end: NodeSeq)(f: T => NodeSeq): NodeSeq = { - var seq: NodeSeq = begin - val i = list - while (i.hasNext) { - seq = seq ++ f(i.next); - if (i.hasNext) seq = seq ++ separator; - } - seq ++ end - } + def mkXML(begin: NodeSeq, separator: NodeSeq, end: NodeSeq)(f: T => NodeSeq): NodeSeq = + begin ++ interleave(list.toSequence map f, separator) ++ end - def mkXML(begin: String, separator: String, end: String)(f: T => NodeSeq): NodeSeq = { - this.mkXML(Text(begin),Text(separator),Text(end))(f); - } + def mkXML(begin: String, separator: String, end: String)(f: T => NodeSeq): NodeSeq = + this.mkXML(Text(begin), Text(separator), Text(end))(f) def surround(open: String, close: String)(f: T => NodeSeq) = if (list.hasNext) mkXML(open, ", ", close)(f) diff --git a/src/library/scala/Ordering.scala b/src/library/scala/Ordering.scala index 2ec1b96ecb..d744dc9257 100644 --- a/src/library/scala/Ordering.scala +++ b/src/library/scala/Ordering.scala @@ -43,6 +43,10 @@ import java.util.Comparator trait Ordering[T] extends Comparator[T] with PartialOrdering[T] { outer => + + /** An Ordering is defined at all x and y. */ + def tryCompare(x: T, y: T) = Some(compare(x, y)) + /** 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 diff --git a/src/library/scala/PartialOrdering.scala b/src/library/scala/PartialOrdering.scala index 7803a58d31..75bc88b001 100644 --- a/src/library/scala/PartialOrdering.scala +++ b/src/library/scala/PartialOrdering.scala @@ -37,6 +37,16 @@ package scala trait PartialOrdering[T] extends Equiv[T] { outer => + + /** Result of comparing <code>x</code> with operand <code>y</code>. + * Returns <code>None</code> if operands are not comparable. + * If operands are comparable, returns <code>Some(r)</code> where + * <code>r < 0</code> iff <code>x < y</code> + * <code>r == 0</code> iff <code>x == y</code> + * <code>r > 0</code> iff <code>x > y</code> + */ + def tryCompare(x: T, y: T): Option[Int] + /** Returns <code>true</code> iff <code>x</code> comes before * <code>y</code> in the ordering. */ @@ -62,9 +72,9 @@ trait PartialOrdering[T] extends Equiv[T] { */ def equiv(x: T, y: T): Boolean = lteq(x,y) && lteq(y,x) - - def reverse : PartialOrdering[T] = new PartialOrdering[T]{ + def reverse : PartialOrdering[T] = new PartialOrdering[T] { override def reverse = outer; - def lteq(x : T, y : T) = outer.lteq(y, x); + def lteq(x: T, y: T) = outer.lteq(y, x) + def tryCompare(x: T, y: T) = outer.tryCompare(y, x) } } diff --git a/src/library/scala/collection/Iterable.scala b/src/library/scala/collection/Iterable.scala index 551106d385..218dcc65e2 100644 --- a/src/library/scala/collection/Iterable.scala +++ b/src/library/scala/collection/Iterable.scala @@ -65,29 +65,11 @@ object Iterable extends TraversableFactory[Iterable] { /** The minimum element of a non-empty sequence of ordered elements */ @deprecated("use seq.min instead") - def min[A <% Ordered[A]](seq: Iterable[A]): A = { - val xs = seq.iterator - if (!xs.hasNext) throw new IllegalArgumentException("min(<empty>)") - var min = xs.next - while (xs.hasNext) { - val x = xs.next - if (x < min) min = x - } - min - } + def min[A](seq: Iterable[A])(implicit ord: Ordering[A]): A = seq.min /** The maximum element of a non-empty sequence of ordered elements */ @deprecated("use seq.max instead") - def max[A <% Ordered[A]](seq: Iterable[A]): A = { - val xs = seq.iterator - if (!xs.hasNext) throw new IllegalArgumentException("max(<empty>)") - var max = xs.next - while (xs.hasNext) { - val x = xs.next - if (max < x) max = x - } - max - } + def max[A](seq: Iterable[A])(implicit ord: Ordering[A]): A = seq.max @deprecated("use View instead") type Projection[A] = IterableView[A, Coll] } |