I have a class that takes in a 2d array of i32 values. Here is my code:
export class Kernel {
public size: i32;
public margin: i32;
public multipliers: Array<Array<i32>>;
public products: Array<Array<i32>>;
constructor(multipliers: Array<Array<i32>>) {
...
}
public getAverage(): i32 {
...
}
public iterate(grid: Array<Array<i32>>): Array<Array<i32>> {
...
}
}
This is how I am trying to instantiate it:
const kernelMultipliers = [
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
];
let ptr = module.__retain(module.__allocArray(module.INT32ARRAY, kernelMultipliers));
let kernel = new module.exports.Kernel(ptr);
Is there a way that I can allocate a 2d array of values? If not, is there an elegant way that I can allocate each 1d array, and pass in an array of Array<i32> pointers to the class constructor?
Edit: I guess I could flatten the 2D array, and then pass in the width, but I am hoping that there is a better solution.
Try to allocate the inner arrays of integers first, and then allocate the outer array of arrays of integers, for example using these ids:
export const INNER_ID = idof<i32[]>();
export const OUTER_ID = idof<i32[][]>();
let ptr = module.__retain(
module.__allocArray(module.OUTER_ID,
kernelMultipliers.map(row => module.__allocArray(module.INNER_ID, row))
)
);
let kernel = new module.Kernel(ptr);
module.__release(ptr); // unless you still need it externally
But if you need real performance always much better using flatten 2d arrays
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
But if you need real performance always much better using flatten 2d arrays