elect
07/01/2024, 9:14 AMArray(size) { IntArray(3) {..}}
constructor?Sam
07/01/2024, 9:20 AMints.asList().chunked(3)
, but you'll get back a List<List<Int>>
. If you actually need an Array<IntArray>
, it's probably more efficient to do it the way you initially described.Youssef Shoaib [MOD]
07/01/2024, 11:09 AMints.asList().chunked(3) { ... }
Only actually creates one extra list, not n/3, because the passed in list is a view on the original list and that view is ephemeral, meaning that the caller shouldn't keep it around, thus the view gets mutated after every call to refer to the next chunkelect
07/01/2024, 11:09 AMFRQDO
07/01/2024, 12:04 PMYoussef Shoaib [MOD]
07/01/2024, 12:05 PMints.asList()
is a super basic wrapper around an array. Allocating new arrays is IMO unnecessary if you can instead use lists as views into itMichael Krussel
07/01/2024, 12:26 PMIntArray
do not require the Int
to be boxed unlike List<Int>
, so if the list is large that can have a performance and memory impact.Youssef Shoaib [MOD]
07/01/2024, 12:27 PMIntList
interface and basically recreate the stdlib chunked logic to have views on your new interfaceKlitos Kyriacou
07/02/2024, 12:07 PMArray<IntArray>
is still an array of references to 3-element int arrays. You will probably lose the advantage of cache locality. What you really want is a 2D IntArray, such as those supported by C#, which are represented as a contiguous 1D array but a[n, m]
is syntactic sugar for a[n * (size of 1st dimension) + m]
. Unfortunately, Kotlin doesn't support 2D arrays (only arrays of arrays, which are a cheap imitation) but you can use a small function to do your indexing arithmetic and the JIT will almost certainly inline it.elect
07/02/2024, 2:08 PMMichael Krussel
07/02/2024, 2:26 PMget
operator to take two arguments. private operator fun IntArray.get(row: Int, column: Int): Int = get(row * 3 + column)
.
An inline class could be created to support a 2D int array for general use.