Karlo Lozovina
09/16/2021, 3:10 PMArray<IntArray>
? I'm trying to store a 2D matrix of ints/floats in a compact way, would this Array of IntArrays be the way to go? Would that be equivalent to int[][]
?Joffrey
09/16/2021, 4:02 PMi * width + j
indexing to map to 2D). This avoids pointer indirectionsKarlo Lozovina
09/16/2021, 4:05 PMint[][]
in memory - it's not a one continuous chunk of memory, it's array of pointers to arrays with continous memory?int[]
array and do the indexing myself...Joffrey
09/16/2021, 4:08 PMYoussef Shoaib [MOD]
09/16/2021, 5:02 PM@JvmInline value class 2DIntArray
that under the hood uses a normal IntArray
and lays out the 2d matrix in a continuous block of memory so that it's still efficient but all the indexing for getting and setting are hidden away in the value classJoffrey
09/16/2021, 5:29 PMYoussef Shoaib [MOD]
09/16/2021, 6:46 PMTobias Berger
09/17/2021, 7:30 AMIntArray
(or some wrapper class if you prefer) that accepts 2 parameters. Of course this would require you to know the "width" of your matrix, but it could look something like this:
operator fun IntArray.get(x: Int, y: Int) = get(y * width + x)
That way you could call my2DArray[x, y]