Skip to content

Commit

Permalink
Minimal changes to get ParIterable and ParVector working
Browse files Browse the repository at this point in the history
Other parallel collections have been commented out.
Old code from scala.collection.generic package has been imported
from the 2.12.x branch.
  • Loading branch information
julienrf committed Aug 13, 2018
1 parent c3c1230 commit 30d4dcd
Show file tree
Hide file tree
Showing 49 changed files with 4,651 additions and 4,225 deletions.
17 changes: 17 additions & 0 deletions core/src/main/scala/scala/collection/Parallel.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */

package scala
package collection

/** A marker trait for collections which have their operations parallelised.
*
* @since 2.9
* @author Aleksandar Prokopec
*/
trait Parallel
2 changes: 1 addition & 1 deletion core/src/main/scala/scala/collection/Parallelizable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import parallel.Combiner
*/
trait Parallelizable[+A, +ParRepr <: Parallel] extends Any {

def seq: TraversableOnce[A]
def seq: IterableOnce[A]

/** Returns a parallel implementation of this collection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import scala.collection.parallel._
* @tparam To the type of the collection to be created.
* @since 2.8
*/
trait CanCombineFrom[-From, -Elem, +To] extends CanBuildFrom[From, Elem, To] with Parallel {
trait CanCombineFrom[-From, -Elem, +To] extends OldCanBuildFrom[From, Elem, To] with Parallel {
def apply(from: From): Combiner[Elem, To]
def apply(): Combiner[Elem, To]
}

// TODO Remove
trait OldCanBuildFrom[-From, -Elem, +To] {
def apply(from: From): collection.mutable.Builder[Elem, To]
def apply(): collection.mutable.Builder[Elem, To]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package generic

import scala.collection.parallel.Combiner
import scala.collection.parallel.ParIterable
import scala.collection.parallel.ParMap
//import scala.collection.parallel.ParMap
import scala.language.higherKinds

/** A template class for companion objects of parallel collection classes.
Expand All @@ -32,7 +32,7 @@ trait GenericParCompanion[+CC[X] <: ParIterable[X]] {
def newCombiner[A]: Combiner[A, CC[A]]
}

trait GenericParMapCompanion[+CC[P, Q] <: ParMap[P, Q]] {
def newCombiner[P, Q]: Combiner[(P, Q), CC[P, Q]]
}
//trait GenericParMapCompanion[+CC[P, Q] <: ParMap[P, Q]] {
// def newCombiner[P, Q]: Combiner[(P, Q), CC[P, Q]]
//}

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package generic

import scala.collection.parallel.Combiner
import scala.collection.parallel.ParIterable
import scala.collection.parallel.ParMap
//import scala.collection.parallel.ParMap

import scala.annotation.unchecked.uncheckedVariance
import scala.language.higherKinds
Expand All @@ -25,10 +25,10 @@ import scala.language.higherKinds
* @since 2.8
*/
trait GenericParTemplate[+A, +CC[X] <: ParIterable[X]]
extends GenericTraversableTemplate[A, CC]
with HasNewCombiner[A, CC[A] @uncheckedVariance]
extends GenericTraversableTemplate[A, CC]
with HasNewCombiner[A, CC[A] @uncheckedVariance]
{
def companion: GenericCompanion[CC] with GenericParCompanion[CC]
def companion: /*GenericCompanion[CC] with*/ GenericParCompanion[CC]

protected[this] override def newBuilder: scala.collection.mutable.Builder[A, CC[A]] = newCombiner

Expand All @@ -47,18 +47,18 @@ extends GenericTraversableTemplate[A, CC]
}


trait GenericParMapTemplate[K, +V, +CC[X, Y] <: ParMap[X, Y]] extends GenericParTemplate[(K, V), ParIterable]
{
protected[this] override def newCombiner: Combiner[(K, V), CC[K, V]] = {
val cb = mapCompanion.newCombiner[K, V]
cb
}

def mapCompanion: GenericParMapCompanion[CC]

def genericMapCombiner[P, Q]: Combiner[(P, Q), CC[P, Q]] = {
val cb = mapCompanion.newCombiner[P, Q]
cb
}
}
//trait GenericParMapTemplate[K, +V, +CC[X, Y] <: ParMap[X, Y]] extends GenericParTemplate[(K, V), ParIterable]
//{
// protected[this] override def newCombiner: Combiner[(K, V), CC[K, V]] = {
// val cb = mapCompanion.newCombiner[K, V]
// cb
// }
//
// def mapCompanion: GenericParMapCompanion[CC]
//
// def genericMapCombiner[P, Q]: Combiner[(P, Q), CC[P, Q]] = {
// val cb = mapCompanion.newCombiner[P, Q]
// cb
// }
//}

Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package scala.collection.generic

import scala.language.higherKinds
import scala.annotation.migration
import scala.annotation.unchecked.uncheckedVariance
//import scala.collection.GenTraversableOnce
import scala.collection.mutable.Builder
import scala.collection.parallel.ParIterable

// TODO inline in GenericParTempalte
trait GenericTraversableTemplate[+A, +CC[X] /*<: GenTraversable[X]*/] extends HasNewBuilder[A, CC[A] @uncheckedVariance] {

def seq: Iterable[A]

/** Applies a function `f` to all elements of this $coll.
*
* @param f the function that is applied for its side-effect to every element.
* The result of function `f` is discarded.
*
* @tparam U the type parameter describing the result of function `f`.
* This result will always be ignored. Typically `U` is `Unit`,
* but this is not necessary.
*
* @usecase def foreach(f: A => Unit): Unit
*/
def foreach[U](f: A => U): Unit

/** Selects the first element of this $coll.
*
* @return the first element of this $coll.
* @throws NoSuchElementException if the $coll is empty.
*/
def head: A

/** Tests whether this $coll is empty.
*
* @return `true` if the $coll contain no elements, `false` otherwise.
*/
def isEmpty: Boolean

/** The factory companion object that builds instances of class $Coll.
* (or its `Iterable` superclass where class $Coll is not a `Seq`.)
*/
// def companion: GenericCompanion[CC]

/** The builder that builds instances of type $Coll[A]
*/
protected[this] def newBuilder: Builder[A, CC[A]]/* = companion.newBuilder[A]*/

/** The generic builder that builds instances of $Coll
* at arbitrary element types.
*/
def genericBuilder[B]: Builder[B, CC[B]]/* = companion.newBuilder[B]*/

private def sequential: IterableOnce[A] = this.asInstanceOf[ParIterable[A]].seq /*this.asInstanceOf[GenTraversableOnce[A]].seq*/

/** Converts this $coll of pairs into two collections of the first and second
* half of each pair.
*
* {{{
* val xs = $Coll(
* (1, "one"),
* (2, "two"),
* (3, "three")).unzip
* // xs == ($Coll(1, 2, 3),
* // $Coll(one, two, three))
* }}}
*
* @tparam A1 the type of the first half of the element pairs
* @tparam A2 the type of the second half of the element pairs
* @param asPair an implicit conversion which asserts that the element type
* of this $coll is a pair.
* @return a pair of ${coll}s, containing the first, respectively second
* half of each element pair of this $coll.
*/
def unzip[A1, A2](implicit asPair: A => (A1, A2)): (CC[A1], CC[A2]) = {
val b1 = genericBuilder[A1]
val b2 = genericBuilder[A2]
for (xy <- sequential.iterator) {
val (x, y) = asPair(xy)
b1 += x
b2 += y
}
(b1.result(), b2.result())
}

/** Converts this $coll of triples into three collections of the first, second,
* and third element of each triple.
*
* {{{
* val xs = $Coll(
* (1, "one", '1'),
* (2, "two", '2'),
* (3, "three", '3')).unzip3
* // xs == ($Coll(1, 2, 3),
* // $Coll(one, two, three),
* // $Coll(1, 2, 3))
* }}}
*
* @tparam A1 the type of the first member of the element triples
* @tparam A2 the type of the second member of the element triples
* @tparam A3 the type of the third member of the element triples
* @param asTriple an implicit conversion which asserts that the element type
* of this $coll is a triple.
* @return a triple of ${coll}s, containing the first, second, respectively
* third member of each element triple of this $coll.
*/
def unzip3[A1, A2, A3](implicit asTriple: A => (A1, A2, A3)): (CC[A1], CC[A2], CC[A3]) = {
val b1 = genericBuilder[A1]
val b2 = genericBuilder[A2]
val b3 = genericBuilder[A3]

for (xyz <- sequential.iterator) {
val (x, y, z) = asTriple(xyz)
b1 += x
b2 += y
b3 += z
}
(b1.result(), b2.result(), b3.result())
}

/** Converts this $coll of traversable collections into
* a $coll formed by the elements of these traversable
* collections.
*
* @tparam B the type of the elements of each traversable collection.
* @param asTraversable an implicit conversion which asserts that the element
* type of this $coll is a `GenTraversable`.
* @return a new $coll resulting from concatenating all element ${coll}s.
*
* @usecase def flatten[B]: $Coll[B]
*
* @inheritdoc
*
* The resulting collection's type will be guided by the
* static type of $coll. For example:
*
* {{{
* val xs = List(
* Set(1, 2, 3),
* Set(1, 2, 3)
* ).flatten
* // xs == List(1, 2, 3, 1, 2, 3)
*
* val ys = Set(
* List(1, 2, 3),
* List(3, 2, 1)
* ).flatten
* // ys == Set(1, 2, 3)
* }}}
*/
def flatten[B](implicit asTraversable: A => /*<:<!!!*/ IterableOnce[B]): CC[B] = {
val b = genericBuilder[B]
for (xs <- sequential.iterator)
b ++= asTraversable(xs)/*.seq*/
b.result()
}

/** Transposes this $coll of traversable collections into
* a $coll of ${coll}s.
*
* The resulting collection's type will be guided by the
* static type of $coll. For example:
*
* {{{
* val xs = List(
* Set(1, 2, 3),
* Set(4, 5, 6)).transpose
* // xs == List(
* // List(1, 4),
* // List(2, 5),
* // List(3, 6))
*
* val ys = Vector(
* List(1, 2, 3),
* List(4, 5, 6)).transpose
* // ys == Vector(
* // Vector(1, 4),
* // Vector(2, 5),
* // Vector(3, 6))
* }}}
*
* @tparam B the type of the elements of each traversable collection.
* @param asTraversable an implicit conversion which asserts that the
* element type of this $coll is a `Traversable`.
* @return a two-dimensional $coll of ${coll}s which has as ''n''th row
* the ''n''th column of this $coll.
* @throws IllegalArgumentException if all collections in this $coll
* are not of the same size.
*/
@migration("`transpose` throws an `IllegalArgumentException` if collections are not uniformly sized.", "2.9.0")
def transpose[B](implicit asTraversable: A => /*<:<!!!*/ IterableOnce[B]): CC[CC[B] @uncheckedVariance] = {
if (isEmpty)
return genericBuilder[CC[B]].result()

def fail = throw new IllegalArgumentException("transpose requires all collections have the same size")

val headSize = asTraversable(head).iterator.size
val bs: IndexedSeq[collection.mutable.Builder[B, CC[B]]] = IndexedSeq.fill(headSize)(genericBuilder[B])
for (xs <- sequential.iterator) {
var i = 0
for (x <- asTraversable(xs).iterator/*.seq*/) {
if (i >= headSize) fail
bs(i) += x
i += 1
}
if (i != headSize)
fail
}
val bb = genericBuilder[CC[B]]
for (b <- bs) bb += b.result
bb.result()
}
}
17 changes: 17 additions & 0 deletions core/src/main/scala/scala/collection/generic/HasNewBuilder.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala
package collection
package generic

import mutable.Builder

trait HasNewBuilder[+A, +Repr] extends Any {
/** The builder that builds instances of Repr */
protected[this] def newBuilder: Builder[A, Repr]
}
8 changes: 4 additions & 4 deletions core/src/main/scala/scala/collection/generic/ParFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ import scala.language.higherKinds
* @since 2.8
*/
abstract class ParFactory[CC[X] <: ParIterable[X] with GenericParTemplate[X, CC]]
extends GenTraversableFactory[CC]
with GenericParCompanion[CC] {
extends /*GenTraversableFactory[CC]
with*/ GenericParCompanion[CC] {

//type EPC[T, C] = scala.collection.parallel.EnvironmentPassingCombiner[T, C]

/*
/** A generic implementation of the `CanCombineFrom` trait, which forwards
* all calls to `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]] {
override def apply(from: Coll) = from.genericCombiner
override def apply() = newBuilder[A]
}
}*/
}
Loading

0 comments on commit 30d4dcd

Please sign in to comment.