Skip to content

Commit 1a5cb26

Browse files
committed
Minimal changes to get ParIterable and ParVector working
Other parallel collections have been commented out. Old code from scala.collection.generic package has been imported from the 2.12.x branch.
1 parent c3c1230 commit 1a5cb26

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4650
-4227
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
9+
package scala
10+
package collection
11+
12+
/** A marker trait for collections which have their operations parallelised.
13+
*
14+
* @since 2.9
15+
* @author Aleksandar Prokopec
16+
*/
17+
trait Parallel

core/src/main/scala/scala/collection/Parallelizable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import parallel.Combiner
2020
*/
2121
trait Parallelizable[+A, +ParRepr <: Parallel] extends Any {
2222

23-
def seq: TraversableOnce[A]
23+
def seq: IterableOnce[A]
2424

2525
/** Returns a parallel implementation of this collection.
2626
*

core/src/main/scala/scala/collection/generic/CanCombineFrom.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ import scala.collection.parallel._
2020
* @tparam To the type of the collection to be created.
2121
* @since 2.8
2222
*/
23-
trait CanCombineFrom[-From, -Elem, +To] extends CanBuildFrom[From, Elem, To] with Parallel {
23+
trait CanCombineFrom[-From, -Elem, +To] extends OldCanBuildFrom[From, Elem, To] with Parallel {
2424
def apply(from: From): Combiner[Elem, To]
2525
def apply(): Combiner[Elem, To]
2626
}
2727

28+
// TODO Remove
29+
trait OldCanBuildFrom[-From, -Elem, +To] {
30+
def apply(from: From): collection.mutable.Builder[Elem, To]
31+
def apply(): collection.mutable.Builder[Elem, To]
32+
}

core/src/main/scala/scala/collection/generic/GenericParCompanion.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package generic
1212

1313
import scala.collection.parallel.Combiner
1414
import scala.collection.parallel.ParIterable
15-
import scala.collection.parallel.ParMap
15+
//import scala.collection.parallel.ParMap
1616
import scala.language.higherKinds
1717

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

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

core/src/main/scala/scala/collection/generic/GenericParTemplate.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package generic
1212

1313
import scala.collection.parallel.Combiner
1414
import scala.collection.parallel.ParIterable
15-
import scala.collection.parallel.ParMap
15+
//import scala.collection.parallel.ParMap
1616

1717
import scala.annotation.unchecked.uncheckedVariance
1818
import scala.language.higherKinds
@@ -25,10 +25,10 @@ import scala.language.higherKinds
2525
* @since 2.8
2626
*/
2727
trait GenericParTemplate[+A, +CC[X] <: ParIterable[X]]
28-
extends GenericTraversableTemplate[A, CC]
29-
with HasNewCombiner[A, CC[A] @uncheckedVariance]
28+
extends GenericTraversableTemplate[A, CC]
29+
with HasNewCombiner[A, CC[A] @uncheckedVariance]
3030
{
31-
def companion: GenericCompanion[CC] with GenericParCompanion[CC]
31+
def companion: /*GenericCompanion[CC] with*/ GenericParCompanion[CC]
3232

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

@@ -47,18 +47,18 @@ extends GenericTraversableTemplate[A, CC]
4747
}
4848

4949

50-
trait GenericParMapTemplate[K, +V, +CC[X, Y] <: ParMap[X, Y]] extends GenericParTemplate[(K, V), ParIterable]
51-
{
52-
protected[this] override def newCombiner: Combiner[(K, V), CC[K, V]] = {
53-
val cb = mapCompanion.newCombiner[K, V]
54-
cb
55-
}
56-
57-
def mapCompanion: GenericParMapCompanion[CC]
58-
59-
def genericMapCombiner[P, Q]: Combiner[(P, Q), CC[P, Q]] = {
60-
val cb = mapCompanion.newCombiner[P, Q]
61-
cb
62-
}
63-
}
50+
//trait GenericParMapTemplate[K, +V, +CC[X, Y] <: ParMap[X, Y]] extends GenericParTemplate[(K, V), ParIterable]
51+
//{
52+
// protected[this] override def newCombiner: Combiner[(K, V), CC[K, V]] = {
53+
// val cb = mapCompanion.newCombiner[K, V]
54+
// cb
55+
// }
56+
//
57+
// def mapCompanion: GenericParMapCompanion[CC]
58+
//
59+
// def genericMapCombiner[P, Q]: Combiner[(P, Q), CC[P, Q]] = {
60+
// val cb = mapCompanion.newCombiner[P, Q]
61+
// cb
62+
// }
63+
//}
6464

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
package scala.collection.generic
2+
3+
import scala.language.higherKinds
4+
import scala.annotation.migration
5+
import scala.annotation.unchecked.uncheckedVariance
6+
//import scala.collection.GenTraversableOnce
7+
import scala.collection.mutable.Builder
8+
import scala.collection.parallel.ParIterable
9+
10+
// TODO inline in GenericParTempalte
11+
trait GenericTraversableTemplate[+A, +CC[X] /*<: GenTraversable[X]*/] extends HasNewBuilder[A, CC[A] @uncheckedVariance] {
12+
13+
def seq: Iterable[A]
14+
15+
/** Applies a function `f` to all elements of this $coll.
16+
*
17+
* @param f the function that is applied for its side-effect to every element.
18+
* The result of function `f` is discarded.
19+
*
20+
* @tparam U the type parameter describing the result of function `f`.
21+
* This result will always be ignored. Typically `U` is `Unit`,
22+
* but this is not necessary.
23+
*
24+
* @usecase def foreach(f: A => Unit): Unit
25+
*/
26+
def foreach[U](f: A => U): Unit
27+
28+
/** Selects the first element of this $coll.
29+
*
30+
* @return the first element of this $coll.
31+
* @throws NoSuchElementException if the $coll is empty.
32+
*/
33+
def head: A
34+
35+
/** Tests whether this $coll is empty.
36+
*
37+
* @return `true` if the $coll contain no elements, `false` otherwise.
38+
*/
39+
def isEmpty: Boolean
40+
41+
/** The factory companion object that builds instances of class $Coll.
42+
* (or its `Iterable` superclass where class $Coll is not a `Seq`.)
43+
*/
44+
// def companion: GenericCompanion[CC]
45+
46+
/** The builder that builds instances of type $Coll[A]
47+
*/
48+
protected[this] def newBuilder: Builder[A, CC[A]]/* = companion.newBuilder[A]*/
49+
50+
/** The generic builder that builds instances of $Coll
51+
* at arbitrary element types.
52+
*/
53+
def genericBuilder[B]: Builder[B, CC[B]]/* = companion.newBuilder[B]*/
54+
55+
private def sequential: IterableOnce[A] = this.asInstanceOf[ParIterable[A]].seq /*this.asInstanceOf[GenTraversableOnce[A]].seq*/
56+
57+
/** Converts this $coll of pairs into two collections of the first and second
58+
* half of each pair.
59+
*
60+
* {{{
61+
* val xs = $Coll(
62+
* (1, "one"),
63+
* (2, "two"),
64+
* (3, "three")).unzip
65+
* // xs == ($Coll(1, 2, 3),
66+
* // $Coll(one, two, three))
67+
* }}}
68+
*
69+
* @tparam A1 the type of the first half of the element pairs
70+
* @tparam A2 the type of the second half of the element pairs
71+
* @param asPair an implicit conversion which asserts that the element type
72+
* of this $coll is a pair.
73+
* @return a pair of ${coll}s, containing the first, respectively second
74+
* half of each element pair of this $coll.
75+
*/
76+
def unzip[A1, A2](implicit asPair: A => (A1, A2)): (CC[A1], CC[A2]) = {
77+
val b1 = genericBuilder[A1]
78+
val b2 = genericBuilder[A2]
79+
for (xy <- sequential.iterator) {
80+
val (x, y) = asPair(xy)
81+
b1 += x
82+
b2 += y
83+
}
84+
(b1.result(), b2.result())
85+
}
86+
87+
/** Converts this $coll of triples into three collections of the first, second,
88+
* and third element of each triple.
89+
*
90+
* {{{
91+
* val xs = $Coll(
92+
* (1, "one", '1'),
93+
* (2, "two", '2'),
94+
* (3, "three", '3')).unzip3
95+
* // xs == ($Coll(1, 2, 3),
96+
* // $Coll(one, two, three),
97+
* // $Coll(1, 2, 3))
98+
* }}}
99+
*
100+
* @tparam A1 the type of the first member of the element triples
101+
* @tparam A2 the type of the second member of the element triples
102+
* @tparam A3 the type of the third member of the element triples
103+
* @param asTriple an implicit conversion which asserts that the element type
104+
* of this $coll is a triple.
105+
* @return a triple of ${coll}s, containing the first, second, respectively
106+
* third member of each element triple of this $coll.
107+
*/
108+
def unzip3[A1, A2, A3](implicit asTriple: A => (A1, A2, A3)): (CC[A1], CC[A2], CC[A3]) = {
109+
val b1 = genericBuilder[A1]
110+
val b2 = genericBuilder[A2]
111+
val b3 = genericBuilder[A3]
112+
113+
for (xyz <- sequential.iterator) {
114+
val (x, y, z) = asTriple(xyz)
115+
b1 += x
116+
b2 += y
117+
b3 += z
118+
}
119+
(b1.result(), b2.result(), b3.result())
120+
}
121+
122+
/** Converts this $coll of traversable collections into
123+
* a $coll formed by the elements of these traversable
124+
* collections.
125+
*
126+
* @tparam B the type of the elements of each traversable collection.
127+
* @param asTraversable an implicit conversion which asserts that the element
128+
* type of this $coll is a `GenTraversable`.
129+
* @return a new $coll resulting from concatenating all element ${coll}s.
130+
*
131+
* @usecase def flatten[B]: $Coll[B]
132+
*
133+
* @inheritdoc
134+
*
135+
* The resulting collection's type will be guided by the
136+
* static type of $coll. For example:
137+
*
138+
* {{{
139+
* val xs = List(
140+
* Set(1, 2, 3),
141+
* Set(1, 2, 3)
142+
* ).flatten
143+
* // xs == List(1, 2, 3, 1, 2, 3)
144+
*
145+
* val ys = Set(
146+
* List(1, 2, 3),
147+
* List(3, 2, 1)
148+
* ).flatten
149+
* // ys == Set(1, 2, 3)
150+
* }}}
151+
*/
152+
def flatten[B](implicit asTraversable: A => /*<:<!!!*/ IterableOnce[B]): CC[B] = {
153+
val b = genericBuilder[B]
154+
for (xs <- sequential.iterator)
155+
b ++= asTraversable(xs)/*.seq*/
156+
b.result()
157+
}
158+
159+
/** Transposes this $coll of traversable collections into
160+
* a $coll of ${coll}s.
161+
*
162+
* The resulting collection's type will be guided by the
163+
* static type of $coll. For example:
164+
*
165+
* {{{
166+
* val xs = List(
167+
* Set(1, 2, 3),
168+
* Set(4, 5, 6)).transpose
169+
* // xs == List(
170+
* // List(1, 4),
171+
* // List(2, 5),
172+
* // List(3, 6))
173+
*
174+
* val ys = Vector(
175+
* List(1, 2, 3),
176+
* List(4, 5, 6)).transpose
177+
* // ys == Vector(
178+
* // Vector(1, 4),
179+
* // Vector(2, 5),
180+
* // Vector(3, 6))
181+
* }}}
182+
*
183+
* @tparam B the type of the elements of each traversable collection.
184+
* @param asTraversable an implicit conversion which asserts that the
185+
* element type of this $coll is a `Traversable`.
186+
* @return a two-dimensional $coll of ${coll}s which has as ''n''th row
187+
* the ''n''th column of this $coll.
188+
* @throws IllegalArgumentException if all collections in this $coll
189+
* are not of the same size.
190+
*/
191+
@migration("`transpose` throws an `IllegalArgumentException` if collections are not uniformly sized.", "2.9.0")
192+
def transpose[B](implicit asTraversable: A => /*<:<!!!*/ IterableOnce[B]): CC[CC[B] @uncheckedVariance] = {
193+
if (isEmpty)
194+
return genericBuilder[CC[B]].result()
195+
196+
def fail = throw new IllegalArgumentException("transpose requires all collections have the same size")
197+
198+
val headSize = asTraversable(head).iterator.size
199+
val bs: IndexedSeq[collection.mutable.Builder[B, CC[B]]] = IndexedSeq.fill(headSize)(genericBuilder[B])
200+
for (xs <- sequential.iterator) {
201+
var i = 0
202+
for (x <- asTraversable(xs).iterator/*.seq*/) {
203+
if (i >= headSize) fail
204+
bs(i) += x
205+
i += 1
206+
}
207+
if (i != headSize)
208+
fail
209+
}
210+
val bb = genericBuilder[CC[B]]
211+
for (b <- bs) bb += b.result
212+
bb.result()
213+
}
214+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
package scala
9+
package collection
10+
package generic
11+
12+
import mutable.Builder
13+
14+
trait HasNewBuilder[+A, +Repr] extends Any {
15+
/** The builder that builds instances of Repr */
16+
protected[this] def newBuilder: Builder[A, Repr]
17+
}

core/src/main/scala/scala/collection/generic/ParFactory.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ import scala.language.higherKinds
2424
* @since 2.8
2525
*/
2626
abstract class ParFactory[CC[X] <: ParIterable[X] with GenericParTemplate[X, CC]]
27-
extends GenTraversableFactory[CC]
28-
with GenericParCompanion[CC] {
27+
extends /*GenTraversableFactory[CC]
28+
with*/ GenericParCompanion[CC] {
2929

3030
//type EPC[T, C] = scala.collection.parallel.EnvironmentPassingCombiner[T, C]
31-
31+
/*
3232
/** A generic implementation of the `CanCombineFrom` trait, which forwards
3333
* all calls to `apply(from)` to the `genericParBuilder` method of the $coll
3434
* `from`, and calls to `apply()` to this factory.
3535
*/
3636
class GenericCanCombineFrom[A] extends GenericCanBuildFrom[A] with CanCombineFrom[CC[_], A, CC[A]] {
3737
override def apply(from: Coll) = from.genericCombiner
3838
override def apply() = newBuilder[A]
39-
}
39+
}*/
4040
}

0 commit comments

Comments
 (0)