From c28be8322697c72237544522d8a8c143f688979d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 8 Jun 2022 09:00:37 +0300 Subject: [PATCH] LazyStructire::deferred -> async --- CHANGELOG.md | 2 ++ build.gradle.kts | 2 +- buildSrc/build.gradle.kts | 1 + buildSrc/gradle.properties | 2 +- .../kscience/kmath/nd/VirtualStructureND.kt | 16 ++++++++++++++++ .../kscience/kmath/structures/LazyStructureND.kt | 10 +++++----- 6 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/VirtualStructureND.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 75833602c..c4b8c06cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Added ### Changed +- Kotlin 1.7 +- `LazyStructure` `deffered` -> `async` to comply with coroutines code style ### Deprecated diff --git a/build.gradle.kts b/build.gradle.kts index 10f2385f5..9fa012fb2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ allprojects { } group = "space.kscience" - version = "0.3.0" + version = "0.3.1" } subprojects { diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index fa5142538..dcb39d448 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -1,4 +1,5 @@ plugins { + kotlin("jvm") version "1.7.0-RC" `kotlin-dsl` `version-catalog` alias(miptNpmLibs.plugins.kotlin.plugin.serialization) diff --git a/buildSrc/gradle.properties b/buildSrc/gradle.properties index a0b05e812..751caec36 100644 --- a/buildSrc/gradle.properties +++ b/buildSrc/gradle.properties @@ -4,4 +4,4 @@ # kotlin.code.style=official -toolsVersion=0.11.2-kotlin-1.6.10 +toolsVersion=0.11.5-kotlin-1.7.0-RC diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/VirtualStructureND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/VirtualStructureND.kt new file mode 100644 index 000000000..799d14b3d --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/VirtualStructureND.kt @@ -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( + override val shape: Shape, + public val producer: (IntArray) -> T, +) : StructureND { + override fun get(index: IntArray): T { + require(check that index is in the shape boundaries) + return producer(index) + } +} \ No newline at end of file diff --git a/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt b/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt index ac9eb773a..2b9265843 100644 --- a/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt +++ b/kmath-coroutines/src/jvmMain/kotlin/space/kscience/kmath/structures/LazyStructureND.kt @@ -18,12 +18,12 @@ public class LazyStructureND( ) : StructureND { private val cache: MutableMap> = HashMap() - public fun deferred(index: IntArray): Deferred = cache.getOrPut(index) { + public fun async(index: IntArray): Deferred = cache.getOrPut(index) { scope.async(context = Dispatchers.Math) { function(index) } } - public suspend fun await(index: IntArray): T = deferred(index).await() - override operator fun get(index: IntArray): T = runBlocking { deferred(index).await() } + public suspend fun await(index: IntArray): T = async(index).await() + override operator fun get(index: IntArray): T = runBlocking { async(index).await() } @OptIn(PerformancePitfall::class) override fun elements(): Sequence> { @@ -33,8 +33,8 @@ public class LazyStructureND( } } -public fun StructureND.deferred(index: IntArray): Deferred = - if (this is LazyStructureND) deferred(index) else CompletableDeferred(get(index)) +public fun StructureND.async(index: IntArray): Deferred = + if (this is LazyStructureND) this@async.async(index) else CompletableDeferred(get(index)) public suspend fun StructureND.await(index: IntArray): T = if (this is LazyStructureND) await(index) else get(index)