|
| 1 | +package com.lukaskusik.coroutines.transformations |
| 2 | + |
| 3 | +import kotlinx.coroutines.async |
| 4 | +import kotlinx.coroutines.coroutineScope |
| 5 | +import kotlinx.coroutines.launch |
| 6 | + |
| 7 | + |
| 8 | +/** |
| 9 | + * Performs in place map transformation on the array using coroutines. |
| 10 | + */ |
| 11 | +suspend fun <T> Array<T>.mapInPlaceIndexedParallelChunked( |
| 12 | + chunksCount: Int = Runtime.getRuntime().availableProcessors(), |
| 13 | + transform: (index: Int,T) -> T |
| 14 | +): Array<T> = coroutineScope { |
| 15 | + val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() |
| 16 | + for (i in indices step chunkSize) { |
| 17 | + launch { |
| 18 | + for (j in i until Math.min(i + chunkSize, size)) |
| 19 | + this@mapInPlaceIndexedParallelChunked[j] = transform(j, this@mapInPlaceIndexedParallelChunked[j]) |
| 20 | + } |
| 21 | + } |
| 22 | + this@mapInPlaceIndexedParallelChunked |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * Performs in place map transformation on the array using coroutines. |
| 28 | + */ |
| 29 | +suspend fun ByteArray.mapInPlaceIndexedParallelChunked( |
| 30 | + chunksCount: Int = Runtime.getRuntime().availableProcessors(), |
| 31 | + transform: (index: Int,Byte) -> Byte |
| 32 | +): ByteArray = coroutineScope { |
| 33 | + val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() |
| 34 | + for (i in indices step chunkSize) { |
| 35 | + launch { |
| 36 | + for (j in i until Math.min(i + chunkSize, size)) |
| 37 | + this@mapInPlaceIndexedParallelChunked[j] = transform(j, this@mapInPlaceIndexedParallelChunked[j]) |
| 38 | + } |
| 39 | + } |
| 40 | + this@mapInPlaceIndexedParallelChunked |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +/** |
| 45 | + * Performs in place map transformation on the array using coroutines. |
| 46 | + */ |
| 47 | +suspend fun ShortArray.mapInPlaceIndexedParallelChunked( |
| 48 | + chunksCount: Int = Runtime.getRuntime().availableProcessors(), |
| 49 | + transform: (index: Int,Short) -> Short |
| 50 | +): ShortArray = coroutineScope { |
| 51 | + val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() |
| 52 | + for (i in indices step chunkSize) { |
| 53 | + launch { |
| 54 | + for (j in i until Math.min(i + chunkSize, size)) |
| 55 | + this@mapInPlaceIndexedParallelChunked[j] = transform(j, this@mapInPlaceIndexedParallelChunked[j]) |
| 56 | + } |
| 57 | + } |
| 58 | + this@mapInPlaceIndexedParallelChunked |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Performs in place map transformation on the array using coroutines. |
| 63 | + */ |
| 64 | +suspend fun IntArray.mapInPlaceIndexedParallelChunked( |
| 65 | + chunksCount: Int = Runtime.getRuntime().availableProcessors(), |
| 66 | + transform: (index: Int,Int) -> Int |
| 67 | +): IntArray = coroutineScope { |
| 68 | + val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() |
| 69 | + for (i in indices step chunkSize) { |
| 70 | + launch { |
| 71 | + for (j in i until Math.min(i + chunkSize, size)) |
| 72 | + this@mapInPlaceIndexedParallelChunked[j] = transform(j, this@mapInPlaceIndexedParallelChunked[j]) |
| 73 | + } |
| 74 | + } |
| 75 | + this@mapInPlaceIndexedParallelChunked |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Performs in place map transformation on the array using coroutines. |
| 80 | + */ |
| 81 | +suspend fun LongArray.mapInPlaceIndexedParallelChunked( |
| 82 | + chunksCount: Int = Runtime.getRuntime().availableProcessors(), |
| 83 | + transform: (index: Int,Long) -> Long |
| 84 | +): LongArray = coroutineScope { |
| 85 | + val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() |
| 86 | + for (i in indices step chunkSize) { |
| 87 | + launch { |
| 88 | + for (j in i until Math.min(i + chunkSize, size)) |
| 89 | + this@mapInPlaceIndexedParallelChunked[j] = transform(j, this@mapInPlaceIndexedParallelChunked[j]) |
| 90 | + } |
| 91 | + } |
| 92 | + this@mapInPlaceIndexedParallelChunked |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Performs in place map transformation on the array using coroutines. |
| 97 | + */ |
| 98 | +suspend fun FloatArray.mapInPlaceIndexedParallelChunked( |
| 99 | + chunksCount: Int = Runtime.getRuntime().availableProcessors(), |
| 100 | + transform: (index: Int,Float) -> Float |
| 101 | +): FloatArray = coroutineScope { |
| 102 | + val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() |
| 103 | + for (i in indices step chunkSize) { |
| 104 | + launch { |
| 105 | + for (j in i until Math.min(i + chunkSize, size)) |
| 106 | + this@mapInPlaceIndexedParallelChunked[j] = transform(j, this@mapInPlaceIndexedParallelChunked[j]) |
| 107 | + } |
| 108 | + } |
| 109 | + this@mapInPlaceIndexedParallelChunked |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Performs in place map transformation on the array using coroutines. |
| 114 | + */ |
| 115 | +suspend fun DoubleArray.mapInPlaceIndexedParallelChunked( |
| 116 | + chunksCount: Int = Runtime.getRuntime().availableProcessors(), |
| 117 | + transform: (index: Int,Double) -> Double |
| 118 | +): DoubleArray = coroutineScope { |
| 119 | + val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() |
| 120 | + for (i in indices step chunkSize) { |
| 121 | + launch { |
| 122 | + for (j in i until Math.min(i + chunkSize, size)) |
| 123 | + this@mapInPlaceIndexedParallelChunked[j] = transform(j, this@mapInPlaceIndexedParallelChunked[j]) |
| 124 | + } |
| 125 | + } |
| 126 | + this@mapInPlaceIndexedParallelChunked |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Performs in place map transformation on the array using coroutines. |
| 131 | + */ |
| 132 | +suspend fun BooleanArray.mapInPlaceIndexedParallelChunked( |
| 133 | + chunksCount: Int = Runtime.getRuntime().availableProcessors(), |
| 134 | + transform: (index: Int,Boolean) -> Boolean |
| 135 | +): BooleanArray = coroutineScope { |
| 136 | + val chunkSize = Math.ceil(size / chunksCount.toDouble()).toInt() |
| 137 | + for (i in indices step chunkSize) { |
| 138 | + launch { |
| 139 | + for (j in i until Math.min(i + chunkSize, size)) |
| 140 | + this@mapInPlaceIndexedParallelChunked[j] = transform(j, this@mapInPlaceIndexedParallelChunked[j]) |
| 141 | + } |
| 142 | + } |
| 143 | + this@mapInPlaceIndexedParallelChunked |
| 144 | +} |
0 commit comments