diff options
Diffstat (limited to 'src/library')
19 files changed, 281 insertions, 962 deletions
diff --git a/src/library/scala/collection/GenIterableView.scala b/src/library/scala/collection/GenIterableView.scala deleted file mode 100644 index cd052ddf79..0000000000 --- a/src/library/scala/collection/GenIterableView.scala +++ /dev/null @@ -1,12 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection - -trait GenIterableView[+A, +Coll] extends GenIterableViewLike[A, Coll, GenIterableView[A, Coll]] { } diff --git a/src/library/scala/collection/GenIterableViewLike.scala b/src/library/scala/collection/GenIterableViewLike.scala deleted file mode 100644 index b519e99ae5..0000000000 --- a/src/library/scala/collection/GenIterableViewLike.scala +++ /dev/null @@ -1,78 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection - -trait GenIterableViewLike[+A, - +Coll, - +This <: GenIterableView[A, Coll] with GenIterableViewLike[A, Coll, This]] -extends GenIterable[A] with GenIterableLike[A, This] with GenTraversableView[A, Coll] with GenTraversableViewLike[A, Coll, This] { -self => - - trait Transformed[+B] extends GenIterableView[B, Coll] with super.Transformed[B] { - def iterator: Iterator[B] - override def foreach[U](f: B => U): Unit = iterator foreach f - override def toString = viewToString - override def isEmpty = !iterator.hasNext - } - - trait EmptyView extends Transformed[Nothing] with super.EmptyView { - final def iterator: Iterator[Nothing] = Iterator.empty - } - - trait Forced[B] extends super.Forced[B] with Transformed[B] { - def iterator = forced.iterator - } - - trait Sliced extends super.Sliced with Transformed[A] { - def iterator: Iterator[A] = self.iterator.slice(from, until) - } - - trait Mapped[B] extends super.Mapped[B] with Transformed[B] { - def iterator = self.iterator map mapping - } - - trait FlatMapped[B] extends super.FlatMapped[B] with Transformed[B] { - def iterator: Iterator[B] = self.iterator flatMap mapping - } - - trait Appended[B >: A] extends super.Appended[B] with Transformed[B] { - def iterator = self.iterator ++ rest - } - - trait Filtered extends super.Filtered with Transformed[A] { - def iterator = self.iterator filter pred - } - - trait TakenWhile extends super.TakenWhile with Transformed[A] { - def iterator = self.iterator takeWhile pred - } - - trait DroppedWhile extends super.DroppedWhile with Transformed[A] { - def iterator = self.iterator dropWhile pred - } - - trait Zipped[B] extends Transformed[(A, B)] { - protected[this] val other: GenIterable[B] - def iterator: Iterator[(A, B)] = self.iterator zip other.iterator - final override protected[this] def viewIdentifier = "Z" - } - - trait ZippedAll[A1 >: A, B] extends Transformed[(A1, B)] { - protected[this] val other: GenIterable[B] - protected[this] val thisElem: A1 - protected[this] val thatElem: B - final override protected[this] def viewIdentifier = "Z" - def iterator: Iterator[(A1, B)] = - self.iterator.zipAll(other.iterator, thisElem, thatElem) - } - -} - - diff --git a/src/library/scala/collection/GenSeqView.scala b/src/library/scala/collection/GenSeqView.scala deleted file mode 100644 index 0a214832ad..0000000000 --- a/src/library/scala/collection/GenSeqView.scala +++ /dev/null @@ -1,12 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection - -trait GenSeqView[+A, +Coll] extends GenSeqViewLike[A, Coll, GenSeqView[A, Coll]] { } diff --git a/src/library/scala/collection/GenSeqViewLike.scala b/src/library/scala/collection/GenSeqViewLike.scala deleted file mode 100644 index d3af953f72..0000000000 --- a/src/library/scala/collection/GenSeqViewLike.scala +++ /dev/null @@ -1,165 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection - - - - -trait GenSeqViewLike[+A, - +Coll, - +This <: GenSeqView[A, Coll] with GenSeqViewLike[A, Coll, This]] -extends GenSeq[A] with GenSeqLike[A, This] with GenIterableView[A, Coll] with GenIterableViewLike[A, Coll, This] { -self => - - trait Transformed[+B] extends GenSeqView[B, Coll] with super.Transformed[B] { - def length: Int - def apply(idx: Int): B - override def toString = viewToString - } - - trait EmptyView extends Transformed[Nothing] with super.EmptyView { - final override def length = 0 - final override def apply(n: Int) = Nil(n) - } - - trait Forced[B] extends super.Forced[B] with Transformed[B] { - def length = forced.length - def apply(idx: Int) = forced.apply(idx) - } - - trait Sliced extends super.Sliced with Transformed[A] { - def length = iterator.size - def apply(idx: Int): A = - if (idx + from < until) self.apply(idx + from) - else throw new IndexOutOfBoundsException(idx.toString) - - override def foreach[U](f: A => U) = iterator foreach f - override def iterator: Iterator[A] = self.iterator drop from take endpoints.width - } - - trait Mapped[B] extends super.Mapped[B] with Transformed[B] { - def length = self.length - def apply(idx: Int): B = mapping(self(idx)) - } - - trait FlatMapped[B] extends super.FlatMapped[B] with Transformed[B] { - protected[this] lazy val index = { - val index = new Array[Int](self.length + 1) - index(0) = 0 - for (i <- 0 until self.length) // note that if the mapping returns a list, performance is bad, bad - index(i + 1) = index(i) + mapping(self(i)).seq.size - index - } - protected[this] def findRow(idx: Int, lo: Int, hi: Int): Int = { - val mid = (lo + hi) / 2 - if (idx < index(mid)) findRow(idx, lo, mid - 1) - else if (idx >= index(mid + 1)) findRow(idx, mid + 1, hi) - else mid - } - def length = index(self.length) - def apply(idx: Int) = { - val row = findRow(idx, 0, self.length - 1) - mapping(self(row)).seq.toSeq(idx - index(row)) - } - } - - trait Appended[B >: A] extends super.Appended[B] with Transformed[B] { - protected[this] lazy val restSeq = rest.toSeq - def length = self.length + restSeq.length - def apply(idx: Int) = - if (idx < self.length) self(idx) else restSeq(idx - self.length) - } - - trait Filtered extends super.Filtered with Transformed[A] { - protected[this] lazy val index = { - var len = 0 - val arr = new Array[Int](self.length) - for (i <- 0 until self.length) - if (pred(self(i))) { - arr(len) = i - len += 1 - } - arr take len - } - def length = index.length - def apply(idx: Int) = self(index(idx)) - } - - trait TakenWhile extends super.TakenWhile with Transformed[A] { - protected[this] lazy val len = self prefixLength pred - def length = len - def apply(idx: Int) = - if (idx < len) self(idx) - else throw new IndexOutOfBoundsException(idx.toString) - } - - trait DroppedWhile extends super.DroppedWhile with Transformed[A] { - protected[this] lazy val start = self prefixLength pred - def length = self.length - start - def apply(idx: Int) = - if (idx >= 0) self(idx + start) - else throw new IndexOutOfBoundsException(idx.toString) - } - - trait Zipped[B] extends super.Zipped[B] with Transformed[(A, B)] { - protected[this] lazy val thatSeq = other.seq.toSeq - /* Have to be careful here - other may be an infinite sequence. */ - def length = if ((thatSeq lengthCompare self.length) <= 0) thatSeq.length else self.length - def apply(idx: Int) = (self.apply(idx), thatSeq.apply(idx)) - } - - trait ZippedAll[A1 >: A, B] extends super.ZippedAll[A1, B] with Transformed[(A1, B)] { - protected[this] lazy val thatSeq = other.seq.toSeq - def length: Int = self.length max thatSeq.length - def apply(idx: Int) = - (if (idx < self.length) self.apply(idx) else thisElem, - if (idx < thatSeq.length) thatSeq.apply(idx) else thatElem) - } - - trait Reversed extends Transformed[A] { - override def iterator: Iterator[A] = createReversedIterator - def length: Int = self.length - def apply(idx: Int): A = self.apply(length - 1 - idx) - final override protected[this] def viewIdentifier = "R" - - private def createReversedIterator = { - var lst = List[A]() - for (elem <- self) lst ::= elem - lst.iterator - } - } - - trait Patched[B >: A] extends Transformed[B] { - protected[this] val from: Int - protected[this] val patch: GenSeq[B] - protected[this] val replaced: Int - private lazy val plen = patch.length - override def iterator: Iterator[B] = self.iterator patch (from, patch.iterator, replaced) - def length: Int = self.length + plen - replaced - def apply(idx: Int): B = - if (idx < from) self.apply(idx) - else if (idx < from + plen) patch.apply(idx - from) - else self.apply(idx - plen + replaced) - final override protected[this] def viewIdentifier = "P" - } - - trait Prepended[B >: A] extends Transformed[B] { - protected[this] val fst: B - override def iterator: Iterator[B] = Iterator.single(fst) ++ self.iterator - def length: Int = 1 + self.length - def apply(idx: Int): B = - if (idx == 0) fst - else self.apply(idx - 1) - final override protected[this] def viewIdentifier = "A" - } - -} - - diff --git a/src/library/scala/collection/GenTraversableView.scala b/src/library/scala/collection/GenTraversableView.scala deleted file mode 100644 index 7d9a6e9777..0000000000 --- a/src/library/scala/collection/GenTraversableView.scala +++ /dev/null @@ -1,12 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection - -trait GenTraversableView[+A, +Coll] extends GenTraversableViewLike[A, Coll, GenTraversableView[A, Coll]] { } diff --git a/src/library/scala/collection/GenTraversableViewLike.scala b/src/library/scala/collection/GenTraversableViewLike.scala deleted file mode 100644 index dde18a7a32..0000000000 --- a/src/library/scala/collection/GenTraversableViewLike.scala +++ /dev/null @@ -1,138 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection - - -import generic._ -import mutable.{ Builder, ArrayBuffer } - -trait GenTraversableViewLike[+A, - +Coll, - +This <: GenTraversableView[A, Coll] with GenTraversableViewLike[A, Coll, This]] -extends GenTraversable[A] with GenTraversableLike[A, This] { -self => - - def force[B >: A, That](implicit bf: CanBuildFrom[Coll, B, That]): That - - protected def underlying: Coll - protected[this] def viewIdentifier: String - protected[this] def viewIdString: String - def viewToString = stringPrefix + viewIdString + "(...)" - - /** The implementation base trait of this view. - * This trait and all its subtraits has to be re-implemented for each - * ViewLike class. - */ - trait Transformed[+B] extends GenTraversableView[B, Coll] { - def foreach[U](f: B => U): Unit - - lazy val underlying = self.underlying - final override protected[this] def viewIdString = self.viewIdString + viewIdentifier - override def stringPrefix = self.stringPrefix - override def toString = viewToString - } - - trait EmptyView extends Transformed[Nothing] { - final override def isEmpty = true - final override def foreach[U](f: Nothing => U): Unit = () - } - - /** A fall back which forces everything into a vector and then applies an operation - * on it. Used for those operations which do not naturally lend themselves to a view - */ - trait Forced[B] extends Transformed[B] { - protected[this] val forced: GenSeq[B] - def foreach[U](f: B => U) = forced foreach f - final override protected[this] def viewIdentifier = "C" - } - - trait Sliced extends Transformed[A] { - protected[this] val endpoints: SliceInterval - protected[this] def from = endpoints.from - protected[this] def until = endpoints.until - // protected def newSliced(_endpoints: SliceInterval): Transformed[A] = - // self.newSliced(endpoints.recalculate(_endpoints)) - - def foreach[U](f: A => U) { - var index = 0 - for (x <- self) { - if (from <= index) { - if (until <= index) return - f(x) - } - index += 1 - } - } - final override protected[this] def viewIdentifier = "S" - } - - trait Mapped[B] extends Transformed[B] { - protected[this] val mapping: A => B - def foreach[U](f: B => U) { - for (x <- self) - f(mapping(x)) - } - final override protected[this] def viewIdentifier = "M" - } - - trait FlatMapped[B] extends Transformed[B] { - protected[this] val mapping: A => GenTraversableOnce[B] - def foreach[U](f: B => U) { - for (x <- self) - for (y <- mapping(x).seq) - f(y) - } - final override protected[this] def viewIdentifier = "N" - } - - trait Appended[B >: A] extends Transformed[B] { - protected[this] val rest: GenTraversable[B] - def foreach[U](f: B => U) { - self foreach f - rest foreach f - } - final override protected[this] def viewIdentifier = "A" - } - - trait Filtered extends Transformed[A] { - protected[this] val pred: A => Boolean - def foreach[U](f: A => U) { - for (x <- self) - if (pred(x)) f(x) - } - final override protected[this] def viewIdentifier = "F" - } - - trait TakenWhile extends Transformed[A] { - protected[this] val pred: A => Boolean - def foreach[U](f: A => U) { - for (x <- self) { - if (!pred(x)) return - f(x) - } - } - final override protected[this] def viewIdentifier = "T" - } - - trait DroppedWhile extends Transformed[A] { - protected[this] val pred: A => Boolean - def foreach[U](f: A => U) { - var go = false - for (x <- self) { - if (!go && !pred(x)) go = true - if (go) f(x) - } - } - final override protected[this] def viewIdentifier = "D" - } - -} - - diff --git a/src/library/scala/collection/IterableView.scala b/src/library/scala/collection/IterableView.scala index 1d631739aa..b5f424d2ab 100644 --- a/src/library/scala/collection/IterableView.scala +++ b/src/library/scala/collection/IterableView.scala @@ -17,7 +17,7 @@ import TraversableView.NoBuilder /** A base trait for non-strict views of `Iterable`s. * $iterableViewInfo */ -trait IterableView[+A, +Coll] extends IterableViewLike[A, Coll, IterableView[A, Coll]] with GenIterableView[A, Coll] +trait IterableView[+A, +Coll] extends IterableViewLike[A, Coll, IterableView[A, Coll]] /** An object containing the necessary implicit definitions to make * `IterableView`s work. Its definitions are generally not accessed directly by clients. diff --git a/src/library/scala/collection/IterableViewLike.scala b/src/library/scala/collection/IterableViewLike.scala index f24b8a075d..668190f700 100644 --- a/src/library/scala/collection/IterableViewLike.scala +++ b/src/library/scala/collection/IterableViewLike.scala @@ -34,39 +34,68 @@ trait IterableViewLike[+A, with IterableLike[A, This] with TraversableView[A, Coll] with TraversableViewLike[A, Coll, This] - with GenIterableViewLike[A, Coll, This] { self => - trait Transformed[+B] extends IterableView[B, Coll] with super[TraversableViewLike].Transformed[B] with super[GenIterableViewLike].Transformed[B] { + /** Explicit instantiation of the `Transformed` trait to reduce class file size in subclasses. */ + private[collection] abstract class AbstractTransformed[+B] extends Iterable[B] with super[TraversableViewLike].Transformed[B] with Transformed[B] + + trait Transformed[+B] extends IterableView[B, Coll] with super.Transformed[B] { def iterator: Iterator[B] override def foreach[U](f: B => U): Unit = iterator foreach f override def toString = viewToString + override def isEmpty = !iterator.hasNext } - /** Explicit instantiation of the `Transformed` trait to reduce class file size in subclasses. */ - private[collection] abstract class AbstractTransformed[+B] extends Iterable[B] with super[TraversableViewLike].Transformed[B] with Transformed[B] - - trait EmptyView extends Transformed[Nothing] with super[TraversableViewLike].EmptyView with super[GenIterableViewLike].EmptyView + trait EmptyView extends Transformed[Nothing] with super.EmptyView { + final def iterator: Iterator[Nothing] = Iterator.empty + } - trait Forced[B] extends super[TraversableViewLike].Forced[B] with super[GenIterableViewLike].Forced[B] with Transformed[B] + trait Forced[B] extends super.Forced[B] with Transformed[B] { + def iterator = forced.iterator + } - trait Sliced extends super[TraversableViewLike].Sliced with super[GenIterableViewLike].Sliced with Transformed[A] + trait Sliced extends super.Sliced with Transformed[A] { + def iterator: Iterator[A] = self.iterator.slice(from, until) + } - trait Mapped[B] extends super[TraversableViewLike].Mapped[B] with super[GenIterableViewLike].Mapped[B] with Transformed[B] + trait Mapped[B] extends super.Mapped[B] with Transformed[B] { + def iterator = self.iterator map mapping + } - trait FlatMapped[B] extends super[TraversableViewLike].FlatMapped[B] with super[GenIterableViewLike].FlatMapped[B] with Transformed[B] + trait FlatMapped[B] extends super.FlatMapped[B] with Transformed[B] { + def iterator: Iterator[B] = self.iterator flatMap mapping + } - trait Appended[B >: A] extends super[TraversableViewLike].Appended[B] with super[GenIterableViewLike].Appended[B] with Transformed[B] + trait Appended[B >: A] extends super.Appended[B] with Transformed[B] { + def iterator = self.iterator ++ rest + } - trait Filtered extends super[TraversableViewLike].Filtered with super[GenIterableViewLike].Filtered with Transformed[A] + trait Filtered extends super.Filtered with Transformed[A] { + def iterator = self.iterator filter pred + } - trait TakenWhile extends super[TraversableViewLike].TakenWhile with super[GenIterableViewLike].TakenWhile with Transformed[A] + trait TakenWhile extends super.TakenWhile with Transformed[A] { + def iterator = self.iterator takeWhile pred + } - trait DroppedWhile extends super[TraversableViewLike].DroppedWhile with super[GenIterableViewLike].DroppedWhile with Transformed[A] + trait DroppedWhile extends super.DroppedWhile with Transformed[A] { + def iterator = self.iterator dropWhile pred + } - trait Zipped[B] extends Transformed[(A, B)] with super[GenIterableViewLike].Zipped[B] + trait Zipped[B] extends Transformed[(A, B)] { + protected[this] val other: GenIterable[B] + def iterator: Iterator[(A, B)] = self.iterator zip other.iterator + final override protected[this] def viewIdentifier = "Z" + } - trait ZippedAll[A1 >: A, B] extends Transformed[(A1, B)] with super[GenIterableViewLike].ZippedAll[A1, B] + trait ZippedAll[A1 >: A, B] extends Transformed[(A1, B)] { + protected[this] val other: GenIterable[B] + protected[this] val thisElem: A1 + protected[this] val thatElem: B + final override protected[this] def viewIdentifier = "Z" + def iterator: Iterator[(A1, B)] = + self.iterator.zipAll(other.iterator, thisElem, thatElem) + } private[this] implicit def asThis(xs: Transformed[A]): This = xs.asInstanceOf[This] diff --git a/src/library/scala/collection/SeqView.scala b/src/library/scala/collection/SeqView.scala index 40dfd50212..4afc5bffcd 100644 --- a/src/library/scala/collection/SeqView.scala +++ b/src/library/scala/collection/SeqView.scala @@ -17,7 +17,7 @@ import TraversableView.NoBuilder /** A base trait for non-strict views of sequences. * $seqViewInfo */ -trait SeqView[+A, +Coll] extends SeqViewLike[A, Coll, SeqView[A, Coll]] with GenSeqView[A, Coll] +trait SeqView[+A, +Coll] extends SeqViewLike[A, Coll, SeqView[A, Coll]] /** An object containing the necessary implicit definitions to make * `SeqView`s work. Its definitions are generally not accessed directly by clients. diff --git a/src/library/scala/collection/SeqViewLike.scala b/src/library/scala/collection/SeqViewLike.scala index a949b56851..5e31ac4a53 100644 --- a/src/library/scala/collection/SeqViewLike.scala +++ b/src/library/scala/collection/SeqViewLike.scala @@ -30,45 +30,153 @@ import Seq.fill trait SeqViewLike[+A, +Coll, +This <: SeqView[A, Coll] with SeqViewLike[A, Coll, This]] - extends Seq[A] with SeqLike[A, This] with IterableView[A, Coll] with IterableViewLike[A, Coll, This] with GenSeqViewLike[A, Coll, This] + extends Seq[A] with SeqLike[A, This] with IterableView[A, Coll] with IterableViewLike[A, Coll, This] { self => - trait Transformed[+B] extends SeqView[B, Coll] with super[IterableViewLike].Transformed[B] with super[GenSeqViewLike].Transformed[B] { + /** Explicit instantiation of the `Transformed` trait to reduce class file size in subclasses. */ + private[collection] abstract class AbstractTransformed[+B] extends Seq[B] with super[IterableViewLike].Transformed[B] with Transformed[B] + + trait Transformed[+B] extends SeqView[B, Coll] with super.Transformed[B] { def length: Int def apply(idx: Int): B override def toString = viewToString } - /** Explicit instantiation of the `Transformed` trait to reduce class file size in subclasses. */ - private[collection] abstract class AbstractTransformed[+B] extends Seq[B] with super[IterableViewLike].Transformed[B] with Transformed[B] + trait EmptyView extends Transformed[Nothing] with super.EmptyView { + final override def length = 0 + final override def apply(n: Int) = Nil(n) + } - trait EmptyView extends Transformed[Nothing] with super[IterableViewLike].EmptyView with super[GenSeqViewLike].EmptyView + trait Forced[B] extends super.Forced[B] with Transformed[B] { + def length = forced.length + def apply(idx: Int) = forced.apply(idx) + } - trait Forced[B] extends super[IterableViewLike].Forced[B] with super[GenSeqViewLike].Forced[B] with Transformed[B] + trait Sliced extends super.Sliced with Transformed[A] { + def length = iterator.size + def apply(idx: Int): A = + if (idx + from < until) self.apply(idx + from) + else throw new IndexOutOfBoundsException(idx.toString) - trait Sliced extends super[IterableViewLike].Sliced with super[GenSeqViewLike].Sliced with Transformed[A] + override def foreach[U](f: A => U) = iterator foreach f + override def iterator: Iterator[A] = self.iterator drop from take endpoints.width + } - trait Mapped[B] extends super[IterableViewLike].Mapped[B] with super[GenSeqViewLike].Mapped[B] with Transformed[B] + trait Mapped[B] extends super.Mapped[B] with Transformed[B] { + def length = self.length + def apply(idx: Int): B = mapping(self(idx)) + } - trait FlatMapped[B] extends super[IterableViewLike].FlatMapped[B] with super[GenSeqViewLike].FlatMapped[B] with Transformed[B] + trait FlatMapped[B] extends super.FlatMapped[B] with Transformed[B] { + protected[this] lazy val index = { + val index = new Array[Int](self.length + 1) + index(0) = 0 + for (i <- 0 until self.length) // note that if the mapping returns a list, performance is bad, bad + index(i + 1) = index(i) + mapping(self(i)).seq.size + index + } + protected[this] def findRow(idx: Int, lo: Int, hi: Int): Int = { + val mid = (lo + hi) / 2 + if (idx < index(mid)) findRow(idx, lo, mid - 1) + else if (idx >= index(mid + 1)) findRow(idx, mid + 1, hi) + else mid + } + def length = index(self.length) + def apply(idx: Int) = { + val row = findRow(idx, 0, self.length - 1) + mapping(self(row)).seq.toSeq(idx - index(row)) + } + } - trait Appended[B >: A] extends super[IterableViewLike].Appended[B] with super[GenSeqViewLike].Appended[B] with Transformed[B] + trait Appended[B >: A] extends super.Appended[B] with Transformed[B] { + protected[this] lazy val restSeq = rest.toSeq + def length = self.length + restSeq.length + def apply(idx: Int) = + if (idx < self.length) self(idx) else restSeq(idx - self.length) + } - trait Filtered extends super[IterableViewLike].Filtered with super[GenSeqViewLike].Filtered with Transformed[A] + trait Filtered extends super.Filtered with Transformed[A] { + protected[this] lazy val index = { + var len = 0 + val arr = new Array[Int](self.length) + for (i <- 0 until self.length) + if (pred(self(i))) { + arr(len) = i + len += 1 + } + arr take len + } + def length = index.length + def apply(idx: Int) = self(index(idx)) + } - trait TakenWhile extends super[IterableViewLike].TakenWhile with super[GenSeqViewLike].TakenWhile with Transformed[A] + trait TakenWhile extends super.TakenWhile with Transformed[A] { + protected[this] lazy val len = self prefixLength pred + def length = len + def apply(idx: Int) = + if (idx < len) self(idx) + else throw new IndexOutOfBoundsException(idx.toString) + } - trait DroppedWhile extends super[IterableViewLike].DroppedWhile with super[GenSeqViewLike].DroppedWhile with Transformed[A] + trait DroppedWhile extends super.DroppedWhile with Transformed[A] { + protected[this] lazy val start = self prefixLength pred + def length = self.length - start + def apply(idx: Int) = + if (idx >= 0) self(idx + start) + else throw new IndexOutOfBoundsException(idx.toString) + } - trait Zipped[B] extends super[IterableViewLike].Zipped[B] with super[GenSeqViewLike].Zipped[B] with Transformed[(A, B)] + trait Zipped[B] extends super.Zipped[B] with Transformed[(A, B)] { + protected[this] lazy val thatSeq = other.seq.toSeq + /* Have to be careful here - other may be an infinite sequence. */ + def length = if ((thatSeq lengthCompare self.length) <= 0) thatSeq.length else self.length + def apply(idx: Int) = (self.apply(idx), thatSeq.apply(idx)) + } - trait ZippedAll[A1 >: A, B] extends super[IterableViewLike].ZippedAll[A1, B] with super[GenSeqViewLike].ZippedAll[A1, B] with Transformed[(A1, B)] + trait ZippedAll[A1 >: A, B] extends super.ZippedAll[A1, B] with Transformed[(A1, B)] { + protected[this] lazy val thatSeq = other.seq.toSeq + def length: Int = self.length max thatSeq.length + def apply(idx: Int) = + (if (idx < self.length) self.apply(idx) else thisElem, + if (idx < thatSeq.length) thatSeq.apply(idx) else thatElem) + } - trait Reversed extends Transformed[A] with super[GenSeqViewLike].Reversed + trait Reversed extends Transformed[A] { + override def iterator: Iterator[A] = createReversedIterator + def length: Int = self.length + def apply(idx: Int): A = self.apply(length - 1 - idx) + final override protected[this] def viewIdentifier = "R" + + private def createReversedIterator = { + var lst = List[A]() + for (elem <- self) lst ::= elem + lst.iterator + } + } - trait Patched[B >: A] extends Transformed[B] with super[GenSeqViewLike].Patched[B] + trait Patched[B >: A] extends Transformed[B] { + protected[this] val from: Int + protected[this] val patch: GenSeq[B] + protected[this] val replaced: Int + private lazy val plen = patch.length + override def iterator: Iterator[B] = self.iterator patch (from, patch.iterator, replaced) + def length: Int = self.length + plen - replaced + def apply(idx: Int): B = + if (idx < from) self.apply(idx) + else if (idx < from + plen) patch.apply(idx - from) + else self.apply(idx - plen + replaced) + final override protected[this] def viewIdentifier = "P" + } - trait Prepended[B >: A] extends Transformed[B] with super[GenSeqViewLike].Prepended[B] + trait Prepended[B >: A] extends Transformed[B] { + protected[this] val fst: B + override def iterator: Iterator[B] = Iterator.single(fst) ++ self.iterator + def length: Int = 1 + self.length + def apply(idx: Int): B = + if (idx == 0) fst + else self.apply(idx - 1) + final override protected[this] def viewIdentifier = "A" + } /** Boilerplate method, to override in each subclass * This method could be eliminated if Scala had virtual classes diff --git a/src/library/scala/collection/TraversableView.scala b/src/library/scala/collection/TraversableView.scala index bbb5bde464..cffce6ff8e 100644 --- a/src/library/scala/collection/TraversableView.scala +++ b/src/library/scala/collection/TraversableView.scala @@ -15,7 +15,7 @@ import mutable.Builder /** A base trait for non-strict views of traversable collections. * $traversableViewInfo */ -trait TraversableView[+A, +Coll] extends TraversableViewLike[A, Coll, TraversableView[A, Coll]] with GenTraversableView[A, Coll] { } +trait TraversableView[+A, +Coll] extends TraversableViewLike[A, Coll, TraversableView[A, Coll]] { } /** An object containing the necessary implicit definitions to make * `TraversableView`s work. Its definitions are generally not accessed directly by clients. diff --git a/src/library/scala/collection/TraversableViewLike.scala b/src/library/scala/collection/TraversableViewLike.scala index d56ecb16ed..5926c69ebf 100644 --- a/src/library/scala/collection/TraversableViewLike.scala +++ b/src/library/scala/collection/TraversableViewLike.scala @@ -70,27 +70,39 @@ trait ViewMkString[+A] { trait TraversableViewLike[+A, +Coll, +This <: TraversableView[A, Coll] with TraversableViewLike[A, Coll, This]] - extends Traversable[A] with TraversableLike[A, This] with ViewMkString[A] with GenTraversableViewLike[A, Coll, This] + extends Traversable[A] with TraversableLike[A, This] with ViewMkString[A] { self => - override protected[this] def newBuilder: Builder[A, This] = - throw new UnsupportedOperationException(this+".newBuilder") - protected def underlying: Coll protected[this] def viewIdentifier: String = "" protected[this] def viewIdString: String = "" + def viewToString = stringPrefix + viewIdString + "(...)" override def stringPrefix = "TraversableView" + override protected[this] def newBuilder: Builder[A, This] = + throw new UnsupportedOperationException(this+".newBuilder") + def force[B >: A, That](implicit bf: CanBuildFrom[Coll, B, That]) = { val b = bf(underlying) b ++= this b.result() } - trait Transformed[+B] extends TraversableView[B, Coll] with super.Transformed[B] { + /** Explicit instantiation of the `Transformed` trait to reduce class file size in subclasses. */ + private[collection] abstract class AbstractTransformed[+B] extends Traversable[B] with Transformed[B] + + + /** The implementation base trait of this view. + * This trait and all its subtraits has to be re-implemented for each + * ViewLike class. + */ + trait Transformed[+B] extends TraversableView[B, Coll] { def foreach[U](f: B => U): Unit + lazy val underlying = self.underlying + final override protected[this] def viewIdString = self.viewIdString + viewIdentifier + // Methods whose standard implementations use "isEmpty" need to be rewritten // for views, else they will end up traversing twice in a situation like: // xs.view.flatMap(f).headOption @@ -116,29 +128,99 @@ trait TraversableViewLike[+A, override def toString = viewToString } - /** Explicit instantiation of the `Transformed` trait to reduce class file size in subclasses. */ - private[collection] abstract class AbstractTransformed[+B] extends Traversable[B] with Transformed[B] - - trait EmptyView extends Transformed[Nothing] with super.EmptyView + trait EmptyView extends Transformed[Nothing] { + final override def isEmpty = true + final override def foreach[U](f: Nothing => U): Unit = () + } /** A fall back which forces everything into a vector and then applies an operation * on it. Used for those operations which do not naturally lend themselves to a view */ - trait Forced[B] extends Transformed[B] with super.Forced[B] + trait Forced[B] extends Transformed[B] { + protected[this] val forced: GenSeq[B] + def foreach[U](f: B => U) = forced foreach f + final override protected[this] def viewIdentifier = "C" + } - trait Sliced extends Transformed[A] with super.Sliced + trait Sliced extends Transformed[A] { + protected[this] val endpoints: SliceInterval + protected[this] def from = endpoints.from + protected[this] def until = endpoints.until + // protected def newSliced(_endpoints: SliceInterval): Transformed[A] = + // self.newSliced(endpoints.recalculate(_endpoints)) + + def foreach[U](f: A => U) { + var index = 0 + for (x <- self) { + if (from <= index) { + if (until <= index) return + f(x) + } + index += 1 + } + } + final override protected[this] def viewIdentifier = "S" + } - trait Mapped[B] extends Transformed[B] with super.Mapped[B] + trait Mapped[B] extends Transformed[B] { + protected[this] val mapping: A => B + def foreach[U](f: B => U) { + for (x <- self) + f(mapping(x)) + } + final override protected[this] def viewIdentifier = "M" + } - trait FlatMapped[B] extends Transformed[B] with super.FlatMapped[B] + trait FlatMapped[B] extends Transformed[B] { + protected[this] val mapping: A => GenTraversableOnce[B] + def foreach[U](f: B => U) { + for (x <- self) + for (y <- mapping(x).seq) + f(y) + } + final override protected[this] def viewIdentifier = "N" + } - trait Appended[B >: A] extends Transformed[B] with super.Appended[B] + trait Appended[B >: A] extends Transformed[B] { + protected[this] val rest: GenTraversable[B] + def foreach[U](f: B => U) { + self foreach f + rest foreach f + } + final override protected[this] def viewIdentifier = "A" + } - trait Filtered extends Transformed[A] with super.Filtered + trait Filtered extends Transformed[A] { + protected[this] val pred: A => Boolean + def foreach[U](f: A => U) { + for (x <- self) + if (pred(x)) f(x) + } + final override protected[this] def viewIdentifier = "F" + } - trait TakenWhile extends Transformed[A] with super.TakenWhile + trait TakenWhile extends Transformed[A] { + protected[this] val pred: A => Boolean + def foreach[U](f: A => U) { + for (x <- self) { + if (!pred(x)) return + f(x) + } + } + final override protected[this] def viewIdentifier = "T" + } - trait DroppedWhile extends Transformed[A] with super.DroppedWhile + trait DroppedWhile extends Transformed[A] { + protected[this] val pred: A => Boolean + def foreach[U](f: A => U) { + var go = false + for (x <- self) { + if (!go && !pred(x)) go = true + if (go) f(x) + } + } + final override protected[this] def viewIdentifier = "D" + } override def ++[B >: A, That](xs: GenTraversableOnce[B])(implicit bf: CanBuildFrom[This, B, That]): That = { newAppended(xs.seq.toTraversable).asInstanceOf[That] diff --git a/src/library/scala/collection/parallel/ParIterableLike.scala b/src/library/scala/collection/parallel/ParIterableLike.scala index 5ec0238c69..6ab76bce89 100644 --- a/src/library/scala/collection/parallel/ParIterableLike.scala +++ b/src/library/scala/collection/parallel/ParIterableLike.scala @@ -841,14 +841,8 @@ self: ParIterableLike[T, Repr, Sequential] => tasksupport.executeAndWaitResult(new ToParMap(combinerFactory(cbf), splitter)(ev) mapResult { _.resultWithTaskSupport }) } - def view = new ParIterableView[T, Repr, Sequential] { - protected lazy val underlying = self.repr - protected[this] def viewIdentifier = "" - protected[this] def viewIdString = "" - override def seq = self.seq.view - def splitter = self.splitter - def size = splitter.remaining - } + @deprecated("Use .seq.view instead", "2.11.0") + def view = seq.view override def toArray[U >: T: ClassTag]: Array[U] = { val arr = new Array[U](size) diff --git a/src/library/scala/collection/parallel/ParIterableView.scala b/src/library/scala/collection/parallel/ParIterableView.scala deleted file mode 100644 index 6dce19db19..0000000000 --- a/src/library/scala/collection/parallel/ParIterableView.scala +++ /dev/null @@ -1,47 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection.parallel - -import scala.collection.{ Parallel, IterableView, GenIterableView, Iterator } -import scala.collection.generic.CanCombineFrom - -/** A template view of a non-strict view of a parallel iterable collection. - * - * @tparam T the type of elements - * @tparam Coll the type of the parallel collection this view was created from - * @tparam CollSeq the type of the sequential collection corresponding to the underlying parallel collection - * - * @since 2.9 - */ -trait ParIterableView[+T, +Coll <: Parallel, +CollSeq] -extends ParIterableViewLike[T, Coll, CollSeq, ParIterableView[T, Coll, CollSeq], IterableView[T, CollSeq]] - with GenIterableView[T, Coll] - - -object ParIterableView { - abstract class NoCombiner[T] extends Combiner[T, Nothing] { -// self: EnvironmentPassingCombiner[T, Nothing] => - def +=(elem: T): this.type = this - def iterator: Iterator[T] = Iterator.empty - def result() = throw new UnsupportedOperationException("ParIterableView.Combiner.result") - def size = throw new UnsupportedOperationException("ParIterableView.Combiner.size") - def clear() {} - def combine[N <: T, NewTo >: Nothing](other: Combiner[N, NewTo]) = - throw new UnsupportedOperationException("ParIterableView.Combiner.result") - } - - type Coll = ParIterableView[_, C, _] forSome { type C <: ParIterable[_] } - - implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParIterableView[T, ParIterable[T], Iterable[T]]] = - new CanCombineFrom[Coll, T, ParIterableView[T, ParIterable[T], Iterable[T]]] { - def apply(from: Coll) = new NoCombiner[T] {} // was: with EnvironmentPassingCombiner[T, Nothing] - def apply() = new NoCombiner[T] {} // was: with EnvironmentPassingCombiner[T, Nothing] - } -} diff --git a/src/library/scala/collection/parallel/ParIterableViewLike.scala b/src/library/scala/collection/parallel/ParIterableViewLike.scala deleted file mode 100644 index 5a7a5f5601..0000000000 --- a/src/library/scala/collection/parallel/ParIterableViewLike.scala +++ /dev/null @@ -1,191 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection.parallel - -import scala.collection.Parallel -import scala.collection.{ IterableView, IterableViewLike } -import scala.collection.{ GenIterableView, GenIterableViewLike } -import scala.collection.GenTraversableOnce -import scala.collection.GenTraversable -import scala.collection.GenIterable -import scala.collection.GenSeq -import scala.collection.generic.{ CanBuildFrom, SliceInterval } -import scala.collection.generic.CanCombineFrom -import scala.collection.parallel.immutable.ParRange -import scala.language.implicitConversions - -/** A template view of a non-strict view of parallel iterable collection. - * - * '''Note:''' Regular view traits have type parameters used to carry information - * about the type of the elements, type of the collection they are derived from and - * their own actual representation type. Parallel views have an additional parameter - * which carries information about the type of the sequential version of the view. - * - * @tparam T the type of the elements this view can traverse - * @tparam Coll the type of the parallel collection this view is derived from - * @tparam CollSeq the type of the sequential collection corresponding to the underlying parallel collection - * @tparam This the actual representation type of this view - * @tparam ThisSeq the type of the sequential representation of this view - * - * @since 2.9 - */ -trait ParIterableViewLike[+T, - +Coll <: Parallel, - +CollSeq, - +This <: ParIterableView[T, Coll, CollSeq] with ParIterableViewLike[T, Coll, CollSeq, This, ThisSeq], - +ThisSeq <: IterableView[T, CollSeq] with IterableViewLike[T, CollSeq, ThisSeq]] -extends GenIterableView[T, Coll] - with GenIterableViewLike[T, Coll, This] - with ParIterable[T] - with ParIterableLike[T, This, ThisSeq] -{ -self => - - override def foreach[U](f: T => U): Unit = super[ParIterableLike].foreach(f) - override protected[this] def newCombiner: Combiner[T, This] = throw new UnsupportedOperationException(this + ".newCombiner") - - protected[this] def viewIdentifier: String - protected[this] def viewIdString: String - - protected def underlying: Coll - - /* wrappers */ - - trait Transformed[+S] extends ParIterableView[S, Coll, CollSeq] with super.Transformed[S] { - override def splitter: IterableSplitter[S] - override def iterator = splitter - def size = splitter.remaining - } - - trait Sliced extends super.Sliced with Transformed[T] { - // override def slice(from1: Int, until1: Int): This = newSliced(from1 max 0, until1 max 0).asInstanceOf[This] - def splitter: IterableSplitter[T] = self.splitter.slice(from, until) - override def seq = self.seq.slice(from, until) - } - - trait Mapped[S] extends super.Mapped[S] with Transformed[S]{ - def splitter: IterableSplitter[S] = self.splitter.map(mapping) - override def seq = self.seq.map(mapping).asInstanceOf[IterableView[S, CollSeq]] - } - - // only use if other is a ParIterable, otherwise force - trait Appended[U >: T] extends super.Appended[U] with Transformed[U] { - def restPar: ParIterable[U] = rest.asParIterable - def splitter: IterableSplitter[U] = self.splitter.appendParIterable[U, IterableSplitter[U]](restPar.splitter) - override def seq = self.seq.++(rest).asInstanceOf[IterableView[U, CollSeq]] - } - - trait Forced[S] extends super.Forced[S] with Transformed[S] { - def forcedPar: ParIterable[S] = forced.asParIterable - def splitter: IterableSplitter[S] = forcedPar.splitter - override def seq = forcedPar.seq.view.asInstanceOf[IterableView[S, CollSeq]] - } - - // only use if other is a ParSeq, otherwise force - trait Zipped[S] extends super.Zipped[S] with Transformed[(T, S)] { - def otherPar: ParSeq[S] = other.asParSeq - def splitter: IterableSplitter[(T, S)] = self.splitter zipParSeq otherPar.splitter - override def seq = (self.seq zip other).asInstanceOf[IterableView[(T, S), CollSeq]] - } - - // only use if other is a ParSeq, otherwise force - trait ZippedAll[U >: T, S] extends super.ZippedAll[U, S] with Transformed[(U, S)] { - def otherPar: ParSeq[S] = other.asParSeq - def splitter: IterableSplitter[(U, S)] = self.splitter.zipAllParSeq(otherPar.splitter, thisElem, thatElem) - override def seq = (self.seq.zipAll(other, thisElem, thatElem)).asInstanceOf[IterableView[(U, S), CollSeq]] - } - - protected[this] def thisParSeq: ParSeq[T] = mutable.ParArray.fromTraversables(this.iterator) - private[this] implicit def asThis(xs: Transformed[T]): This = xs.asInstanceOf[This] - - /* operation overrides */ - - override def take(n: Int): This = newSliced(SliceInterval(0, n)) - override def drop(n: Int): This = newSliced(SliceInterval(n, splitter.remaining)) - override def splitAt(n: Int): (This, This) = (take(n), drop(n)) - override def slice(from: Int, until: Int): This = newSliced(SliceInterval(from, until)) - override def map[S, That](f: T => S)(implicit bf: CanBuildFrom[This, S, That]): That = newMapped(f).asInstanceOf[That] - override def ++[U >: T, That](xs: GenTraversableOnce[U])(implicit bf: CanBuildFrom[This, U, That]): That = newAppendedTryParIterable(xs.toTraversable).asInstanceOf[That] - - override def filter(p: T => Boolean): This = newForced(thisParSeq.filter(p)).asInstanceOf[This] - override def filterNot(p: T => Boolean): This = newForced(thisParSeq.filterNot(p)).asInstanceOf[This] - override def partition(p: T => Boolean): (This, This) = { - val (t, f) = thisParSeq.partition(p) - (newForced(t).asInstanceOf[This], newForced(f).asInstanceOf[This]) - } - override def takeWhile(p: T => Boolean): This = newForced(thisParSeq.takeWhile(p)).asInstanceOf[This] - override def dropWhile(p: T => Boolean): This = newForced(thisParSeq.dropWhile(p)).asInstanceOf[This] - override def span(p: T => Boolean): (This, This) = { - val (pref, suff) = thisParSeq.span(p) - (newForced(pref).asInstanceOf[This], newForced(suff).asInstanceOf[This]) - } - override def flatMap[S, That](f: T => GenTraversableOnce[S])(implicit bf: CanBuildFrom[This, S, That]): That = newForced(thisParSeq.flatMap(f)).asInstanceOf[That] - - override def zip[U >: T, S, That](that: GenIterable[S])(implicit bf: CanBuildFrom[This, (U, S), That]): That = newZippedTryParSeq(that).asInstanceOf[That] - override def zipWithIndex[U >: T, That](implicit bf: CanBuildFrom[This, (U, Int), That]): That = - newZipped(ParRange(0, splitter.remaining, 1, inclusive = false)).asInstanceOf[That] - override def zipAll[S, U >: T, That](that: GenIterable[S], thisElem: U, thatElem: S)(implicit bf: CanBuildFrom[This, (U, S), That]): That = - newZippedAllTryParSeq(that, thisElem, thatElem).asInstanceOf[That] - - override def force[U >: T, That](implicit bf: CanBuildFrom[Coll, U, That]) = bf ifParallel { pbf => - tasksupport.executeAndWaitResult(new Force(pbf, splitter).mapResult(_.result).asInstanceOf[Task[That, ResultMapping[_, Force[U, That], That]]]) - } otherwise { - val b = bf(underlying) - b ++= this.iterator - b.result() - } - - /* wrapper virtual ctors */ - - protected def newSliced(_endpoints: SliceInterval): Transformed[T] = new { val endpoints = _endpoints } with Sliced - protected def newMapped[S](f: T => S): Transformed[S] = new Mapped[S] { val mapping = f } - protected def newForced[S](xs: => GenSeq[S]): Transformed[S] = new Forced[S] { val forced = xs } - protected def newAppended[U >: T](that: GenTraversable[U]): Transformed[U] = new Appended[U] { val rest = that } - protected def newDroppedWhile(p: T => Boolean) = unsupported - protected def newTakenWhile(p: T => Boolean) = unsupported - protected def newFlatMapped[S](f: T => GenTraversableOnce[S]) = unsupported - protected def newFiltered(p: T => Boolean) = unsupported - protected def newZipped[S](that: GenIterable[S]): Transformed[(T, S)] = new Zipped[S] { val other = that } - protected def newZippedAll[U >: T, S](that: GenIterable[S], _thisElem: U, _thatElem: S): Transformed[(U, S)] = new ZippedAll[U, S] { - val other = that - val thisElem = _thisElem - val thatElem = _thatElem - } - - /* argument sequence dependent ctors */ - - protected def newForcedTryParIterable[S](xs: => GenSeq[S]): Transformed[S] = { - if (xs.isParIterable) newForced[S](xs) - else newForced(mutable.ParArray.fromTraversables(xs)) - } - protected def newAppendedTryParIterable[U >: T](that: GenTraversable[U]): Transformed[U] = { - // we only append if `that` is a parallel iterable, i.e. it has a splitter - if (that.isParIterable) newAppended(that) - else newAppended(mutable.ParArray.fromTraversables(that)) - } - protected def newZippedTryParSeq[S](that: GenIterable[S]): Transformed[(T, S)] = { - if (that.isParSeq) newZipped[S](that) - else newZipped[S](mutable.ParArray.fromTraversables(that)) - } - protected def newZippedAllTryParSeq[S, U >: T](that: GenIterable[S], thisElem: U, thatElem: S): Transformed[(U, S)] = { - if (that.isParSeq) newZippedAll(that, thisElem, thatElem) - else newZippedAll(mutable.ParArray.fromTraversables(that), thisElem, thatElem) - } - - /* tasks */ - - protected[this] class Force[U >: T, That](cbf: CanCombineFrom[Coll, U, That], protected[this] val pit: IterableSplitter[T]) - extends Transformer[Combiner[U, That], Force[U, That]] { - var result: Combiner[U, That] = null - def leaf(prev: Option[Combiner[U, That]]) = result = pit.copy2builder[U, That, Combiner[U, That]](reuse(prev, cbf(self.underlying))) - protected[this] def newSubtask(p: IterableSplitter[T]) = new Force(cbf, p) - override def merge(that: Force[U, That]) = result = result combine that.result - } -} diff --git a/src/library/scala/collection/parallel/ParSeqLike.scala b/src/library/scala/collection/parallel/ParSeqLike.scala index 6693e30fcd..ca21f24534 100644 --- a/src/library/scala/collection/parallel/ParSeqLike.scala +++ b/src/library/scala/collection/parallel/ParSeqLike.scala @@ -323,15 +323,8 @@ self => override def toSeq = this.asInstanceOf[ParSeq[T]] - override def view = new ParSeqView[T, Repr, Sequential] { - protected lazy val underlying = self.repr - protected[this] def viewIdentifier = "" - protected[this] def viewIdString = "" - def length = self.length - def apply(idx: Int) = self(idx) - override def seq = self.seq.view - def splitter = self.splitter - } + @deprecated("use .seq.view", "2.11.0") + override def view = seq.view /* tasks */ diff --git a/src/library/scala/collection/parallel/ParSeqView.scala b/src/library/scala/collection/parallel/ParSeqView.scala deleted file mode 100644 index b80994514b..0000000000 --- a/src/library/scala/collection/parallel/ParSeqView.scala +++ /dev/null @@ -1,46 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection.parallel - -import scala.collection.{ SeqView, Parallel, Iterator } -import scala.collection.generic.CanCombineFrom - -/** A template view of a non-strict view of a parallel sequence. - * - * @tparam T the type of elements in this parallel sequence - * @tparam Coll the type of the underlying parallel collection - * @tparam CollSeq the type of the sequential collection corresponding to the underlying parallel collection - * - * @since 2.9 - */ -trait ParSeqView[+T, +Coll <: Parallel, +CollSeq] -extends ParSeqViewLike[T, Coll, CollSeq, ParSeqView[T, Coll, CollSeq], SeqView[T, CollSeq]] - - -object ParSeqView { - abstract class NoCombiner[T] extends Combiner[T, Nothing] { -// self: EnvironmentPassingCombiner[T, Nothing] => - def +=(elem: T): this.type = this - def iterator: Iterator[T] = Iterator.empty - def result() = throw new UnsupportedOperationException("ParSeqView.Combiner.result") - def size = throw new UnsupportedOperationException("ParSeqView.Combiner.size") - def clear() {} - def combine[N <: T, NewTo >: Nothing](other: Combiner[N, NewTo]) = - throw new UnsupportedOperationException("ParSeqView.Combiner.result") - } - - type Coll = ParSeqView[_, C, _] forSome { type C <: ParSeq[_] } - - implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParSeqView[T, ParSeq[T], Seq[T]]] = - new CanCombineFrom[Coll, T, ParSeqView[T, ParSeq[T], Seq[T]]] { - def apply(from: Coll) = new NoCombiner[T] {} // was: with EnvironmentPassingCombiner[T, Nothing] - def apply() = new NoCombiner[T] {} // was: with EnvironmentPassingCombiner[T, Nothing] - } -} diff --git a/src/library/scala/collection/parallel/ParSeqViewLike.scala b/src/library/scala/collection/parallel/ParSeqViewLike.scala deleted file mode 100644 index 9d30a052de..0000000000 --- a/src/library/scala/collection/parallel/ParSeqViewLike.scala +++ /dev/null @@ -1,189 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala -package collection.parallel - -import scala.collection.{ Parallel, SeqView, SeqViewLike, GenSeqView, GenSeqViewLike, GenSeq } -import scala.collection.{ GenIterable, GenTraversable, GenTraversableOnce, Iterator } -import scala.collection.generic.{ CanBuildFrom, SliceInterval } -import scala.collection.generic.CanCombineFrom -import scala.collection.parallel.immutable.ParRange - - -/** A template view of a non-strict view of parallel sequence. - * - * @tparam T the type of the elements in this view - * @tparam Coll type of the collection this view is derived from - * @tparam CollSeq type of the sequential collection corresponding to the underlying parallel collection - * @tparam This actual representation type of this view - * @tparam ThisSeq type of the sequential version of this view - * - * @since 2.9 - */ -trait ParSeqViewLike[+T, - +Coll <: Parallel, - +CollSeq, - +This <: ParSeqView[T, Coll, CollSeq] with ParSeqViewLike[T, Coll, CollSeq, This, ThisSeq], - +ThisSeq <: SeqView[T, CollSeq] with SeqViewLike[T, CollSeq, ThisSeq]] -extends GenSeqView[T, Coll] - with GenSeqViewLike[T, Coll, This] - with ParIterableView[T, Coll, CollSeq] - with ParIterableViewLike[T, Coll, CollSeq, This, ThisSeq] - with ParSeq[T] - with ParSeqLike[T, This, ThisSeq] -{ -self => - - trait Transformed[+S] extends ParSeqView[S, Coll, CollSeq] - with super[ParIterableView].Transformed[S] with super[GenSeqViewLike].Transformed[S] { - override def splitter: SeqSplitter[S] - override def iterator = splitter - override def size = length - } - - trait Sliced extends super[GenSeqViewLike].Sliced with super[ParIterableViewLike].Sliced with Transformed[T] { - // override def slice(from1: Int, until1: Int): This = newSliced(from1 max 0, until1 max 0).asInstanceOf[This] - override def splitter = self.splitter.psplit(from, until - from)(1) - override def seq = self.seq.slice(from, until) - } - - trait Mapped[S] extends super[GenSeqViewLike].Mapped[S] with super[ParIterableViewLike].Mapped[S] with Transformed[S] { - override def splitter = self.splitter.map(mapping) - override def seq = self.seq.map(mapping).asInstanceOf[SeqView[S, CollSeq]] - } - - trait Appended[U >: T] extends super[GenSeqViewLike].Appended[U] with super[ParIterableViewLike].Appended[U] with Transformed[U] { - override def restPar: ParSeq[U] = rest.asParSeq - override def splitter = self.splitter.appendParSeq[U, SeqSplitter[U]](restPar.splitter) - override def seq = self.seq.++(rest).asInstanceOf[SeqView[U, CollSeq]] - } - - trait Forced[S] extends super[GenSeqViewLike].Forced[S] with super[ParIterableViewLike].Forced[S] with Transformed[S] { - override def forcedPar: ParSeq[S] = forced.asParSeq - override def splitter: SeqSplitter[S] = forcedPar.splitter - override def seq = forcedPar.seq.view.asInstanceOf[SeqView[S, CollSeq]] - } - - trait Zipped[S] extends super[GenSeqViewLike].Zipped[S] with super[ParIterableViewLike].Zipped[S] with Transformed[(T, S)] { - override def splitter = self.splitter zipParSeq otherPar.splitter - override def seq = (self.seq zip other).asInstanceOf[SeqView[(T, S), CollSeq]] - } - - trait ZippedAll[U >: T, S] extends super[GenSeqViewLike].ZippedAll[U, S] with super[ParIterableViewLike].ZippedAll[U, S] with Transformed[(U, S)] { - override def splitter: SeqSplitter[(U, S)] = self.splitter.zipAllParSeq(otherPar.splitter, thisElem, thatElem) - override def seq = (self.seq.zipAll(other, thisElem, thatElem)).asInstanceOf[SeqView[(U, S), CollSeq]] - } - - trait Reversed extends super.Reversed with Transformed[T] { - override def splitter: SeqSplitter[T] = self.splitter.reverse - override def seq = self.seq.reverse.asInstanceOf[SeqView[T, CollSeq]] - } - - // use only with ParSeq patches, otherwise force - trait Patched[U >: T] extends super.Patched[U] with Transformed[U] { - def patchPar: ParSeq[U] = patch.asInstanceOf[ParSeq[U]] - override def splitter: SeqSplitter[U] = self.splitter.patchParSeq[U](from, patchPar.splitter, replaced) - override def seq = self.seq.patch(from, patch, replaced).asInstanceOf[SeqView[U, CollSeq]] - } - - // !!! - // - // What is up with this trait and method, why are they here doing - // nothing but throwing exceptions, without even being deprecated? - // They're not implementing something abstract; why aren't they - // just removed? - // - // use Patched instead - trait Prepended[U >: T] extends super.Prepended[U] with Transformed[U] { - unsupported - } - protected def newPrepended[U >: T](elem: U): Transformed[U] = unsupported - - /* wrapper virtual ctors */ - - protected override def newSliced(_endpoints: SliceInterval): Transformed[T] = new { val endpoints = _endpoints } with Sliced - protected override def newAppended[U >: T](that: GenTraversable[U]): Transformed[U] = { - // we only append if `that` is a parallel sequence, i.e. it has a precise splitter - if (that.isParSeq) new Appended[U] { val rest = that } - else newForced(mutable.ParArray.fromTraversables(this, that)) - } - protected override def newForced[S](xs: => GenSeq[S]): Transformed[S] = { - if (xs.isParSeq) new Forced[S] { val forced = xs } - else new Forced[S] { val forced = mutable.ParArray.fromTraversables(xs) } - } - protected override def newMapped[S](f: T => S): Transformed[S] = new Mapped[S] { val mapping = f } - protected override def newZipped[S](that: GenIterable[S]): Transformed[(T, S)] = new Zipped[S] { val other = that } - protected override def newZippedAll[U >: T, S](that: GenIterable[S], _thisElem: U, _thatElem: S): Transformed[(U, S)] = new ZippedAll[U, S] { - val other = that - val thisElem = _thisElem - val thatElem = _thatElem - } - protected def newReversed: Transformed[T] = new Reversed { } - protected def newPatched[U >: T](_from: Int, _patch: GenSeq[U], _replaced: Int): Transformed[U] = new { - val from = _from - val patch = _patch - val replaced = _replaced - } with Patched[U] - - /* operation overrides */ - - /* sliced */ - override def slice(from: Int, until: Int): This = newSliced(SliceInterval(from, until)).asInstanceOf[This] - override def take(n: Int): This = newSliced(SliceInterval(0, n)).asInstanceOf[This] - override def drop(n: Int): This = newSliced(SliceInterval(n, length)).asInstanceOf[This] - override def splitAt(n: Int): (This, This) = (take(n), drop(n)) - - /* appended */ - override def ++[U >: T, That](xs: GenTraversableOnce[U])(implicit bf: CanBuildFrom[This, U, That]): That = newAppended(xs.toTraversable).asInstanceOf[That] - override def :+[U >: T, That](elem: U)(implicit bf: CanBuildFrom[This, U, That]): That = ++(Iterator.single(elem))(bf) - //override def union[U >: T, That](that: GenSeq[U])(implicit bf: CanBuildFrom[This, U, That]): That = this ++ that - - /* misc */ - override def map[S, That](f: T => S)(implicit bf: CanBuildFrom[This, S, That]): That = newMapped(f).asInstanceOf[That] - override def zip[U >: T, S, That](that: GenIterable[S])(implicit bf: CanBuildFrom[This, (U, S), That]): That = newZippedTryParSeq(that).asInstanceOf[That] - override def zipWithIndex[U >: T, That](implicit bf: CanBuildFrom[This, (U, Int), That]): That = - newZipped(ParRange(0, splitter.remaining, 1, inclusive = false)).asInstanceOf[That] - override def reverse: This = newReversed.asInstanceOf[This] - override def reverseMap[S, That](f: T => S)(implicit bf: CanBuildFrom[This, S, That]): That = reverse.map(f) - - /* patched */ - override def updated[U >: T, That](index: Int, elem: U)(implicit bf: CanBuildFrom[This, U, That]): That = { - require(0 <= index && index < length) - patch(index, List(elem), 1)(bf) - } - override def padTo[U >: T, That](len: Int, elem: U)(implicit bf: CanBuildFrom[This, U, That]): That = patch(length, Seq.fill(len - length)(elem), 0) - override def +:[U >: T, That](elem: U)(implicit bf: CanBuildFrom[This, U, That]): That = patch(0, mutable.ParArray.fromTraversables(Iterator.single(elem)), 0) - override def patch[U >: T, That](from: Int, patch: GenSeq[U], replace: Int)(implicit bf: CanBuildFrom[This, U, That]): That = newPatched(from, patch, replace).asInstanceOf[That] - - /* forced */ - // override def diff[U >: T](that: GenSeq[U]): This = newForced(thisParSeq diff that).asInstanceOf[This] - // override def intersect[U >: T](that: GenSeq[U]): This = newForced(thisParSeq intersect that).asInstanceOf[This] - // override def sorted[U >: T](implicit ord: Ordering[U]): This = newForced(thisParSeq sorted ord).asInstanceOf[This] - override def collect[S, That](pf: PartialFunction[T, S])(implicit bf: CanBuildFrom[This, S, That]): That = filter(pf.isDefinedAt).map(pf)(bf) - override def scanLeft[S, That](z: S)(op: (S, T) => S)(implicit bf: CanBuildFrom[This, S, That]): That = newForced(thisParSeq.scanLeft(z)(op)).asInstanceOf[That] - override def scanRight[S, That](z: S)(op: (T, S) => S)(implicit bf: CanBuildFrom[This, S, That]): That = newForced(thisParSeq.scanRight(z)(op)).asInstanceOf[That] - override def groupBy[K](f: T => K): immutable.ParMap[K, This] = thisParSeq.groupBy(f).map(kv => (kv._1, newForced(kv._2).asInstanceOf[This])) - override def force[U >: T, That](implicit bf: CanBuildFrom[Coll, U, That]) = bf ifParallel { pbf => - tasksupport.executeAndWaitResult(new Force(pbf, splitter).mapResult(_.result).asInstanceOf[Task[That, _]]) - } otherwise { - val b = bf(underlying) - b ++= this.iterator - b.result() - } - - /* tasks */ - - protected[this] class Force[U >: T, That](cbf: CanCombineFrom[Coll, U, That], protected[this] val pit: SeqSplitter[T]) - extends Transformer[Combiner[U, That], Force[U, That]] { - var result: Combiner[U, That] = null - def leaf(prev: Option[Combiner[U, That]]) = result = pit.copy2builder[U, That, Combiner[U, That]](reuse(prev, cbf(self.underlying))) - protected[this] def newSubtask(p: SuperParIterator) = new Force(cbf, down(p)) - override def merge(that: Force[U, That]) = result = result combine that.result - } -} diff --git a/src/library/scala/concurrent/Future.scala b/src/library/scala/concurrent/Future.scala index f905785bd6..d271c4cdeb 100644 --- a/src/library/scala/concurrent/Future.scala +++ b/src/library/scala/concurrent/Future.scala @@ -384,7 +384,10 @@ trait Future[+T] extends Awaitable[T] { val p = Promise[U]() onComplete { case s @ Success(_) => p complete s - case _ => p completeWith that + case f @ Failure(_) => that onComplete { + case s2 @ Success(_) => p complete s2 + case _ => p complete f // Use the first failure as the failure + } } p.future } |