Compare commits
2 Commits
203a350650
...
82196250f6
Author | SHA1 | Date | |
---|---|---|---|
82196250f6 | |||
86324a9219 |
@ -9,8 +9,7 @@ kscience {
|
|||||||
wasm()
|
wasm()
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api(project(":kmath-core"))
|
api(projects.kmathCore)
|
||||||
api(project(":kmath-complex"))
|
|
||||||
api(spclibs.kotlinx.coroutines.core)
|
api(spclibs.kotlinx.coroutines.core)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,8 @@ public class RingBuffer<T>(
|
|||||||
|
|
||||||
private val mutex: Mutex = Mutex()
|
private val mutex: Mutex = Mutex()
|
||||||
|
|
||||||
|
public val capacity: Int get() = buffer.size
|
||||||
|
|
||||||
override var size: Int = size
|
override var size: Int = size
|
||||||
private set
|
private set
|
||||||
|
|
||||||
@ -32,7 +34,7 @@ public class RingBuffer<T>(
|
|||||||
return buffer[startIndex.forward(index)]
|
return buffer[startIndex.forward(index)]
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun isFull(): Boolean = size == buffer.size
|
public fun isFull(): Boolean = size == capacity
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterator could provide wrong results if buffer is changed in initialization (iteration is safe)
|
* Iterator could provide wrong results if buffer is changed in initialization (iteration is safe)
|
||||||
@ -59,6 +61,9 @@ public class RingBuffer<T>(
|
|||||||
VirtualBuffer(size) { i -> copy[startIndex.forward(i)] }
|
VirtualBuffer(size) { i -> copy[startIndex.forward(i)] }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an element to the end of the [RingBuffer]. If buffer capacity is reached, the first element is automatically removed.
|
||||||
|
*/
|
||||||
public suspend fun push(element: T) {
|
public suspend fun push(element: T) {
|
||||||
mutex.withLock {
|
mutex.withLock {
|
||||||
buffer[startIndex.forward(size)] = element
|
buffer[startIndex.forward(size)] = element
|
||||||
@ -66,7 +71,15 @@ public class RingBuffer<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Int.forward(n: Int): Int = (this + n) % (buffer.size)
|
/**
|
||||||
|
* Reset buffer to its empty state
|
||||||
|
*/
|
||||||
|
public suspend fun reset(): Unit = mutex.withLock {
|
||||||
|
startIndex = 0
|
||||||
|
size = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Int.forward(n: Int): Int = (this + n) % capacity
|
||||||
|
|
||||||
override fun toString(): String = Buffer.toString(this)
|
override fun toString(): String = Buffer.toString(this)
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ kotlin{
|
|||||||
sourceSets{
|
sourceSets{
|
||||||
commonMain{
|
commonMain{
|
||||||
dependencies{
|
dependencies{
|
||||||
api(project(":kmath-tensors"))
|
api(projects.kmathTensors)
|
||||||
api("org.jetbrains.kotlinx:multik-core:$multikVersion")
|
api("org.jetbrains.kotlinx:multik-core:$multikVersion")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import org.jetbrains.kotlinx.multik.ndarray.data.*
|
|||||||
import space.kscience.attributes.SafeType
|
import space.kscience.attributes.SafeType
|
||||||
import space.kscience.attributes.safeTypeOf
|
import space.kscience.attributes.safeTypeOf
|
||||||
import space.kscience.kmath.PerformancePitfall
|
import space.kscience.kmath.PerformancePitfall
|
||||||
import space.kscience.kmath.complex.ComplexField
|
|
||||||
import space.kscience.kmath.nd.ShapeND
|
import space.kscience.kmath.nd.ShapeND
|
||||||
import space.kscience.kmath.operations.*
|
import space.kscience.kmath.operations.*
|
||||||
import space.kscience.kmath.tensors.api.Tensor
|
import space.kscience.kmath.tensors.api.Tensor
|
||||||
@ -24,7 +23,7 @@ public val DataType.type: SafeType<*>
|
|||||||
DataType.FloatDataType -> Float32Field.type
|
DataType.FloatDataType -> Float32Field.type
|
||||||
DataType.DoubleDataType -> Float64Field.type
|
DataType.DoubleDataType -> Float64Field.type
|
||||||
DataType.ComplexFloatDataType -> safeTypeOf<Pair<Float, Float>>()
|
DataType.ComplexFloatDataType -> safeTypeOf<Pair<Float, Float>>()
|
||||||
DataType.ComplexDoubleDataType -> ComplexField.type
|
DataType.ComplexDoubleDataType -> safeTypeOf<Pair<Double, Double>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user