-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
11 changed files
with
202 additions
and
69 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
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
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
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
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
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
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
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/kl3jvi/animity/utils/ParallelCollections.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,33 @@ | ||
package com.kl3jvi.animity.utils | ||
|
||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.runBlocking | ||
import java.util.* | ||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.collections.ArrayList | ||
|
||
fun <T, R> Iterable<T>.pmap( | ||
numThreads: Int = maxOf(Runtime.getRuntime().availableProcessors() - 2, 1), | ||
exec: ExecutorService = Executors.newFixedThreadPool(numThreads), | ||
transform: (T) -> R, | ||
): List<R> { | ||
|
||
// default size is just an inlined version of kotlin.collections.collectionSizeOrDefault | ||
val defaultSize = if (this is Collection<*>) this.size else 10 | ||
val destination = Collections.synchronizedList(ArrayList<R>(defaultSize)) | ||
|
||
for (item in this) { | ||
exec.submit { destination.add(transform(item)) } | ||
} | ||
|
||
exec.shutdown() | ||
exec.awaitTermination(1, TimeUnit.DAYS) | ||
|
||
return ArrayList<R>(destination) | ||
} | ||
|
||
fun <A, B>List<A>.apmap(f: suspend (A) -> B): List<B> = runBlocking { | ||
map { async { f(it) } }.map { it.await() } | ||
} |
Oops, something went wrong.