Skip to content

Commit edf0644

Browse files
committed
Added marker trait to indicate when splits are efficient.
1 parent 7413c0c commit edf0644

File tree

6 files changed

+213
-118
lines changed

6 files changed

+213
-118
lines changed

src/main/scala/scala/compat/java8/StepConverters.scala

Lines changed: 176 additions & 113 deletions
Large diffs are not rendered by default.

src/main/scala/scala/compat/java8/StreamConverters.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import language.implicitConversions
44

55
import java.util.stream._
66
import scala.compat.java8.collectionImpl._
7+
import scala.compat.java8.converterImpls._
78

89
trait PrimitiveStreamAccumulator[S, AA] {
910
def streamAccumulate(stream: S): AA
@@ -13,7 +14,7 @@ trait PrimitiveStreamUnboxer[A, S] {
1314
def apply(boxed: Stream[A]): S
1415
}
1516

16-
trait Priority3StreamConverters {
17+
trait Priority6StreamConverters {
1718
implicit class EnrichAnyScalaCollectionWithStream[A](t: TraversableOnce[A]) {
1819
private def mkAcc() = {
1920
val acc = new Accumulator[A]
@@ -27,7 +28,7 @@ trait Priority3StreamConverters {
2728
}
2829
}
2930

30-
trait Priority2StreamConverters extends Priority3StreamConverters {
31+
trait Priority5StreamConverters extends Priority6StreamConverters {
3132
implicit class EnrichScalaCollectionWithStream[A <: AnyRef](t: TraversableOnce[A]) {
3233
private def mkArr()(implicit tag: reflect.ClassTag[A]): Array[A] = {
3334
if (t.isTraversableAgain && t.hasDefiniteSize) {
@@ -43,8 +44,21 @@ trait Priority2StreamConverters extends Priority3StreamConverters {
4344
java.util.Arrays.stream(mkArr())
4445

4546
def parStream(implicit tag: reflect.ClassTag[A]): Stream[A] = seqStream.parallel
47+
}
48+
}
49+
50+
trait Priority4StreamConverters extends Priority5StreamConverters {
51+
implicit class EnrichAnySteppableWithStream[A, CC](cc: CC)(implicit steppize: CC => MakesAnyStepper[A]) {
52+
def seqStream: Stream[A] = java.util.stream.StreamSupport.stream(steppize(cc).stepper, false)
53+
def parStream: Stream[A] = java.util.stream.StreamSupport.stream(steppize(cc).stepper, true)
4654
}
55+
}
4756

57+
trait Priority3StreamConverters extends Priority4StreamConverters {
58+
59+
}
60+
61+
trait Priority2StreamConverters extends Priority3StreamConverters {
4862
implicit class EnrichMissingPrimitiveArrayWithStream[A](a: Array[A]) {
4963
private def mkAcc() = {
5064
val acc = new Accumulator[A]

src/main/scala/scala/compat/java8/collectionImpl/Stepper.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ import java.util.Spliterator
3838
*/
3939
trait Stepper[@specialized(Double, Int, Long) A] extends StepperLike[A, Stepper[A]] {}
4040

41+
/** An (optional) marker trait that indicates that a `Stepper` can call `substep` with
42+
* at worst O(log N) time and space complexity, and that the division is likely to
43+
* be reasonably even.
44+
*/
45+
trait EfficientSubstep {}
46+
4147
/** Provides functionality for Stepper while keeping track of a more precise type of the collection.
4248
*/
4349
trait StepperLike[@specialized(Double, Int, Long) A, +CC] { self =>

src/main/scala/scala/compat/java8/collectionImpl/StepsLikeGapped.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ abstract class AbstractStepsLikeGapped[Sub >: Null, Semi <: Sub](protected val u
3535
*/
3636
abstract class StepsLikeGapped[A, STA >: Null <: StepsLikeGapped[A, _]](_underlying: Array[AnyRef], _i0: Int, _iN: Int)
3737
extends AbstractStepsLikeGapped[AnyStepper[A], STA](_underlying, _i0, _iN)
38-
with AnyStepper[A]
38+
with AnyStepper[A]
39+
with EfficientSubstep
3940
{}
4041

4142
/** Abstracts the process of stepping through an incompletely filled array of `AnyRefs`
@@ -44,7 +45,8 @@ abstract class StepsLikeGapped[A, STA >: Null <: StepsLikeGapped[A, _]](_underly
4445
*/
4546
abstract class StepsDoubleLikeGapped[STD >: Null <: StepsDoubleLikeGapped[_]](_underlying: Array[AnyRef], _i0: Int, _iN: Int)
4647
extends AbstractStepsLikeGapped[DoubleStepper, STD](_underlying, _i0, _iN)
47-
with DoubleStepper
48+
with DoubleStepper
49+
with EfficientSubstep
4850
{}
4951

5052
/** Abstracts the process of stepping through an incompletely filled array of `AnyRefs`
@@ -53,7 +55,8 @@ abstract class StepsDoubleLikeGapped[STD >: Null <: StepsDoubleLikeGapped[_]](_u
5355
*/
5456
abstract class StepsIntLikeGapped[STI >: Null <: StepsIntLikeGapped[_]](_underlying: Array[AnyRef], _i0: Int, _iN: Int)
5557
extends AbstractStepsLikeGapped[IntStepper, STI](_underlying, _i0, _iN)
56-
with IntStepper
58+
with IntStepper
59+
with EfficientSubstep
5760
{}
5861

5962
/** Abstracts the process of stepping through an incompletely filled array of `AnyRefs`
@@ -63,4 +66,5 @@ abstract class StepsIntLikeGapped[STI >: Null <: StepsIntLikeGapped[_]](_underly
6366
abstract class StepsLongLikeGapped[STL >: Null <: StepsLongLikeGapped[_]](_underlying: Array[AnyRef], _i0: Int, _iN: Int)
6467
extends AbstractStepsLikeGapped[LongStepper, STL](_underlying, _i0, _iN)
6568
with LongStepper
69+
with EfficientSubstep
6670
{}

src/main/scala/scala/compat/java8/collectionImpl/StepsLikeIndexed.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,26 @@ abstract class AbstractStepsLikeIndexed[Sub >: Null, Semi <: Sub](protected var
2424
abstract class StepsLikeIndexed[A, STA >: Null <: StepsLikeIndexed[A, _]](_i0: Int, _iN: Int)
2525
extends AbstractStepsLikeIndexed[AnyStepper[A], STA](_i0, _iN)
2626
with AnyStepper[A]
27+
with EfficientSubstep
2728
{}
2829

2930
/** Abstracts the operation of stepping over an indexable collection of Doubles */
3031
abstract class StepsDoubleLikeIndexed[STD >: Null <: StepsDoubleLikeIndexed[_]](_i0: Int, _iN: Int)
3132
extends AbstractStepsLikeIndexed[DoubleStepper, STD](_i0, _iN)
3233
with DoubleStepper
34+
with EfficientSubstep
3335
{}
3436

3537
/** Abstracts the operation of stepping over an indexable collection of Ints */
3638
abstract class StepsIntLikeIndexed[STI >: Null <: StepsIntLikeIndexed[_]](_i0: Int, _iN: Int)
3739
extends AbstractStepsLikeIndexed[IntStepper, STI](_i0, _iN)
3840
with IntStepper
41+
with EfficientSubstep
3942
{}
4043

4144
/** Abstracts the operation of stepping over an indexable collection of Longs */
4245
abstract class StepsLongLikeIndexed[STL >: Null <: StepsLongLikeIndexed[_]](_i0: Int, _iN: Int)
4346
extends AbstractStepsLikeIndexed[LongStepper, STL](_i0, _iN)
4447
with LongStepper
48+
with EfficientSubstep
4549
{}

src/main/scala/scala/compat/java8/collectionImpl/StepsLikeSliced.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,26 @@ abstract class AbstractStepsLikeSliced[Coll, Sub >: Null, Semi <: Sub](protected
1919
abstract class StepsLikeSliced[A, AA, STA >: Null <: StepsLikeSliced[A, AA, _]](_underlying: AA, _i0: Int, _iN: Int)
2020
extends AbstractStepsLikeSliced[AA, AnyStepper[A], STA](_underlying, _i0, _iN)
2121
with AnyStepper[A]
22+
with EfficientSubstep
2223
{}
2324

2425
/** Abstracts the operation of stepping over a collection of Doubles that can be efficiently sliced or otherwise subdivided */
2526
abstract class StepsDoubleLikeSliced[AA, STA >: Null <: StepsDoubleLikeSliced[AA, STA]](_underlying: AA, _i0: Int, _iN: Int)
2627
extends AbstractStepsLikeSliced[AA, DoubleStepper, STA](_underlying, _i0, _iN)
2728
with DoubleStepper
29+
with EfficientSubstep
2830
{}
2931

3032
/** Abstracts the operation of stepping over a collection of Ints that can be efficiently sliced or otherwise subdivided */
3133
abstract class StepsIntLikeSliced[AA, STA >: Null <: StepsIntLikeSliced[AA, STA]](_underlying: AA, _i0: Int, _iN: Int)
3234
extends AbstractStepsLikeSliced[AA, IntStepper, STA](_underlying, _i0, _iN)
3335
with IntStepper
36+
with EfficientSubstep
3437
{}
3538

3639
/** Abstracts the operation of stepping over a collection of Longs that can be efficiently sliced or otherwise subdivided */
3740
abstract class StepsLongLikeSliced[AA, STA >: Null <: StepsLongLikeSliced[AA, STA]](_underlying: AA, _i0: Int, _iN: Int)
3841
extends AbstractStepsLikeSliced[AA, LongStepper, STA](_underlying, _i0, _iN)
3942
with LongStepper
43+
with EfficientSubstep
4044
{}

0 commit comments

Comments
 (0)