Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3440: Provide a restricted implementation of javalib Array parallel methods #3445

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
239 changes: 237 additions & 2 deletions javalib/src/main/scala/java/util/Arrays.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Ported from Scala.js commit: ba618ed dated: 2020-10-05
// Arrays.spliterator() methods added for Scala Native.
// Arrays.stream() methods added for Scala Native.

/*
Arrays.spliterator() methods added for Scala Native.
Arrays.stream() methods added for Scala Native.
Arrays.setAll*() methods added for Scala Native.
Arrays.parallel*() methods added for Scala Native.
*/

package java.util

Expand Down Expand Up @@ -1004,6 +1009,236 @@ object Arrays {

// Scala Native additions --------------------------------------------------

/* Note:
* For now all of parallelPrefix(), parallelSetAll() and parallelSort()
* methods are restricted to a parallelism of 1, i.e. sequential.
*
* Later evolutions could/should increase the parallelism when
* multithreading has been enabled.
*/

def parallelPrefix(array: Array[Double], op: DoubleBinaryOperator): Unit = {
parallelPrefix(array, 0, array.length, op)
}

def parallelPrefix(
array: Array[Double],
fromIndex: Int,
toIndex: Int,
op: DoubleBinaryOperator
): Unit = {
checkRangeIndices(array, fromIndex, toIndex)
val rangeSize = toIndex - fromIndex

if (rangeSize >= 2) { // rangeSize == 0 or 1 leaves array unmodified.
for (j <- (fromIndex + 1) until toIndex) {
array(j) = op.applyAsDouble(array(j - 1), array(j))
}
}
}

def parallelPrefix(array: Array[Int], op: IntBinaryOperator): Unit = {
parallelPrefix(array, 0, array.length, op)
}

def parallelPrefix(
array: Array[Int],
fromIndex: Int,
toIndex: Int,
op: IntBinaryOperator
): Unit = {
checkRangeIndices(array, fromIndex, toIndex)
val rangeSize = toIndex - fromIndex

if (rangeSize >= 2) { // rangeSize == 0 or 1 leaves array unmodified.
for (j <- (fromIndex + 1) until toIndex) {
array(j) = op.applyAsInt(array(j - 1), array(j))
}
}
}

def parallelPrefix(array: Array[Long], op: LongBinaryOperator): Unit = {
parallelPrefix(array, 0, array.length, op)
}

def parallelPrefix(
array: Array[Long],
fromIndex: Int,
toIndex: Int,
op: LongBinaryOperator
): Unit = {
checkRangeIndices(array, fromIndex, toIndex)
val rangeSize = toIndex - fromIndex

if (rangeSize >= 2) { // rangeSize == 0 or 1 leaves array unmodified.
for (j <- (fromIndex + 1) until toIndex) {
array(j) = op.applyAsLong(array(j - 1), array(j))
}
}
}

def parallelPrefix[T <: AnyRef](
array: Array[T],
op: BinaryOperator[T]
): Unit = {
parallelPrefix[T](array, 0, array.length, op)
}

def parallelPrefix[T <: AnyRef](
array: Array[T],
fromIndex: Int,
toIndex: Int,
op: BinaryOperator[T]
): Unit = {
checkRangeIndices(array, fromIndex, toIndex)
val rangeSize = toIndex - fromIndex

if (rangeSize >= 2) { // rangeSize == 0 or 1 leaves array unmodified.
for (j <- (fromIndex + 1) until toIndex) {
array(j) = op.apply(array(j - 1), array(j))
}
}
}

def parallelSetAll(
array: Array[Double],
generator: IntToDoubleFunction
): Unit = {
setAll(array, generator)
}

def parallelSetAll(array: Array[Int], generator: IntUnaryOperator): Unit = {
setAll(array, generator)
}

def parallelSetAll(array: Array[Long], generator: IntToLongFunction): Unit = {
setAll(array, generator)
}

def parallelSetAll[T <: AnyRef](
array: Array[T],
generator: IntFunction[_ <: T]
): Unit = {
setAll(array, generator)
}

// parallelSort(byte[])
def parallelSort(a: Array[Byte]): Unit =
sort(a)

// parallelSort(byte[] a, int fromIndex, int toIndex)
def parallelSort(
a: Array[Byte],
fromIndex: Int,
toIndex: Int
): Unit =
sort(a, fromIndex, toIndex)

// parallelSort(char[])
def parallelSort(a: Array[Char]): Unit =
sort(a)

// parallelSort(char[] a, int fromIndex, int toIndex)
def parallelSort(
a: Array[Char],
fromIndex: Int,
toIndex: Int
): Unit =
sort(a, fromIndex, toIndex)

// parallelSort(double[])
def parallelSort(array: Array[Double]): Unit =
sort(array)

// parallelSort(double[] a, int fromIndex, int toIndex)
def parallelSort(
array: Array[Double],
fromIndex: Int,
toIndex: Int
): Unit =
sort(array, fromIndex, toIndex)

// parallelSort(float[])
def parallelSort(a: Array[Float]): Unit =
sort(a)

// parallelSort(float[] a, int fromIndex, int toIndex)
def parallelSort(
a: Array[Float],
fromIndex: Int,
toIndex: Int
): Unit =
sort(a, fromIndex, toIndex)

// parallelSort(int[])
def parallelSort(a: Array[Int]): Unit =
sort(a)

// parallelSort(int[] a, int fromIndex, int toIndex)
def parallelSort(a: Array[Int], fromIndex: Int, toIndex: Int): Unit =
sort(a, fromIndex, toIndex)

// parallelSort(long[])
def parallelSort(a: Array[Long]): Unit =
sort(a)
// parallelSort(long[] a, int fromIndex, int toIndex)
def parallelSort(
a: Array[Long],
fromIndex: Int,
toIndex: Int
): Unit =
sort(a, fromIndex, toIndex)

// parallelSort(short[])
def parallelSort(a: Array[Short]): Unit =
sort(a)

// parallelSort(short[] a, int fromIndex, int toIndex)
def parallelSort(
a: Array[Short],
fromIndex: Int,
toIndex: Int
): Unit =
sort(a, fromIndex, toIndex)

// parallelSort(T[])
// def parallelSort(a: Array[AnyRef]): Unit =
// sort(a)

// def parallelSort[T <: Comparable[AnyRef]](
def parallelSort[T <: Comparable[_ <: AnyRef]](
array: Array[T]
): Unit = {
sort(array.asInstanceOf[Array[AnyRef]])
}

// parallelSort(T[] a, Comparator<? super T> cmp)
def parallelSort[T <: AnyRef](
array: Array[T],
comparator: Comparator[_ >: T]
): Unit = {
sort[T](array, comparator)
}

// parallelSort(T[] a, int fromIndex, int toIndex)
def parallelSort[T <: Comparable[_ <: AnyRef]](
array: Array[T],
fromIndex: Int,
toIndex: Int
): Unit =
sort(array.asInstanceOf[Array[AnyRef]], fromIndex, toIndex)

// parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)

def parallelSort[T <: AnyRef](
array: Array[T],
fromIndex: Int,
toIndex: Int,
comparator: Comparator[_ >: T]
): Unit = {
sort[T](array, fromIndex, toIndex, comparator)
}

def setAll(array: Array[Double], generator: IntToDoubleFunction): Unit = {
for (j <- 0 until array.size)
array(j) = generator.applyAsDouble(j)
Expand Down