Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exchanging two dimensional array? #926

Closed
Iainmon opened this issue Oct 23, 2019 · 3 comments
Closed

Exchanging two dimensional array? #926

Iainmon opened this issue Oct 23, 2019 · 3 comments

Comments

@Iainmon
Copy link

Iainmon commented Oct 23, 2019

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.

@dcodeIO
Copy link
Member

dcodeIO commented Oct 23, 2019

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

@MaxGraey
Copy link
Member

But if you need real performance always much better using flatten 2d arrays

@stale
Copy link

stale bot commented Nov 22, 2019

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.

@stale stale bot added the stale label Nov 22, 2019
@stale stale bot closed this as completed Nov 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants