Skip to content

Commit f01deed

Browse files
committed
Indexed operations
1 parent 1f14edb commit f01deed

File tree

5 files changed

+417
-0
lines changed

5 files changed

+417
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.lukaskusik.coroutines.transformations
2+
3+
fun <T> Array<T>.mapInPlaceIndexed(transform: (index: Int, T) -> T): Array<T> {
4+
for (i in this.indices) this[i] = transform(i, this[i])
5+
return this
6+
}
7+
8+
fun ByteArray.mapInPlaceIndexed(transform: (index: Int, Byte) -> Byte): ByteArray {
9+
for (i in this.indices) this[i] = transform(i, this[i])
10+
return this
11+
}
12+
13+
fun ShortArray.mapInPlaceIndexed(transform: (index: Int, Short) -> Short): ShortArray {
14+
for (i in this.indices) this[i] = transform(i, this[i])
15+
return this
16+
}
17+
18+
fun IntArray.mapInPlaceIndexed(transform: (index: Int, Int) -> Int): IntArray {
19+
for (i in this.indices) this[i] = transform(i, this[i])
20+
return this
21+
}
22+
23+
fun LongArray.mapInPlaceIndexed(transform: (index: Int, Long) -> Long): LongArray {
24+
for (i in this.indices) this[i] = transform(i, this[i])
25+
return this
26+
}
27+
28+
fun FloatArray.mapInPlaceIndexed(transform: (index: Int, Float) -> Float): FloatArray {
29+
for (i in this.indices) this[i] = transform(i, this[i])
30+
return this
31+
}
32+
33+
fun DoubleArray.mapInPlaceIndexed(transform: (index: Int, Double) -> Double): DoubleArray {
34+
for (i in this.indices) this[i] = transform(i, this[i])
35+
return this
36+
}
37+
38+
fun BooleanArray.mapInPlaceIndexed(transform: (index: Int, Boolean) -> Boolean): BooleanArray {
39+
for (i in this.indices) this[i] = transform(i, this[i])
40+
return this
41+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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>.mapInPlaceIndexedParallel(
12+
transform: (index:Int,T) -> T
13+
): Array<T> = coroutineScope {
14+
for (i in indices) {
15+
launch { this@mapInPlaceIndexedParallel[i] = transform(i, this@mapInPlaceIndexedParallel[i]) }
16+
}
17+
this@mapInPlaceIndexedParallel
18+
}
19+
20+
21+
/**
22+
* Performs in place map transformation on the array using coroutines.
23+
*/
24+
suspend fun ByteArray.mapInPlaceIndexedParallel(
25+
transform: (index:Int,Byte) -> Byte
26+
): ByteArray = coroutineScope {
27+
for (i in indices) {
28+
launch { this@mapInPlaceIndexedParallel[i] = transform(i, this@mapInPlaceIndexedParallel[i]) }
29+
}
30+
this@mapInPlaceIndexedParallel
31+
}
32+
33+
34+
/**
35+
* Performs in place map transformation on the array using coroutines.
36+
*/
37+
suspend fun ShortArray.mapInPlaceIndexedParallel(
38+
transform: (index:Int,Short) -> Short
39+
): ShortArray = coroutineScope {
40+
for (i in indices) {
41+
launch { this@mapInPlaceIndexedParallel[i] = transform(i, this@mapInPlaceIndexedParallel[i]) }
42+
}
43+
this@mapInPlaceIndexedParallel
44+
}
45+
46+
/**
47+
* Performs in place map transformation on the array using coroutines.
48+
*/
49+
suspend fun IntArray.mapInPlaceIndexedParallel(
50+
transform: (index:Int,Int) -> Int
51+
): IntArray = coroutineScope {
52+
for (i in indices) {
53+
launch { this@mapInPlaceIndexedParallel[i] = transform(i, this@mapInPlaceIndexedParallel[i]) }
54+
}
55+
this@mapInPlaceIndexedParallel
56+
}
57+
58+
/**
59+
* Performs in place map transformation on the array using coroutines.
60+
*/
61+
suspend fun LongArray.mapInPlaceIndexedParallel(
62+
transform: (index:Int,Long) -> Long
63+
): LongArray = coroutineScope {
64+
for (i in indices) {
65+
launch { this@mapInPlaceIndexedParallel[i] = transform(i, this@mapInPlaceIndexedParallel[i]) }
66+
}
67+
this@mapInPlaceIndexedParallel
68+
}
69+
70+
/**
71+
* Performs in place map transformation on the array using coroutines.
72+
*/
73+
suspend fun FloatArray.mapInPlaceIndexedParallel(
74+
transform: (index:Int,Float) -> Float
75+
): FloatArray = coroutineScope {
76+
for (i in indices) {
77+
launch { this@mapInPlaceIndexedParallel[i] = transform(i, this@mapInPlaceIndexedParallel[i]) }
78+
}
79+
this@mapInPlaceIndexedParallel
80+
}
81+
82+
/**
83+
* Performs in place map transformation on the array using coroutines.
84+
*/
85+
suspend fun DoubleArray.mapInPlaceIndexedParallel(
86+
transform: (index:Int,Double) -> Double
87+
): DoubleArray = coroutineScope {
88+
for (i in indices) {
89+
launch { this@mapInPlaceIndexedParallel[i] = transform(i, this@mapInPlaceIndexedParallel[i]) }
90+
}
91+
this@mapInPlaceIndexedParallel
92+
}
93+
94+
/**
95+
* Performs in place map transformation on the array using coroutines.
96+
*/
97+
suspend fun BooleanArray.mapInPlaceIndexedParallel(
98+
transform: (index:Int,Boolean) -> Boolean
99+
): BooleanArray = coroutineScope {
100+
for (i in indices) {
101+
launch { this@mapInPlaceIndexedParallel[i] = transform(i, this@mapInPlaceIndexedParallel[i]) }
102+
}
103+
this@mapInPlaceIndexedParallel
104+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.lukaskusik.coroutines.transformations
2+
3+
import kotlinx.coroutines.async
4+
import kotlinx.coroutines.coroutineScope
5+
6+
/**
7+
* Performs map transformation on the iterable using coroutines.
8+
*/
9+
suspend fun <T, R> Iterable<T>.mapIndexedParallel(
10+
transform: (index: Int, T) -> R
11+
): List<R> = coroutineScope {
12+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
13+
}
14+
15+
//region Arrays
16+
/**
17+
* Performs map transformation on the array using coroutines.
18+
*/
19+
suspend fun <T, R> Array<out T>.mapIndexedParallel(
20+
transform: (index: Int, T) -> R
21+
): List<R> = coroutineScope {
22+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
23+
}
24+
25+
/**
26+
* Performs map transformation on the array using coroutines.
27+
*/
28+
suspend fun <R> ByteArray.mapIndexedParallel(
29+
transform: (index: Int, Byte) -> R
30+
): List<R> = coroutineScope {
31+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
32+
}
33+
34+
/**
35+
* Performs map transformation on the array using coroutines.
36+
*/
37+
suspend fun <R> ShortArray.mapIndexedParallel(
38+
transform: (index: Int, Short) -> R
39+
): List<R> = coroutineScope {
40+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
41+
}
42+
43+
/**
44+
* Performs map transformation on the array using coroutines.
45+
*/
46+
suspend fun <R> IntArray.mapIndexedParallel(
47+
transform: (index: Int, Int) -> R
48+
): List<R> = coroutineScope {
49+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
50+
}
51+
52+
/**
53+
* Performs map transformation on the array using coroutines.
54+
*/
55+
suspend fun <R> LongArray.mapIndexedParallel(
56+
transform: (index: Int, Long) -> R
57+
): List<R> = coroutineScope {
58+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
59+
}
60+
61+
/**
62+
* Performs map transformation on the array using coroutines.
63+
*/
64+
suspend fun <R> FloatArray.mapIndexedParallel(
65+
transform: (index: Int, Float) -> R
66+
): List<R> = coroutineScope {
67+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
68+
}
69+
70+
/**
71+
* Performs map transformation on the array using coroutines.
72+
*/
73+
suspend fun <R> DoubleArray.mapIndexedParallel(
74+
transform: (index: Int, Double) -> R
75+
): List<R> = coroutineScope {
76+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
77+
}
78+
79+
/**
80+
* Performs map transformation on the array using coroutines.
81+
*/
82+
suspend fun <R> BooleanArray.mapIndexedParallel(
83+
transform: (index: Int, Boolean) -> R
84+
): List<R> = coroutineScope {
85+
mapIndexed { i, elem -> async { transform(i, elem) } }.map { it.await() }
86+
}
87+
//endregion

0 commit comments

Comments
 (0)