forked from kscience/kmath
Fixed atomicfu build issues (no js)
This commit is contained in:
parent
002ddbee48
commit
3413d21385
@ -4,6 +4,7 @@ buildscript {
|
|||||||
val kotlinVersion: String by rootProject.extra("1.3.20")
|
val kotlinVersion: String by rootProject.extra("1.3.20")
|
||||||
val ioVersion: String by rootProject.extra("0.1.2")
|
val ioVersion: String by rootProject.extra("0.1.2")
|
||||||
val coroutinesVersion: String by rootProject.extra("1.1.1")
|
val coroutinesVersion: String by rootProject.extra("1.1.1")
|
||||||
|
val atomicfuVersion: String by rootProject.extra("0.12.1")
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
//maven("https://dl.bintray.com/kotlin/kotlin-eap")
|
//maven("https://dl.bintray.com/kotlin/kotlin-eap")
|
||||||
|
@ -3,13 +3,8 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
targets {
|
jvm()
|
||||||
fromPreset(presets.jvm, 'jvm')
|
js()
|
||||||
// For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
|
|
||||||
// For Linux, preset should be changed to e.g. presets.linuxX64
|
|
||||||
// For MacOS, preset should be changed to e.g. presets.macosX64
|
|
||||||
//fromPreset(presets.mingwX64, 'mingw')
|
|
||||||
}
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
commonMain {
|
commonMain {
|
||||||
dependencies {
|
dependencies {
|
||||||
@ -34,6 +29,11 @@ kotlin {
|
|||||||
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
|
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
jsMain{
|
||||||
|
dependencies{
|
||||||
|
api "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion"
|
||||||
|
}
|
||||||
|
}
|
||||||
// mingwMain {
|
// mingwMain {
|
||||||
// }
|
// }
|
||||||
// mingwTest {
|
// mingwTest {
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package scientifik.kmath.structures
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
|
actual fun <R> runBlocking(
|
||||||
|
context: CoroutineContext,
|
||||||
|
function: suspend CoroutineScope.() -> R
|
||||||
|
): R {
|
||||||
|
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||||
|
}
|
@ -1,19 +1,20 @@
|
|||||||
plugins {
|
plugins {
|
||||||
kotlin("multiplatform")
|
kotlin("multiplatform")
|
||||||
id("kotlinx-atomicfu")
|
id("kotlinx-atomicfu") version "0.12.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val atomicfuVersion: String by rootProject.extra
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
jvm ()
|
jvm ()
|
||||||
js()
|
//js()
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
val commonMain by getting {
|
val commonMain by getting {
|
||||||
dependencies {
|
dependencies {
|
||||||
api(project(":kmath-core"))
|
api(project(":kmath-core"))
|
||||||
api(project(":kmath-coroutines"))
|
api(project(":kmath-coroutines"))
|
||||||
compileOnly("org.jetbrains.kotlinx:atomicfu-common:0.12.1")
|
compileOnly("org.jetbrains.kotlinx:atomicfu-common:$atomicfuVersion")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val commonTest by getting {
|
val commonTest by getting {
|
||||||
@ -24,7 +25,7 @@ kotlin {
|
|||||||
}
|
}
|
||||||
val jvmMain by getting {
|
val jvmMain by getting {
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly("org.jetbrains.kotlinx:atomicfu:0.12.1")
|
compileOnly("org.jetbrains.kotlinx:atomicfu:$atomicfuVersion")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val jvmTest by getting {
|
val jvmTest by getting {
|
||||||
@ -35,17 +36,18 @@ kotlin {
|
|||||||
}
|
}
|
||||||
// val jsMain by getting {
|
// val jsMain by getting {
|
||||||
// dependencies {
|
// dependencies {
|
||||||
// api(kotlin("stdlib-js"))
|
// compileOnly("org.jetbrains.kotlinx:atomicfu-js:$atomicfuVersion")
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
val jsTest by getting {
|
// val jsTest by getting {
|
||||||
dependencies {
|
// dependencies {
|
||||||
implementation(kotlin("test-js"))
|
// implementation(kotlin("test-js"))
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
// mingwMain {
|
|
||||||
// }
|
|
||||||
// mingwTest {
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
atomicfu {
|
||||||
|
variant = "VH"
|
||||||
|
}
|
@ -1,10 +1,7 @@
|
|||||||
package scientifik.kmath.sequential
|
package scientifik.kmath.sequential
|
||||||
|
|
||||||
import kotlinx.atomicfu.atomic
|
import kotlinx.atomicfu.atomic
|
||||||
import kotlinx.atomicfu.atomicArrayOfNulls
|
|
||||||
import kotlinx.atomicfu.getAndUpdate
|
import kotlinx.atomicfu.getAndUpdate
|
||||||
import kotlinx.coroutines.GlobalScope
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.channels.ReceiveChannel
|
import kotlinx.coroutines.channels.ReceiveChannel
|
||||||
import scientifik.kmath.operations.Space
|
import scientifik.kmath.operations.Space
|
||||||
|
|
||||||
@ -46,7 +43,7 @@ suspend fun <T> Accumulator<T>.offerAll(channel: ReceiveChannel<T>) {
|
|||||||
*/
|
*/
|
||||||
class GenericMean<T : Any>(val context: Space<T>) : Accumulator<T> {
|
class GenericMean<T : Any>(val context: Space<T>) : Accumulator<T> {
|
||||||
//TODO add guard against overflow
|
//TODO add guard against overflow
|
||||||
val counter = atomic(0)
|
private val counter = atomic(0)
|
||||||
val sum = atomic(context.zero)
|
val sum = atomic(context.zero)
|
||||||
|
|
||||||
val value get() = with(context) { sum.value / counter.value }
|
val value get() = with(context) { sum.value / counter.value }
|
||||||
|
@ -97,11 +97,12 @@ class MarkovChain<out R : Any>(private val seed: () -> R, private val gen: suspe
|
|||||||
|
|
||||||
constructor(seed: R, gen: suspend (R) -> R) : this({ seed }, gen)
|
constructor(seed: R, gen: suspend (R) -> R) : this({ seed }, gen)
|
||||||
|
|
||||||
private val atomicValue by lazy { atomic(seed()) }
|
private val atomicValue = atomic<R?>(null)
|
||||||
override val value: R get() = atomicValue.value
|
override val value: R get() = atomicValue.value ?: seed()
|
||||||
|
|
||||||
override suspend fun next(): R {
|
override suspend fun next(): R {
|
||||||
atomicValue.lazySet(gen(value))
|
val newValue = gen(value)
|
||||||
|
atomicValue.lazySet(newValue)
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,11 +123,12 @@ class StatefulChain<S, out R>(
|
|||||||
|
|
||||||
constructor(state: S, seed: R, gen: suspend S.(R) -> R) : this(state, { seed }, gen)
|
constructor(state: S, seed: R, gen: suspend S.(R) -> R) : this(state, { seed }, gen)
|
||||||
|
|
||||||
private val atomicValue by lazy { atomic(seed(state)) }
|
private val atomicValue = atomic<R?>(null)
|
||||||
override val value: R get() = atomicValue.value
|
override val value: R get() = atomicValue.value ?: seed(state)
|
||||||
|
|
||||||
override suspend fun next(): R {
|
override suspend fun next(): R {
|
||||||
atomicValue.lazySet(gen(state, value))
|
val newValue = gen(state, value)
|
||||||
|
atomicValue.lazySet(newValue)
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@ package scientifik.kmath.sequential
|
|||||||
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.channels.ReceiveChannel
|
import kotlinx.coroutines.channels.ReceiveChannel
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import scientifik.kmath.operations.Space
|
import scientifik.kmath.operations.Space
|
||||||
import scientifik.kmath.structures.runBlocking
|
import scientifik.kmath.structures.runBlocking
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -12,13 +14,16 @@ import java.util.*
|
|||||||
class MovingAverage<T : Any>(val window: Int, val context: Space<T>) : Accumulator<T> {
|
class MovingAverage<T : Any>(val window: Int, val context: Space<T>) : Accumulator<T> {
|
||||||
private val outputChannel = Channel<T>()
|
private val outputChannel = Channel<T>()
|
||||||
private val queue = ArrayDeque<T>(window)
|
private val queue = ArrayDeque<T>(window)
|
||||||
|
private val mutex = Mutex()
|
||||||
|
|
||||||
override suspend fun send(value: T) {
|
override suspend fun send(value: T) {
|
||||||
queue.add(value)
|
mutex.withLock {
|
||||||
if (queue.size == window) {
|
queue.add(value)
|
||||||
val sum = queue.fold(context.zero) { a, b -> context.run { a + b } }
|
if (queue.size == window) {
|
||||||
outputChannel.send(context.run { sum / window })
|
val sum = queue.fold(context.zero) { a, b -> context.run { a + b } }
|
||||||
queue.pop()
|
outputChannel.send(context.run { sum / window })
|
||||||
|
queue.pop()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,3 @@
|
|||||||
buildscript {
|
|
||||||
dependencies {
|
|
||||||
classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.12.1")
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
jcenter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pluginManagement {
|
pluginManagement {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
@ -18,7 +8,7 @@ pluginManagement {
|
|||||||
eachPlugin {
|
eachPlugin {
|
||||||
when (requested.id.id) {
|
when (requested.id.id) {
|
||||||
"kotlinx-atomicfu" -> {
|
"kotlinx-atomicfu" -> {
|
||||||
useModule("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.12.1")
|
useModule("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${requested.version}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user