-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,080 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
transformations/src/main/kotlin/com/lukaskusik/coroutines/transformations/MapInPlace.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.lukaskusik.coroutines.transformations | ||
|
||
fun <T> Array<T>.mapInPlace(transform: (T) -> T): Array<T> { | ||
for (i in this.indices) this[i] = transform(this[i]) | ||
return this | ||
} | ||
|
||
fun ByteArray.mapInPlace(transform: (Byte) -> Byte): ByteArray { | ||
for (i in this.indices) this[i] = transform(this[i]) | ||
return this | ||
} | ||
|
||
fun ShortArray.mapInPlace(transform: (Short) -> Short): ShortArray { | ||
for (i in this.indices) this[i] = transform(this[i]) | ||
return this | ||
} | ||
|
||
fun IntArray.mapInPlace(transform: (Int) -> Int): IntArray { | ||
for (i in this.indices) this[i] = transform(this[i]) | ||
return this | ||
} | ||
|
||
fun LongArray.mapInPlace(transform: (Long) -> Long): LongArray { | ||
for (i in this.indices) this[i] = transform(this[i]) | ||
return this | ||
} | ||
|
||
fun FloatArray.mapInPlace(transform: (Float) -> Float): FloatArray { | ||
for (i in this.indices) this[i] = transform(this[i]) | ||
return this | ||
} | ||
|
||
fun DoubleArray.mapInPlace(transform: (Double) -> Double): DoubleArray { | ||
for (i in this.indices) this[i] = transform(this[i]) | ||
return this | ||
} | ||
|
||
fun BooleanArray.mapInPlace(transform: (Boolean) -> Boolean): BooleanArray { | ||
for (i in this.indices) this[i] = transform(this[i]) | ||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...ormations/src/main/kotlin/com/lukaskusik/coroutines/transformations/ParallelMapChunked.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.lukaskusik.coroutines.transformations | ||
|
||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
|
||
/** | ||
* Performs map transformation on the iterable using coroutines. | ||
* The chunkSize parameter is used to run multiple transformations on a single coroutine. | ||
* | ||
* @param chunkSize Size of each sub-collection that will be reduced in each coroutine. | ||
*/ | ||
suspend fun <T, R> Iterable<T>.mapParallelChunked( | ||
chunkSize: Int, | ||
transform: (T) -> R | ||
): List<R> = coroutineScope { | ||
chunked(chunkSize).map { subChunk -> | ||
async { | ||
subChunk.map(transform) | ||
} | ||
}.flatMap { | ||
it.await() | ||
} | ||
} | ||
|
||
/** | ||
* Performs map transformation on the collection using coroutines. | ||
* | ||
* It can split the collection into multiple chunks using the chunksCount parameter. | ||
* Each chunk will then run on a single coroutine, minimizing thread management, etc. | ||
* The default and recommended chunksCount for multithreading is the number of CPU threads, e.g. 4 or 8. | ||
* | ||
* @param chunksCount How many chunks should the collection be split into. Defaults to the number of available processors. | ||
* | ||
*/ | ||
suspend fun <T, E> Collection<T>.mapParallelChunked( | ||
chunksCount: Int = Runtime.getRuntime().availableProcessors(), | ||
transform: (T) -> E | ||
): List<E> { | ||
val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() | ||
return asIterable().mapParallelChunked(chunkSize, transform) | ||
} |
104 changes: 104 additions & 0 deletions
104
...ormations/src/main/kotlin/com/lukaskusik/coroutines/transformations/ParallelMapInPlace.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.lukaskusik.coroutines.transformations | ||
|
||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
|
||
/** | ||
* Performs in place map transformation on the array using coroutines. | ||
*/ | ||
suspend fun <T> Array<T>.mapInPlaceParallel( | ||
transform: (T) -> T | ||
): Array<T> = coroutineScope { | ||
for (i in indices) { | ||
launch { this@mapInPlaceParallel[i] = transform(this@mapInPlaceParallel[i]) } | ||
} | ||
this@mapInPlaceParallel | ||
} | ||
|
||
|
||
/** | ||
* Performs in place map transformation on the array using coroutines. | ||
*/ | ||
suspend fun ByteArray.mapInPlaceParallel( | ||
transform: (Byte) -> Byte | ||
): ByteArray = coroutineScope { | ||
for (i in indices) { | ||
launch { this@mapInPlaceParallel[i] = transform(this@mapInPlaceParallel[i]) } | ||
} | ||
this@mapInPlaceParallel | ||
} | ||
|
||
|
||
/** | ||
* Performs in place map transformation on the array using coroutines. | ||
*/ | ||
suspend fun ShortArray.mapInPlaceParallel( | ||
transform: (Short) -> Short | ||
): ShortArray = coroutineScope { | ||
for (i in indices) { | ||
launch { this@mapInPlaceParallel[i] = transform(this@mapInPlaceParallel[i]) } | ||
} | ||
this@mapInPlaceParallel | ||
} | ||
|
||
/** | ||
* Performs in place map transformation on the array using coroutines. | ||
*/ | ||
suspend fun IntArray.mapInPlaceParallel( | ||
transform: (Int) -> Int | ||
): IntArray = coroutineScope { | ||
for (i in indices) { | ||
launch { this@mapInPlaceParallel[i] = transform(this@mapInPlaceParallel[i]) } | ||
} | ||
this@mapInPlaceParallel | ||
} | ||
|
||
/** | ||
* Performs in place map transformation on the array using coroutines. | ||
*/ | ||
suspend fun LongArray.mapInPlaceParallel( | ||
transform: (Long) -> Long | ||
): LongArray = coroutineScope { | ||
for (i in indices) { | ||
launch { this@mapInPlaceParallel[i] = transform(this@mapInPlaceParallel[i]) } | ||
} | ||
this@mapInPlaceParallel | ||
} | ||
|
||
/** | ||
* Performs in place map transformation on the array using coroutines. | ||
*/ | ||
suspend fun FloatArray.mapInPlaceParallel( | ||
transform: (Float) -> Float | ||
): FloatArray = coroutineScope { | ||
for (i in indices) { | ||
launch { this@mapInPlaceParallel[i] = transform(this@mapInPlaceParallel[i]) } | ||
} | ||
this@mapInPlaceParallel | ||
} | ||
|
||
/** | ||
* Performs in place map transformation on the array using coroutines. | ||
*/ | ||
suspend fun DoubleArray.mapInPlaceParallel( | ||
transform: (Double) -> Double | ||
): DoubleArray = coroutineScope { | ||
for (i in indices) { | ||
launch { this@mapInPlaceParallel[i] = transform(this@mapInPlaceParallel[i]) } | ||
} | ||
this@mapInPlaceParallel | ||
} | ||
|
||
/** | ||
* Performs in place map transformation on the array using coroutines. | ||
*/ | ||
suspend fun BooleanArray.mapInPlaceParallel( | ||
transform: (Boolean) -> Boolean | ||
): BooleanArray = coroutineScope { | ||
for (i in indices) { | ||
launch { this@mapInPlaceParallel[i] = transform(this@mapInPlaceParallel[i]) } | ||
} | ||
this@mapInPlaceParallel | ||
} |
Oops, something went wrong.