diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-07-23 16:39:51 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-07-23 16:39:51 +0000 |
commit | da234921b783d8ab31ddeb54a9c912f80715846d (patch) | |
tree | fd32fc40df0973011bc0460b7d49121f8aa1a709 | |
parent | 97b7cc4ddb806641ce5d1584ae913312755c012d (diff) | |
download | scala-da234921b783d8ab31ddeb54a9c912f80715846d.tar.gz scala-da234921b783d8ab31ddeb54a9c912f80715846d.tar.bz2 scala-da234921b783d8ab31ddeb54a9c912f80715846d.zip |
Parallel collection library renamings. No review
31 files changed, 645 insertions, 606 deletions
diff --git a/src/library/scala/collection/generic/GenericParallelCompanion.scala b/src/library/scala/collection/generic/GenericParCompanion.scala index e5ba36f846..0da1a3a543 100644 --- a/src/library/scala/collection/generic/GenericParallelCompanion.scala +++ b/src/library/scala/collection/generic/GenericParCompanion.scala @@ -2,8 +2,8 @@ package scala.collection.generic import scala.collection.parallel.Combiner -import scala.collection.parallel.ParallelIterable -import scala.collection.parallel.ParallelMap +import scala.collection.parallel.ParIterable +import scala.collection.parallel.ParMap @@ -12,7 +12,7 @@ import scala.collection.parallel.ParallelMap * @tparam CC the type constructor representing the collection class * @since 2.8 */ -trait GenericParallelCompanion[+CC[X] <: ParallelIterable[X]] { +trait GenericParCompanion[+CC[X] <: ParIterable[X]] { /** The default builder for $Coll objects. */ def newBuilder[A]: Combiner[A, CC[A]] @@ -22,7 +22,7 @@ trait GenericParallelCompanion[+CC[X] <: ParallelIterable[X]] { def newCombiner[A]: Combiner[A, CC[A]] } -trait GenericParallelMapCompanion[+CC[P, Q] <: ParallelMap[P, Q]] { +trait GenericParMapCompanion[+CC[P, Q] <: ParMap[P, Q]] { def newCombiner[P, Q]: Combiner[(P, Q), CC[P, Q]] } diff --git a/src/library/scala/collection/generic/GenericParallelTemplate.scala b/src/library/scala/collection/generic/GenericParTemplate.scala index e98c13fa36..1aac428438 100644 --- a/src/library/scala/collection/generic/GenericParallelTemplate.scala +++ b/src/library/scala/collection/generic/GenericParTemplate.scala @@ -3,8 +3,8 @@ package scala.collection.generic import scala.collection.parallel.Combiner -import scala.collection.parallel.ParallelIterable -import scala.collection.parallel.ParallelMap +import scala.collection.parallel.ParIterable +import scala.collection.parallel.ParMap import scala.collection.parallel.TaskSupport @@ -22,12 +22,12 @@ import annotation.unchecked.uncheckedVariance * @since 2.8 * @author prokopec */ -trait GenericParallelTemplate[+A, +CC[X] <: ParallelIterable[X]] +trait GenericParTemplate[+A, +CC[X] <: ParIterable[X]] extends GenericTraversableTemplate[A, CC] with HasNewCombiner[A, CC[A] @uncheckedVariance] with TaskSupport { - def companion: GenericCompanion[CC] with GenericParallelCompanion[CC] + def companion: GenericCompanion[CC] with GenericParCompanion[CC] protected[this] override def newBuilder: collection.mutable.Builder[A, CC[A]] = newCombiner @@ -48,10 +48,10 @@ extends GenericTraversableTemplate[A, CC] } -trait GenericParallelMapTemplate[K, +V, +CC[X, Y] <: ParallelMap[X, Y]] +trait GenericParMapTemplate[K, +V, +CC[X, Y] <: ParMap[X, Y]] extends TaskSupport { - def mapCompanion: GenericParallelMapCompanion[CC] + def mapCompanion: GenericParMapCompanion[CC] def genericMapCombiner[P, Q]: Combiner[(P, Q), CC[P, Q]] = { val cb = mapCompanion.newCombiner[P, Q] diff --git a/src/library/scala/collection/generic/ParallelFactory.scala b/src/library/scala/collection/generic/ParFactory.scala index fd33631640..a7ebb8c912 100644 --- a/src/library/scala/collection/generic/ParallelFactory.scala +++ b/src/library/scala/collection/generic/ParFactory.scala @@ -1,26 +1,26 @@ package scala.collection.generic -import scala.collection.parallel.ParallelIterable +import scala.collection.parallel.ParIterable import scala.collection.parallel.Combiner -/** A template class for companion objects of `ParallelIterable` and subclasses thereof. +/** A template class for companion objects of `ParIterable` and subclasses thereof. * This class extends `TraversableFactory` and provides a set of operations to create `$Coll` objects. * * @define $coll parallel collection - * @define $Coll ParallelIterable + * @define $Coll ParIterable */ -abstract class ParallelFactory[CC[X] <: ParallelIterable[X] with GenericParallelTemplate[X, CC]] +abstract class ParFactory[CC[X] <: ParIterable[X] with GenericParTemplate[X, CC]] extends TraversableFactory[CC] - with GenericParallelCompanion[CC] { + with GenericParCompanion[CC] { type EPC[T, C] = collection.parallel.EnvironmentPassingCombiner[T, C] /** * A generic implementation of the `CanCombineFrom` trait, which forwards all calls to - * `apply(from)` to the `genericParallelBuilder` method of the $coll `from`, and calls to `apply()` + * `apply(from)` to the `genericParBuilder` method of the $coll `from`, and calls to `apply()` * to this factory. */ class GenericCanCombineFrom[A] extends GenericCanBuildFrom[A] with CanCombineFrom[CC[_], A, CC[A]] { diff --git a/src/library/scala/collection/generic/ParallelMapFactory.scala b/src/library/scala/collection/generic/ParMapFactory.scala index 8f779b4029..2fba05c743 100644 --- a/src/library/scala/collection/generic/ParallelMapFactory.scala +++ b/src/library/scala/collection/generic/ParMapFactory.scala @@ -2,23 +2,23 @@ package scala.collection.generic -import scala.collection.parallel.ParallelMap -import scala.collection.parallel.ParallelMapLike +import scala.collection.parallel.ParMap +import scala.collection.parallel.ParMapLike import scala.collection.parallel.Combiner import scala.collection.mutable.Builder -/** A template class for companion objects of `ParallelMap` and subclasses thereof. +/** A template class for companion objects of `ParMap` and subclasses thereof. * This class extends `TraversableFactory` and provides a set of operations to create `$Coll` objects. * * @define $coll parallel map - * @define $Coll ParallelMap + * @define $Coll ParMap */ -abstract class ParallelMapFactory[CC[X, Y] <: ParallelMap[X, Y] with ParallelMapLike[X, Y, CC[X, Y], _]] +abstract class ParMapFactory[CC[X, Y] <: ParMap[X, Y] with ParMapLike[X, Y, CC[X, Y], _]] extends MapFactory[CC] - with GenericParallelMapCompanion[CC] { + with GenericParMapCompanion[CC] { type MapColl = CC[_, _] diff --git a/src/library/scala/collection/parallel/ParIterable.scala b/src/library/scala/collection/parallel/ParIterable.scala new file mode 100644 index 0000000000..7dd9b3038a --- /dev/null +++ b/src/library/scala/collection/parallel/ParIterable.scala @@ -0,0 +1,49 @@ +package scala.collection.parallel + + +import scala.collection.generic._ +import scala.collection.parallel.mutable.ParArrayCombiner +import scala.collection.parallel.mutable.ParArray + + +/** A template trait for parallel iterable collections. + * + * $paralleliterableinfo + * + * $sideeffects + * + * @tparam T the element type of the collection + * + * @author prokopec + * @since 2.8 + */ +trait ParIterable[+T] extends Iterable[T] + with GenericParTemplate[T, ParIterable] + with ParIterableLike[T, ParIterable[T], Iterable[T]] { + override def companion: GenericCompanion[ParIterable] with GenericParCompanion[ParIterable] = ParIterable +} + +/** $factoryinfo + */ +object ParIterable extends ParFactory[ParIterable] { + implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParIterable[T]] = + new GenericCanCombineFrom[T] + + def newBuilder[T]: Combiner[T, ParIterable[T]] = ParArrayCombiner[T] + + def newCombiner[T]: Combiner[T, ParIterable[T]] = ParArrayCombiner[T] +} + + + + + + + + + + + + + + diff --git a/src/library/scala/collection/parallel/ParallelIterableLike.scala b/src/library/scala/collection/parallel/ParIterableLike.scala index 7ac2713b55..82e1b60e25 100644 --- a/src/library/scala/collection/parallel/ParallelIterableLike.scala +++ b/src/library/scala/collection/parallel/ParIterableLike.scala @@ -15,7 +15,7 @@ import scala.collection.generic._ // TODO update docs!! -/** A template trait for parallel collections of type `ParallelIterable[T]`. +/** A template trait for parallel collections of type `ParIterable[T]`. * * $paralleliterableinfo * @@ -74,7 +74,7 @@ import scala.collection.generic._ * thumb is the number of elements divided by 8 times the parallelism level. This method may * be overridden in concrete implementations if necessary. * - * Finally, method `newParallelBuilder` produces a new parallel builder. + * Finally, method `newCombiner` produces a new parallel builder. * * Since this trait extends the `Iterable` trait, methods like `size` and `iterator` must also * be implemented. @@ -115,7 +115,7 @@ import scala.collection.generic._ * This method will provide sequential views it produces with `indexFlag` signalling capabilities. This means * that sequential views may set and read `indexFlag` state. */ -trait ParallelIterableLike[+T, +Repr <: Parallel, +SequentialView <: Iterable[T]] +trait ParIterableLike[+T, +Repr <: Parallel, +SequentialView <: Iterable[T]] extends IterableLike[T, Repr] with Parallelizable[Repr] with Sequentializable[T, SequentialView] @@ -135,18 +135,18 @@ extends IterableLike[T, Repr] * The self-type ensures that signal context passing behaviour gets mixed in * a concrete object instance. */ - trait ParallelIterator extends ParallelIterableIterator[T, Repr] { - me: SignalContextPassingIterator[ParallelIterator] => + trait ParIterator extends ParIterableIterator[T, Repr] { + me: SignalContextPassingIterator[ParIterator] => var signalDelegate: Signalling = IdleSignalling def repr = self.repr - def split: Seq[ParallelIterator] + def split: Seq[ParIterator] } /** A stackable modification that ensures signal contexts get passed along the iterators. - * A self-type requirement in `ParallelIterator` ensures that this trait gets mixed into + * A self-type requirement in `ParIterator` ensures that this trait gets mixed into * concrete iterators. */ - trait SignalContextPassingIterator[+IterRepr <: ParallelIterator] extends ParallelIterator { + trait SignalContextPassingIterator[+IterRepr <: ParIterator] extends ParIterator { // Note: This functionality must be factored out to this inner trait to avoid boilerplate. // Also, one could omit the cast below. However, this leads to return type inconsistencies, // due to inability to override the return type of _abstract overrides_. @@ -162,7 +162,7 @@ extends IterableLike[T, Repr] /** Convenience for signal context passing iterator. */ - type SCPI <: SignalContextPassingIterator[ParallelIterator] + type SCPI <: SignalContextPassingIterator[ParIterator] /** Creates a new parallel iterator used to traverse the elements of this parallel collection. * This iterator is more specific than the iterator of the returned by `iterator`, and augmented @@ -170,7 +170,7 @@ extends IterableLike[T, Repr] * * @return a parallel iterator */ - protected def parallelIterator: ParallelIterator + protected def parallelIterator: ParIterator /** Creates a new split iterator used to traverse the elements of this collection. * @@ -234,7 +234,7 @@ extends IterableLike[T, Repr] } /* convenience iterator operations wrapper */ - protected implicit def iterator2ops[PI <: ParallelIterator](it: PI) = new { + protected implicit def iterator2ops[PI <: ParIterator](it: PI) = new { def assign(cntx: Signalling): PI = { it.signalDelegate = cntx it @@ -430,7 +430,7 @@ extends IterableLike[T, Repr] override def ++[U >: T, That](that: TraversableOnce[U])(implicit bf: CanBuildFrom[Repr, U, That]): That = { if (that.isParallel && bf.isParallel) { // println("case both are parallel") - val other = that.asParallelIterable + val other = that.asParIterable val pbf = bf.asParallel val copythis = new Copy(() => pbf(repr), parallelIterator) val copythat = wrap { @@ -596,8 +596,8 @@ extends IterableLike[T, Repr] */ protected trait Accessor[R, Tp] extends super.Task[R, Tp] { - val pit: ParallelIterator - def newSubtask(p: ParallelIterator): Accessor[R, Tp] + val pit: ParIterator + def newSubtask(p: ParIterator): Accessor[R, Tp] def shouldSplitFurther = pit.remaining > threshold(size, parallelismLevel) def split = pit.split.map(newSubtask(_)) // default split procedure override def toString = "Accessor(" + pit.toString + ")" @@ -652,152 +652,152 @@ extends IterableLike[T, Repr] protected trait Transformer[R, Tp] extends Accessor[R, Tp] - protected[this] class Foreach[S](op: T => S, val pit: ParallelIterator) extends Accessor[Unit, Foreach[S]] { + protected[this] class Foreach[S](op: T => S, val pit: ParIterator) extends Accessor[Unit, Foreach[S]] { var result: Unit = () def leaf(prevr: Option[Unit]) = pit.foreach(op) - def newSubtask(p: ParallelIterator) = new Foreach[S](op, p) + def newSubtask(p: ParIterator) = new Foreach[S](op, p) } - protected[this] class Count(pred: T => Boolean, val pit: ParallelIterator) extends Accessor[Int, Count] { + protected[this] class Count(pred: T => Boolean, val pit: ParIterator) extends Accessor[Int, Count] { var result: Int = 0 def leaf(prevr: Option[Int]) = result = pit.count(pred) - def newSubtask(p: ParallelIterator) = new Count(pred, p) + def newSubtask(p: ParIterator) = new Count(pred, p) override def merge(that: Count) = result = result + that.result } - protected[this] class Reduce[U >: T](op: (U, U) => U, val pit: ParallelIterator) extends Accessor[U, Reduce[U]] { + protected[this] class Reduce[U >: T](op: (U, U) => U, val pit: ParIterator) extends Accessor[U, Reduce[U]] { var result: U = null.asInstanceOf[U] def leaf(prevr: Option[U]) = result = pit.reduce(op) - def newSubtask(p: ParallelIterator) = new Reduce(op, p) + def newSubtask(p: ParIterator) = new Reduce(op, p) override def merge(that: Reduce[U]) = result = op(result, that.result) } - protected[this] class Fold[U >: T](z: U, op: (U, U) => U, val pit: ParallelIterator) extends Accessor[U, Fold[U]] { + protected[this] class Fold[U >: T](z: U, op: (U, U) => U, val pit: ParIterator) extends Accessor[U, Fold[U]] { var result: U = null.asInstanceOf[U] def leaf(prevr: Option[U]) = result = pit.fold(z)(op) - def newSubtask(p: ParallelIterator) = new Fold(z, op, p) + def newSubtask(p: ParIterator) = new Fold(z, op, p) override def merge(that: Fold[U]) = result = op(result, that.result) } - protected[this] class Aggregate[S](z: S, seqop: (S, T) => S, combop: (S, S) => S, val pit: ParallelIterator) + protected[this] class Aggregate[S](z: S, seqop: (S, T) => S, combop: (S, S) => S, val pit: ParIterator) extends Accessor[S, Aggregate[S]] { var result: S = null.asInstanceOf[S] def leaf(prevr: Option[S]) = result = pit.foldLeft(z)(seqop) - def newSubtask(p: ParallelIterator) = new Aggregate(z, seqop, combop, p) + def newSubtask(p: ParIterator) = new Aggregate(z, seqop, combop, p) override def merge(that: Aggregate[S]) = result = combop(result, that.result) } - protected[this] class Sum[U >: T](num: Numeric[U], val pit: ParallelIterator) extends Accessor[U, Sum[U]] { + protected[this] class Sum[U >: T](num: Numeric[U], val pit: ParIterator) extends Accessor[U, Sum[U]] { var result: U = null.asInstanceOf[U] def leaf(prevr: Option[U]) = result = pit.sum(num) - def newSubtask(p: ParallelIterator) = new Sum(num, p) + def newSubtask(p: ParIterator) = new Sum(num, p) override def merge(that: Sum[U]) = result = num.plus(result, that.result) } - protected[this] class Product[U >: T](num: Numeric[U], val pit: ParallelIterator) extends Accessor[U, Product[U]] { + protected[this] class Product[U >: T](num: Numeric[U], val pit: ParIterator) extends Accessor[U, Product[U]] { var result: U = null.asInstanceOf[U] def leaf(prevr: Option[U]) = result = pit.product(num) - def newSubtask(p: ParallelIterator) = new Product(num, p) + def newSubtask(p: ParIterator) = new Product(num, p) override def merge(that: Product[U]) = result = num.times(result, that.result) } - protected[this] class Min[U >: T](ord: Ordering[U], val pit: ParallelIterator) extends Accessor[U, Min[U]] { + protected[this] class Min[U >: T](ord: Ordering[U], val pit: ParIterator) extends Accessor[U, Min[U]] { var result: U = null.asInstanceOf[U] def leaf(prevr: Option[U]) = result = pit.min(ord) - def newSubtask(p: ParallelIterator) = new Min(ord, p) + def newSubtask(p: ParIterator) = new Min(ord, p) override def merge(that: Min[U]) = result = if (ord.lteq(result, that.result)) result else that.result } - protected[this] class Max[U >: T](ord: Ordering[U], val pit: ParallelIterator) extends Accessor[U, Max[U]] { + protected[this] class Max[U >: T](ord: Ordering[U], val pit: ParIterator) extends Accessor[U, Max[U]] { var result: U = null.asInstanceOf[U] def leaf(prevr: Option[U]) = result = pit.max(ord) - def newSubtask(p: ParallelIterator) = new Max(ord, p) + def newSubtask(p: ParIterator) = new Max(ord, p) override def merge(that: Max[U]) = result = if (ord.gteq(result, that.result)) result else that.result } - protected[this] class Map[S, That](f: T => S, pbf: CanCombineFrom[Repr, S, That], val pit: ParallelIterator) + protected[this] class Map[S, That](f: T => S, pbf: CanCombineFrom[Repr, S, That], val pit: ParIterator) extends Transformer[Combiner[S, That], Map[S, That]] { var result: Combiner[S, That] = null def leaf(prev: Option[Combiner[S, That]]) = result = pit.map2combiner(f, reuse(prev, pbf(self.repr))) - def newSubtask(p: ParallelIterator) = new Map(f, pbf, p) + def newSubtask(p: ParIterator) = new Map(f, pbf, p) override def merge(that: Map[S, That]) = result = result combine that.result } protected[this] class Collect[S, That] - (pf: PartialFunction[T, S], pbf: CanCombineFrom[Repr, S, That], val pit: ParallelIterator) + (pf: PartialFunction[T, S], pbf: CanCombineFrom[Repr, S, That], val pit: ParIterator) extends Transformer[Combiner[S, That], Collect[S, That]] { var result: Combiner[S, That] = null def leaf(prev: Option[Combiner[S, That]]) = result = pit.collect2combiner[S, That](pf, pbf) // TODO - def newSubtask(p: ParallelIterator) = new Collect(pf, pbf, p) + def newSubtask(p: ParIterator) = new Collect(pf, pbf, p) override def merge(that: Collect[S, That]) = result = result combine that.result } - protected[this] class FlatMap[S, That](f: T => Traversable[S], pbf: CanCombineFrom[Repr, S, That], val pit: ParallelIterator) + protected[this] class FlatMap[S, That](f: T => Traversable[S], pbf: CanCombineFrom[Repr, S, That], val pit: ParIterator) extends Transformer[Combiner[S, That], FlatMap[S, That]] { var result: Combiner[S, That] = null def leaf(prev: Option[Combiner[S, That]]) = result = pit.flatmap2combiner(f, pbf) // TODO - def newSubtask(p: ParallelIterator) = new FlatMap(f, pbf, p) + def newSubtask(p: ParIterator) = new FlatMap(f, pbf, p) override def merge(that: FlatMap[S, That]) = result = result combine that.result } - protected[this] class Forall(pred: T => Boolean, val pit: ParallelIterator) extends Accessor[Boolean, Forall] { + protected[this] class Forall(pred: T => Boolean, val pit: ParIterator) extends Accessor[Boolean, Forall] { var result: Boolean = true def leaf(prev: Option[Boolean]) = { if (!pit.isAborted) result = pit.forall(pred); if (result == false) pit.abort } - def newSubtask(p: ParallelIterator) = new Forall(pred, p) + def newSubtask(p: ParIterator) = new Forall(pred, p) override def merge(that: Forall) = result = result && that.result } - protected[this] class Exists(pred: T => Boolean, val pit: ParallelIterator) extends Accessor[Boolean, Exists] { + protected[this] class Exists(pred: T => Boolean, val pit: ParIterator) extends Accessor[Boolean, Exists] { var result: Boolean = false def leaf(prev: Option[Boolean]) = { if (!pit.isAborted) result = pit.exists(pred); if (result == true) pit.abort } - def newSubtask(p: ParallelIterator) = new Exists(pred, p) + def newSubtask(p: ParIterator) = new Exists(pred, p) override def merge(that: Exists) = result = result || that.result } - protected[this] class Find[U >: T](pred: T => Boolean, val pit: ParallelIterator) extends Accessor[Option[U], Find[U]] { + protected[this] class Find[U >: T](pred: T => Boolean, val pit: ParIterator) extends Accessor[Option[U], Find[U]] { var result: Option[U] = None def leaf(prev: Option[Option[U]]) = { if (!pit.isAborted) result = pit.find(pred); if (result != None) pit.abort } - def newSubtask(p: ParallelIterator) = new Find(pred, p) + def newSubtask(p: ParIterator) = new Find(pred, p) override def merge(that: Find[U]) = if (this.result == None) result = that.result } - protected[this] class Filter[U >: T, This >: Repr](pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParallelIterator) + protected[this] class Filter[U >: T, This >: Repr](pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[Combiner[U, This], Filter[U, This]] { var result: Combiner[U, This] = null def leaf(prev: Option[Combiner[U, This]]) = result = pit.filter2combiner(pred, reuse(prev, cbf())) - def newSubtask(p: ParallelIterator) = new Filter(pred, cbf, p) + def newSubtask(p: ParIterator) = new Filter(pred, cbf, p) override def merge(that: Filter[U, This]) = result = result combine that.result } - protected[this] class FilterNot[U >: T, This >: Repr](pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParallelIterator) + protected[this] class FilterNot[U >: T, This >: Repr](pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[Combiner[U, This], FilterNot[U, This]] { var result: Combiner[U, This] = null def leaf(prev: Option[Combiner[U, This]]) = result = pit.filterNot2combiner(pred, reuse(prev, cbf())) - def newSubtask(p: ParallelIterator) = new FilterNot(pred, cbf, p) + def newSubtask(p: ParIterator) = new FilterNot(pred, cbf, p) override def merge(that: FilterNot[U, This]) = result = result combine that.result } - protected class Copy[U >: T, That](cfactory: () => Combiner[U, That], val pit: ParallelIterator) + protected class Copy[U >: T, That](cfactory: () => Combiner[U, That], val pit: ParIterator) extends Transformer[Combiner[U, That], Copy[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, cfactory())) - def newSubtask(p: ParallelIterator) = new Copy[U, That](cfactory, p) + def newSubtask(p: ParIterator) = new Copy[U, That](cfactory, p) override def merge(that: Copy[U, That]) = result = result combine that.result } - protected[this] class Partition[U >: T, This >: Repr](pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParallelIterator) + protected[this] class Partition[U >: T, This >: Repr](pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[(Combiner[U, This], Combiner[U, This]), Partition[U, This]] { var result: (Combiner[U, This], Combiner[U, This]) = null def leaf(prev: Option[(Combiner[U, This], Combiner[U, This])]) = result = pit.partition2combiners(pred, reuse(prev.map(_._1), cbf()), reuse(prev.map(_._2), cbf())) - def newSubtask(p: ParallelIterator) = new Partition(pred, cbf, p) + def newSubtask(p: ParIterator) = new Partition(pred, cbf, p) override def merge(that: Partition[U, This]) = result = (result._1 combine that.result._1, result._2 combine that.result._2) } - protected[this] class Take[U >: T, This >: Repr](n: Int, cbf: () => Combiner[U, This], val pit: ParallelIterator) + protected[this] class Take[U >: T, This >: Repr](n: Int, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[Combiner[U, This], Take[U, This]] { var result: Combiner[U, This] = null def leaf(prev: Option[Combiner[U, This]]) = result = pit.take2combiner(n, reuse(prev, cbf())) - def newSubtask(p: ParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: ParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split val sizes = pits.scanLeft(0)(_ + _.remaining) @@ -809,11 +809,11 @@ extends IterableLike[T, Repr] override def merge(that: Take[U, This]) = result = result combine that.result } - protected[this] class Drop[U >: T, This >: Repr](n: Int, cbf: () => Combiner[U, This], val pit: ParallelIterator) + protected[this] class Drop[U >: T, This >: Repr](n: Int, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[Combiner[U, This], Drop[U, This]] { var result: Combiner[U, This] = null def leaf(prev: Option[Combiner[U, This]]) = result = pit.drop2combiner(n, reuse(prev, cbf())) - def newSubtask(p: ParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: ParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split val sizes = pits.scanLeft(0)(_ + _.remaining) @@ -825,11 +825,11 @@ extends IterableLike[T, Repr] override def merge(that: Drop[U, This]) = result = result combine that.result } - protected[this] class Slice[U >: T, This >: Repr](from: Int, until: Int, cbf: () => Combiner[U, This], val pit: ParallelIterator) + protected[this] class Slice[U >: T, This >: Repr](from: Int, until: Int, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[Combiner[U, This], Slice[U, This]] { var result: Combiner[U, This] = null def leaf(prev: Option[Combiner[U, This]]) = result = pit.slice2combiner(from, until, reuse(prev, cbf())) - def newSubtask(p: ParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: ParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split val sizes = pits.scanLeft(0)(_ + _.remaining) @@ -842,11 +842,11 @@ extends IterableLike[T, Repr] override def merge(that: Slice[U, This]) = result = result combine that.result } - protected[this] class SplitAt[U >: T, This >: Repr](at: Int, cbf: () => Combiner[U, This], val pit: ParallelIterator) + protected[this] class SplitAt[U >: T, This >: Repr](at: Int, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[(Combiner[U, This], Combiner[U, This]), SplitAt[U, This]] { var result: (Combiner[U, This], Combiner[U, This]) = null def leaf(prev: Option[(Combiner[U, This], Combiner[U, This])]) = result = pit.splitAt2combiners(at, reuse(prev.map(_._1), cbf()), reuse(prev.map(_._2), cbf())) - def newSubtask(p: ParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: ParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split val sizes = pits.scanLeft(0)(_ + _.remaining) @@ -856,14 +856,14 @@ extends IterableLike[T, Repr] } protected[this] class TakeWhile[U >: T, This >: Repr] - (pos: Int, pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParallelIterator) + (pos: Int, pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[(Combiner[U, This], Boolean), TakeWhile[U, This]] { var result: (Combiner[U, This], Boolean) = null def leaf(prev: Option[(Combiner[U, This], Boolean)]) = if (pos < pit.indexFlag) { result = pit.takeWhile2combiner(pred, reuse(prev.map(_._1), cbf())) if (!result._2) pit.setIndexFlagIfLesser(pos) } else result = (reuse(prev.map(_._1), cbf()), false) - def newSubtask(p: ParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: ParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split for ((p, untilp) <- pits zip pits.scanLeft(0)(_ + _.remaining)) yield new TakeWhile(pos + untilp, pred, cbf, p) @@ -874,7 +874,7 @@ extends IterableLike[T, Repr] } protected[this] class Span[U >: T, This >: Repr] - (pos: Int, pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParallelIterator) + (pos: Int, pred: T => Boolean, cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[(Combiner[U, This], Combiner[U, This]), Span[U, This]] { var result: (Combiner[U, This], Combiner[U, This]) = null def leaf(prev: Option[(Combiner[U, This], Combiner[U, This])]) = if (pos < pit.indexFlag) { @@ -883,7 +883,7 @@ extends IterableLike[T, Repr] } else { result = (reuse(prev.map(_._2), cbf()), pit.copy2builder[U, This, Combiner[U, This]](reuse(prev.map(_._2), cbf()))) } - def newSubtask(p: ParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: ParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split for ((p, untilp) <- pits zip pits.scanLeft(0)(_ + _.remaining)) yield new Span(pos + untilp, pred, cbf, p) @@ -895,11 +895,11 @@ extends IterableLike[T, Repr] } } - protected[this] class CopyToArray[U >: T, This >: Repr](from: Int, len: Int, array: Array[U], val pit: ParallelIterator) + protected[this] class CopyToArray[U >: T, This >: Repr](from: Int, len: Int, array: Array[U], val pit: ParIterator) extends Accessor[Unit, CopyToArray[U, This]] { var result: Unit = () def leaf(prev: Option[Unit]) = pit.copyToArray(array, from, len) - def newSubtask(p: ParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: ParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split for ((p, untilp) <- pits zip pits.scanLeft(0)(_ + _.remaining); if untilp < len) yield { diff --git a/src/library/scala/collection/parallel/ParallelIterableView.scala b/src/library/scala/collection/parallel/ParIterableView.scala index f40f02eb3b..dd703b5c8b 100644 --- a/src/library/scala/collection/parallel/ParallelIterableView.scala +++ b/src/library/scala/collection/parallel/ParIterableView.scala @@ -17,8 +17,8 @@ import scala.collection.IterableView * * @since 2.8 */ -trait ParallelIterableView[+T, +Coll <: Parallel, +CollSeq] -extends ParallelIterableViewLike[T, Coll, CollSeq, ParallelIterableView[T, Coll, CollSeq], IterableView[T, CollSeq]] +trait ParIterableView[+T, +Coll <: Parallel, +CollSeq] +extends ParIterableViewLike[T, Coll, CollSeq, ParIterableView[T, Coll, CollSeq], IterableView[T, CollSeq]] diff --git a/src/library/scala/collection/parallel/ParallelIterableViewLike.scala b/src/library/scala/collection/parallel/ParIterableViewLike.scala index 024eb48d25..18ae0ae097 100644 --- a/src/library/scala/collection/parallel/ParallelIterableViewLike.scala +++ b/src/library/scala/collection/parallel/ParIterableViewLike.scala @@ -27,24 +27,23 @@ import scala.collection.IterableViewLike * * @since 2.8 */ -trait ParallelIterableViewLike[+T, - +Coll <: Parallel, - +CollSeq, - +This <: ParallelIterableView[T, Coll, CollSeq] with ParallelIterableViewLike[T, Coll, CollSeq, This, ThisSeq], - +ThisSeq <: IterableView[T, CollSeq] with IterableViewLike[T, CollSeq, ThisSeq]] +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 IterableView[T, Coll] with IterableViewLike[T, Coll, This] - with ParallelIterable[T] - with ParallelIterableLike[T, This, ThisSeq] + with ParIterable[T] + with ParIterableLike[T, This, ThisSeq] { self => override protected[this] def newCombiner: Combiner[T, This] = throw new UnsupportedOperationException(this + ".newCombiner"); - //type SCPI = SignalContextPassingIterator[ParallelIterator] // complains when overriden further in inh. hier., TODO check it out - type CPI = SignalContextPassingIterator[ParallelIterator] + type CPI = SignalContextPassingIterator[ParIterator] - trait Transformed[+S] extends ParallelIterableView[S, Coll, CollSeq] with super.Transformed[S] + trait Transformed[+S] extends ParIterableView[S, Coll, CollSeq] with super.Transformed[S] } diff --git a/src/library/scala/collection/parallel/ParMap.scala b/src/library/scala/collection/parallel/ParMap.scala new file mode 100644 index 0000000000..bf6d9ef644 --- /dev/null +++ b/src/library/scala/collection/parallel/ParMap.scala @@ -0,0 +1,71 @@ +package scala.collection.parallel + + + + + +import scala.collection.Map +import scala.collection.mutable.Builder +import scala.collection.generic.ParMapFactory +import scala.collection.generic.GenericParMapTemplate +import scala.collection.generic.GenericParMapCompanion +import scala.collection.generic.CanCombineFrom + + + + + + +trait ParMap[K, +V] +extends Map[K, V] + with GenericParMapTemplate[K, V, ParMap] + with ParIterable[(K, V)] + with ParMapLike[K, V, ParMap[K, V], Map[K, V]] +{ +self => + + def mapCompanion: GenericParMapCompanion[ParMap] = ParMap + + override def empty: ParMap[K, V] = new immutable.ParHashTrie[K, V] + + override def stringPrefix = "ParMap" +} + + + +object ParMap extends ParMapFactory[ParMap] { + def empty[K, V]: ParMap[K, V] = new immutable.ParHashTrie[K, V] + + def newCombiner[K, V]: Combiner[(K, V), ParMap[K, V]] = immutable.HashTrieCombiner[K, V] + + implicit def canBuildFrom[K, V]: CanCombineFrom[Coll, (K, V), ParMap[K, V]] = new CanCombineFromMap[K, V] + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/library/scala/collection/parallel/ParallelMapLike.scala b/src/library/scala/collection/parallel/ParMapLike.scala index 8a0b54525f..252ca2446f 100644 --- a/src/library/scala/collection/parallel/ParallelMapLike.scala +++ b/src/library/scala/collection/parallel/ParMapLike.scala @@ -14,12 +14,12 @@ import scala.collection.mutable.Builder -trait ParallelMapLike[K, - +V, - +Repr <: ParallelMapLike[K, V, Repr, SequentialView] with ParallelMap[K, V], - +SequentialView <: Map[K, V]] +trait ParMapLike[K, + +V, + +Repr <: ParMapLike[K, V, Repr, SequentialView] with ParMap[K, V], + +SequentialView <: Map[K, V]] extends MapLike[K, V, Repr] - with ParallelIterableLike[(K, V), Repr, SequentialView] + with ParIterableLike[(K, V), Repr, SequentialView] { self => protected[this] override def newBuilder: Builder[(K, V), Repr] = newCombiner diff --git a/src/library/scala/collection/parallel/ParSeq.scala b/src/library/scala/collection/parallel/ParSeq.scala new file mode 100644 index 0000000000..970160ec56 --- /dev/null +++ b/src/library/scala/collection/parallel/ParSeq.scala @@ -0,0 +1,64 @@ +package scala.collection.parallel + + + +import scala.collection.generic.GenericCompanion +import scala.collection.generic.GenericParCompanion +import scala.collection.generic.GenericParTemplate +import scala.collection.generic.ParFactory +import scala.collection.generic.CanCombineFrom +import scala.collection.parallel.mutable.ParArrayCombiner +import scala.collection.parallel.mutable.ParArray + + +/** A template trait for parallel sequences. + * + * $parallelseqinfo + * + * $sideeffects + */ +trait ParSeq[+T] extends Seq[T] + with ParIterable[T] + with GenericParTemplate[T, ParSeq] + with ParSeqLike[T, ParSeq[T], Seq[T]] { + override def companion: GenericCompanion[ParSeq] with GenericParCompanion[ParSeq] = ParSeq + + def apply(i: Int): T + + override def toString = super[ParIterable].toString +} + + +object ParSeq extends ParFactory[ParSeq] { + implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParSeq[T]] = new GenericCanCombineFrom[T] + + def newBuilder[T]: Combiner[T, ParSeq[T]] = ParArrayCombiner[T] + + def newCombiner[T]: Combiner[T, ParSeq[T]] = ParArrayCombiner[T] +} + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/library/scala/collection/parallel/ParallelSeqLike.scala b/src/library/scala/collection/parallel/ParSeqLike.scala index 18b0c83f23..dcad5b4094 100644 --- a/src/library/scala/collection/parallel/ParallelSeqLike.scala +++ b/src/library/scala/collection/parallel/ParSeqLike.scala @@ -13,7 +13,7 @@ import scala.collection.generic.VolatileAbort // TODO update docs!! -/** A template trait for sequences of type `ParallelSeq[T]`, representing +/** A template trait for sequences of type `ParSeq[T]`, representing * parallel sequences with element type `T`. * * $parallelseqinfo @@ -36,12 +36,12 @@ import scala.collection.generic.VolatileAbort * @author prokopec * @since 2.8 */ -trait ParallelSeqLike[+T, +Repr <: Parallel, +Sequential <: Seq[T] with SeqLike[T, Sequential]] +trait ParSeqLike[+T, +Repr <: Parallel, +Sequential <: Seq[T] with SeqLike[T, Sequential]] extends scala.collection.SeqLike[T, Repr] - with ParallelIterableLike[T, Repr, Sequential] { - self => + with ParIterableLike[T, Repr, Sequential] { +self => - type SuperParallelIterator = super.ParallelIterator + type SuperParIterator = super.ParIterator /** An iterator that can be split into arbitrary subsets of iterators. * The self-type requirement ensures that the signal context passing behaviour gets mixed in @@ -50,18 +50,18 @@ extends scala.collection.SeqLike[T, Repr] * '''Note:''' In concrete collection classes, collection implementers might want to override the iterator * `reverse2builder` method to ensure higher efficiency. */ - trait ParallelIterator extends ParallelSeqIterator[T, Repr] with super.ParallelIterator { - me: SignalContextPassingIterator[ParallelIterator] => - def split: Seq[ParallelIterator] - def psplit(sizes: Int*): Seq[ParallelIterator] + trait ParIterator extends ParSeqIterator[T, Repr] with super.ParIterator { + me: SignalContextPassingIterator[ParIterator] => + def split: Seq[ParIterator] + def psplit(sizes: Int*): Seq[ParIterator] } /** A stackable modification that ensures signal contexts get passed along the iterators. * A self-type requirement in `ParallelIterator` ensures that this trait gets mixed into * concrete iterators. */ - trait SignalContextPassingIterator[+IterRepr <: ParallelIterator] - extends ParallelIterator with super.SignalContextPassingIterator[IterRepr] { + trait SignalContextPassingIterator[+IterRepr <: ParIterator] + extends ParIterator with super.SignalContextPassingIterator[IterRepr] { // Note: See explanation in `ParallelIterableLike.this.SignalContextPassingIterator` // to understand why we do the cast here, and have a type parameter. // Bottomline: avoiding boilerplate and fighting against inability to override stackable modifications. @@ -74,22 +74,22 @@ extends scala.collection.SeqLike[T, Repr] /** A convenient shorthand for the signal context passing stackable modification. */ - type SCPI <: SignalContextPassingIterator[ParallelIterator] + type SCPI <: SignalContextPassingIterator[ParIterator] /** A more refined version of the iterator found in the `ParallelIterable` trait, * this iterator can be split into arbitrary subsets of iterators. * * @return an iterator that can be split into subsets of precise size */ - protected def parallelIterator: ParallelIterator + protected def parallelIterator: ParIterator override def iterator: PreciseSplitter[T] = parallelIterator override def size = length /** Used to iterate elements using indices */ - protected abstract class Elements(start: Int, val end: Int) extends ParallelIterator with BufferedIterator[T] { - me: SignalContextPassingIterator[ParallelIterator] => + protected abstract class Elements(start: Int, val end: Int) extends ParIterator with BufferedIterator[T] { + me: SignalContextPassingIterator[ParIterator] => private var i = start @@ -110,7 +110,7 @@ extends scala.collection.SeqLike[T, Repr] def psplit(sizes: Int*) = { val incr = sizes.scanLeft(0)(_ + _) for ((from, until) <- incr.init zip incr.tail) yield { - new Elements(start + from, (start + until) min end) with SignalContextPassingIterator[ParallelIterator] + new Elements(start + from, (start + until) min end) with SignalContextPassingIterator[ParIterator] } } @@ -203,7 +203,7 @@ extends scala.collection.SeqLike[T, Repr] * @param offset the starting offset for the search * @return `true` if there is a sequence `that` starting at `offset` in this sequence, `false` otherwise */ - override def startsWith[S](that: Seq[S], offset: Int): Boolean = that ifParallelSeq { pthat => + override def startsWith[S](that: Seq[S], offset: Int): Boolean = that ifParSeq { pthat => if (offset < 0 || offset >= length) offset == length && pthat.length == 0 else if (pthat.length == 0) true else if (pthat.length > length - offset) false @@ -213,7 +213,7 @@ extends scala.collection.SeqLike[T, Repr] } } otherwise super.startsWith(that, offset) - override def sameElements[U >: T](that: Iterable[U]): Boolean = that ifParallelSeq { pthat => + override def sameElements[U >: T](that: Iterable[U]): Boolean = that ifParSeq { pthat => val ctx = new DefaultSignalling with VolatileAbort length == pthat.length && executeAndWaitResult(new SameElements(parallelIterator assign ctx, pthat.parallelIterator)) } otherwise super.sameElements(that) @@ -226,7 +226,7 @@ extends scala.collection.SeqLike[T, Repr] * @param that the sequence to test * @return `true` if this $coll has `that` as a suffix, `false` otherwise */ - override def endsWith[S](that: Seq[S]): Boolean = that ifParallelSeq { pthat => + override def endsWith[S](that: Seq[S]): Boolean = that ifParSeq { pthat => if (that.length == 0) true else if (that.length > length) false else { @@ -237,8 +237,8 @@ extends scala.collection.SeqLike[T, Repr] } otherwise super.endsWith(that) override def patch[U >: T, That](from: Int, patch: Seq[U], replaced: Int) - (implicit bf: CanBuildFrom[Repr, U, That]): That = if (patch.isParallelSeq && bf.isParallel) { - val that = patch.asParallelSeq + (implicit bf: CanBuildFrom[Repr, U, That]): That = if (patch.isParSeq && bf.isParallel) { + val that = patch.asParSeq val pbf = bf.asParallel val realreplaced = replaced min (length - from) val pits = parallelIterator.psplit(from, replaced, length - from - realreplaced) @@ -267,16 +267,22 @@ extends scala.collection.SeqLike[T, Repr] } otherwise super.updated(index, elem) override def +:[U >: T, That](elem: U)(implicit bf: CanBuildFrom[Repr, U, That]): That = { - patch(0, mutable.ParallelArray(elem), 0) + patch(0, mutable.ParArray(elem), 0) } override def :+[U >: T, That](elem: U)(implicit bf: CanBuildFrom[Repr, U, That]): That = { - patch(length, mutable.ParallelArray(elem), 0) + patch(length, mutable.ParArray(elem), 0) } override def padTo[U >: T, That](len: Int, elem: U)(implicit bf: CanBuildFrom[Repr, U, That]): That = if (length < len) { patch(length, new immutable.Repetition(elem, len - length), 0) - } else patch(length, Nil, 0) + } else patch(length, Nil, 0); + + override def zip[U >: T, S, That](that: Iterable[S])(implicit bf: CanBuildFrom[Repr, (U, S), That]): That = if (bf.isParallel && that.isParSeq) { + val pbf = bf.asParallel + val thatseq = that.asParSeq + executeAndWaitResult(new Zip(length min thatseq.length, pbf, parallelIterator, thatseq.parallelIterator) mapResult { _.result }); + } else super.zip(that)(bf) /** Tests whether every element of this $coll relates to the * corresponding element of another parallel sequence by satisfying a test predicate. @@ -290,14 +296,14 @@ extends scala.collection.SeqLike[T, Repr] * `p(x, y)` is `true` for all corresponding elements `x` of this $coll * and `y` of `that`, otherwise `false` */ - override def corresponds[S](that: Seq[S])(p: (T, S) => Boolean): Boolean = that ifParallelSeq { pthat => + override def corresponds[S](that: Seq[S])(p: (T, S) => Boolean): Boolean = that ifParSeq { pthat => val ctx = new DefaultSignalling with VolatileAbort length == pthat.length && executeAndWaitResult(new Corresponds(p, parallelIterator assign ctx, pthat.parallelIterator)) } otherwise super.corresponds(that)(p) override def toString = seq.mkString(stringPrefix + "(", ", ", ")") - override def view = new ParallelSeqView[T, Repr, Sequential] { + override def view = new ParSeqView[T, Repr, Sequential] { protected lazy val underlying = self.repr def length = self.length def apply(idx: Int) = self(idx) @@ -309,15 +315,15 @@ extends scala.collection.SeqLike[T, Repr] /* tasks */ - protected def down(p: SuperParallelIterator) = p.asInstanceOf[ParallelIterator] + protected def down(p: SuperParIterator) = p.asInstanceOf[ParIterator] protected trait Accessor[R, Tp] extends super.Accessor[R, Tp] { - val pit: ParallelIterator + val pit: ParIterator } protected trait Transformer[R, Tp] extends Accessor[R, Tp] with super.Transformer[R, Tp] - protected[this] class SegmentLength(pred: T => Boolean, from: Int, val pit: ParallelIterator) + protected[this] class SegmentLength(pred: T => Boolean, from: Int, val pit: ParIterator) extends Accessor[(Int, Boolean), SegmentLength] { var result: (Int, Boolean) = null def leaf(prev: Option[(Int, Boolean)]) = if (from < pit.indexFlag) { @@ -326,7 +332,7 @@ extends scala.collection.SeqLike[T, Repr] result = (seglen, itsize == seglen) if (!result._2) pit.setIndexFlagIfLesser(from) } else result = (0, false) - def newSubtask(p: SuperParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: SuperParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split for ((p, untilp) <- pits zip pits.scanLeft(0)(_ + _.remaining)) yield new SegmentLength(pred, from + untilp, p) @@ -334,7 +340,7 @@ extends scala.collection.SeqLike[T, Repr] override def merge(that: SegmentLength) = if (result._2) result = (result._1 + that.result._1, that.result._2) } - protected[this] class IndexWhere(pred: T => Boolean, from: Int, val pit: ParallelIterator) + protected[this] class IndexWhere(pred: T => Boolean, from: Int, val pit: ParIterator) extends Accessor[Int, IndexWhere] { var result: Int = -1 def leaf(prev: Option[Int]) = if (from < pit.indexFlag) { @@ -344,7 +350,7 @@ extends scala.collection.SeqLike[T, Repr] pit.setIndexFlagIfLesser(from) } } - def newSubtask(p: SuperParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: SuperParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split for ((p, untilp) <- pits zip pits.scanLeft(from)(_ + _.remaining)) yield new IndexWhere(pred, untilp, p) @@ -354,7 +360,7 @@ extends scala.collection.SeqLike[T, Repr] } } - protected[this] class LastIndexWhere(pred: T => Boolean, pos: Int, val pit: ParallelIterator) + protected[this] class LastIndexWhere(pred: T => Boolean, pos: Int, val pit: ParIterator) extends Accessor[Int, LastIndexWhere] { var result: Int = -1 def leaf(prev: Option[Int]) = if (pos > pit.indexFlag) { @@ -364,7 +370,7 @@ extends scala.collection.SeqLike[T, Repr] pit.setIndexFlagIfGreater(pos) } } - def newSubtask(p: SuperParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: SuperParIterator) = throw new UnsupportedOperationException override def split = { val pits = pit.split for ((p, untilp) <- pits zip pits.scanLeft(pos)(_ + _.remaining)) yield new LastIndexWhere(pred, untilp, p) @@ -374,30 +380,30 @@ extends scala.collection.SeqLike[T, Repr] } } - protected[this] class Reverse[U >: T, This >: Repr](cbf: () => Combiner[U, This], val pit: ParallelIterator) + protected[this] class Reverse[U >: T, This >: Repr](cbf: () => Combiner[U, This], val pit: ParIterator) extends Transformer[Combiner[U, This], Reverse[U, This]] { var result: Combiner[U, This] = null def leaf(prev: Option[Combiner[U, This]]) = result = pit.reverse2combiner(reuse(prev, cbf())) - def newSubtask(p: SuperParallelIterator) = new Reverse(cbf, down(p)) + def newSubtask(p: SuperParIterator) = new Reverse(cbf, down(p)) override def merge(that: Reverse[U, This]) = result = that.result combine result } - protected[this] class ReverseMap[S, That](f: T => S, pbf: CanCombineFrom[Repr, S, That], val pit: ParallelIterator) + protected[this] class ReverseMap[S, That](f: T => S, pbf: CanCombineFrom[Repr, S, That], val pit: ParIterator) extends Transformer[Combiner[S, That], ReverseMap[S, That]] { var result: Combiner[S, That] = null def leaf(prev: Option[Combiner[S, That]]) = result = pit.reverseMap2combiner(f, pbf) // TODO - def newSubtask(p: SuperParallelIterator) = new ReverseMap(f, pbf, down(p)) + def newSubtask(p: SuperParIterator) = new ReverseMap(f, pbf, down(p)) override def merge(that: ReverseMap[S, That]) = result = that.result combine result } - protected[this] class SameElements[U >: T](val pit: ParallelIterator, val otherpit: PreciseSplitter[U]) + protected[this] class SameElements[U >: T](val pit: ParIterator, val otherpit: PreciseSplitter[U]) extends Accessor[Boolean, SameElements[U]] { var result: Boolean = true def leaf(prev: Option[Boolean]) = if (!pit.isAborted) { result = pit.sameElements(otherpit) if (!result) pit.abort } - def newSubtask(p: SuperParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: SuperParIterator) = unsupported override def split = { val fp = pit.remaining / 2 val sp = pit.remaining - fp @@ -406,11 +412,11 @@ extends scala.collection.SeqLike[T, Repr] override def merge(that: SameElements[U]) = result = result && that.result } - protected[this] class Updated[U >: T, That](pos: Int, elem: U, pbf: CanCombineFrom[Repr, U, That], val pit: ParallelIterator) + protected[this] class Updated[U >: T, That](pos: Int, elem: U, pbf: CanCombineFrom[Repr, U, That], val pit: ParIterator) extends Transformer[Combiner[U, That], Updated[U, That]] { var result: Combiner[U, That] = null def leaf(prev: Option[Combiner[U, That]]) = result = pit.updated2combiner(pos, elem, pbf) // TODO - def newSubtask(p: SuperParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: SuperParIterator) = unsupported override def split = { val pits = pit.split for ((p, untilp) <- pits zip pits.scanLeft(0)(_ + _.remaining)) yield new Updated(pos - untilp, elem, pbf, p) @@ -418,14 +424,32 @@ extends scala.collection.SeqLike[T, Repr] override def merge(that: Updated[U, That]) = result = result combine that.result } - protected[this] class Corresponds[S](corr: (T, S) => Boolean, val pit: ParallelIterator, val otherpit: PreciseSplitter[S]) + protected[this] class Zip[U >: T, S, That](len: Int, pbf: CanCombineFrom[Repr, (U, S), That], val pit: ParIterator, val otherpit: PreciseSplitter[S]) + extends Transformer[Combiner[(U, S), That], Zip[U, S, That]] { + var result: Result = null + def leaf(prev: Option[Result]) = result = pit.zip2combiner[U, S, That](otherpit)(pbf) + def newSubtask(p: SuperParIterator) = unsupported + override def split = { + val fp = len / 2 + val sp = len - len / 2 + val pits = pit.psplit(fp, sp) + val opits = otherpit.psplit(fp, sp) + Seq( + new Zip(fp, pbf, pits(0), opits(0)), + new Zip(sp, pbf, pits(1), opits(1)) + ) + } + override def merge(that: Zip[U, S, That]) = result = result combine that.result + } + + protected[this] class Corresponds[S](corr: (T, S) => Boolean, val pit: ParIterator, val otherpit: PreciseSplitter[S]) extends Accessor[Boolean, Corresponds[S]] { var result: Boolean = true def leaf(prev: Option[Boolean]) = if (!pit.isAborted) { result = pit.corresponds(corr)(otherpit) if (!result) pit.abort } - def newSubtask(p: SuperParallelIterator) = throw new UnsupportedOperationException + def newSubtask(p: SuperParIterator) = unsupported override def split = { val fp = pit.remaining / 2 val sp = pit.remaining - fp diff --git a/src/library/scala/collection/parallel/ParallelSeqView.scala b/src/library/scala/collection/parallel/ParSeqView.scala index 7862e99f44..f46be33513 100644 --- a/src/library/scala/collection/parallel/ParallelSeqView.scala +++ b/src/library/scala/collection/parallel/ParSeqView.scala @@ -19,27 +19,27 @@ import scala.collection.generic.CanCombineFrom * * @since 2.8 */ -trait ParallelSeqView[+T, +Coll <: Parallel, +CollSeq] -extends ParallelSeqViewLike[T, Coll, CollSeq, ParallelSeqView[T, Coll, CollSeq], SeqView[T, CollSeq]] +trait ParSeqView[+T, +Coll <: Parallel, +CollSeq] +extends ParSeqViewLike[T, Coll, CollSeq, ParSeqView[T, Coll, CollSeq], SeqView[T, CollSeq]] -object ParallelSeqView { +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("ParallelSeqView.Combiner.result") - def size = throw new UnsupportedOperationException("ParallelSeqView.Combiner.size") + 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("ParallelSeqView.Combiner.result") + throw new UnsupportedOperationException("ParSeqView.Combiner.result") } - type Coll = ParallelSeqView[_, C, _] forSome { type C <: ParallelSeq[_] } + type Coll = ParSeqView[_, C, _] forSome { type C <: ParSeq[_] } - implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParallelSeqView[T, ParallelSeq[T], Seq[T]]] = - new CanCombineFrom[Coll, T, ParallelSeqView[T, ParallelSeq[T], Seq[T]]] { + 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] with EnvironmentPassingCombiner[T, Nothing] def apply() = new NoCombiner[T] with EnvironmentPassingCombiner[T, Nothing] } diff --git a/src/library/scala/collection/parallel/ParallelSeqViewLike.scala b/src/library/scala/collection/parallel/ParSeqViewLike.scala index eab4d7ad5f..6c41b17518 100644 --- a/src/library/scala/collection/parallel/ParallelSeqViewLike.scala +++ b/src/library/scala/collection/parallel/ParSeqViewLike.scala @@ -26,24 +26,24 @@ import scala.collection.generic.CanCombineFrom * * @since 2.8 */ -trait ParallelSeqViewLike[+T, - +Coll <: Parallel, - +CollSeq, - +This <: ParallelSeqView[T, Coll, CollSeq] with ParallelSeqViewLike[T, Coll, CollSeq, This, ThisSeq], - +ThisSeq <: SeqView[T, CollSeq] with SeqViewLike[T, CollSeq, ThisSeq]] +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 SeqView[T, Coll] with SeqViewLike[T, Coll, This] - with ParallelIterableView[T, Coll, CollSeq] - with ParallelIterableViewLike[T, Coll, CollSeq, This, ThisSeq] - with ParallelSeq[T] - with ParallelSeqLike[T, This, ThisSeq] + with ParIterableView[T, Coll, CollSeq] + with ParIterableViewLike[T, Coll, CollSeq, This, ThisSeq] + with ParSeq[T] + with ParSeqLike[T, This, ThisSeq] { self => - type SCPI = SignalContextPassingIterator[ParallelIterator] + type SCPI = SignalContextPassingIterator[ParIterator] - trait Transformed[+S] extends ParallelSeqView[S, Coll, CollSeq] - with super[ParallelIterableView].Transformed[S] with super[SeqView].Transformed[S] { + trait Transformed[+S] extends ParSeqView[S, Coll, CollSeq] + with super[ParIterableView].Transformed[S] with super[SeqView].Transformed[S] { override def parallelIterator = new Elements(0, length) with SCPI {} override def iterator = parallelIterator environment = self.environment @@ -161,11 +161,11 @@ extends SeqView[T, Coll] /* tasks */ - protected[this] class Force[U >: T, That](cbf: CanCombineFrom[Coll, U, That], val pit: ParallelIterator) + protected[this] class Force[U >: T, That](cbf: CanCombineFrom[Coll, U, That], val pit: ParIterator) 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))) - def newSubtask(p: SuperParallelIterator) = new Force(cbf, down(p)) + 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/collection/parallel/ParallelIterable.scala b/src/library/scala/collection/parallel/ParallelIterable.scala deleted file mode 100644 index 4882dc19ee..0000000000 --- a/src/library/scala/collection/parallel/ParallelIterable.scala +++ /dev/null @@ -1,49 +0,0 @@ -package scala.collection.parallel - - -import scala.collection.generic._ -import scala.collection.parallel.mutable.ParallelArrayCombiner -import scala.collection.parallel.mutable.ParallelArray - - -/** A template trait for parallel iterable collections. - * - * $paralleliterableinfo - * - * $sideeffects - * - * @tparam T the element type of the collection - * - * @author prokopec - * @since 2.8 - */ -trait ParallelIterable[+T] extends Iterable[T] - with GenericParallelTemplate[T, ParallelIterable] - with ParallelIterableLike[T, ParallelIterable[T], Iterable[T]] { - override def companion: GenericCompanion[ParallelIterable] with GenericParallelCompanion[ParallelIterable] = ParallelIterable -} - -/** $factoryinfo - */ -object ParallelIterable extends ParallelFactory[ParallelIterable] { - implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParallelIterable[T]] = - new GenericCanCombineFrom[T] - - def newBuilder[T]: Combiner[T, ParallelIterable[T]] = ParallelArrayCombiner[T] - - def newCombiner[T]: Combiner[T, ParallelIterable[T]] = ParallelArrayCombiner[T] -} - - - - - - - - - - - - - - diff --git a/src/library/scala/collection/parallel/ParallelMap.scala b/src/library/scala/collection/parallel/ParallelMap.scala deleted file mode 100644 index 5ce61469bc..0000000000 --- a/src/library/scala/collection/parallel/ParallelMap.scala +++ /dev/null @@ -1,71 +0,0 @@ -package scala.collection.parallel - - - - - -import scala.collection.Map -import scala.collection.mutable.Builder -import scala.collection.generic.ParallelMapFactory -import scala.collection.generic.GenericParallelMapTemplate -import scala.collection.generic.GenericParallelMapCompanion -import scala.collection.generic.CanCombineFrom - - - - - - -trait ParallelMap[K, +V] -extends Map[K, V] - with GenericParallelMapTemplate[K, V, ParallelMap] - with ParallelIterable[(K, V)] - with ParallelMapLike[K, V, ParallelMap[K, V], Map[K, V]] -{ -self => - - def mapCompanion: GenericParallelMapCompanion[ParallelMap] = ParallelMap - - override def empty: ParallelMap[K, V] = new immutable.ParallelHashTrie[K, V] - - override def stringPrefix = "ParallelMap" -} - - - -object ParallelMap extends ParallelMapFactory[ParallelMap] { - def empty[K, V]: ParallelMap[K, V] = new immutable.ParallelHashTrie[K, V] - - def newCombiner[K, V]: Combiner[(K, V), ParallelMap[K, V]] = immutable.HashTrieCombiner[K, V] - - implicit def canBuildFrom[K, V]: CanCombineFrom[Coll, (K, V), ParallelMap[K, V]] = new CanCombineFromMap[K, V] - -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/library/scala/collection/parallel/ParallelSeq.scala b/src/library/scala/collection/parallel/ParallelSeq.scala deleted file mode 100644 index 71b802cd11..0000000000 --- a/src/library/scala/collection/parallel/ParallelSeq.scala +++ /dev/null @@ -1,64 +0,0 @@ -package scala.collection.parallel - - - -import scala.collection.generic.GenericCompanion -import scala.collection.generic.GenericParallelCompanion -import scala.collection.generic.GenericParallelTemplate -import scala.collection.generic.ParallelFactory -import scala.collection.generic.CanCombineFrom -import scala.collection.parallel.mutable.ParallelArrayCombiner -import scala.collection.parallel.mutable.ParallelArray - - -/** A template trait for parallel sequences. - * - * $parallelseqinfo - * - * $sideeffects - */ -trait ParallelSeq[+T] extends Seq[T] - with ParallelIterable[T] - with GenericParallelTemplate[T, ParallelSeq] - with ParallelSeqLike[T, ParallelSeq[T], Seq[T]] { - override def companion: GenericCompanion[ParallelSeq] with GenericParallelCompanion[ParallelSeq] = ParallelSeq - - def apply(i: Int): T - - override def toString = super[ParallelIterable].toString -} - - -object ParallelSeq extends ParallelFactory[ParallelSeq] { - implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParallelSeq[T]] = new GenericCanCombineFrom[T] - - def newBuilder[T]: Combiner[T, ParallelSeq[T]] = ParallelArrayCombiner[T] - - def newCombiner[T]: Combiner[T, ParallelSeq[T]] = ParallelArrayCombiner[T] -} - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/library/scala/collection/parallel/RemainsIterator.scala b/src/library/scala/collection/parallel/RemainsIterator.scala index 6ed6d235d2..bf8ae4a834 100644 --- a/src/library/scala/collection/parallel/RemainsIterator.scala +++ b/src/library/scala/collection/parallel/RemainsIterator.scala @@ -297,17 +297,28 @@ trait AugmentedSeqIterator[+T, +Repr <: Parallel] extends AugmentedIterableItera cb } + /** Iterator `otherpit` must have equal or more elements. + */ + def zip2combiner[U >: T, S, That](otherpit: Iterator[S])(implicit cbf: CanCombineFrom[Repr, (U, S), That]): Combiner[(U, S), That] = { + val cb = cbf(repr) + cb.sizeHint(remaining) + while (hasNext) { + cb += ((next, otherpit.next)) + } + cb + } + } -trait ParallelIterableIterator[+T, +Repr <: Parallel] +trait ParIterableIterator[+T, +Repr <: Parallel] extends AugmentedIterableIterator[T, Repr] with Splitter[T] with Signalling with DelegatedSignalling { - def split: Seq[ParallelIterableIterator[T, Repr]] + def split: Seq[ParIterableIterator[T, Repr]] /** The number of elements this iterator has yet to traverse. This method * doesn't change the state of the iterator. @@ -320,7 +331,7 @@ extends AugmentedIterableIterator[T, Repr] * * In that case, 2 considerations must be taken into account: * - * 1) classes that inherit `ParallelIterable` must reimplement methods `take`, `drop`, `slice`, `splitAt` and `copyToArray`. + * 1) classes that inherit `ParIterable` must reimplement methods `take`, `drop`, `slice`, `splitAt` and `copyToArray`. * * 2) if an iterator provides an upper bound on the number of elements, then after splitting the sum * of `remaining` values of split iterators must be less than or equal to this upper bound. @@ -329,17 +340,17 @@ extends AugmentedIterableIterator[T, Repr] } -trait ParallelSeqIterator[+T, +Repr <: Parallel] -extends ParallelIterableIterator[T, Repr] +trait ParSeqIterator[+T, +Repr <: Parallel] +extends ParIterableIterator[T, Repr] with AugmentedSeqIterator[T, Repr] with PreciseSplitter[T] { - def split: Seq[ParallelSeqIterator[T, Repr]] - def psplit(sizes: Int*): Seq[ParallelSeqIterator[T, Repr]] + def split: Seq[ParSeqIterator[T, Repr]] + def psplit(sizes: Int*): Seq[ParSeqIterator[T, Repr]] /** The number of elements this iterator has yet to traverse. This method * doesn't change the state of the iterator. Unlike the version of this method in the supertrait, - * method `remaining` in `ParallelSeqLike.this.ParallelIterator` must return an exact number + * method `remaining` in `ParSeqLike.this.ParIterator` must return an exact number * of elements remaining in the iterator. * * @return an exact number of elements this iterator has yet to iterate diff --git a/src/library/scala/collection/parallel/Tasks.scala b/src/library/scala/collection/parallel/Tasks.scala index 3ef60f8c7a..bbd894f89b 100644 --- a/src/library/scala/collection/parallel/Tasks.scala +++ b/src/library/scala/collection/parallel/Tasks.scala @@ -35,6 +35,7 @@ trait Tasks { * Finally, it defines the task result of type `U`. */ trait Task[R, +Tp] { + type Result = R def repr = this.asInstanceOf[Tp] /** Code that gets called after the task gets started - it may spawn other tasks instead of calling `leaf`. */ def compute diff --git a/src/library/scala/collection/parallel/immutable/ParallelHashTrie.scala b/src/library/scala/collection/parallel/immutable/ParHashTrie.scala index a9e08913ea..bb157b13dd 100644 --- a/src/library/scala/collection/parallel/immutable/ParallelHashTrie.scala +++ b/src/library/scala/collection/parallel/immutable/ParHashTrie.scala @@ -6,14 +6,14 @@ package scala.collection.parallel.immutable -import scala.collection.parallel.ParallelMap -import scala.collection.parallel.ParallelMapLike +import scala.collection.parallel.ParMap +import scala.collection.parallel.ParMapLike import scala.collection.parallel.Combiner import scala.collection.parallel.EnvironmentPassingCombiner -import scala.collection.generic.ParallelMapFactory +import scala.collection.generic.ParMapFactory import scala.collection.generic.CanCombineFrom -import scala.collection.generic.GenericParallelMapTemplate -import scala.collection.generic.GenericParallelMapCompanion +import scala.collection.generic.GenericParMapTemplate +import scala.collection.generic.GenericParMapCompanion import scala.collection.immutable.HashMap @@ -25,26 +25,26 @@ import scala.collection.immutable.HashMap * * @author prokopec */ -class ParallelHashTrie[K, +V] private[immutable] (private[this] val trie: HashMap[K, V]) -extends ParallelMap[K, V] - with GenericParallelMapTemplate[K, V, ParallelHashTrie] - with ParallelMapLike[K, V, ParallelHashTrie[K, V], HashMap[K, V]] +class ParHashTrie[K, +V] private[immutable] (private[this] val trie: HashMap[K, V]) +extends ParMap[K, V] + with GenericParMapTemplate[K, V, ParHashTrie] + with ParMapLike[K, V, ParHashTrie[K, V], HashMap[K, V]] { self => def this() = this(HashMap.empty[K, V]) - override def mapCompanion: GenericParallelMapCompanion[ParallelHashTrie] = ParallelHashTrie + override def mapCompanion: GenericParMapCompanion[ParHashTrie] = ParHashTrie - override def empty: ParallelHashTrie[K, V] = new ParallelHashTrie[K, V] + override def empty: ParHashTrie[K, V] = new ParHashTrie[K, V] - def parallelIterator = new ParallelHashTrieIterator(trie) with SCPI + def parallelIterator = new ParHashTrieIterator(trie) with SCPI def seq = trie - def -(k: K) = new ParallelHashTrie(trie - k) + def -(k: K) = new ParHashTrie(trie - k) - def +[U >: V](kv: (K, U)) = new ParallelHashTrie(trie + kv) + def +[U >: V](kv: (K, U)) = new ParHashTrie(trie + kv) def get(k: K) = trie.get(k) @@ -55,17 +55,17 @@ self => case None => newc } - type SCPI = SignalContextPassingIterator[ParallelHashTrieIterator] + type SCPI = SignalContextPassingIterator[ParHashTrieIterator] - class ParallelHashTrieIterator(val ht: HashMap[K, V]) - extends super.ParallelIterator { - self: SignalContextPassingIterator[ParallelHashTrieIterator] => + class ParHashTrieIterator(val ht: HashMap[K, V]) + extends super.ParIterator { + self: SignalContextPassingIterator[ParHashTrieIterator] => // println("created iterator " + ht) var i = 0 lazy val triter = ht.iterator - def split: Seq[ParallelIterator] = { - // println("splitting " + ht + " into " + ht.split.map(new ParallelHashTrieIterator(_) with SCPI).map(_.toList)) - ht.split.map(new ParallelHashTrieIterator(_) with SCPI) + def split: Seq[ParIterator] = { + // println("splitting " + ht + " into " + ht.split.map(new ParHashTrieIterator(_) with SCPI).map(_.toList)) + ht.split.map(new ParHashTrieIterator(_) with SCPI) } def next: (K, V) = { // println("taking next after " + i + ", in " + ht) @@ -82,24 +82,24 @@ self => } -object ParallelHashTrie extends ParallelMapFactory[ParallelHashTrie] { - def empty[K, V]: ParallelHashTrie[K, V] = new ParallelHashTrie[K, V] +object ParHashTrie extends ParMapFactory[ParHashTrie] { + def empty[K, V]: ParHashTrie[K, V] = new ParHashTrie[K, V] - def newCombiner[K, V]: Combiner[(K, V), ParallelHashTrie[K, V]] = HashTrieCombiner[K, V] + def newCombiner[K, V]: Combiner[(K, V), ParHashTrie[K, V]] = HashTrieCombiner[K, V] - implicit def canBuildFrom[K, V]: CanCombineFrom[Coll, (K, V), ParallelHashTrie[K, V]] = { + implicit def canBuildFrom[K, V]: CanCombineFrom[Coll, (K, V), ParHashTrie[K, V]] = { new CanCombineFromMap[K, V] } - def fromTrie[K, V](t: HashMap[K, V]) = new ParallelHashTrie(t) + def fromTrie[K, V](t: HashMap[K, V]) = new ParHashTrie(t) var totalcombines = new java.util.concurrent.atomic.AtomicInteger(0) } trait HashTrieCombiner[K, V] -extends Combiner[(K, V), ParallelHashTrie[K, V]] { -self: EnvironmentPassingCombiner[(K, V), ParallelHashTrie[K, V]] => +extends Combiner[(K, V), ParHashTrie[K, V]] { +self: EnvironmentPassingCombiner[(K, V), ParHashTrie[K, V]] => import HashTrieCombiner._ var heads = new Array[Unrolled[K, V]](rootsize) var lasts = new Array[Unrolled[K, V]](rootsize) @@ -124,8 +124,8 @@ self: EnvironmentPassingCombiner[(K, V), ParallelHashTrie[K, V]] => this } - def combine[N <: (K, V), NewTo >: ParallelHashTrie[K, V]](other: Combiner[N, NewTo]): Combiner[N, NewTo] = if (this ne other) { - // ParallelHashTrie.totalcombines.incrementAndGet + def combine[N <: (K, V), NewTo >: ParHashTrie[K, V]](other: Combiner[N, NewTo]): Combiner[N, NewTo] = if (this ne other) { + // ParHashTrie.totalcombines.incrementAndGet if (other.isInstanceOf[HashTrieCombiner[_, _]]) { val that = other.asInstanceOf[HashTrieCombiner[K, V]] var i = 0 @@ -158,11 +158,11 @@ self: EnvironmentPassingCombiner[(K, V), ParallelHashTrie[K, V]] => } val sz = root.foldLeft(0)(_ + _.size) - if (sz == 0) new ParallelHashTrie[K, V] - else if (sz == 1) new ParallelHashTrie[K, V](root(0)) + if (sz == 0) new ParHashTrie[K, V] + else if (sz == 1) new ParHashTrie[K, V](root(0)) else { val trie = new HashMap.HashTrieMap(bitmap, root, sz) - new ParallelHashTrie[K, V](trie) + new ParHashTrie[K, V](trie) } } @@ -209,7 +209,7 @@ self: EnvironmentPassingCombiner[(K, V), ParallelHashTrie[K, V]] => object HashTrieCombiner { - def apply[K, V] = new HashTrieCombiner[K, V] with EnvironmentPassingCombiner[(K, V), ParallelHashTrie[K, V]] {} + def apply[K, V] = new HashTrieCombiner[K, V] with EnvironmentPassingCombiner[(K, V), ParHashTrie[K, V]] {} private[immutable] val rootbits = 5 private[immutable] val rootsize = 1 << 5 diff --git a/src/library/scala/collection/parallel/immutable/ParallelRange.scala b/src/library/scala/collection/parallel/immutable/ParRange.scala index 85a33c7431..df13d41d10 100644 --- a/src/library/scala/collection/parallel/immutable/ParallelRange.scala +++ b/src/library/scala/collection/parallel/immutable/ParRange.scala @@ -4,15 +4,15 @@ package scala.collection.parallel.immutable import scala.collection.immutable.Range import scala.collection.immutable.RangeUtils -import scala.collection.parallel.ParallelSeq +import scala.collection.parallel.ParSeq import scala.collection.parallel.Combiner import scala.collection.generic.CanCombineFrom -class ParallelRange(val start: Int, val end: Int, val step: Int, val inclusive: Boolean) -extends ParallelSeq[Int] - with RangeUtils[ParallelRange] { +class ParRange(val start: Int, val end: Int, val step: Int, val inclusive: Boolean) +extends ParSeq[Int] + with RangeUtils[ParRange] { self => def seq = new Range(start, end, step) @@ -21,31 +21,31 @@ extends ParallelSeq[Int] def apply(idx: Int) = _apply(idx) - def create(_start: Int, _end: Int, _step: Int, _inclusive: Boolean) = new ParallelRange(_start, _end, _step, _inclusive) + def create(_start: Int, _end: Int, _step: Int, _inclusive: Boolean) = new ParRange(_start, _end, _step, _inclusive) - def parallelIterator = new ParallelRangeIterator with SCPI + def parallelIterator = new ParRangeIterator with SCPI override def toString = seq.toString // TODO - type SCPI = SignalContextPassingIterator[ParallelRangeIterator] + type SCPI = SignalContextPassingIterator[ParRangeIterator] - class ParallelRangeIterator + class ParRangeIterator (var start: Int = self.start, val end: Int = self.end, val step: Int = self.step, val inclusive: Boolean = self.inclusive) - extends ParallelIterator with RangeUtils[ParallelRangeIterator] { - me: SignalContextPassingIterator[ParallelRangeIterator] => + extends ParIterator with RangeUtils[ParRangeIterator] { + me: SignalContextPassingIterator[ParRangeIterator] => def remaining = _length def next = { val r = start; start += step; r } def hasNext = remaining > 0 - def split: Seq[ParallelIterator] = psplit(remaining / 2, remaining - remaining / 2) - def psplit(sizes: Int*): Seq[ParallelIterator] = { + def split: Seq[ParIterator] = psplit(remaining / 2, remaining - remaining / 2) + def psplit(sizes: Int*): Seq[ParIterator] = { val incr = sizes.scanLeft(0)(_ + _) for ((from, until) <- incr.init zip incr.tail) yield _slice(from, until) } def create(_start: Int, _end: Int, _step: Int, _inclusive: Boolean) = { - new ParallelRangeIterator(_start, _end, _step, _inclusive) with SCPI + new ParRangeIterator(_start, _end, _step, _inclusive) with SCPI } - override def toString = "ParallelRangeIterator(" + start + ", " + end + ", " + step + ", incl: " + inclusive + ")" + override def toString = "ParRangeIterator(" + start + ", " + end + ", " + step + ", incl: " + inclusive + ")" /* accessors */ diff --git a/src/library/scala/collection/parallel/immutable/ParallelIterable.scala.disabled b/src/library/scala/collection/parallel/immutable/ParallelIterable.scala.disabled index 25a3217258..c1606727c4 100644 --- a/src/library/scala/collection/parallel/immutable/ParallelIterable.scala.disabled +++ b/src/library/scala/collection/parallel/immutable/ParallelIterable.scala.disabled @@ -3,7 +3,7 @@ package scala.collection.parallel.immutable import scala.collection.generic._ -import scala.collection.parallel.ParallelIterableLike +import scala.collection.parallel.ParIterableLike import scala.collection.parallel.Combiner diff --git a/src/library/scala/collection/parallel/immutable/ParallelSeq.scala.disabled b/src/library/scala/collection/parallel/immutable/ParallelSeq.scala.disabled index ddae095f0d..e0e4e2ce54 100644 --- a/src/library/scala/collection/parallel/immutable/ParallelSeq.scala.disabled +++ b/src/library/scala/collection/parallel/immutable/ParallelSeq.scala.disabled @@ -1,12 +1,12 @@ package scala.collection.parallel.immutable -import scala.collection.generic.GenericParallelTemplate +import scala.collection.generic.GenericParTemplate import scala.collection.generic.GenericCompanion -import scala.collection.generic.GenericParallelCompanion +import scala.collection.generic.GenericParCompanion import scala.collection.generic.CanCombineFrom -import scala.collection.generic.ParallelFactory -import scala.collection.parallel.ParallelSeqLike +import scala.collection.generic.ParFactory +import scala.collection.parallel.ParSeqLike import scala.collection.parallel.Combiner diff --git a/src/library/scala/collection/parallel/immutable/package.scala b/src/library/scala/collection/parallel/immutable/package.scala index 054786afaf..6049eaee15 100644 --- a/src/library/scala/collection/parallel/immutable/package.scala +++ b/src/library/scala/collection/parallel/immutable/package.scala @@ -18,28 +18,28 @@ package object immutable { * @param elem the element in the repetition * @param length the length of the collection */ - private[parallel] class Repetition[T](elem: T, val length: Int) extends ParallelSeq[T] { + private[parallel] class Repetition[T](elem: T, val length: Int) extends ParSeq[T] { self => def apply(idx: Int) = if (0 <= idx && idx < length) elem else throw new IndexOutOfBoundsException def seq = throw new UnsupportedOperationException def update(idx: Int, elem: T) = throw new UnsupportedOperationException - type SCPI = SignalContextPassingIterator[ParallelIterator] + type SCPI = SignalContextPassingIterator[ParIterator] - class ParallelIterator(var i: Int = 0, val until: Int = length, elem: T = self.elem) extends super.ParallelIterator { - me: SignalContextPassingIterator[ParallelIterator] => + class ParIterator(var i: Int = 0, val until: Int = length, elem: T = self.elem) extends super.ParIterator { + me: SignalContextPassingIterator[ParIterator] => def remaining = until - i def hasNext = i < until def next = { i += 1; elem } def psplit(sizes: Int*) = { val incr = sizes.scanLeft(0)(_ + _) - for ((start, end) <- incr.init zip incr.tail) yield new ParallelIterator(i + start, (i + end) min until, elem) with SCPI + for ((start, end) <- incr.init zip incr.tail) yield new ParIterator(i + start, (i + end) min until, elem) with SCPI } def split = psplit(remaining / 2, remaining - remaining / 2) } - def parallelIterator = new ParallelIterator with SCPI + def parallelIterator = new ParIterator with SCPI } diff --git a/src/library/scala/collection/parallel/mutable/ParallelArray.scala b/src/library/scala/collection/parallel/mutable/ParArray.scala index c16cc6da15..2443888465 100644 --- a/src/library/scala/collection/parallel/mutable/ParallelArray.scala +++ b/src/library/scala/collection/parallel/mutable/ParArray.scala @@ -2,15 +2,15 @@ package scala.collection.parallel.mutable -import scala.collection.generic.GenericParallelTemplate +import scala.collection.generic.GenericParTemplate import scala.collection.generic.GenericCompanion -import scala.collection.generic.GenericParallelCompanion +import scala.collection.generic.GenericParCompanion import scala.collection.generic.CanCombineFrom import scala.collection.generic.CanBuildFrom -import scala.collection.generic.ParallelFactory +import scala.collection.generic.ParFactory import scala.collection.generic.Sizing import scala.collection.parallel.Combiner -import scala.collection.parallel.ParallelSeqLike +import scala.collection.parallel.ParSeqLike import scala.collection.parallel.CHECK_RATE import scala.collection.mutable.ArraySeq import scala.collection.mutable.Builder @@ -21,12 +21,12 @@ import scala.collection.Sequentializable /** Parallel sequence holding elements in a linear array. * - * `ParallelArray` is a parallel sequence with a predefined size. The size of the array + * `ParArray` is a parallel sequence with a predefined size. The size of the array * cannot be changed after it's been created. * - * `ParallelArray` internally keeps an array containing the elements. This means that + * `ParArray` internally keeps an array containing the elements. This means that * bulk operations based on traversal are fast, but those returning a parallel array as a result - * are slightly slower. The reason for this is that `ParallelArray` uses lazy builders that + * are slightly slower. The reason for this is that `ParArray` uses lazy builders that * create the internal data array only after the size of the array is known. The fragments * are then copied into the resulting data array in parallel using fast array copy operations. * Operations for which the resulting array size is known in advance are optimised to use this @@ -34,19 +34,19 @@ import scala.collection.Sequentializable * * @tparam T type of the elements in the array * - * @define Coll ParallelArray + * @define Coll ParArray * @define coll parallel array */ -class ParallelArray[T] private[mutable] (val arrayseq: ArraySeq[T]) -extends ParallelSeq[T] - with GenericParallelTemplate[T, ParallelArray] - with ParallelSeqLike[T, ParallelArray[T], ArraySeq[T]] +class ParArray[T] private[mutable] (val arrayseq: ArraySeq[T]) +extends ParSeq[T] + with GenericParTemplate[T, ParArray] + with ParSeqLike[T, ParArray[T], ArraySeq[T]] { self => private val array: Array[Any] = arrayseq.array.asInstanceOf[Array[Any]] - override def companion: GenericCompanion[ParallelArray] with GenericParallelCompanion[ParallelArray] = ParallelArray + override def companion: GenericCompanion[ParArray] with GenericParCompanion[ParArray] = ParArray def this(sz: Int) = this { require(sz >= 0) @@ -61,16 +61,16 @@ extends ParallelSeq[T] def seq = arrayseq - type SCPI = SignalContextPassingIterator[ParallelArrayIterator] + type SCPI = SignalContextPassingIterator[ParArrayIterator] - def parallelIterator: ParallelArrayIterator = { - val pit = new ParallelArrayIterator with SCPI + def parallelIterator: ParArrayIterator = { + val pit = new ParArrayIterator with SCPI pit } - class ParallelArrayIterator(var i: Int = 0, val until: Int = length, val arr: Array[Any] = array) - extends super.ParallelIterator { - me: SignalContextPassingIterator[ParallelArrayIterator] => + class ParArrayIterator(var i: Int = 0, val until: Int = length, val arr: Array[Any] = array) + extends super.ParIterator { + me: SignalContextPassingIterator[ParArrayIterator] => def hasNext = i < until @@ -82,7 +82,7 @@ extends ParallelSeq[T] def remaining = until - i - def psplit(sizesIncomplete: Int*): Seq[ParallelIterator] = { + def psplit(sizesIncomplete: Int*): Seq[ParIterator] = { var traversed = i val total = sizesIncomplete.reduceLeft(_ + _) val left = remaining @@ -91,24 +91,24 @@ extends ParallelSeq[T] val start = traversed val end = (traversed + sz) min until traversed = end - new ParallelArrayIterator(start, end, arr) with SCPI + new ParArrayIterator(start, end, arr) with SCPI } else { - new ParallelArrayIterator(traversed, traversed, arr) with SCPI + new ParArrayIterator(traversed, traversed, arr) with SCPI } } - override def split: Seq[ParallelIterator] = { + override def split: Seq[ParIterator] = { val left = remaining if (left >= 2) { val splitpoint = left / 2 - Seq(new ParallelArrayIterator(i, i + splitpoint, arr) with SCPI, - new ParallelArrayIterator(i + splitpoint, until, arr) with SCPI) + Seq(new ParArrayIterator(i, i + splitpoint, arr) with SCPI, + new ParArrayIterator(i + splitpoint, until, arr) with SCPI) } else { Seq(this) } } - override def toString = "ParallelArrayIterator(" + i + ", " + until + ")" + override def toString = "ParArrayIterator(" + i + ", " + until + ")" /* overrides for efficiency */ @@ -279,7 +279,7 @@ extends ParallelSeq[T] return None } - override def drop(n: Int): ParallelArrayIterator = { + override def drop(n: Int): ParArrayIterator = { i += n this } @@ -374,7 +374,7 @@ extends ParallelSeq[T] } } - override def collect2combiner[S, That](pf: PartialFunction[T, S], pbf: CanCombineFrom[ParallelArray[T], S, That]): Combiner[S, That] = { + override def collect2combiner[S, That](pf: PartialFunction[T, S], pbf: CanCombineFrom[ParArray[T], S, That]): Combiner[S, That] = { val cb = pbf(self.repr) collect2combiner_quick(pf, arr, cb, until, i) i = until @@ -390,7 +390,7 @@ extends ParallelSeq[T] } } - override def flatmap2combiner[S, That](f: T => Traversable[S], pbf: CanCombineFrom[ParallelArray[T], S, That]): Combiner[S, That] = { + override def flatmap2combiner[S, That](f: T => Traversable[S], pbf: CanCombineFrom[ParArray[T], S, That]): Combiner[S, That] = { val cb = pbf(self.repr) while (i < until) { val traversable = f(arr(i).asInstanceOf[T]) @@ -401,13 +401,13 @@ extends ParallelSeq[T] cb } - override def filter2combiner[U >: T, This >: ParallelArray[T]](pred: T => Boolean, cb: Combiner[U, This]) = { + override def filter2combiner[U >: T, This >: ParArray[T]](pred: T => Boolean, cb: Combiner[U, This]) = { filter2combiner_quick(pred, cb, arr, until, i) i = until cb } - private def filter2combiner_quick[U >: T, This >: ParallelArray[T]](pred: T => Boolean, cb: Builder[U, This], a: Array[Any], ntil: Int, from: Int) { + private def filter2combiner_quick[U >: T, This >: ParArray[T]](pred: T => Boolean, cb: Builder[U, This], a: Array[Any], ntil: Int, from: Int) { var j = i while(j < ntil) { var curr = a(j).asInstanceOf[T] @@ -416,13 +416,13 @@ extends ParallelSeq[T] } } - override def filterNot2combiner[U >: T, This >: ParallelArray[T]](pred: T => Boolean, cb: Combiner[U, This]) = { + override def filterNot2combiner[U >: T, This >: ParArray[T]](pred: T => Boolean, cb: Combiner[U, This]) = { filterNot2combiner_quick(pred, cb, arr, until, i) i = until cb } - private def filterNot2combiner_quick[U >: T, This >: ParallelArray[T]](pred: T => Boolean, cb: Builder[U, This], a: Array[Any], ntil: Int, from: Int) { + private def filterNot2combiner_quick[U >: T, This >: ParArray[T]](pred: T => Boolean, cb: Builder[U, This], a: Array[Any], ntil: Int, from: Int) { var j = i while(j < ntil) { var curr = a(j).asInstanceOf[T] @@ -433,7 +433,7 @@ extends ParallelSeq[T] override def copy2builder[U >: T, Coll, Bld <: Builder[U, Coll]](cb: Bld): Bld = { cb.sizeHint(remaining) - cb.ifIs[ParallelArrayCombiner[T]] { pac => + cb.ifIs[ParArrayCombiner[T]] { pac => val targetarr: Array[Any] = pac.lastbuff.internalArray.asInstanceOf[Array[Any]] Array.copy(arr, i, targetarr, pac.lastbuff.size, until - i) pac.lastbuff.setInternalSize(remaining) @@ -452,13 +452,13 @@ extends ParallelSeq[T] } } - override def partition2combiners[U >: T, This >: ParallelArray[T]](pred: T => Boolean, btrue: Combiner[U, This], bfalse: Combiner[U, This]) = { + override def partition2combiners[U >: T, This >: ParArray[T]](pred: T => Boolean, btrue: Combiner[U, This], bfalse: Combiner[U, This]) = { partition2combiners_quick(pred, btrue, bfalse, arr, until, i) i = until (btrue, bfalse) } - private def partition2combiners_quick[U >: T, This >: ParallelArray[T]](p: T => Boolean, btrue: Builder[U, This], bfalse: Builder[U, This], a: Array[Any], ntil: Int, from: Int) { + private def partition2combiners_quick[U >: T, This >: ParArray[T]](p: T => Boolean, btrue: Builder[U, This], bfalse: Builder[U, This], a: Array[Any], ntil: Int, from: Int) { var j = from while (j < ntil) { val curr = a(j).asInstanceOf[T] @@ -467,7 +467,7 @@ extends ParallelSeq[T] } } - override def take2combiner[U >: T, This >: ParallelArray[T]](n: Int, cb: Combiner[U, This]) = { + override def take2combiner[U >: T, This >: ParArray[T]](n: Int, cb: Combiner[U, This]) = { cb.sizeHint(n) val ntil = i + n val a = arr @@ -478,7 +478,7 @@ extends ParallelSeq[T] cb } - override def drop2combiner[U >: T, This >: ParallelArray[T]](n: Int, cb: Combiner[U, This]) = { + override def drop2combiner[U >: T, This >: ParArray[T]](n: Int, cb: Combiner[U, This]) = { drop(n) cb.sizeHint(remaining) while (i < until) { @@ -488,8 +488,8 @@ extends ParallelSeq[T] cb } - override def reverse2combiner[U >: T, This >: ParallelArray[T]](cb: Combiner[U, This]): Combiner[U, This] = { - cb.ifIs[ParallelArrayCombiner[T]] { pac => + override def reverse2combiner[U >: T, This >: ParArray[T]](cb: Combiner[U, This]): Combiner[U, This] = { + cb.ifIs[ParArrayCombiner[T]] { pac => val sz = remaining pac.sizeHint(sz) val targetarr: Array[Any] = pac.lastbuff.internalArray.asInstanceOf[Array[Any]] @@ -514,9 +514,9 @@ extends ParallelSeq[T] /* operations */ - private def buildsArray[S, That](c: Builder[S, That]) = c.isInstanceOf[ParallelArrayCombiner[_]] + private def buildsArray[S, That](c: Builder[S, That]) = c.isInstanceOf[ParArrayCombiner[_]] - override def map[S, That](f: T => S)(implicit bf: CanBuildFrom[ParallelArray[T], S, That]) = if (buildsArray(bf(repr))) { + override def map[S, That](f: T => S)(implicit bf: CanBuildFrom[ParArray[T], S, That]) = if (buildsArray(bf(repr))) { // reserve array val targetarr = new Array[Any](length) @@ -524,7 +524,7 @@ extends ParallelSeq[T] executeAndWait(new Map[S](f, targetarr, 0, length)) // wrap it into a parallel array - (new ParallelArray[S](new ExposedArraySeq[S](targetarr.asInstanceOf[Array[AnyRef]], length))).asInstanceOf[That] + (new ParArray[S](new ExposedArraySeq[S](targetarr.asInstanceOf[Array[AnyRef]], length))).asInstanceOf[That] } else super.map(f)(bf) /* tasks */ @@ -554,25 +554,25 @@ extends ParallelSeq[T] -object ParallelArray extends ParallelFactory[ParallelArray] { - implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParallelArray[T]] = new GenericCanCombineFrom[T] - def newBuilder[T]: Combiner[T, ParallelArray[T]] = newCombiner - def newCombiner[T]: Combiner[T, ParallelArray[T]] = ParallelArrayCombiner[T] +object ParArray extends ParFactory[ParArray] { + implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParArray[T]] = new GenericCanCombineFrom[T] + def newBuilder[T]: Combiner[T, ParArray[T]] = newCombiner + def newCombiner[T]: Combiner[T, ParArray[T]] = ParArrayCombiner[T] /** Creates a new parallel array by wrapping the specified array. */ - def handoff[T](arr: Array[T]): ParallelArray[T] = wrapOrRebuild(arr, arr.length) + def handoff[T](arr: Array[T]): ParArray[T] = wrapOrRebuild(arr, arr.length) /** Creates a new parallel array by wrapping a part of the specified array. */ - def handoff[T](arr: Array[T], sz: Int): ParallelArray[T] = wrapOrRebuild(arr, sz) + def handoff[T](arr: Array[T], sz: Int): ParArray[T] = wrapOrRebuild(arr, sz) private def wrapOrRebuild[T](arr: AnyRef, sz: Int) = arr match { - case arr: Array[AnyRef] => new ParallelArray[T](new ExposedArraySeq[T](arr, sz)) - case _ => new ParallelArray[T](new ExposedArraySeq[T](runtime.ScalaRunTime.toObjectArray(arr), sz)) + case arr: Array[AnyRef] => new ParArray[T](new ExposedArraySeq[T](arr, sz)) + case _ => new ParArray[T](new ExposedArraySeq[T](runtime.ScalaRunTime.toObjectArray(arr), sz)) } - def createFromCopy[T <: AnyRef : ClassManifest](arr: Array[T]): ParallelArray[T] = { + def createFromCopy[T <: AnyRef : ClassManifest](arr: Array[T]): ParArray[T] = { val newarr = new Array[T](arr.length) Array.copy(arr, 0, newarr, 0, arr.length) handoff(newarr) diff --git a/src/library/scala/collection/parallel/mutable/ParallelArrayCombiner.scala b/src/library/scala/collection/parallel/mutable/ParArrayCombiner.scala index 2991344be2..4cef521cf8 100644 --- a/src/library/scala/collection/parallel/mutable/ParallelArrayCombiner.scala +++ b/src/library/scala/collection/parallel/mutable/ParArrayCombiner.scala @@ -16,14 +16,14 @@ import scala.collection.parallel.EnvironmentPassingCombiner -trait ParallelArrayCombiner[T] -extends LazyCombiner[T, ParallelArray[T], ExposedArrayBuffer[T]] +trait ParArrayCombiner[T] +extends LazyCombiner[T, ParArray[T], ExposedArrayBuffer[T]] with TaskSupport { - self: EnvironmentPassingCombiner[T, ParallelArray[T]] => + self: EnvironmentPassingCombiner[T, ParArray[T]] => override def sizeHint(sz: Int) = if (chain.length == 1) chain(0).sizeHint(sz) - def newLazyCombiner(c: ArrayBuffer[ExposedArrayBuffer[T]]) = ParallelArrayCombiner(c) + def newLazyCombiner(c: ArrayBuffer[ExposedArrayBuffer[T]]) = ParArrayCombiner(c) def allocateAndCopy = if (chain.size > 1) { val arrayseq = new ArraySeq[T](size) @@ -31,13 +31,13 @@ extends LazyCombiner[T, ParallelArray[T], ExposedArrayBuffer[T]] executeAndWait(new CopyChainToArray(array, 0, size)) - new ParallelArray(arrayseq) + new ParArray(arrayseq) } else { // optimisation if there is only 1 array - val pa = new ParallelArray(new ExposedArraySeq[T](chain(0).internalArray, size)) + val pa = new ParArray(new ExposedArraySeq[T](chain(0).internalArray, size)) pa } - override def toString = "ParallelArrayCombiner(" + size + "): " + chain + override def toString = "ParArrayCombiner(" + size + "): " + chain /* tasks */ @@ -85,11 +85,11 @@ extends LazyCombiner[T, ParallelArray[T], ExposedArrayBuffer[T]] } -object ParallelArrayCombiner { - def apply[T](c: ArrayBuffer[ExposedArrayBuffer[T]]): ParallelArrayCombiner[T] = { - new { val chain = c } with ParallelArrayCombiner[T] with EnvironmentPassingCombiner[T, ParallelArray[T]] +object ParArrayCombiner { + def apply[T](c: ArrayBuffer[ExposedArrayBuffer[T]]): ParArrayCombiner[T] = { + new { val chain = c } with ParArrayCombiner[T] with EnvironmentPassingCombiner[T, ParArray[T]] } - def apply[T]: ParallelArrayCombiner[T] = apply(new ArrayBuffer[ExposedArrayBuffer[T]] += new ExposedArrayBuffer[T]) + def apply[T]: ParArrayCombiner[T] = apply(new ArrayBuffer[ExposedArrayBuffer[T]] += new ExposedArrayBuffer[T]) } diff --git a/src/library/scala/collection/parallel/mutable/ParIterable.scala b/src/library/scala/collection/parallel/mutable/ParIterable.scala new file mode 100644 index 0000000000..5049e3507a --- /dev/null +++ b/src/library/scala/collection/parallel/mutable/ParIterable.scala @@ -0,0 +1,51 @@ +package scala.collection.parallel.mutable + + +import scala.collection.generic._ + +import scala.collection.parallel.ParIterableLike +import scala.collection.parallel.Combiner + + +/** A template trait for parallel iterable collections. + * + * $paralleliterableinfo + * + * $sideeffects + * + * @tparam T the element type of the collection + * + * @author prokopec + * @since 2.8 + */ +trait ParIterable[T] extends collection.mutable.Iterable[T] + with collection.parallel.ParIterable[T] + with GenericParTemplate[T, ParIterable] + with ParIterableLike[T, ParIterable[T], Iterable[T]] { + override def companion: GenericCompanion[ParIterable] with GenericParCompanion[ParIterable] = ParIterable +} + +/** $factoryinfo + */ +object ParIterable extends ParFactory[ParIterable] { + implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParIterable[T]] = + new GenericCanCombineFrom[T] + + def newBuilder[T]: Combiner[T, ParIterable[T]] = ParArrayCombiner[T] + + def newCombiner[T]: Combiner[T, ParIterable[T]] = ParArrayCombiner[T] +} + + + + + + + + + + + + + + diff --git a/src/library/scala/collection/parallel/mutable/ParSeq.scala b/src/library/scala/collection/parallel/mutable/ParSeq.scala new file mode 100644 index 0000000000..a11ebf42d9 --- /dev/null +++ b/src/library/scala/collection/parallel/mutable/ParSeq.scala @@ -0,0 +1,61 @@ +package scala.collection.parallel.mutable + + +import scala.collection.generic.GenericParTemplate +import scala.collection.generic.GenericCompanion +import scala.collection.generic.GenericParCompanion +import scala.collection.generic.CanCombineFrom +import scala.collection.generic.ParFactory +import scala.collection.parallel.ParSeqLike +import scala.collection.parallel.Combiner + + + + + + + +/** A mutable variant of `ParSeq`. + * + * @define Coll mutable.ParSeq + * @define coll mutable parallel sequence + */ +trait ParSeq[T] extends collection.mutable.Seq[T] + with ParIterable[T] + with collection.parallel.ParSeq[T] + with GenericParTemplate[T, ParSeq] + with ParSeqLike[T, ParSeq[T], Seq[T]] { + self => + override def companion: GenericCompanion[ParSeq] with GenericParCompanion[ParSeq] = ParSeq + + def update(i: Int, elem: T): Unit + +} + + +/** $factoryInfo + * @define Coll mutable.ParSeq + * @define coll mutable parallel sequence + */ +object ParSeq extends ParFactory[ParSeq] { + implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParSeq[T]] = new GenericCanCombineFrom[T] + + def newBuilder[T]: Combiner[T, ParSeq[T]] = ParArrayCombiner[T] + + def newCombiner[T]: Combiner[T, ParSeq[T]] = ParArrayCombiner[T] +} + + + + + + + + + + + + + + + diff --git a/src/library/scala/collection/parallel/mutable/ParallelIterable.scala b/src/library/scala/collection/parallel/mutable/ParallelIterable.scala deleted file mode 100644 index bd0a46bc43..0000000000 --- a/src/library/scala/collection/parallel/mutable/ParallelIterable.scala +++ /dev/null @@ -1,51 +0,0 @@ -package scala.collection.parallel.mutable - - -import scala.collection.generic._ - -import scala.collection.parallel.ParallelIterableLike -import scala.collection.parallel.Combiner - - -/** A template trait for parallel iterable collections. - * - * $paralleliterableinfo - * - * $sideeffects - * - * @tparam T the element type of the collection - * - * @author prokopec - * @since 2.8 - */ -trait ParallelIterable[T] extends collection.mutable.Iterable[T] - with collection.parallel.ParallelIterable[T] - with GenericParallelTemplate[T, ParallelIterable] - with ParallelIterableLike[T, ParallelIterable[T], Iterable[T]] { - override def companion: GenericCompanion[ParallelIterable] with GenericParallelCompanion[ParallelIterable] = ParallelIterable -} - -/** $factoryinfo - */ -object ParallelIterable extends ParallelFactory[ParallelIterable] { - implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParallelIterable[T]] = - new GenericCanCombineFrom[T] - - def newBuilder[T]: Combiner[T, ParallelIterable[T]] = ParallelArrayCombiner[T] - - def newCombiner[T]: Combiner[T, ParallelIterable[T]] = ParallelArrayCombiner[T] -} - - - - - - - - - - - - - - diff --git a/src/library/scala/collection/parallel/mutable/ParallelSeq.scala b/src/library/scala/collection/parallel/mutable/ParallelSeq.scala deleted file mode 100644 index 636ba1ac3d..0000000000 --- a/src/library/scala/collection/parallel/mutable/ParallelSeq.scala +++ /dev/null @@ -1,61 +0,0 @@ -package scala.collection.parallel.mutable - - -import scala.collection.generic.GenericParallelTemplate -import scala.collection.generic.GenericCompanion -import scala.collection.generic.GenericParallelCompanion -import scala.collection.generic.CanCombineFrom -import scala.collection.generic.ParallelFactory -import scala.collection.parallel.ParallelSeqLike -import scala.collection.parallel.Combiner - - - - - - - -/** A mutable variant of `ParallelSeq`. - * - * @define Coll mutable.ParallelSeq - * @define coll mutable parallel sequence - */ -trait ParallelSeq[T] extends collection.mutable.Seq[T] - with ParallelIterable[T] - with collection.parallel.ParallelSeq[T] - with GenericParallelTemplate[T, ParallelSeq] - with ParallelSeqLike[T, ParallelSeq[T], Seq[T]] { - self => - override def companion: GenericCompanion[ParallelSeq] with GenericParallelCompanion[ParallelSeq] = ParallelSeq - - def update(i: Int, elem: T): Unit - -} - - -/** $factoryInfo - * @define Coll mutable.ParallelSeq - * @define coll mutable parallel sequence - */ -object ParallelSeq extends ParallelFactory[ParallelSeq] { - implicit def canBuildFrom[T]: CanCombineFrom[Coll, T, ParallelSeq[T]] = new GenericCanCombineFrom[T] - - def newBuilder[T]: Combiner[T, ParallelSeq[T]] = ParallelArrayCombiner[T] - - def newCombiner[T]: Combiner[T, ParallelSeq[T]] = ParallelArrayCombiner[T] -} - - - - - - - - - - - - - - - diff --git a/src/library/scala/collection/parallel/package.scala b/src/library/scala/collection/parallel/package.scala index cddf098966..c0379b819e 100644 --- a/src/library/scala/collection/parallel/package.scala +++ b/src/library/scala/collection/parallel/package.scala @@ -21,6 +21,10 @@ package object parallel { else sz } + def unsupported(msg: String) = throw new UnsupportedOperationException(msg) + + def unsupported = throw new UnsupportedOperationException + /** An implicit conversion providing arrays with a `par` method, which * returns a parallel array. * @@ -28,8 +32,8 @@ package object parallel { * @param array the array to be parallelized * @return a `Parallelizable` object with a `par` method */ - implicit def array2ParallelArray[T <: AnyRef](array: Array[T]) = new Parallelizable[mutable.ParallelArray[T]] { - def par = mutable.ParallelArray.handoff[T](array) + implicit def array2ParArray[T <: AnyRef](array: Array[T]) = new Parallelizable[mutable.ParArray[T]] { + def par = mutable.ParArray.handoff[T](array) } implicit def factory2ops[From, Elem, To](bf: CanBuildFrom[From, Elem, To]) = new { @@ -42,12 +46,12 @@ package object parallel { implicit def traversable2ops[T](t: TraversableOnce[T]) = new { def isParallel = t.isInstanceOf[Parallel] - def isParallelIterable = t.isInstanceOf[ParallelIterable[_]] - def asParallelIterable = t.asInstanceOf[ParallelIterable[T]] - def isParallelSeq = t.isInstanceOf[ParallelSeq[_]] - def asParallelSeq = t.asInstanceOf[ParallelSeq[T]] - def ifParallelSeq[R](isbody: ParallelSeq[T] => R) = new { - def otherwise(notbody: => R) = if (isParallel) isbody(asParallelSeq) else notbody + def isParIterable = t.isInstanceOf[ParIterable[_]] + def asParIterable = t.asInstanceOf[ParIterable[T]] + def isParSeq = t.isInstanceOf[ParSeq[_]] + def asParSeq = t.asInstanceOf[ParSeq[T]] + def ifParSeq[R](isbody: ParSeq[T] => R) = new { + def otherwise(notbody: => R) = if (isParallel) isbody(asParSeq) else notbody } } |