fuookami.ospf.kotlin.utils.multi_array.MultiArray.kt Maven / Gradle / Ivy
package fuookami.ospf.kotlin.utils.multi_array
import fuookami.ospf.kotlin.utils.concept.*
sealed class AbstractMultiArray(
val shape: S,
ctor: ((Int, IntArray) -> T)? = null
) : Collection {
internal lateinit var list: MutableList<@UnsafeVariance T>
init {
if (ctor != null) {
init(ctor)
}
}
val dimension by shape::dimension
override val size get() = list.size
protected fun init(ctor: (Int, IntArray) -> @UnsafeVariance T) {
if (!::list.isInitialized) {
list = (0..): Boolean {
return list.containsAll(elements)
}
override fun contains(element: @UnsafeVariance T): Boolean {
return list.contains(element)
}
override fun isEmpty(): Boolean {
return list.isEmpty()
}
override fun iterator(): Iterator {
return list.iterator()
}
operator fun get(i: Int): T {
return list[i]
}
operator fun get(e: Indexed): T {
return list[e.index]
}
@JvmName("getByIntArray")
operator fun get(v: IntArray): T {
return list[shape.index(v)]
}
@JvmName("getByInts")
operator fun get(vararg v: Int): T {
return list[shape.index(v)]
}
operator fun get(vararg v: Indexed): T {
return list[shape.index(v.map { it.index }.toIntArray())]
}
operator fun get(vararg v: Any): MultiArrayView {
return MultiArrayView(this, shape.dummyVector(*v))
}
}
open class MultiArray(
shape: S,
ctor: ((Int, IntArray) -> T)? = null
) : AbstractMultiArray(shape, ctor)
open class MutableMultiArray(
shape: S,
ctor: ((Int, IntArray) -> T)? = null
) : AbstractMultiArray(shape, ctor) {
operator fun set(i: Int, value: T) {
list[i] = value
}
operator fun set(e: Indexed, value: T) {
list[e.index] = value
}
operator fun set(vararg v: Int, e: T) {
list[shape.index(v)] = e
}
operator fun set(vararg v: Indexed, value: T) {
list[shape.index(v.map { it.index }.toIntArray())] = value
}
}
typealias MultiArray1 = MultiArray
typealias MultiArray2 = MultiArray
typealias MultiArray3 = MultiArray
typealias MultiArray4 = MultiArray
typealias DynMultiArray = MultiArray
typealias MutableMultiArray1 = MutableMultiArray
typealias MutableMultiArray2 = MutableMultiArray
typealias MutableMultiArray3 = MutableMultiArray
typealias MutableMultiArray4 = MutableMultiArray
typealias DynMutableMultiArray = MutableMultiArray