Skip to content

Commit

Permalink
added a method iterate_function_inplace, which acts as a transform ap…
Browse files Browse the repository at this point in the history
…plying a given function against every item in the matrix and returning the result
  • Loading branch information
Whiteknight committed Oct 28, 2009
1 parent 7bf83a9 commit 779e591
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/pmc/nummatrix2d.pmc
Expand Up @@ -290,8 +290,8 @@ pmclass NumMatrix2D dynpmc auto_attrs {
FLOATVAL * old_s = attrs->storage;

INTVAL i, j;
for (i = 0; i < min_x; i++) {
for (j = 0; j < min_y; j++) {
for (i = 0; i < old_x; i++) {
for (j = 0; j < old_y; j++) {
ITEM_XY_ROWMAJOR(new_s, new_x, new_y, j, i) =
ITEM_XY_ROWMAJOR(old_s, old_x, old_y, i, j);
}
Expand All @@ -301,5 +301,26 @@ pmclass NumMatrix2D dynpmc auto_attrs {
attrs->y = new_y;
free(old_s);
}

METHOD iterate_function_inplace(PMC * func, PMC * args :slurpy) {
Parrot_NumMatrix2D_attributes * const attrs = PARROT_NUMMATRIX2D(self);
const INTVAL x = attrs->x;
const INTVAL y = attrs->y;
const INTVAL newsize = x * y;
FLOATVAL * old_s = attrs->storage;
FLOATVAL * new_s = (FLOATVAL *)mem_sys_allocate_zeroed(newsize * sizeof (FLOATVAL));

INTVAL i, j;
for (i = 0; i < min_x; i++) {
for (j = 0; j < min_y; j++) {
FLOATVAL value = ITEM_XY_ROWMAJOR(old_s, x, y, i, j);
FLOATVAL result = 0.0;
Parrot_ext_call(INTERP, func, "PiNPf->N", SELF, value, args, &result);
ITEM_XY_ROWMAJOR(new_s, x, y, i, j) = result;
}
}
attrs->storage = new_s;
free(old_s);
}
}

0 comments on commit 779e591

Please sign in to comment.