Open kyonifer opened 5 years ago
Here's a sketch of the idea I was talking about in #47,
interface NDArrayShape {
fun shape(): List<T>
}
interface NDArrayCursor: NDArrayShape, Collection<Int> {
val linear: Int
val pos: IntArray // this would be implemented as lazy by { linearToIdx(linear) }
// Collection<Int> would be implemented as calls to pos
}
/// and then codegen a bunch of these so that destructuring works
interface NDArrayCursor1: NDArrayCursor { fun component1() }
And then use the NDArrayCursor in places that currently use IntArrays as indices, so that code that only needs linear can use it efficiently, and code that needs the full position can read it without having to compute it itself.
If NDArrayCursor
implements the []
operator, existing code that expects an IntArray
should mostly continue to work.
It may make sense to refactor out the code that converts linear indices into N-dimensional indices and vice versa into a separate object that could be re-used. See discussion in #47 for background.