pre-0.0.3 #46
@ -4,6 +4,7 @@ buildscript {
|
||||
val kotlinVersion: String by rootProject.extra("1.3.20")
|
||||
val ioVersion: String by rootProject.extra("0.1.2")
|
||||
val coroutinesVersion: String by rootProject.extra("1.1.1")
|
||||
val atomicfuVersion: String by rootProject.extra("0.12.1")
|
||||
|
||||
repositories {
|
||||
//maven("https://dl.bintray.com/kotlin/kotlin-eap")
|
||||
|
@ -3,13 +3,8 @@ plugins {
|
||||
}
|
||||
|
||||
kotlin {
|
||||
targets {
|
||||
fromPreset(presets.jvm, 'jvm')
|
||||
// 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')
|
||||
}
|
||||
jvm()
|
||||
js()
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
@ -34,6 +29,11 @@ kotlin {
|
||||
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
|
||||
}
|
||||
}
|
||||
jsMain{
|
||||
dependencies{
|
||||
api "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion"
|
||||
}
|
||||
}
|
||||
// mingwMain {
|
||||
// }
|
||||
// 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 {
|
||||
kotlin("multiplatform")
|
||||
id("kotlinx-atomicfu")
|
||||
id("kotlinx-atomicfu") version "0.12.1"
|
||||
}
|
||||
|
||||
val atomicfuVersion: String by rootProject.extra
|
||||
|
||||
kotlin {
|
||||
jvm ()
|
||||
js()
|
||||
//js()
|
||||
|
||||
sourceSets {
|
||||
val commonMain by getting {
|
||||
dependencies {
|
||||
api(project(":kmath-core"))
|
||||
api(project(":kmath-coroutines"))
|
||||
compileOnly("org.jetbrains.kotlinx:atomicfu-common:0.12.1")
|
||||
compileOnly("org.jetbrains.kotlinx:atomicfu-common:$atomicfuVersion")
|
||||
}
|
||||
}
|
||||
val commonTest by getting {
|
||||
@ -24,7 +25,7 @@ kotlin {
|
||||
}
|
||||
val jvmMain by getting {
|
||||
dependencies {
|
||||
compileOnly("org.jetbrains.kotlinx:atomicfu:0.12.1")
|
||||
compileOnly("org.jetbrains.kotlinx:atomicfu:$atomicfuVersion")
|
||||
}
|
||||
}
|
||||
val jvmTest by getting {
|
||||
@ -35,17 +36,18 @@ kotlin {
|
||||
}
|
||||
// val jsMain by getting {
|
||||
// dependencies {
|
||||
// api(kotlin("stdlib-js"))
|
||||
// compileOnly("org.jetbrains.kotlinx:atomicfu-js:$atomicfuVersion")
|
||||
// }
|
||||
// }
|
||||
val jsTest by getting {
|
||||
dependencies {
|
||||
implementation(kotlin("test-js"))
|
||||
}
|
||||
}
|
||||
// mingwMain {
|
||||
// }
|
||||
// mingwTest {
|
||||
// val jsTest by getting {
|
||||
// dependencies {
|
||||
// implementation(kotlin("test-js"))
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
atomicfu {
|
||||
variant = "VH"
|
||||
}
|
@ -1,10 +1,7 @@
|
||||
package scientifik.kmath.sequential
|
||||
|
||||
import kotlinx.atomicfu.atomic
|
||||
import kotlinx.atomicfu.atomicArrayOfNulls
|
||||
import kotlinx.atomicfu.getAndUpdate
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
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> {
|
||||
//TODO add guard against overflow
|
||||
val counter = atomic(0)
|
||||
private val counter = atomic(0)
|
||||
val sum = atomic(context.zero)
|
||||
|
||||
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)
|
||||
|
||||
private val atomicValue by lazy { atomic(seed()) }
|
||||
override val value: R get() = atomicValue.value
|
||||
private val atomicValue = atomic<R?>(null)
|
||||
override val value: R get() = atomicValue.value ?: seed()
|
||||
|
||||
override suspend fun next(): R {
|
||||
atomicValue.lazySet(gen(value))
|
||||
val newValue = gen(value)
|
||||
atomicValue.lazySet(newValue)
|
||||
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)
|
||||
|
||||
private val atomicValue by lazy { atomic(seed(state)) }
|
||||
override val value: R get() = atomicValue.value
|
||||
private val atomicValue = atomic<R?>(null)
|
||||
override val value: R get() = atomicValue.value ?: seed(state)
|
||||
|
||||
override suspend fun next(): R {
|
||||
atomicValue.lazySet(gen(state, value))
|
||||
val newValue = gen(state, value)
|
||||
atomicValue.lazySet(newValue)
|
||||
return value
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,8 @@ package scientifik.kmath.sequential
|
||||
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import scientifik.kmath.operations.Space
|
||||
import scientifik.kmath.structures.runBlocking
|
||||
import java.util.*
|
||||
@ -12,13 +14,16 @@ import java.util.*
|
||||
class MovingAverage<T : Any>(val window: Int, val context: Space<T>) : Accumulator<T> {
|
||||
private val outputChannel = Channel<T>()
|
||||
private val queue = ArrayDeque<T>(window)
|
||||
private val mutex = Mutex()
|
||||
|
||||
override suspend fun send(value: T) {
|
||||
queue.add(value)
|
||||
if (queue.size == window) {
|
||||
val sum = queue.fold(context.zero) { a, b -> context.run { a + b } }
|
||||
outputChannel.send(context.run { sum / window })
|
||||
queue.pop()
|
||||
mutex.withLock {
|
||||
queue.add(value)
|
||||
if (queue.size == window) {
|
||||
val sum = queue.fold(context.zero) { a, b -> context.run { a + b } }
|
||||
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 {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@ -18,7 +8,7 @@ pluginManagement {
|
||||
eachPlugin {
|
||||
when (requested.id.id) {
|
||||
"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