kyonifer / koma

A scientific computing library for Kotlin. https://kyonifer.github.io/koma
Other
270 stars 23 forks source link

Refactor out index wrangling code #68

Open kyonifer opened 5 years ago

kyonifer commented 5 years ago

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.

drmoose commented 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.

peastman commented 5 years ago

If NDArrayCursor implements the [] operator, existing code that expects an IntArrayshould mostly continue to work.