From 9be10bc084e8da77a773d6736c6a232ecd40b0c0 Mon Sep 17 00:00:00 2001 From: Rex Kerr Date: Thu, 13 Feb 2014 16:28:36 -0800 Subject: SI-8072 rationalize public implicits in scala parallel collections Pretty much everything seems like it's intended for internal use, so I moved it to a package-private object. Split toParArray out and put it in an implicit class. Added ability to .toParArray from Array and String also. Added test to verify implicits are gone. --- .../collection/parallel/ParIterableLike.scala | 2 + .../scala/collection/parallel/ParSeqLike.scala | 1 + .../scala/collection/parallel/package.scala | 72 ++++++++++++---------- 3 files changed, 43 insertions(+), 32 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/collection/parallel/ParIterableLike.scala b/src/library/scala/collection/parallel/ParIterableLike.scala index 6ab76bce89..445edd23cb 100644 --- a/src/library/scala/collection/parallel/ParIterableLike.scala +++ b/src/library/scala/collection/parallel/ParIterableLike.scala @@ -29,6 +29,8 @@ import scala.annotation.unchecked.uncheckedVariance import scala.annotation.unchecked.uncheckedStable import scala.language.{ higherKinds, implicitConversions } +import scala.collection.parallel.ParallelCollectionImplicits._ + /** A template trait for parallel collections of type `ParIterable[T]`. * diff --git a/src/library/scala/collection/parallel/ParSeqLike.scala b/src/library/scala/collection/parallel/ParSeqLike.scala index ca21f24534..0b6fec364e 100644 --- a/src/library/scala/collection/parallel/ParSeqLike.scala +++ b/src/library/scala/collection/parallel/ParSeqLike.scala @@ -16,6 +16,7 @@ import scala.collection.generic.CanBuildFrom import scala.collection.generic.CanCombineFrom import scala.collection.generic.VolatileAbort +import scala.collection.parallel.ParallelCollectionImplicits._ /** A template trait for sequences of type `ParSeq[T]`, representing * parallel sequences with element type `T`. diff --git a/src/library/scala/collection/parallel/package.scala b/src/library/scala/collection/parallel/package.scala index 923e21e5a7..91c54fa8f1 100644 --- a/src/library/scala/collection/parallel/package.scala +++ b/src/library/scala/collection/parallel/package.scala @@ -53,43 +53,52 @@ package object parallel { c } - /* implicit conversions */ - - implicit def factory2ops[From, Elem, To](bf: CanBuildFrom[From, Elem, To]) = new FactoryOps[From, Elem, To] { - def isParallel = bf.isInstanceOf[Parallel] - def asParallel = bf.asInstanceOf[CanCombineFrom[From, Elem, To]] - def ifParallel[R](isbody: CanCombineFrom[From, Elem, To] => R) = new Otherwise[R] { - def otherwise(notbody: => R) = if (isParallel) isbody(asParallel) else notbody - } - } - implicit def traversable2ops[T](t: scala.collection.GenTraversableOnce[T]) = new TraversableOps[T] { - def isParallel = t.isInstanceOf[Parallel] - 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 Otherwise[R] { - def otherwise(notbody: => R) = if (isParallel) isbody(asParSeq) else notbody - } - def toParArray = if (t.isInstanceOf[ParArray[_]]) t.asInstanceOf[ParArray[T]] else { - val it = t.toIterator - val cb = mutable.ParArrayCombiner[T]() - while (it.hasNext) cb += it.next - cb.result - } - } - implicit def throwable2ops(self: Throwable) = new ThrowableOps { - def alongWith(that: Throwable) = (self, that) match { - case (self: CompositeThrowable, that: CompositeThrowable) => new CompositeThrowable(self.throwables ++ that.throwables) - case (self: CompositeThrowable, _) => new CompositeThrowable(self.throwables + that) - case (_, that: CompositeThrowable) => new CompositeThrowable(that.throwables + self) - case _ => new CompositeThrowable(Set(self, that)) + /** Adds toParArray method to collection classes. */ + implicit class CollectionsHaveToParArray[C, T](c: C)(implicit asGto: C => scala.collection.GenTraversableOnce[T]) { + def toParArray = { + val t = asGto(c) + if (t.isInstanceOf[ParArray[_]]) t.asInstanceOf[ParArray[T]] + else { + val it = t.toIterator + val cb = mutable.ParArrayCombiner[T]() + while (it.hasNext) cb += it.next + cb.result + } } } } package parallel { + /** Implicit conversions used in the implementation of parallel collections. */ + private[collection] object ParallelCollectionImplicits { + implicit def factory2ops[From, Elem, To](bf: CanBuildFrom[From, Elem, To]) = new FactoryOps[From, Elem, To] { + def isParallel = bf.isInstanceOf[Parallel] + def asParallel = bf.asInstanceOf[CanCombineFrom[From, Elem, To]] + def ifParallel[R](isbody: CanCombineFrom[From, Elem, To] => R) = new Otherwise[R] { + def otherwise(notbody: => R) = if (isParallel) isbody(asParallel) else notbody + } + } + implicit def traversable2ops[T](t: scala.collection.GenTraversableOnce[T]) = new TraversableOps[T] { + def isParallel = t.isInstanceOf[Parallel] + 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 Otherwise[R] { + def otherwise(notbody: => R) = if (isParallel) isbody(asParSeq) else notbody + } + } + implicit def throwable2ops(self: Throwable) = new ThrowableOps { + def alongWith(that: Throwable) = (self, that) match { + case (self: CompositeThrowable, that: CompositeThrowable) => new CompositeThrowable(self.throwables ++ that.throwables) + case (self: CompositeThrowable, _) => new CompositeThrowable(self.throwables + that) + case (_, that: CompositeThrowable) => new CompositeThrowable(that.throwables + self) + case _ => new CompositeThrowable(Set(self, that)) + } + } + } + trait FactoryOps[From, Elem, To] { trait Otherwise[R] { def otherwise(notbody: => R): R @@ -111,7 +120,6 @@ package parallel { def isParSeq: Boolean def asParSeq: ParSeq[T] def ifParSeq[R](isbody: ParSeq[T] => R): Otherwise[R] - def toParArray: ParArray[T] } @deprecated("This trait will be removed.", "2.11.0") -- cgit v1.2.3