
commonMain.space.kscience.kmath.nd.Structure1D.kt Maven / Gradle / Ivy
package space.kscience.kmath.nd
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asSequence
/**
* A structure that is guaranteed to be one-dimensional
*/
public interface Structure1D : NDStructure, Buffer {
public override val dimension: Int get() = 1
public override operator fun get(index: IntArray): T {
require(index.size == 1) { "Index dimension mismatch. Expected 1 but found ${index.size}" }
return get(index[0])
}
public override operator fun iterator(): Iterator = (0 until size).asSequence().map(::get).iterator()
}
/**
* A 1D wrapper for nd-structure
*/
private inline class Structure1DWrapper(val structure: NDStructure) : Structure1D {
override val shape: IntArray get() = structure.shape
override val size: Int get() = structure.shape[0]
override operator fun get(index: Int): T = structure[index]
override fun elements(): Sequence> = structure.elements()
}
/**
* A structure wrapper for buffer
*/
private inline class Buffer1DWrapper(val buffer: Buffer) : Structure1D {
override val shape: IntArray get() = intArrayOf(buffer.size)
override val size: Int get() = buffer.size
override fun elements(): Sequence> =
buffer.asSequence().mapIndexed { index, value -> intArrayOf(index) to value }
override operator fun get(index: Int): T = buffer[index]
}
/**
* Represent a [NDStructure] as [Structure1D]. Throw error in case of dimension mismatch
*/
public fun NDStructure.as1D(): Structure1D = if (shape.size == 1) {
if (this is NDBuffer) Buffer1DWrapper(this.buffer) else Structure1DWrapper(this)
} else
error("Can't create 1d-structure from ${shape.size}d-structure")
/**
* Represent this buffer as 1D structure
*/
public fun Buffer.asND(): Structure1D = Buffer1DWrapper(this)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy