altavir/diff #494
@ -4,6 +4,8 @@
|
|||||||
### Added
|
### Added
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
- Kotlin 1.7
|
||||||
|
- `LazyStructure` `deffered` -> `async` to comply with coroutines code style
|
||||||
|
|
||||||
### Deprecated
|
### Deprecated
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ allprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "space.kscience"
|
group = "space.kscience"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
|
@ -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)
|
||||||
|
@ -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
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user