All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ru.casperix.math.array.float32.FloatBasedArray.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
package ru.casperix.math.array.float32

import ru.casperix.math.array.MutableArray1D
import ru.casperix.math.iteration.map


class FloatBasedArray(val array: FloatArray, private val codec: FloatCodec) :
    MutableArray1D {
    private val floatsPerRecord = codec.floatsPerRecord
    override val size = array.size / floatsPerRecord

    constructor(recordsAmount: Int, codec: FloatCodec) : this(
        FloatArray(recordsAmount * codec.floatsPerRecord),
        codec
    )

    init {
        if (array.size % floatsPerRecord != 0) {
            throw Exception("size: ${array.size}; floatsPerVertex: $floatsPerRecord. Not compatibled")
        }
    }

    override operator fun get(index: Int): Record {
        val offset = index * floatsPerRecord
        val pixel = array.sliceArray(offset until offset + floatsPerRecord)
        return codec.decode(pixel)
    }

    override operator fun set(index: Int, value: Record) {
        val pixel = codec.encode(value)
        val offset = index * floatsPerRecord
        pixel.copyInto(array, offset)
    }

    override fun iterator(): Iterator {
        return (0 until size).iterator().map { get(it) }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy