Skip to content

Commit

Permalink
actually pretend to do the resizing function correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiteknight committed Oct 24, 2009
1 parent 8c86b73 commit 6ecabdb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/pmc/NumMatrix2D.pmc
Expand Up @@ -14,12 +14,17 @@
#define INDEX_MIN(a, b) (((a) <= (b))?(a):(b))
#define INDEX_MAX(a, b) (((a) >= (b))?(a):(b))

/* Resize the matrix internal storage to be able to hold a point at position
(x, y). The matrix grows but does not shrink. New spaces in the matrix
are initialized to 0.0 */
void
resize_matrix(PARROT_INTERP, PMC * self, INTVAL new_x, INTVAL new_y)
resize_matrix(PARROT_INTERP, PMC * self, INTVAL x, INTVAL y)
{
Parrot_NumMatrix2D_attributes * const attrs = PARROT_NUMMATRIX2D(self);
const INTVAL old_x = attrs->x;
const INTVAL old_y = attrs->y;
const INTVAL new_x = INDEX_MAX(old_x, x);
const INTVAL new_y = INDEX_MAX(old_y, y);
const INTVAL newsize = new_x * new_y;
FLOATVAL * new_s = (FLOATVAL *)calloc(newsize * sizeof (FLOATVAL));
FLOATVAL * old_s = attrs->storage;
Expand All @@ -33,6 +38,8 @@ resize_matrix(PARROT_INTERP, PMC * self, INTVAL new_x, INTVAL new_y)
}
}
attrs->storage = new_s;
attrs->x = new_x;
attrs->y = new_y;
free(old_s);
}

Expand Down Expand Up @@ -64,7 +71,7 @@ pmclass NumMatrix2D auto_attrs {
x_size = attrs->x;
y_size = attrs->y;
GET_INDICES_FROM_KEY(INTERP, key, x, y);
if (x >= x_size || y >= y_size)
if (x >= x_size || y >= y_size) {
resize_matrix(INTERP, SELF, x, y);
ITEM_XY_ROWMAJOR(attrs->storage, x_size, y_size, x, y) = value;
}
Expand Down
8 changes: 6 additions & 2 deletions src/pmc/NumMatrix2D.pmc~
Expand Up @@ -15,11 +15,13 @@
#define INDEX_MAX(a, b) (((a) >= (b))?(a):(b))

void
resize_matrix(PARROT_INTERP, PMC * self, INTVAL new_x, INTVAL new_y)
resize_matrix(PARROT_INTERP, PMC * self, INTVAL x, INTVAL y)
{
Parrot_NumMatrix2D_attributes * const attrs = PARROT_NUMMATRIX2D(self);
const INTVAL old_x = attrs->x;
const INTVAL old_y = attrs->y;
const INTVAL new_x = INDEX_MAX(old_x, x);
const INTVAL new_y = INDEX_MAX(old_y, y);
const INTVAL newsize = new_x * new_y;
FLOATVAL * new_s = (FLOATVAL *)calloc(newsize * sizeof (FLOATVAL));
FLOATVAL * old_s = attrs->storage;
Expand All @@ -33,6 +35,8 @@ resize_matrix(PARROT_INTERP, PMC * self, INTVAL new_x, INTVAL new_y)
}
}
attrs->storage = new_s;
attrs->x = new_x;
attrs->y = new_y;
free(old_s);
}

Expand Down Expand Up @@ -64,7 +68,7 @@ pmclass NumMatrix2D auto_attrs {
x_size = attrs->x;
y_size = attrs->y;
GET_INDICES_FROM_KEY(INTERP, key, x, y);
if (x >= x_size || y >= y_size)
if (x >= x_size || y >= y_size) {
resize_matrix(INTERP, SELF, x, y);
ITEM_XY_ROWMAJOR(attrs->storage, x_size, y_size, x, y) = value;
}
Expand Down

0 comments on commit 6ecabdb

Please sign in to comment.