diff options
author | Martin Odersky <odersky@gmail.com> | 2011-04-24 20:13:40 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2011-04-24 20:13:40 +0000 |
commit | 290f3711d51da0e013e37ef2703cf976a26747cf (patch) | |
tree | 8097db9aef65c240f618051fef56926928de060a | |
parent | 60463a8721728545d7d626d96f77e8688084c71f (diff) | |
download | scala-290f3711d51da0e013e37ef2703cf976a26747cf.tar.gz scala-290f3711d51da0e013e37ef2703cf976a26747cf.tar.bz2 scala-290f3711d51da0e013e37ef2703cf976a26747cf.zip |
Added a bunch of bridges to make ameliorate bin...
Added a bunch of bridges to make ameliorate binary compatibility of new
collections. Review by prokopec. Review by extempore.
31 files changed, 604 insertions, 285 deletions
diff --git a/src/library/scala/collection/GenIterable.scala b/src/library/scala/collection/GenIterable.scala index 8d735dd86d..9d25f21c38 100644 --- a/src/library/scala/collection/GenIterable.scala +++ b/src/library/scala/collection/GenIterable.scala @@ -29,7 +29,7 @@ extends GenIterableLike[A, GenIterable[A]] } -object GenIterable extends TraversableFactory[GenIterable] { +object GenIterable extends GenTraversableFactory[GenIterable] { implicit def canBuildFrom[A] = new GenericCanBuildFrom[A] def newBuilder[A] = Iterable.newBuilder } diff --git a/src/library/scala/collection/GenMap.scala b/src/library/scala/collection/GenMap.scala index 7e7b6c7fb0..0556069371 100644 --- a/src/library/scala/collection/GenMap.scala +++ b/src/library/scala/collection/GenMap.scala @@ -26,7 +26,7 @@ extends GenMapLike[A, B, GenMap[A, B]] } -object GenMap extends MapFactory[GenMap] { +object GenMap extends GenMapFactory[GenMap] { def empty[A, B]: immutable.Map[A, B] = immutable.Map.empty /** $mapCanBuildFromInfo */ diff --git a/src/library/scala/collection/GenSeq.scala b/src/library/scala/collection/GenSeq.scala index ac5f953e88..9a7ee9890e 100644 --- a/src/library/scala/collection/GenSeq.scala +++ b/src/library/scala/collection/GenSeq.scala @@ -30,7 +30,7 @@ extends GenSeqLike[A, GenSeq[A]] } -object GenSeq extends TraversableFactory[GenSeq] { +object GenSeq extends GenTraversableFactory[GenSeq] { implicit def canBuildFrom[A] = new GenericCanBuildFrom[A] def newBuilder[A] = Seq.newBuilder } diff --git a/src/library/scala/collection/GenSeqLike.scala b/src/library/scala/collection/GenSeqLike.scala index 1502853d8e..f5e8113d28 100644 --- a/src/library/scala/collection/GenSeqLike.scala +++ b/src/library/scala/collection/GenSeqLike.scala @@ -9,6 +9,7 @@ package scala.collection import generic._ +import annotation.bridge /** A template trait for all sequences which may be traversed * in parallel. @@ -209,6 +210,9 @@ private[collection] trait GenSeqLike[+A, +Repr] extends GenIterableLike[A, Repr] */ def startsWith[B](that: GenSeq[B]): Boolean = startsWith(that, 0) + @bridge + def startsWith[B](that: Seq[B]): Boolean = startsWith(that: GenSeq[B]) + /** Tests whether this $coll contains the given sequence at a given index. * * If the both the receiver object, <code>this</code> and @@ -338,6 +342,10 @@ private[collection] trait GenSeqLike[+A, +Repr] extends GenIterableLike[A, Repr] */ def union[B >: A, That](that: GenSeq[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = this ++ that + @bridge + def union[B >: A, That](that: Seq[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = + union(that: GenSeq[B])(bf) + /** Computes the multiset difference between this $coll and another sequence. * $willNotTerminateInf * diff --git a/src/library/scala/collection/GenSet.scala b/src/library/scala/collection/GenSet.scala index ec066844b4..fdf6d1edb2 100644 --- a/src/library/scala/collection/GenSet.scala +++ b/src/library/scala/collection/GenSet.scala @@ -30,7 +30,7 @@ extends GenSetLike[A, GenSet[A]] } -object GenSet extends TraversableFactory[GenSet] { +object GenSet extends GenTraversableFactory[GenSet] { implicit def canBuildFrom[A] = new GenericCanBuildFrom[A] def newBuilder[A] = Set.newBuilder } diff --git a/src/library/scala/collection/GenSetLike.scala b/src/library/scala/collection/GenSetLike.scala index d45810d2e7..1c998351b4 100644 --- a/src/library/scala/collection/GenSetLike.scala +++ b/src/library/scala/collection/GenSetLike.scala @@ -8,6 +8,8 @@ package scala.collection +import annotation.bridge + /** A template trait for sets which may possibly * have their operations implemented in parallel. * @@ -49,6 +51,9 @@ private[collection] trait GenSetLike[A, +Repr] */ def intersect(that: GenSet[A]): Repr = this filter that + @bridge + def intersect(that: Set[A]): Repr = intersect(that: GenSet[A]) + /** Computes the intersection between this set and another set. * * '''Note:''' Same as `intersect`. @@ -58,6 +63,9 @@ private[collection] trait GenSetLike[A, +Repr] */ def &(that: GenSet[A]): Repr = this intersect that + @bridge + def &(that: Set[A]): Repr = &(that: GenSet[A]) + /** Computes the union between of set and another set. * * @param that the set to form the union with. @@ -75,6 +83,9 @@ private[collection] trait GenSetLike[A, +Repr] */ def | (that: GenSet[A]): Repr = this union that + @bridge + def | (that: Set[A]): Repr = | (that: GenSet[A]) + /** Computes the difference of this set and another set. * * @param that the set of elements to exclude. @@ -92,6 +103,9 @@ private[collection] trait GenSetLike[A, +Repr] */ def &~(that: GenSet[A]): Repr = this diff that + @bridge + def &~(that: Set[A]): Repr = &~(that: GenSet[A]) + /** Tests whether this set is a subset of another set. * * @param that the set to test. @@ -100,6 +114,9 @@ private[collection] trait GenSetLike[A, +Repr] */ def subsetOf(that: GenSet[A]): Boolean = this forall that + @bridge + def subsetOf(that: Set[A]): Boolean = subsetOf(that: GenSet[A]) + /** Compares this set with another object for equality. * * '''Note:''' This operation contains an unchecked cast: if `that` diff --git a/src/library/scala/collection/GenTraversable.scala b/src/library/scala/collection/GenTraversable.scala index fc1f84d2e5..8e6ab50f1a 100644 --- a/src/library/scala/collection/GenTraversable.scala +++ b/src/library/scala/collection/GenTraversable.scala @@ -31,7 +31,7 @@ extends GenTraversableLike[A, GenTraversable[A]] } -object GenTraversable extends TraversableFactory[GenTraversable] { +object GenTraversable extends GenTraversableFactory[GenTraversable] { implicit def canBuildFrom[A] = new GenericCanBuildFrom[A] def newBuilder[A] = Traversable.newBuilder } diff --git a/src/library/scala/collection/IterableLike.scala b/src/library/scala/collection/IterableLike.scala index b6713b65e4..55c12b8e8c 100644 --- a/src/library/scala/collection/IterableLike.scala +++ b/src/library/scala/collection/IterableLike.scala @@ -12,7 +12,7 @@ package scala.collection import generic._ import immutable.{ List, Stream } import annotation.unchecked.uncheckedVariance - +import annotation.bridge /** A template trait for iterable collections of type `Iterable[A]`. * $iterableInfo @@ -237,6 +237,10 @@ self => b.result } + @bridge + def zip[A1 >: A, B, That](that: Iterable[B])(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = + zip(that: GenIterable[B])(bf) + def zipAll[B, A1 >: A, That](that: GenIterable[B], thisElem: A1, thatElem: B)(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = { val b = bf(repr) val these = this.iterator @@ -250,6 +254,10 @@ self => b.result } + @bridge + def zipAll[B, A1 >: A, That](that: Iterable[B], thisElem: A1, thatElem: B)(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = + zipAll(that: GenIterable[B], thisElem, thatElem)(bf) + def zipWithIndex[A1 >: A, That](implicit bf: CanBuildFrom[Repr, (A1, Int), That]): That = { val b = bf(repr) var i = 0 @@ -270,6 +278,9 @@ self => !these.hasNext && !those.hasNext } + @bridge + def sameElements[B >: A](that: Iterable[B]): Boolean = sameElements(that: GenIterable[B]) + override /*TraversableLike*/ def toStream: Stream[A] = iterator.toStream /** Method called from equality methods, so that user-defined subclasses can diff --git a/src/library/scala/collection/MapLike.scala b/src/library/scala/collection/MapLike.scala index a832f9089d..92be57aa89 100644 --- a/src/library/scala/collection/MapLike.scala +++ b/src/library/scala/collection/MapLike.scala @@ -11,7 +11,7 @@ package scala.collection import generic._ import mutable.{ Builder, MapBuilder } -import annotation.migration +import annotation.{migration, bridge} import parallel.ParMap /** A template trait for maps, which associate keys with values. @@ -290,6 +290,9 @@ self => def ++[B1 >: B](xs: GenTraversableOnce[(A, B1)]): Map[A, B1] = ((repr: Map[A, B1]) /: xs.seq) (_ + _) + @bridge + def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): Map[A, B1] = ++(xs: GenTraversableOnce[(A, B1)]) + /** Returns a new map with all key/value pairs for which the predicate * `p` returns `true`. * diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala index 87649e8b03..0411a2987e 100644 --- a/src/library/scala/collection/SeqLike.scala +++ b/src/library/scala/collection/SeqLike.scala @@ -14,6 +14,7 @@ import mutable.{ListBuffer, HashMap, ArraySeq} import immutable.{List, Range} import generic._ import parallel.ParSeq +import annotation.bridge /** A template trait for sequences of type `Seq[A]` * $seqInfo @@ -303,6 +304,9 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] !j.hasNext } + @bridge + def startsWith[B](that: Seq[B], offset: Int): Boolean = startsWith(that: GenSeq[B], offset) + def endsWith[B](that: GenSeq[B]): Boolean = { val i = this.iterator.drop(length - that.length) val j = that.iterator @@ -313,6 +317,10 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] !j.hasNext } + @bridge + def endsWith[B](that: Seq[B]): Boolean = endsWith(that: GenSeq[B]) + + /** Finds first index where this $coll contains a given sequence as a slice. * $mayNotTerminateInf * @param that the sequence to test @@ -321,6 +329,9 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] */ def indexOfSlice[B >: A](that: GenSeq[B]): Int = indexOfSlice(that, 0) + @bridge + def indexOfSlice[B >: A](that: Seq[B]): Int = indexOfSlice(that: GenSeq[B]) + /** Finds first index after or at a start index where this $coll contains a given sequence as a slice. * $mayNotTerminateInf * @param that the sequence to test @@ -344,6 +355,9 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] -1 } + @bridge + def indexOfSlice[B >: A](that: Seq[B], from: Int): Int = indexOfSlice(that: GenSeq[B], from) + /** Finds last index where this $coll contains a given sequence as a slice. * $willNotTerminateInf * @param that the sequence to test @@ -352,6 +366,9 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] */ def lastIndexOfSlice[B >: A](that: GenSeq[B]): Int = lastIndexOfSlice(that, length) + @bridge + def lastIndexOfSlice[B >: A](that: Seq[B]): Int = lastIndexOfSlice(that: GenSeq[B]) + /** Finds last index before or at a given end index where this $coll contains a given sequence as a slice. * @param that the sequence to test * @param end the end index @@ -361,6 +378,9 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] def lastIndexOfSlice[B >: A](that: GenSeq[B], end: Int): Int = SeqLike.lastIndexOf(thisCollection, 0, length, that.seq, 0, that.length, end) + @bridge + def lastIndexOfSlice[B >: A](that: Seq[B], end: Int): Int = lastIndexOfSlice(that: GenSeq[B], end) + /** Tests whether this $coll contains a given sequence as a slice. * $mayNotTerminateInf * @param that the sequence to test @@ -369,6 +389,9 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] */ def containsSlice[B](that: GenSeq[B]): Boolean = indexOfSlice(that) != -1 + @bridge + def containsSlice[B](that: Seq[B]): Boolean = containsSlice(that: GenSeq[B]) + /** Tests whether this $coll contains a given value as an element. * $mayNotTerminateInf * @@ -429,6 +452,9 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] b.result } + @bridge + def diff[B >: A](that: Seq[B]): Repr = diff(that: GenSeq[B]) + /** Computes the multiset intersection between this $coll and another sequence. * $mayNotTerminateInf * @@ -459,6 +485,9 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] b.result } + @bridge + def intersect[B >: A](that: Seq[B]): Repr = intersect(that: GenSeq[B]) + private def occCounts[B](sq: Seq[B]): mutable.Map[B, Int] = { val occ = new mutable.HashMap[B, Int] { override def default(k: B) = 0 } for (y <- sq.seq) occ(y) += 1 @@ -491,6 +520,10 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] b.result } + @bridge + def patch[B >: A, That](from: Int, patch: Seq[B], replaced: Int)(implicit bf: CanBuildFrom[Repr, B, That]): That = + this.patch(from, patch: GenSeq[B], replaced)(bf) + def updated[B >: A, That](index: Int, elem: B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) val (prefix, rest) = this.splitAt(index) @@ -536,6 +569,10 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] with GenSeqLike[A, Repr] !i.hasNext && !j.hasNext } + @bridge + def corresponds[B](that: Seq[B])(p: (A,B) => Boolean): Boolean = + corresponds(that: GenSeq[B])(p) + /** Sorts this $coll according to a comparison function. * $willNotTerminateInf * diff --git a/src/library/scala/collection/SetLike.scala b/src/library/scala/collection/SetLike.scala index c5b97c135d..2a208e0e3f 100644 --- a/src/library/scala/collection/SetLike.scala +++ b/src/library/scala/collection/SetLike.scala @@ -11,7 +11,7 @@ package scala.collection import generic._ import mutable.{ Builder, SetBuilder } -import scala.annotation.migration +import annotation.{migration, bridge} import parallel.ParSet /** A template trait for sets. @@ -127,6 +127,9 @@ self => */ def ++ (elems: GenTraversableOnce[A]): This = newBuilder ++= seq ++= elems.seq result + @bridge + def ++ (elems: TraversableOnce[A]): This = ++ (elems: GenTraversableOnce[A]) + /** Creates a new set with a given element removed from this set. * * @param elem the element to be removed @@ -148,7 +151,7 @@ self => * @param that the set to intersect with */ @deprecated("use & instead", "2.8.0") - def ** (that: GenSet[A]): This = &(that) + def ** (that: Set[A]): This = &(that) /** Computes the union between of set and another set. * @@ -158,6 +161,9 @@ self => */ def union(that: GenSet[A]): This = this ++ that + @bridge + def union(that: Set[A]): This = union(that: GenSet[A]) + /** Computes the difference of this set and another set. * * @param that the set of elements to exclude. @@ -166,6 +172,9 @@ self => */ def diff(that: GenSet[A]): This = this -- that + @bridge + def diff(that: Set[A]): This = diff(that: GenSet[A]) + /** An iterator over all subsets of this set of the given size. * If the requested size is impossible, an empty iterator is returned. * diff --git a/src/library/scala/collection/Traversable.scala b/src/library/scala/collection/Traversable.scala index 500c340368..71c5839d43 100644 --- a/src/library/scala/collection/Traversable.scala +++ b/src/library/scala/collection/Traversable.scala @@ -13,6 +13,7 @@ package scala.collection import generic._ import mutable.{Builder, Buffer, ArrayBuffer, ListBuffer} import scala.util.control.Breaks +import annotation.bridge /** A trait for traversable collections. * All operations are guaranteed to be performed in a single-threaded manner. @@ -27,6 +28,12 @@ trait Traversable[+A] extends TraversableLike[A, Traversable[A]] override def seq: Traversable[A] = this + @bridge + def flatten[B](implicit asTraversable: A => /*<:<!!!*/ TraversableOnce[B]): Traversable[B] = super.flatten(asTraversable) + + @bridge + def transpose[B](implicit asTraversable: A => /*<:<!!!*/ TraversableOnce[B]): Traversable[Traversable[B]] = super.transpose(asTraversable) + /* The following methods are inherited from TraversableLike * override def isEmpty: Boolean diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala index fe1d5d7d77..c8b8dfda5f 100644 --- a/src/library/scala/collection/TraversableLike.scala +++ b/src/library/scala/collection/TraversableLike.scala @@ -10,8 +10,7 @@ package scala.collection import generic._ import mutable.{ Builder, ListBuffer } -import annotation.tailrec -import annotation.migration +import annotation.{tailrec, migration, bridge} import annotation.unchecked.{ uncheckedVariance => uV } import parallel.ParIterable @@ -153,6 +152,10 @@ trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] b.result } + @bridge + def ++[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = + ++(that: GenTraversableOnce[B])(bf) + /** Concatenates this $coll with the elements of a traversable collection. * It differs from ++ in that the right operand determines the type of the * resulting collection rather than the left one. diff --git a/src/library/scala/collection/generic/Addable.scala b/src/library/scala/collection/generic/Addable.scala index 8f33a62f10..e2a34c1ed2 100644 --- a/src/library/scala/collection/generic/Addable.scala +++ b/src/library/scala/collection/generic/Addable.scala @@ -10,6 +10,8 @@ package scala.collection package generic +import annotation.bridge + /** This trait represents collection-like objects that can be added to * using a '+' operator. It defines variants of `+` and `++` * as convenience methods in terms of single-element addition `+`. @@ -53,4 +55,7 @@ trait Addable[A, +Repr <: Addable[A, Repr]] { self => * @return a new $coll with the given elements added. */ def ++ (xs: GenTraversableOnce[A]): Repr = (repr /: xs.seq) (_ + _) + + @bridge + def ++ (xs: TraversableOnce[A]): Repr = ++ (xs: GenTraversableOnce[A]) } diff --git a/src/library/scala/collection/generic/GenMapFactory.scala b/src/library/scala/collection/generic/GenMapFactory.scala new file mode 100644 index 0000000000..f3537e8c6b --- /dev/null +++ b/src/library/scala/collection/generic/GenMapFactory.scala @@ -0,0 +1,62 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection +package generic + + +import mutable.{Builder, MapBuilder} + + +/** A template for companion objects of `Map` and subclasses thereof. + * + * @define coll map + * @define Coll Map + * @define factoryInfo + * This object provides a set of operations needed to create `$Coll` values. + * @author Martin Odersky + * @version 2.8 + * @since 2.8 + * @define canBuildFromInfo + * The standard `CanBuildFrom` instance for `$Coll` objects. + * @see CanBuildFrom + * @define mapCanBuildFromInfo + * The standard `CanBuildFrom` instance for `$Coll` objects. + * The created value is an instance of class `MapCanBuildFrom`. + * @see CanBuildFrom + * @see GenericCanBuildFrom + */ +abstract class GenMapFactory[CC[A, B] <: GenMap[A, B] with GenMapLike[A, B, CC[A, B]]] { + + /** The type constructor of the collection that can be built by this factory */ + type Coll = CC[_, _] + + /** An empty $Coll */ + def empty[A, B]: CC[A, B] + + /** A collection of type $Coll that contains given key/value bindings. + * @param elems the key/value pairs that make up the $coll + * @tparam A the type of the keys + * @tparam B the type of the associated values + * @return a new $coll consisting key/value pairs given by `elems`. + */ + def apply[A, B](elems: (A, B)*): CC[A, B] = (newBuilder[A, B] ++= elems).result + + /** The default builder for $Coll objects. + * @tparam A the type of the keys + * @tparam B the type of the associated values + */ + def newBuilder[A, B]: Builder[(A, B), CC[A, B]] = new MapBuilder[A, B, CC[A, B]](empty[A, B]) + + /** The standard `CanBuildFrom` class for maps. + */ + class MapCanBuildFrom[A, B] extends CanBuildFrom[Coll, (A, B), CC[A, B]] { + def apply(from: Coll) = newBuilder[A, B] + def apply() = newBuilder + } +} diff --git a/src/library/scala/collection/generic/GenSeqFactory.scala b/src/library/scala/collection/generic/GenSeqFactory.scala new file mode 100644 index 0000000000..6fcecc422b --- /dev/null +++ b/src/library/scala/collection/generic/GenSeqFactory.scala @@ -0,0 +1,26 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.collection +package generic + +/** A template for companion objects of Seq and subclasses thereof. + * + * @since 2.8 + */ +abstract class GenSeqFactory[CC[X] <: GenSeq[X] with GenericTraversableTemplate[X, CC]] extends GenTraversableFactory[CC] { + + /** This method is called in a pattern match { case Seq(...) => }. + * + * @param x the selector value + * @return sequence wrapped in an option, if this is a Seq, otherwise none + */ + def unapplySeq[A](x: CC[A]): Some[CC[A]] = Some(x) +} diff --git a/src/library/scala/collection/generic/GenSetFactory.scala b/src/library/scala/collection/generic/GenSetFactory.scala new file mode 100644 index 0000000000..d83f248aff --- /dev/null +++ b/src/library/scala/collection/generic/GenSetFactory.scala @@ -0,0 +1,44 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.collection +package generic + +import mutable.Builder + +/** A template for companion objects of `Set` and subclasses thereof. + * + * @define coll set + * @define Coll Set + * @define factoryInfo + * This object provides a set of operations needed to create `$Coll` values. + * @author Martin Odersky + * @version 2.8 + * @since 2.8 + * @define canBuildFromInfo + * The standard `CanBuildFrom` instance for `$Coll` objects. + * @see CanBuildFrom + * @define setCanBuildFromInfo + * The standard `CanBuildFrom` instance for `$Coll` objects. + * @see CanBuildFrom + * @see GenericCanBuildFrom + */ +abstract class GenSetFactory[CC[X] <: GenSet[X] with GenSetLike[X, CC[X]]] + extends GenericCompanion[CC] { + + def newBuilder[A]: Builder[A, CC[A]] + + /** $setCanBuildFromInfo + */ + def setCanBuildFrom[A] = new CanBuildFrom[CC[_], A, CC[A]] { + def apply(from: CC[_]) = newBuilder[A] + def apply() = newBuilder[A] + } +} diff --git a/src/library/scala/collection/generic/GenTraversableFactory.scala b/src/library/scala/collection/generic/GenTraversableFactory.scala new file mode 100644 index 0000000000..c2718e5ee2 --- /dev/null +++ b/src/library/scala/collection/generic/GenTraversableFactory.scala @@ -0,0 +1,245 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + +package scala.collection +package generic + +/** A template for companion objects of `Traversable` and subclasses thereof. + * This class provides a set of operations to create `$Coll` objects. + * It is typically inherited by companion objects of subclasses of `Traversable`. + * + * @since 2.8 + * + * @define coll collection + * @define Coll Traversable + * @define factoryInfo + * This object provides a set of operations to create `$Coll` values. + * @author Martin Odersky + * @version 2.8 + * @define canBuildFromInfo + * The standard `CanBuildFrom` instance for $Coll objects. + * @see CanBuildFrom + * @define genericCanBuildFromInfo + * The standard `CanBuildFrom` instance for $Coll objects. + * The created value is an instance of class `GenericCanBuildFrom`, + * which forwards calls to create a new builder to the + * `genericBuilder` method of the requesting collection. + * @see CanBuildFrom + * @see GenericCanBuildFrom + */ +abstract class GenTraversableFactory[CC[X] <: GenTraversable[X] with GenericTraversableTemplate[X, CC]] + extends GenericCompanion[CC] { + + /** A generic implementation of the `CanBuildFrom` trait, which forwards + * all calls to `apply(from)` to the `genericBuilder` method of + * $coll `from`, and which forwards all calls of `apply()` to the + * `newBuilder` method of this factory. + */ + class GenericCanBuildFrom[A] extends CanBuildFrom[CC[_], A, CC[A]] { + /** Creates a new builder on request of a collection. + * @param from the collection requesting the builder to be created. + * @return the result of invoking the `genericBuilder` method on `from`. + */ + def apply(from: Coll) = from.genericBuilder[A] + + /** Creates a new builder from scratch + * @return the result of invoking the `newBuilder` method of this factory. + */ + def apply() = newBuilder[A] + } + + /** Concatenates all argument collections into a single $coll. + * + * @param xss the collections that are to be concatenated. + * @return the concatenation of all the collections. + */ + def concat[A](xss: Traversable[A]*): CC[A] = { + val b = newBuilder[A] + // At present we're using IndexedSeq as a proxy for "has a cheap size method". + if (xss forall (_.isInstanceOf[IndexedSeq[_]])) + b.sizeHint(xss map (_.size) sum) + + for (xs <- xss.seq) b ++= xs + b.result + } + + /** Produces a $coll containing the results of some element computation a number of times. + * @param n the number of elements contained in the $coll. + * @param elem the element computation + * @return A $coll that contains the results of `n` evaluations of `elem`. + */ + def fill[A](n: Int)(elem: => A): CC[A] = { + val b = newBuilder[A] + b.sizeHint(n) + var i = 0 + while (i < n) { + b += elem + i += 1 + } + b.result + } + + /** Produces a two-dimensional $coll containing the results of some element computation a number of times. + * @param n1 the number of elements in the 1st dimension + * @param n2 the number of elements in the 2nd dimension + * @param elem the element computation + * @return A $coll that contains the results of `n1 x n2` evaluations of `elem`. + */ + def fill[A](n1: Int, n2: Int)(elem: => A): CC[CC[A]] = + tabulate(n1)(_ => fill(n2)(elem)) + + /** Produces a three-dimensional $coll containing the results of some element computation a number of times. + * @param n1 the number of elements in the 1st dimension + * @param n2 the number of elements in the 2nd dimension + * @param n3 the number of elements in the 3nd dimension + * @param elem the element computation + * @return A $coll that contains the results of `n1 x n2 x n3` evaluations of `elem`. + */ + def fill[A](n1: Int, n2: Int, n3: Int)(elem: => A): CC[CC[CC[A]]] = + tabulate(n1)(_ => fill(n2, n3)(elem)) + + /** Produces a four-dimensional $coll containing the results of some element computation a number of times. + * @param n1 the number of elements in the 1st dimension + * @param n2 the number of elements in the 2nd dimension + * @param n3 the number of elements in the 3nd dimension + * @param n4 the number of elements in the 4th dimension + * @param elem the element computation + * @return A $coll that contains the results of `n1 x n2 x n3 x n4` evaluations of `elem`. + */ + def fill[A](n1: Int, n2: Int, n3: Int, n4: Int)(elem: => A): CC[CC[CC[CC[A]]]] = + tabulate(n1)(_ => fill(n2, n3, n4)(elem)) + + /** Produces a five-dimensional $coll containing the results of some element computation a number of times. + * @param n1 the number of elements in the 1st dimension + * @param n2 the number of elements in the 2nd dimension + * @param n3 the number of elements in the 3nd dimension + * @param n4 the number of elements in the 4th dimension + * @param n5 the number of elements in the 5th dimension + * @param elem the element computation + * @return A $coll that contains the results of `n1 x n2 x n3 x n4 x n5` evaluations of `elem`. + */ + def fill[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(elem: => A): CC[CC[CC[CC[CC[A]]]]] = + tabulate(n1)(_ => fill(n2, n3, n4, n5)(elem)) + + /** Produces a $coll containing values of a given function over a range of integer values starting from 0. + * @param n The number of elements in the $coll + * @param f The function computing element values + * @return A $coll consisting of elements `f(0), ..., f(n -1)` + */ + def tabulate[A](n: Int)(f: Int => A): CC[A] = { + val b = newBuilder[A] + b.sizeHint(n) + var i = 0 + while (i < n) { + b += f(i) + i += 1 + } + b.result + } + + /** Produces a two-dimensional $coll containing values of a given function over ranges of integer values starting from 0. + * @param n1 the number of elements in the 1st dimension + * @param n2 the number of elements in the 2nd dimension + * @param f The function computing element values + * @return A $coll consisting of elements `f(i1, i2)` + * for `0 <= i1 < n1` and `0 <= i2 < n2`. + */ + def tabulate[A](n1: Int, n2: Int)(f: (Int, Int) => A): CC[CC[A]] = + tabulate(n1)(i1 => tabulate(n2)(f(i1, _))) + + /** Produces a three-dimensional $coll containing values of a given function over ranges of integer values starting from 0. + * @param n1 the number of elements in the 1st dimension + * @param n2 the number of elements in the 2nd dimension + * @param n3 the number of elements in the 3nd dimension + * @param f The function computing element values + * @return A $coll consisting of elements `f(i1, i2, i3)` + * for `0 <= i1 < n1`, `0 <= i2 < n2`, and `0 <= i3 < n3`. + */ + def tabulate[A](n1: Int, n2: Int, n3: Int)(f: (Int, Int, Int) => A): CC[CC[CC[A]]] = + tabulate(n1)(i1 => tabulate(n2, n3)(f(i1, _, _))) + + /** Produces a four-dimensional $coll containing values of a given function over ranges of integer values starting from 0. + * @param n1 the number of elements in the 1st dimension + * @param n2 the number of elements in the 2nd dimension + * @param n3 the number of elements in the 3nd dimension + * @param n4 the number of elements in the 4th dimension + * @param f The function computing element values + * @return A $coll consisting of elements `f(i1, i2, i3, i4)` + * for `0 <= i1 < n1`, `0 <= i2 < n2`, `0 <= i3 < n3`, and `0 <= i4 < n4`. + */ + def tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int)(f: (Int, Int, Int, Int) => A): CC[CC[CC[CC[A]]]] = + tabulate(n1)(i1 => tabulate(n2, n3, n4)(f(i1, _, _, _))) + + /** Produces a five-dimensional $coll containing values of a given function over ranges of integer values starting from 0. + * @param n1 the number of elements in the 1st dimension + * @param n2 the number of elements in the 2nd dimension + * @param n3 the number of elements in the 3nd dimension + * @param n4 the number of elements in the 4th dimension + * @param n5 the number of elements in the 5th dimension + * @param f The function computing element values + * @return A $coll consisting of elements `f(i1, i2, i3, i4, i5)` + * for `0 <= i1 < n1`, `0 <= i2 < n2`, `0 <= i3 < n3`, `0 <= i4 < n4`, and `0 <= i5 < n5`. + */ + def tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(f: (Int, Int, Int, Int, Int) => A): CC[CC[CC[CC[CC[A]]]]] = + tabulate(n1)(i1 => tabulate(n2, n3, n4, n5)(f(i1, _, _, _, _))) + + /** Produces a $coll containing a sequence of increasing of integers. + * + * @param from the first element of the $coll + * @param end the end value of the $coll (the first value NOT contained) + * @return a $coll with values `start, start + 1, ..., end - 1` + */ + def range[T: Integral](start: T, end: T): CC[T] = range(start, end, implicitly[Integral[T]].one) + + /** Produces a $coll containing equally spaced values in some integer interval. + * @param start the start value of the $coll + * @param end the end value of the $coll (the first value NOT contained) + * @param step the difference between successive elements of the $coll (must be positive or negative) + * @return a $coll with values `start, start + step, ...` up to, but excluding `end` + */ + def range[T: Integral](start: T, end: T, step: T): CC[T] = { + val num = implicitly[Integral[T]] + import num._ + + if (step == zero) throw new IllegalArgumentException("zero step") + val b = newBuilder[T] + b sizeHint immutable.NumericRange.count(start, end, step, false) + var i = start + while (if (step < zero) end < i else i < end) { + b += i + i += step + } + b.result + } + + /** Produces a $coll containing repeated applications of a function to a start value. + * + * @param start the start value of the $coll + * @param len the number of elements contained inthe $coll + * @param f the function that's repeatedly applied + * @return a $coll with `len` values in the sequence `start, f(start), f(f(start)), ...` + */ + def iterate[A](start: A, len: Int)(f: A => A): CC[A] = { + val b = newBuilder[A] + if (len > 0) { + b.sizeHint(len) + var acc = start + var i = 1 + b += acc + + while (i < len) { + acc = f(acc) + i += 1 + b += acc + } + } + b.result + } +} + diff --git a/src/library/scala/collection/generic/GenericSeqCompanion.scala b/src/library/scala/collection/generic/GenericSeqCompanion.scala new file mode 100644 index 0000000000..41e8d6dd39 --- /dev/null +++ b/src/library/scala/collection/generic/GenericSeqCompanion.scala @@ -0,0 +1,24 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + +package scala.collection +package generic + +import annotation.bridge + +trait GenericSeqCompanion[CC[X] <: Traversable[X]] + extends GenericCompanion[CC] { + + @bridge + override def empty[A]: CC[A] = super.empty[A] + + @bridge + override def apply[A](elems: A*): CC[A] = super.apply(elems: _*) + +} diff --git a/src/library/scala/collection/generic/MapFactory.scala b/src/library/scala/collection/generic/MapFactory.scala index 122d1c9313..a60e3032c1 100644 --- a/src/library/scala/collection/generic/MapFactory.scala +++ b/src/library/scala/collection/generic/MapFactory.scala @@ -11,7 +11,7 @@ package generic import mutable.{Builder, MapBuilder} - +import annotation.bridge /** A template for companion objects of `Map` and subclasses thereof. * @@ -31,32 +31,10 @@ import mutable.{Builder, MapBuilder} * @see CanBuildFrom * @see GenericCanBuildFrom */ -abstract class MapFactory[CC[A, B] <: GenMap[A, B] with GenMapLike[A, B, CC[A, B]]] { - - /** The type constructor of the collection that can be built by this factory */ - type Coll = CC[_, _] +abstract class MapFactory[CC[A, B] <: Map[A, B] with MapLike[A, B, CC[A, B]]] extends GenMapFactory[CC] { - /** An empty $Coll */ def empty[A, B]: CC[A, B] - /** A collection of type $Coll that contains given key/value bindings. - * @param elems the key/value pairs that make up the $coll - * @tparam A the type of the keys - * @tparam B the type of the associated values - * @return a new $coll consisting key/value pairs given by `elems`. - */ - def apply[A, B](elems: (A, B)*): CC[A, B] = (newBuilder[A, B] ++= elems).result - - /** The default builder for $Coll objects. - * @tparam A the type of the keys - * @tparam B the type of the associated values - */ - def newBuilder[A, B]: Builder[(A, B), CC[A, B]] = new MapBuilder[A, B, CC[A, B]](empty[A, B]) - - /** The standard `CanBuildFrom` class for maps. - */ - class MapCanBuildFrom[A, B] extends CanBuildFrom[Coll, (A, B), CC[A, B]] { - def apply(from: Coll) = newBuilder[A, B] - def apply() = newBuilder - } + @bridge + override def apply[A, B](elems: (A, B)*): CC[A, B] = super.apply(elems: _*) } diff --git a/src/library/scala/collection/generic/ParFactory.scala b/src/library/scala/collection/generic/ParFactory.scala index e8c2b239c3..637a1dca6a 100644 --- a/src/library/scala/collection/generic/ParFactory.scala +++ b/src/library/scala/collection/generic/ParFactory.scala @@ -21,7 +21,7 @@ import scala.collection.parallel.Combiner * @define Coll ParIterable */ abstract class ParFactory[CC[X] <: ParIterable[X] with GenericParTemplate[X, CC]] -extends TraversableFactory[CC] +extends GenTraversableFactory[CC] with GenericParCompanion[CC] { //type EPC[T, C] = collection.parallel.EnvironmentPassingCombiner[T, C] diff --git a/src/library/scala/collection/generic/ParMapFactory.scala b/src/library/scala/collection/generic/ParMapFactory.scala index 1a9efdf7a7..06105c9756 100644 --- a/src/library/scala/collection/generic/ParMapFactory.scala +++ b/src/library/scala/collection/generic/ParMapFactory.scala @@ -20,7 +20,7 @@ import scala.collection.mutable.Builder * @define Coll ParMap */ abstract class ParMapFactory[CC[X, Y] <: ParMap[X, Y] with ParMapLike[X, Y, CC[X, Y], _]] -extends MapFactory[CC] +extends GenMapFactory[CC] with GenericParMapCompanion[CC] { type MapColl = CC[_, _] diff --git a/src/library/scala/collection/generic/ParSetFactory.scala b/src/library/scala/collection/generic/ParSetFactory.scala index 4cf39f9041..221e893593 100644 --- a/src/library/scala/collection/generic/ParSetFactory.scala +++ b/src/library/scala/collection/generic/ParSetFactory.scala @@ -23,7 +23,7 @@ import collection.parallel.ParSetLike abstract class ParSetFactory[CC[X] <: ParSet[X] with ParSetLike[X, CC[X], _] with GenericParTemplate[X, CC]] - extends SetFactory[CC] + extends GenSetFactory[CC] with GenericParCompanion[CC] { def newBuilder[A]: Combiner[A, CC[A]] = newCombiner[A] diff --git a/src/library/scala/collection/generic/SeqFactory.scala b/src/library/scala/collection/generic/SeqFactory.scala index 5e733d8444..544d8f4d6f 100644 --- a/src/library/scala/collection/generic/SeqFactory.scala +++ b/src/library/scala/collection/generic/SeqFactory.scala @@ -11,16 +11,12 @@ package scala.collection package generic +import annotation.bridge + /** A template for companion objects of Seq and subclasses thereof. * * @since 2.8 */ -abstract class SeqFactory[CC[X] <: GenSeq[X] with GenericTraversableTemplate[X, CC]] extends TraversableFactory[CC] { +abstract class SeqFactory[CC[X] <: Seq[X] with GenericTraversableTemplate[X, CC]] + extends GenSeqFactory[CC] with TraversableFactory[CC] - /** This method is called in a pattern match { case Seq(...) => }. - * - * @param x the selector value - * @return sequence wrapped in an option, if this is a Seq, otherwise none - */ - def unapplySeq[A](x: CC[A]): Some[CC[A]] = Some(x) -} diff --git a/src/library/scala/collection/generic/SetFactory.scala b/src/library/scala/collection/generic/SetFactory.scala index 3b8b5d3d47..348743a120 100644 --- a/src/library/scala/collection/generic/SetFactory.scala +++ b/src/library/scala/collection/generic/SetFactory.scala @@ -12,33 +12,14 @@ package scala.collection package generic import mutable.Builder +import annotation.bridge -/** A template for companion objects of `Set` and subclasses thereof. - * - * @define coll set - * @define Coll Set - * @define factoryInfo - * This object provides a set of operations needed to create `$Coll` values. - * @author Martin Odersky - * @version 2.8 - * @since 2.8 - * @define canBuildFromInfo - * The standard `CanBuildFrom` instance for `$Coll` objects. - * @see CanBuildFrom - * @define setCanBuildFromInfo - * The standard `CanBuildFrom` instance for `$Coll` objects. - * @see CanBuildFrom - * @see GenericCanBuildFrom - */ -abstract class SetFactory[CC[X] <: GenSet[X] with GenSetLike[X, CC[X]]] - extends GenericCompanion[CC] { +abstract class SetFactory[CC[X] <: Set[X] with SetLike[X, CC[X]]] + extends GenSetFactory[CC] with GenericSeqCompanion[CC] { - def newBuilder[A]: Builder[A, CC[A]] + @bridge + override def empty[A]: CC[A] = super.empty[A] - /** $setCanBuildFromInfo - */ - def setCanBuildFrom[A] = new CanBuildFrom[CC[_], A, CC[A]] { - def apply(from: CC[_]) = newBuilder[A] - def apply() = newBuilder[A] - } + @bridge + override def apply[A](elems: A*): CC[A] = super.apply(elems: _*) } diff --git a/src/library/scala/collection/generic/Subtractable.scala b/src/library/scala/collection/generic/Subtractable.scala index 1e151f9212..1ca9d706f0 100644 --- a/src/library/scala/collection/generic/Subtractable.scala +++ b/src/library/scala/collection/generic/Subtractable.scala @@ -10,6 +10,8 @@ package scala.collection package generic +import annotation.bridge + /** This trait represents collection-like objects that can be reduced * using a '+' operator. It defines variants of `-` and `--` * as convenience methods in terms of single-element removal `-`. @@ -56,4 +58,7 @@ trait Subtractable[A, +Repr <: Subtractable[A, Repr]] { self => * except one less occurrence of each of the elements of `elems`. */ def --(xs: GenTraversableOnce[A]): Repr = (repr /: xs.seq) (_ - _) + + @bridge + def --(xs: TraversableOnce[A]): Repr = --(xs: GenTraversableOnce[A]) } diff --git a/src/library/scala/collection/generic/TraversableFactory.scala b/src/library/scala/collection/generic/TraversableFactory.scala index 66e5d2db77..e71de1252c 100644 --- a/src/library/scala/collection/generic/TraversableFactory.scala +++ b/src/library/scala/collection/generic/TraversableFactory.scala @@ -10,6 +10,8 @@ package scala.collection package generic +import annotation.bridge + /** A template for companion objects of `Traversable` and subclasses thereof. * This class provides a set of operations to create `$Coll` objects. * It is typically inherited by companion objects of subclasses of `Traversable`. @@ -33,213 +35,49 @@ package generic * @see CanBuildFrom * @see GenericCanBuildFrom */ -abstract class TraversableFactory[CC[X] <: GenTraversable[X] with GenericTraversableTemplate[X, CC]] - extends GenericCompanion[CC] { - - /** A generic implementation of the `CanBuildFrom` trait, which forwards - * all calls to `apply(from)` to the `genericBuilder` method of - * $coll `from`, and which forwards all calls of `apply()` to the - * `newBuilder` method of this factory. - */ - class GenericCanBuildFrom[A] extends CanBuildFrom[CC[_], A, CC[A]] { - /** Creates a new builder on request of a collection. - * @param from the collection requesting the builder to be created. - * @return the result of invoking the `genericBuilder` method on `from`. - */ - def apply(from: Coll) = from.genericBuilder[A] - - /** Creates a new builder from scratch - * @return the result of invoking the `newBuilder` method of this factory. - */ - def apply() = newBuilder[A] - } - - /** Concatenates all argument collections into a single $coll. - * - * @param xss the collections that are to be concatenated. - * @return the concatenation of all the collections. - */ - def concat[A](xss: Traversable[A]*): CC[A] = { - val b = newBuilder[A] - // At present we're using IndexedSeq as a proxy for "has a cheap size method". - if (xss forall (_.isInstanceOf[IndexedSeq[_]])) - b.sizeHint(xss map (_.size) sum) - - for (xs <- xss.seq) b ++= xs - b.result - } - - /** Produces a $coll containing the results of some element computation a number of times. - * @param n the number of elements contained in the $coll. - * @param elem the element computation - * @return A $coll that contains the results of `n` evaluations of `elem`. - */ - def fill[A](n: Int)(elem: => A): CC[A] = { - val b = newBuilder[A] - b.sizeHint(n) - var i = 0 - while (i < n) { - b += elem - i += 1 - } - b.result - } - - /** Produces a two-dimensional $coll containing the results of some element computation a number of times. - * @param n1 the number of elements in the 1st dimension - * @param n2 the number of elements in the 2nd dimension - * @param elem the element computation - * @return A $coll that contains the results of `n1 x n2` evaluations of `elem`. - */ - def fill[A](n1: Int, n2: Int)(elem: => A): CC[CC[A]] = - tabulate(n1)(_ => fill(n2)(elem)) - - /** Produces a three-dimensional $coll containing the results of some element computation a number of times. - * @param n1 the number of elements in the 1st dimension - * @param n2 the number of elements in the 2nd dimension - * @param n3 the number of elements in the 3nd dimension - * @param elem the element computation - * @return A $coll that contains the results of `n1 x n2 x n3` evaluations of `elem`. - */ - def fill[A](n1: Int, n2: Int, n3: Int)(elem: => A): CC[CC[CC[A]]] = - tabulate(n1)(_ => fill(n2, n3)(elem)) - - /** Produces a four-dimensional $coll containing the results of some element computation a number of times. - * @param n1 the number of elements in the 1st dimension - * @param n2 the number of elements in the 2nd dimension - * @param n3 the number of elements in the 3nd dimension - * @param n4 the number of elements in the 4th dimension - * @param elem the element computation - * @return A $coll that contains the results of `n1 x n2 x n3 x n4` evaluations of `elem`. - */ - def fill[A](n1: Int, n2: Int, n3: Int, n4: Int)(elem: => A): CC[CC[CC[CC[A]]]] = - tabulate(n1)(_ => fill(n2, n3, n4)(elem)) - - /** Produces a five-dimensional $coll containing the results of some element computation a number of times. - * @param n1 the number of elements in the 1st dimension - * @param n2 the number of elements in the 2nd dimension - * @param n3 the number of elements in the 3nd dimension - * @param n4 the number of elements in the 4th dimension - * @param n5 the number of elements in the 5th dimension - * @param elem the element computation - * @return A $coll that contains the results of `n1 x n2 x n3 x n4 x n5` evaluations of `elem`. - */ - def fill[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(elem: => A): CC[CC[CC[CC[CC[A]]]]] = - tabulate(n1)(_ => fill(n2, n3, n4, n5)(elem)) - - /** Produces a $coll containing values of a given function over a range of integer values starting from 0. - * @param n The number of elements in the $coll - * @param f The function computing element values - * @return A $coll consisting of elements `f(0), ..., f(n -1)` - */ - def tabulate[A](n: Int)(f: Int => A): CC[A] = { - val b = newBuilder[A] - b.sizeHint(n) - var i = 0 - while (i < n) { - b += f(i) - i += 1 - } - b.result - } - - /** Produces a two-dimensional $coll containing values of a given function over ranges of integer values starting from 0. - * @param n1 the number of elements in the 1st dimension - * @param n2 the number of elements in the 2nd dimension - * @param f The function computing element values - * @return A $coll consisting of elements `f(i1, i2)` - * for `0 <= i1 < n1` and `0 <= i2 < n2`. - */ - def tabulate[A](n1: Int, n2: Int)(f: (Int, Int) => A): CC[CC[A]] = - tabulate(n1)(i1 => tabulate(n2)(f(i1, _))) - - /** Produces a three-dimensional $coll containing values of a given function over ranges of integer values starting from 0. - * @param n1 the number of elements in the 1st dimension - * @param n2 the number of elements in the 2nd dimension - * @param n3 the number of elements in the 3nd dimension - * @param f The function computing element values - * @return A $coll consisting of elements `f(i1, i2, i3)` - * for `0 <= i1 < n1`, `0 <= i2 < n2`, and `0 <= i3 < n3`. - */ - def tabulate[A](n1: Int, n2: Int, n3: Int)(f: (Int, Int, Int) => A): CC[CC[CC[A]]] = - tabulate(n1)(i1 => tabulate(n2, n3)(f(i1, _, _))) - - /** Produces a four-dimensional $coll containing values of a given function over ranges of integer values starting from 0. - * @param n1 the number of elements in the 1st dimension - * @param n2 the number of elements in the 2nd dimension - * @param n3 the number of elements in the 3nd dimension - * @param n4 the number of elements in the 4th dimension - * @param f The function computing element values - * @return A $coll consisting of elements `f(i1, i2, i3, i4)` - * for `0 <= i1 < n1`, `0 <= i2 < n2`, `0 <= i3 < n3`, and `0 <= i4 < n4`. - */ - def tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int)(f: (Int, Int, Int, Int) => A): CC[CC[CC[CC[A]]]] = - tabulate(n1)(i1 => tabulate(n2, n3, n4)(f(i1, _, _, _))) - - /** Produces a five-dimensional $coll containing values of a given function over ranges of integer values starting from 0. - * @param n1 the number of elements in the 1st dimension - * @param n2 the number of elements in the 2nd dimension - * @param n3 the number of elements in the 3nd dimension - * @param n4 the number of elements in the 4th dimension - * @param n5 the number of elements in the 5th dimension - * @param f The function computing element values - * @return A $coll consisting of elements `f(i1, i2, i3, i4, i5)` - * for `0 <= i1 < n1`, `0 <= i2 < n2`, `0 <= i3 < n3`, `0 <= i4 < n4`, and `0 <= i5 < n5`. - */ - def tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(f: (Int, Int, Int, Int, Int) => A): CC[CC[CC[CC[CC[A]]]]] = - tabulate(n1)(i1 => tabulate(n2, n3, n4, n5)(f(i1, _, _, _, _))) - - /** Produces a $coll containing a sequence of increasing of integers. - * - * @param from the first element of the $coll - * @param end the end value of the $coll (the first value NOT contained) - * @return a $coll with values `start, start + 1, ..., end - 1` - */ - def range[T: Integral](start: T, end: T): CC[T] = range(start, end, implicitly[Integral[T]].one) - - /** Produces a $coll containing equally spaced values in some integer interval. - * @param start the start value of the $coll - * @param end the end value of the $coll (the first value NOT contained) - * @param step the difference between successive elements of the $coll (must be positive or negative) - * @return a $coll with values `start, start + step, ...` up to, but excluding `end` - */ - def range[T: Integral](start: T, end: T, step: T): CC[T] = { - val num = implicitly[Integral[T]] - import num._ - - if (step == zero) throw new IllegalArgumentException("zero step") - val b = newBuilder[T] - b sizeHint immutable.NumericRange.count(start, end, step, false) - var i = start - while (if (step < zero) end < i else i < end) { - b += i - i += step - } - b.result - } - - /** Produces a $coll containing repeated applications of a function to a start value. - * - * @param start the start value of the $coll - * @param len the number of elements contained inthe $coll - * @param f the function that's repeatedly applied - * @return a $coll with `len` values in the sequence `start, f(start), f(f(start)), ...` - */ - def iterate[A](start: A, len: Int)(f: A => A): CC[A] = { - val b = newBuilder[A] - if (len > 0) { - b.sizeHint(len) - var acc = start - var i = 1 - b += acc - - while (i < len) { - acc = f(acc) - i += 1 - b += acc - } - } - b.result - } +trait TraversableFactory[CC[X] <: Traversable[X] with GenericTraversableTemplate[X, CC]] + extends GenTraversableFactory[CC] with GenericSeqCompanion[CC] { + + @bridge + override def concat[A](xss: Traversable[A]*): CC[A] = super.concat(xss: _*) + + @bridge + override def fill[A](n: Int)(elem: => A): CC[A] = super.fill(n)(elem) + + @bridge + override def fill[A](n1: Int, n2: Int)(elem: => A): CC[CC[A]] = super.fill(n1, n2)(elem) + + @bridge + override def fill[A](n1: Int, n2: Int, n3: Int)(elem: => A): CC[CC[CC[A]]] = super.fill(n1, n2, n3)(elem) + + @bridge + override def fill[A](n1: Int, n2: Int, n3: Int, n4: Int)(elem: => A): CC[CC[CC[CC[A]]]] = super.fill(n1, n2, n3, n4)(elem) + + @bridge + override def fill[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(elem: => A): CC[CC[CC[CC[CC[A]]]]] = super.fill(n1, n2, n3, n4, n5)(elem) + + @bridge + override def tabulate[A](n: Int)(f: Int => A): CC[A] = super.tabulate(n)(f) + + @bridge + override def tabulate[A](n1: Int, n2: Int)(f: (Int, Int) => A): CC[CC[A]] = super.tabulate(n1, n2)(f) + + @bridge + override def tabulate[A](n1: Int, n2: Int, n3: Int)(f: (Int, Int, Int) => A): CC[CC[CC[A]]] = super.tabulate(n1, n2, n3)(f) + + @bridge + override def tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int)(f: (Int, Int, Int, Int) => A): CC[CC[CC[CC[A]]]] = super.tabulate(n1, n2, n3, n4)(f) + + @bridge + override def tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(f: (Int, Int, Int, Int, Int) => A): CC[CC[CC[CC[CC[A]]]]] = super.tabulate(n1, n2, n3, n4, n5)(f) + + @bridge + override def range[T: Integral](start: T, end: T): CC[T] = super.range(start, end) + + @bridge + override def range[T: Integral](start: T, end: T, step: T): CC[T] = super.range(start, end, step) + + @bridge + override def iterate[A](start: A, len: Int)(f: A => A): CC[A] = super.iterate(start, len)(f) } diff --git a/src/library/scala/collection/immutable/Range.scala b/src/library/scala/collection/immutable/Range.scala index 2a5ba9839f..2b1e4bff13 100644 --- a/src/library/scala/collection/immutable/Range.scala +++ b/src/library/scala/collection/immutable/Range.scala @@ -10,6 +10,7 @@ package scala.collection.immutable import scala.collection.parallel.immutable.ParRange +import annotation.bridge /** The `Range` class represents integer values in range * ''[start;end)'' with non-zero step value `step`. @@ -326,4 +327,10 @@ object Range { def apply(start: Int, end: Int, step: Int) = NumericRange(start, end, step) def inclusive(start: Int, end: Int, step: Int) = NumericRange.inclusive(start, end, step) } + + @deprecated("use Range instead") + trait ByOne extends Range { + @bridge override def foreach[@specialized(Unit) U](f: Int => U) = + super.foreach(f) + } } diff --git a/src/library/scala/collection/mutable/BufferLike.scala b/src/library/scala/collection/mutable/BufferLike.scala index 3c9e336e6f..c48dd5d621 100644 --- a/src/library/scala/collection/mutable/BufferLike.scala +++ b/src/library/scala/collection/mutable/BufferLike.scala @@ -13,7 +13,7 @@ package mutable import generic._ import script._ -import annotation.migration +import annotation.{migration, bridge} /** A template trait for buffers of type `Buffer[A]`. * @@ -271,6 +271,9 @@ trait BufferLike[A, +This <: BufferLike[A, This] with Buffer[A]] ) def ++(xs: GenTraversableOnce[A]): This = clone() ++= xs.seq + @bridge + def ++(xs: TraversableOnce[A]): This = ++(xs: GenTraversableOnce[A]) + /** Creates a new collection with all the elements of this collection except `elem`. * * @param elem the element to remove. diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala index 59648f160c..2c03f329a7 100644 --- a/src/library/scala/collection/mutable/PriorityQueue.scala +++ b/src/library/scala/collection/mutable/PriorityQueue.scala @@ -12,7 +12,7 @@ package scala.collection package mutable import generic._ -import annotation.migration +import annotation.{migration, bridge} /** This class implements priority queues using a heap. * To prioritize elements of type A there must be an implicit @@ -127,7 +127,10 @@ class PriorityQueue[A](implicit val ord: Ordering[A]) * @param xs a traversable object. * @return a new priority queue containing elements of both `xs` and `this`. */ - def ++(xs: GenTraversableOnce[A]) = { this.clone() ++= xs.seq } + def ++(xs: GenTraversableOnce[A]): PriorityQueue[A] = { this.clone() ++= xs.seq } + + @bridge + def ++(xs: TraversableOnce[A]): PriorityQueue[A] = ++ (xs: GenTraversableOnce[A]) /** Adds all elements to the queue. * diff --git a/src/library/scala/runtime/RichInt.scala b/src/library/scala/runtime/RichInt.scala index 771d430c92..cb02b6d3a8 100644 --- a/src/library/scala/runtime/RichInt.scala +++ b/src/library/scala/runtime/RichInt.scala @@ -10,6 +10,7 @@ package scala.runtime import scala.collection.immutable.Range +import annotation.bridge // Note that this does not implement IntegralProxy[Int] so that it can return // the Int-specific Range class from until/to. @@ -20,10 +21,16 @@ final class RichInt(val self: Int) extends ScalaNumberProxy[Int] with RangedProx def until(end: Int): Range = Range(self, end) def until(end: Int, step: Int): Range = Range(self, end, step) + @bridge + def until(end: Int): Range with Range.ByOne = new Range(self, end, 1) with Range.ByOne + /** like `until`, but includes the last index */ def to(end: Int): Range.Inclusive = Range.inclusive(self, end) def to(end: Int, step: Int): Range.Inclusive = Range.inclusive(self, end, step) + @bridge + def to(end: Int): Range with Range.ByOne = new Range.Inclusive(self, end, 1) with Range.ByOne + override def min(that: Int): Int = if (self < that) self else that override def max(that: Int): Int = if (self > that) self else that override def abs: Int = if (self < 0) -self else self |