Skip to content

Commit

Permalink
update charmatrix2d to use rows/cols isntead of x/y internally, for c…
Browse files Browse the repository at this point in the history
…larity
  • Loading branch information
Whiteknight committed Mar 15, 2010
1 parent a13f126 commit 6f0ce99
Showing 1 changed file with 102 additions and 102 deletions.
204 changes: 102 additions & 102 deletions src/pmc/charmatrix2d.pmc
Expand Up @@ -3,8 +3,8 @@
#define ALLOCATE_STORAGE(s) (char *)mem_sys_allocate_zeroed(s * sizeof (char))

/* 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. Parameters x and y are the indices that are trying
(rows, cols). The matrix grows but does not shrink. New spaces in the matrix
are initialized to 0.0. Parameters rows and cols are the indices that are trying
to be accessed, so we must resize the matrix to be able to accomodate those
indices. Notice that the matrix type is zero-indexed, so the size is one
plus the highest index that we need to access.
Expand All @@ -14,32 +14,32 @@
index. To shrink the matrix, use the get_block() method, or that
algorithm. */
static void
resize_matrix(PARROT_INTERP, PMC * self, INTVAL x, INTVAL y)
resize_matrix(PARROT_INTERP, PMC * self, INTVAL rows, INTVAL cols)
{
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(self);
/* Store the old values */
const INTVAL old_x = attrs->x;
const INTVAL old_y = attrs->y;
const INTVAL old_rows = attrs->rows;
const INTVAL old_cols = attrs->cols;
char * old_s = attrs->storage;

/* x and y are indices, not sizes. Resize the matrix to accomodate this
/* rows and cols are indices, not sizes. Resize the matrix to accomodate this
new point without shrinking by taking the max. */
const INTVAL new_x = INDEX_MAX(old_x, x + 1);
const INTVAL new_y = INDEX_MAX(old_y, y + 1);
const INTVAL newsize = new_x * new_y;
const INTVAL new_rows = INDEX_MAX(old_rows, rows + 1);
const INTVAL new_cols = INDEX_MAX(old_cols, cols + 1);
const INTVAL newsize = new_rows * new_cols;
char * new_s = ALLOCATE_STORAGE(newsize);

INTVAL i, j;
memset(new_s, ' ', newsize);
for (i = 0; i < old_x; i++) {
for (j = 0; j < old_y; j++) {
ITEM_XY_COLMAJOR(new_s, new_x, new_y, i, j) =
ITEM_XY_COLMAJOR(old_s, old_x, old_y, i, j);
for (i = 0; i < old_rows; i++) {
for (j = 0; j < old_cols; j++) {
ITEM_XY_COLMAJOR(new_s, new_rows, new_cols, i, j) =
ITEM_XY_COLMAJOR(old_s, old_rows, old_cols, i, j);
}
}
attrs->storage = new_s;
attrs->x = new_x;
attrs->y = new_y;
attrs->rows = new_rows;
attrs->cols = new_cols;
if (old_s)
mem_sys_free(old_s);
}
Expand All @@ -58,22 +58,22 @@ insert_string_at_row_offset(PARROT_INTERP, PMC * self, INTVAL row, INTVAL offset
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(self);
char * const cstr = Parrot_str_to_cstring(interp, str);
const INTVAL length = strlen(cstr);
INTVAL y_size, x_size, i;
INTVAL y_size, rows_size, i;
resize_matrix(interp, self, offset + length - 1, row);
x_size = attrs->x;
y_size = attrs->y;
rows_size = attrs->rows;
y_size = attrs->cols;
for (i = 0; i < length; i++)
ITEM_XY_COLMAJOR(attrs->storage, x_size, y_size, offset + i, row) = cstr[i];
ITEM_XY_COLMAJOR(attrs->storage, rows_size, y_size, offset + i, row) = cstr[i];
for (i = length + offset; i < y_size; i++)
ITEM_XY_COLMAJOR(attrs->storage, x_size, y_size, i, row) = ' ';
ITEM_XY_COLMAJOR(attrs->storage, rows_size, y_size, i, row) = ' ';
Parrot_str_free_cstring(cstr);
return length;
}

pmclass CharMatrix2D dynpmc auto_attrs provides matrix {
ATTR char * storage;
ATTR INTVAL x;
ATTR INTVAL y;
ATTR INTVAL rows;
ATTR INTVAL cols;
ATTR INTVAL flags;

/*
Expand All @@ -91,8 +91,8 @@ pmclass CharMatrix2D dynpmc auto_attrs provides matrix {
VTABLE void init() {
Parrot_CharMatrix2D_attributes * const a = PARROT_CHARMATRIX2D(SELF);
a->storage = NULL;
a->x = 0;
a->y = 0;
a->rows = 0;
a->cols = 0;
a->flags = 0;
PObj_custom_destroy_SET(SELF);
}
Expand All @@ -115,8 +115,8 @@ Returns the string at row N

VTABLE STRING * get_string_keyed_int(INTVAL key) {
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(SELF);
const INTVAL x_size = attrs->x;
const INTVAL y_size = attrs->y;
const INTVAL x_size = attrs->rows;
const INTVAL y_size = attrs->cols;
if (key >= y_size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"CharMatrix2D: Attempt to access non-existant row");
Expand All @@ -128,22 +128,22 @@ Returns the string at row N

=item* set_integer_keyed

Sets the character value at coordinates x,y
Sets the character value at coordinates rows,cols

=cut

*/

VTABLE void set_integer_keyed(PMC * key, INTVAL value) {
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(SELF);
INTVAL x, y, x_size = attrs->x, y_size = attrs->y;
GET_KEY_INDICES_ROWMAJOR(INTERP, key, x, y);
if (x >= x_size || y >= y_size) {
resize_matrix(INTERP, SELF, x, y);
x_size = attrs->x;
y_size = attrs->y;
INTVAL rows, cols, x_size = attrs->rows, y_size = attrs->cols;
GET_KEY_INDICES_ROWMAJOR(INTERP, key, rows, cols);
if (rows >= x_size || cols >= y_size) {
resize_matrix(INTERP, SELF, rows, cols);
x_size = attrs->rows;
y_size = attrs->cols;
}
ITEM_XY_COLMAJOR(attrs->storage, x_size, y_size, x, y) = (char)value;
ITEM_XY_COLMAJOR(attrs->storage, x_size, y_size, rows, cols) = (char)value;
}

VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
Expand All @@ -163,9 +163,9 @@ Sets the character value at coordinates x,y
}

VTABLE void set_string_keyed(PMC * key, STRING * value) {
INTVAL x, y;
GET_KEY_INDICES_ROWMAJOR(INTERP, key, x, y);
insert_string_at_row_offset(INTERP, SELF, y, x, value);
INTVAL rows, cols;
GET_KEY_INDICES_ROWMAJOR(INTERP, key, rows, cols);
insert_string_at_row_offset(INTERP, SELF, cols, rows, value);
}

VTABLE void set_string_keyed_int(INTVAL key, STRING * str) {
Expand Down Expand Up @@ -196,8 +196,8 @@ Sets the character value at coordinates x,y
STRING * const newline = Parrot_str_new(INTERP, "\n", 1);
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(SELF);
char * const s = attrs->storage;
const INTVAL x_size = attrs->x;
const INTVAL y_size = attrs->y;
const INTVAL x_size = attrs->rows;
const INTVAL y_size = attrs->cols;
for (i = 0; i < y_size; i++) {
STRING * const row = get_string_from_row(INTERP, s, x_size, i);
pstr = Parrot_str_append(INTERP, pstr, row);
Expand All @@ -209,18 +209,18 @@ Sets the character value at coordinates x,y
VTABLE PMC * get_attr_str(STRING * idx) {
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(SELF);
if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "cols"))) {
PMC * const x = Parrot_pmc_new(INTERP, enum_class_Integer);
VTABLE_set_integer_native(INTERP, x, attrs->x);
return x;
PMC * const cols = Parrot_pmc_new(INTERP, enum_class_Integer);
VTABLE_set_integer_native(INTERP, cols, attrs->cols);
return cols;
}
else if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "rows"))) {
PMC * const y = Parrot_pmc_new(INTERP, enum_class_Integer);
VTABLE_set_integer_native(INTERP, y, attrs->y);
return y;
PMC * const rows = Parrot_pmc_new(INTERP, enum_class_Integer);
VTABLE_set_integer_native(INTERP, rows, attrs->rows);
return rows;
}
else if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "size"))) {
PMC * const size = Parrot_pmc_new(INTERP, enum_class_Integer);
VTABLE_set_integer_native(INTERP, size, attrs->y * attrs->x);
VTABLE_set_integer_native(INTERP, size, attrs->cols * attrs->rows);
return size;
}
return PMCNULL;
Expand All @@ -230,46 +230,46 @@ Sets the character value at coordinates x,y
PMC * const c = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
Parrot_CharMatrix2D_attributes * const old_atts = PARROT_CHARMATRIX2D(SELF);
Parrot_CharMatrix2D_attributes * const new_atts = PARROT_CHARMATRIX2D(c);
INTVAL x, y;
INTVAL const x_size = old_atts->x;
INTVAL const y_size = old_atts->y;
INTVAL rows, cols;
INTVAL const x_size = old_atts->rows;
INTVAL const y_size = old_atts->cols;
INTVAL const newsize = x_size * y_size;
char * const old_s = old_atts->storage;
char * const new_s = ALLOCATE_STORAGE(newsize);
for (x = 0; x < x_size; ++x) {
for (y = 0; y < y_size; ++y) {
ITEM_XY_COLMAJOR(new_s, x_size, y_size, x, y) =
ITEM_XY_COLMAJOR(old_s, x_size, y_size, x, y);
for (rows = 0; rows < x_size; ++rows) {
for (cols = 0; cols < y_size; ++cols) {
ITEM_XY_COLMAJOR(new_s, x_size, y_size, rows, cols) =
ITEM_XY_COLMAJOR(old_s, x_size, y_size, rows, cols);
}
}
new_atts->storage = new_s;
new_atts->flags = old_atts->flags;
new_atts->x = x_size;
new_atts->y = y_size;
new_atts->rows = x_size;
new_atts->cols = y_size;
return c;
}

VTABLE INTVAL is_equal(PMC * other) {
if (other->vtable->base_type == SELF->vtable->base_type) {
Parrot_CharMatrix2D_attributes * const self_attrs = PARROT_CHARMATRIX2D(SELF);
Parrot_CharMatrix2D_attributes * const other_attrs = PARROT_CHARMATRIX2D(other);
const INTVAL self_x = self_attrs->x;
const INTVAL self_y = self_attrs->y;
const INTVAL other_x = other_attrs->x;
const INTVAL other_y = other_attrs->y;
const INTVAL self_rows = self_attrs->rows;
const INTVAL self_cols = self_attrs->cols;
const INTVAL other_rows = other_attrs->rows;
const INTVAL other_cols = other_attrs->cols;
char * const self_s = self_attrs->storage;
char * const other_s = other_attrs->storage;
INTVAL x, y;
INTVAL rows, cols;

if (self_x != other_x || self_y != other_y)
if (self_rows != other_rows || self_cols != other_cols)
return 0;

for (y = 0; y < self_y; y++) {
for (x = 0; x < self_x; x++) {
for (cols = 0; cols < self_cols; cols++) {
for (rows = 0; rows < self_rows; rows++) {
const char self_value =
ITEM_XY_COLMAJOR(self_s, self_x, self_y, x, y);
ITEM_XY_ROWMAJOR(self_s, self_rows, self_cols, rows, cols);
const char other_value =
ITEM_XY_COLMAJOR(other_s, other_x, other_y, x, y);
ITEM_XY_ROWMAJOR(other_s, other_rows, other_cols, rows, cols);
if (self_value != other_value)
return 0;
}
Expand Down Expand Up @@ -297,8 +297,8 @@ Sets the character value at coordinates x,y

*/

METHOD resize(INTVAL new_x, INTVAL new_y) {
resize_matrix(INTERP, SELF, new_x - 1, new_y - 1);
METHOD resize(INTVAL new_rows, INTVAL new_cols) {
resize_matrix(INTERP, SELF, new_rows - 1, new_cols - 1);
}

/*
Expand All @@ -313,30 +313,30 @@ sizes, growing the matrix if needed.
*/

METHOD fill(INTVAL value,
INTVAL x_size :optional, INTVAL has_x_size :opt_flag,
INTVAL y_size :optional, INTVAL has_y_size :opt_flag
INTVAL x_size :optional, INTVAL has_rows_size :opt_flag,
INTVAL y_size :optional, INTVAL has_cols_size :opt_flag
) {
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(SELF);
const char v = (char)value;
char * s = attrs->storage;
INTVAL const curr_x_size = attrs->x;
INTVAL const curr_y_size = attrs->y;
INTVAL x, y;
INTVAL const curr_rows_size = attrs->rows;
INTVAL const curr_cols_size = attrs->cols;
INTVAL rows, cols;

if (!has_x_size)
x_size = curr_x_size;
if (!has_y_size)
y_size = curr_y_size;
if (!has_rows_size)
x_size = curr_rows_size;
if (!has_cols_size)
y_size = curr_cols_size;

if ((has_x_size && x_size > curr_x_size) ||
(has_y_size && y_size > curr_y_size)) {
if ((has_rows_size && x_size > curr_rows_size) ||
(has_cols_size && y_size > curr_cols_size)) {
resize_matrix(INTERP, SELF, x_size - 1, y_size - 1);
s = attrs->storage;
}

for (x = 0; x < x_size; ++x) {
for (y = 0; y < y_size; ++y) {
ITEM_XY_ROWMAJOR(s, x_size, y_size, x, y) = v;
for (rows = 0; rows < x_size; ++rows) {
for (cols = 0; cols < y_size; ++cols) {
ITEM_XY_ROWMAJOR(s, x_size, y_size, rows, cols) = v;
}
}
}
Expand All @@ -346,8 +346,8 @@ sizes, growing the matrix if needed.
INTVAL i = 0;
PMC * iter = Parrot_pmc_new_init(INTERP, enum_class_ArrayIterator, array);
while (VTABLE_get_bool(INTERP, iter)) {
INTVAL const x_size = attrs->x;
INTVAL const y_size = attrs->y;
INTVAL const x_size = attrs->rows;
INTVAL const y_size = attrs->cols;
PMC * const item = VTABLE_shift_pmc(INTERP, iter);
switch(item->vtable->base_type) {
case enum_class_Float: {
Expand Down Expand Up @@ -389,22 +389,22 @@ value with the return value of the called function.

METHOD iterate_function_inplace(PMC * func, PMC * args :slurpy) {
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(SELF);
const INTVAL x_size = attrs->x;
const INTVAL y_size = attrs->y;
const INTVAL newsize = x_size * y_size;
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
const INTVAL newsize = rows_size * cols_size;
char * old_s = attrs->storage;
char * new_s = ALLOCATE_STORAGE(newsize);
INTVAL i, j;
INTVAL row, col;

if (newsize == 0 || old_s == NULL)
RETURN();

for (j = 0; j < y_size; j++) {
for (i = 0; i < x_size; i++) {
const INTVAL value = ITEM_XY_ROWMAJOR(old_s, x_size, y_size, i, j);
for (row = 0; row < rows_size; row++) {
for (col = 0; col < cols_size; col++) {
const INTVAL value = ITEM_XY_ROWMAJOR(old_s, rows_size, cols_size, row, col);
INTVAL result = 0.0;
Parrot_ext_call(INTERP, func, "PIIIPf->I", SELF, value, i, j, args, &result);
ITEM_XY_ROWMAJOR(new_s, x_size, y_size, i, j) = (char)result;
Parrot_ext_call(INTERP, func, "PIIIPf->I", SELF, value, row, col, args, &result);
ITEM_XY_ROWMAJOR(new_s, rows_size, cols_size, row, col) = (char)result;
}
}
attrs->storage = new_s;
Expand All @@ -416,26 +416,26 @@ value with the return value of the called function.
Parrot_CharMatrix2D_attributes * const attrs = PARROT_CHARMATRIX2D(SELF);
PMC * const new_matrix = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
Parrot_CharMatrix2D_attributes * new_attrs;
const INTVAL x_size = attrs->x;
const INTVAL y_size = attrs->y;
const INTVAL newsize = x_size * y_size;
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
const INTVAL newsize = rows_size * cols_size;
char * const self_s = attrs->storage;
char * new_s;
INTVAL i, j;
INTVAL col, row;

if (newsize == 0 || self_s == NULL)
RETURN(PMC * new_matrix);

resize_matrix(INTERP, new_matrix, x_size - 1, y_size - 1);
resize_matrix(INTERP, new_matrix, rows_size - 1, cols_size - 1);
new_attrs = PARROT_CHARMATRIX2D(new_matrix);
new_s = new_attrs->storage;

for (j = 0; j < y_size; j++) {
for (i = 0; i < x_size; i++) {
INTVAL value = ITEM_XY_ROWMAJOR(self_s, x_size, y_size, i, j);
for (row = 0; row < rows_size; row++) {
for (col = 0; col < cols_size; col++) {
INTVAL value = ITEM_XY_ROWMAJOR(self_s, rows_size, cols_size, row, col);
INTVAL result = 0.0;
Parrot_ext_call(INTERP, func, "PIIIPf->I", SELF, value, i, j, args, &result);
ITEM_XY_ROWMAJOR(new_s, x_size, y_size, i, j) = (char)result;
Parrot_ext_call(INTERP, func, "PIIIPf->I", SELF, value, row, col, args, &result);
ITEM_XY_ROWMAJOR(new_s, rows_size, cols_size, row, col) = (char)result;
}
}
RETURN(PMC * new_matrix);
Expand Down

0 comments on commit 6f0ce99

Please sign in to comment.