LazyStructire::deferred -> async

This commit is contained in:
Alexander Nozik 2022-06-08 09:00:37 +03:00
parent 5a36c3e03c
commit c28be83226
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
6 changed files with 26 additions and 7 deletions

View File

@ -4,6 +4,8 @@
### Added ### Added
### Changed ### Changed
- Kotlin 1.7
- `LazyStructure` `deffered` -> `async` to comply with coroutines code style
### Deprecated ### Deprecated

View File

@ -11,7 +11,7 @@ allprojects {
} }
group = "space.kscience" group = "space.kscience"
version = "0.3.0" version = "0.3.1"
} }
subprojects { subprojects {

View File

@ -1,4 +1,5 @@
plugins { plugins {
kotlin("jvm") version "1.7.0-RC"
`kotlin-dsl` `kotlin-dsl`
`version-catalog` `version-catalog`
alias(miptNpmLibs.plugins.kotlin.plugin.serialization) alias(miptNpmLibs.plugins.kotlin.plugin.serialization)

View File

@ -4,4 +4,4 @@
# #
kotlin.code.style=official kotlin.code.style=official
toolsVersion=0.11.2-kotlin-1.6.10 toolsVersion=0.11.5-kotlin-1.7.0-RC

View File

@ -0,0 +1,16 @@
/*
* Copyright 2018-2021 KMath contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package space.kscience.kmath.nd
public open class VirtualStructureND<T>(
override val shape: Shape,
public val producer: (IntArray) -> T,
) : StructureND<T> {
override fun get(index: IntArray): T {
require(check that index is in the shape boundaries)
return producer(index)
}
}

View File

@ -18,12 +18,12 @@ public class LazyStructureND<out T>(
) : StructureND<T> { ) : StructureND<T> {
private val cache: MutableMap<IntArray, Deferred<T>> = HashMap() private val cache: MutableMap<IntArray, Deferred<T>> = HashMap()
public fun deferred(index: IntArray): Deferred<T> = cache.getOrPut(index) { public fun async(index: IntArray): Deferred<T> = cache.getOrPut(index) {
scope.async(context = Dispatchers.Math) { function(index) } scope.async(context = Dispatchers.Math) { function(index) }
} }
public suspend fun await(index: IntArray): T = deferred(index).await() public suspend fun await(index: IntArray): T = async(index).await()
override operator fun get(index: IntArray): T = runBlocking { deferred(index).await() } override operator fun get(index: IntArray): T = runBlocking { async(index).await() }
@OptIn(PerformancePitfall::class) @OptIn(PerformancePitfall::class)
override fun elements(): Sequence<Pair<IntArray, T>> { override fun elements(): Sequence<Pair<IntArray, T>> {
@ -33,8 +33,8 @@ public class LazyStructureND<out T>(
} }
} }
public fun <T> StructureND<T>.deferred(index: IntArray): Deferred<T> = public fun <T> StructureND<T>.async(index: IntArray): Deferred<T> =
if (this is LazyStructureND<T>) deferred(index) else CompletableDeferred(get(index)) if (this is LazyStructureND<T>) this@async.async(index) else CompletableDeferred(get(index))
public suspend fun <T> StructureND<T>.await(index: IntArray): T = public suspend fun <T> StructureND<T>.await(index: IntArray): T =
if (this is LazyStructureND<T>) await(index) else get(index) if (this is LazyStructureND<T>) await(index) else get(index)