From 24799a691f23805fba77342d31b9ced16617fc84 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 23 Nov 2021 10:32:51 +0300 Subject: [PATCH] Add inc/dec to counters --- .../space/kscience/kmath/histogram/Counter.kt | 26 ++++++++++++++++--- .../kmath/histogram/DoubleHistogramSpace.kt | 2 +- .../kmath/histogram/IndexedHistogramSpace.kt | 1 - .../kmath/histogram/TreeHistogramSpace.kt | 2 +- .../kmath/tensorflow/TensorFlowAlgebra.kt | 4 ++- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt index 4f5a1ceba..5755e05f7 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.histogram import kotlinx.atomicfu.atomic import kotlinx.atomicfu.getAndUpdate import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.Ring +import space.kscience.kmath.operations.Group /** * Common representation for atomic counters @@ -18,7 +18,7 @@ public interface Counter { public val value: T public companion object { - public fun real(): ObjectCounter = ObjectCounter(DoubleField) + public fun double(): ObjectCounter = ObjectCounter(DoubleField) } } @@ -32,6 +32,16 @@ public class IntCounter : Counter { override val value: Int get() = innerValue.value } +public operator fun IntCounter.inc(): IntCounter { + add(1) + return this +} + +public operator fun IntCounter.dec(): IntCounter { + add(-1) + return this +} + public class LongCounter : Counter { private val innerValue = atomic(0L) @@ -42,7 +52,17 @@ public class LongCounter : Counter { override val value: Long get() = innerValue.value } -public class ObjectCounter(public val group: Ring) : Counter { +public operator fun LongCounter.inc(): LongCounter { + add(1L) + return this +} + +public operator fun LongCounter.dec(): LongCounter { + add(-1L) + return this +} + +public class ObjectCounter(private val group: Group) : Counter { private val innerValue = atomic(group.zero) override fun add(delta: T) { diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/DoubleHistogramSpace.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/DoubleHistogramSpace.kt index c452edc9c..39121f301 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/DoubleHistogramSpace.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/DoubleHistogramSpace.kt @@ -74,7 +74,7 @@ public class DoubleHistogramSpace( } override fun produce(builder: HistogramBuilder.() -> Unit): IndexedHistogram { - val ndCounter = StructureND.auto(shape) { Counter.real() } + val ndCounter = StructureND.auto(shape) { Counter.double() } val hBuilder = HistogramBuilder { point, value -> val index = getIndex(point) ndCounter[index].add(value.toDouble()) diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt index f36f45389..cbf0bea1a 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/IndexedHistogramSpace.kt @@ -41,7 +41,6 @@ public class IndexedHistogram, V : Any>( get() = DefaultStrides(context.shape).asSequence().map { context.produceBin(it, values[it]) }.asIterable() - } /** diff --git a/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/TreeHistogramSpace.kt b/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/TreeHistogramSpace.kt index cc54d7e1a..4103fa653 100644 --- a/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/TreeHistogramSpace.kt +++ b/kmath-histograms/src/jvmMain/kotlin/space/kscience/kmath/histogram/TreeHistogramSpace.kt @@ -39,7 +39,7 @@ public class TreeHistogram( @PublishedApi internal class TreeHistogramBuilder(val binFactory: (Double) -> UnivariateDomain) : UnivariateHistogramBuilder { - internal class BinCounter(val domain: UnivariateDomain, val counter: Counter = Counter.real()) : + internal class BinCounter(val domain: UnivariateDomain, val counter: Counter = Counter.double()) : ClosedFloatingPointRange by domain.range private val bins: TreeMap = TreeMap() diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt index 1f545d121..7ad91c267 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt @@ -232,4 +232,6 @@ public abstract class TensorFlowAlgebra> internal con @OptIn(UnstableKMathAPI::class) override fun export(arg: StructureND): StructureND = if (arg is TensorFlowOutput) arg.actualTensor else arg -} \ No newline at end of file +} + +//TODO add TensorFlow expressions \ No newline at end of file