Skip to content

Commit

Permalink
add resize function (thought I already did) and fix the exception tha…
Browse files Browse the repository at this point in the history
…t gets thrown
  • Loading branch information
Whiteknight committed Oct 24, 2009
1 parent 2d8fbeb commit 8c86b73
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/pmc/NumMatrix2D.pmc
Expand Up @@ -53,7 +53,8 @@ pmclass NumMatrix2D auto_attrs {
y_size = attrs->y;
GET_INDICES_FROM_KEY(INTERP, key, x, y);
if (x >= x_size || y >= y_size)
Parrot_ex_throw_from_c_args(INTERP, ...);
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"NumMatrix2d: indices out of bounds");
return ITEM_XY_ROWMAJOR(attrs->storage, x_size, y_size, x, y);
}

Expand Down
25 changes: 22 additions & 3 deletions src/pmc/NumMatrix2D.pmc~
Expand Up @@ -15,8 +15,26 @@
#define INDEX_MAX(a, b) (((a) >= (b))?(a):(b))

void
resize_matrix(PARROT_INTERP, PMC * self, INTVAL x, INTVAL y)
resize_matrix(PARROT_INTERP, PMC * self, INTVAL new_x, INTVAL new_y)
{
Parrot_NumMatrix2D_attributes * const attrs = PARROT_NUMMATRIX2D(self);
const INTVAL old_x = attrs->x;
const INTVAL old_y = attrs->y;
const INTVAL newsize = new_x * new_y;
FLOATVAL * new_s = (FLOATVAL *)calloc(newsize * sizeof (FLOATVAL));
FLOATVAL * old_s = attrs->storage;
const INTVAL min_x = INDEX_MIN(old_x, new_x);
const INTVAL min_y = INDEX_MIN(old_y, new_y);
INTVAL i, j;
for (i = 0; i < min_x; i++) {
for (j = 0; j < min_y; j++) {
ITEM_XY_ROWMAJOR(new_s, new_x, new_y, i, j) =
ITEM_XY_ROWMAJOR(old_s, old_x, old_y, i, j);
}
}
attrs->storage = new_s;
free(old_s);
}


pmclass NumMatrix2D auto_attrs {
Expand All @@ -35,7 +53,8 @@ pmclass NumMatrix2D auto_attrs {
y_size = attrs->y;
GET_INDICES_FROM_KEY(INTERP, key, x, y);
if (x >= x_size || y >= y_size)
Parrot_ex_throw_from_c_args(INTERP, ...);
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"NumMatrix2d: indices out of bounds");
return ITEM_XY_ROWMAJOR(attrs->storage, x_size, y_size, x, y);
}

Expand All @@ -49,4 +68,4 @@ pmclass NumMatrix2D auto_attrs {
resize_matrix(INTERP, SELF, x, y);
ITEM_XY_ROWMAJOR(attrs->storage, x_size, y_size, x, y) = value;
}
}
}

0 comments on commit 8c86b73

Please sign in to comment.