diff --git a/src/pmc/charmatrix2d.pmc b/src/pmc/charmatrix2d.pmc index 050ed46..125413e 100644 --- a/src/pmc/charmatrix2d.pmc +++ b/src/pmc/charmatrix2d.pmc @@ -198,12 +198,12 @@ 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, "X"))) { + if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "cols"))) { PMC * const x = pmc_new(INTERP, enum_class_Integer); VTABLE_set_integer_native(INTERP, x, attrs->x); return x; } - else if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "Y"))) { + else if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "rows"))) { PMC * const y = pmc_new(INTERP, enum_class_Integer); VTABLE_set_integer_native(INTERP, y, attrs->y); return y; diff --git a/src/pmc/complexmatrix2d.pmc b/src/pmc/complexmatrix2d.pmc index 0d1e179..ebe144c 100644 --- a/src/pmc/complexmatrix2d.pmc +++ b/src/pmc/complexmatrix2d.pmc @@ -284,12 +284,12 @@ pmclass ComplexMatrix2D dynpmc auto_attrs provides matrix { VTABLE PMC * get_attr_str(STRING * idx) { Parrot_ComplexMatrix2D_attributes * const attrs = PARROT_COMPLEXMATRIX2D(SELF); - if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "X"))) { + if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "rows"))) { PMC * const rows = 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, "Y"))) { + else if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "cols"))) { PMC * const cols = pmc_new(INTERP, enum_class_Integer); VTABLE_set_integer_native(INTERP, cols, attrs->cols); return cols; diff --git a/src/pmc/nummatrix2d.pmc b/src/pmc/nummatrix2d.pmc index 8076540..1d056cc 100644 --- a/src/pmc/nummatrix2d.pmc +++ b/src/pmc/nummatrix2d.pmc @@ -482,12 +482,12 @@ pmclass NumMatrix2D dynpmc auto_attrs provides matrix { VTABLE PMC * get_attr_str(STRING * idx) { Parrot_NumMatrix2D_attributes * const attrs = PARROT_NUMMATRIX2D(SELF); - if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "X"))) { + if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "rows"))) { PMC * const rows = 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, "Y"))) { + else if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "cols"))) { PMC * const cols = pmc_new(INTERP, enum_class_Integer); VTABLE_set_integer_native(INTERP, cols, attrs->cols); return cols; diff --git a/src/pmc/pmcmatrix2d.pmc b/src/pmc/pmcmatrix2d.pmc index 0a53b99..1db8fac 100644 --- a/src/pmc/pmcmatrix2d.pmc +++ b/src/pmc/pmcmatrix2d.pmc @@ -7,36 +7,36 @@ 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. */ static void -resize_matrix(PARROT_INTERP, PMC * self, INTVAL x, INTVAL y) +resize_matrix(PARROT_INTERP, PMC * self, INTVAL col, INTVAL row) { Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(self); - const INTVAL old_x = attrs->x; - const INTVAL old_y = attrs->y; - 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 old_cols = attrs->cols; + const INTVAL old_rows = attrs->rows; + const INTVAL new_cols = INDEX_MAX(old_cols, col + 1); + const INTVAL new_rows = INDEX_MAX(old_rows, row + 1); + const INTVAL newsize = new_cols * new_rows; PMC ** new_s = (PMC **)mem_sys_allocate_zeroed(newsize * sizeof (PMC *)); PMC ** old_s = attrs->storage; - const INTVAL min_x = INDEX_MIN(old_x, new_x); - const INTVAL min_y = INDEX_MIN(old_y, new_y); + const INTVAL min_cols = INDEX_MIN(old_cols, new_cols); + const INTVAL min_rows = INDEX_MIN(old_rows, new_rows); 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); + for (i = 0; i < min_cols; i++) { + for (j = 0; j < min_rows; j++) { + ITEM_XY_ROWMAJOR(new_s, new_cols, new_rows, i, j) = + ITEM_XY_ROWMAJOR(old_s, old_cols, old_rows, i, j); } } attrs->storage = new_s; - attrs->x = new_x; - attrs->y = new_y; + attrs->cols = new_cols; + attrs->rows = new_rows; free(old_s); } pmclass PMCMatrix2D dynpmc auto_attrs provides matrix { ATTR PMC ** storage; - ATTR INTVAL x; - ATTR INTVAL y; + ATTR INTVAL cols; + ATTR INTVAL rows; VTABLE void init() { PObj_custom_mark_SET(SELF); @@ -53,8 +53,8 @@ pmclass PMCMatrix2D dynpmc auto_attrs provides matrix { VTABLE void mark() { Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF); PMC ** s = attrs->storage; - const INTVAL rows = attrs->y; - const INTVAL cols = attrs->x; + const INTVAL rows = attrs->rows; + const INTVAL cols = attrs->cols; const INTVAL size = rows * cols; INTVAL i; for (i = 0; i < size; i++) { @@ -65,21 +65,20 @@ pmclass PMCMatrix2D dynpmc auto_attrs provides matrix { } VTABLE PMC * get_pmc_keyed(PMC * key) { - INTVAL x, y, x_size, y_size; - Parrot_PMCMatrix2D_attributes * const attrs - = (Parrot_PMCMatrix2D_attributes *) PARROT_PMCMATRIX2D(SELF); - x_size = attrs->x; - y_size = attrs->y; - GET_KEY_INDICES_ROWMAJOR(INTERP, key, x, y); - if (x >= x_size || y >= y_size) + INTVAL cols, rows, cols_size, rows_size; + Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF); + cols_size = attrs->cols; + rows_size = attrs->rows; + GET_KEY_INDICES_ROWMAJOR(INTERP, key, cols, rows); + if (cols >= cols_size || rows >= rows_size) Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, "PMCMatrix2d: indices out of bounds"); - return ITEM_XY_ROWMAJOR(attrs->storage, x_size, y_size, x, y); + return ITEM_XY_ROWMAJOR(attrs->storage, cols_size, rows_size, cols, rows); } VTABLE PMC * get_pmc_keyed_int(INTVAL key) { Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF); - const INTVAL total_size = attrs->x * attrs->y; + const INTVAL total_size = attrs->cols * attrs->rows; if (key >= total_size || key < 0) { Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS, "PMCMatrix2D: indices out of bounds."); @@ -103,18 +102,17 @@ pmclass PMCMatrix2D dynpmc auto_attrs provides matrix { } VTABLE void set_pmc_keyed(PMC * key, PMC * value) { - INTVAL x, y, x_size, y_size; - Parrot_PMCMatrix2D_attributes * const attrs - = (Parrot_PMCMatrix2D_attributes *) PARROT_PMCMATRIX2D(SELF); - 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 cols, rows, cols_size, rows_size; + Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF); + cols_size = attrs->cols; + rows_size = attrs->rows; + GET_KEY_INDICES_ROWMAJOR(INTERP, key, cols, rows); + if (cols >= cols_size || rows >= rows_size) { + resize_matrix(INTERP, SELF, cols, rows); + cols_size = attrs->cols; + rows_size = attrs->rows; } - ITEM_XY_ROWMAJOR(attrs->storage, x_size, y_size, x, y) = value; + ITEM_XY_ROWMAJOR(attrs->storage, cols_size, rows_size, cols, rows) = value; } VTABLE void set_integer_keyed(PMC * key, INTVAL value) { @@ -142,8 +140,8 @@ pmclass PMCMatrix2D dynpmc auto_attrs provides matrix { STRING * const newline = Parrot_str_new(INTERP, "\n", 1); Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF); PMC ** const s = attrs->storage; - const INTVAL rows_size = attrs->x; - const INTVAL cols_size = attrs->y; + const INTVAL rows_size = attrs->rows; + const INTVAL cols_size = attrs->cols; for (cols = 0; cols < cols_size; ++cols) { for (rows = 0; rows < rows_size; ++rows) { PMC * const item = ITEM_XY_ROWMAJOR(s, rows_size, cols_size, rows, cols); @@ -158,6 +156,26 @@ pmclass PMCMatrix2D dynpmc auto_attrs provides matrix { pstr = Parrot_str_append(INTERP, pstr, newline); return pstr; } + + VTABLE PMC * get_attr_str(STRING * idx) { + Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF); + if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "rows"))) { + PMC * const rows = 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, "cols"))) { + PMC * const cols = 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, "size"))) { + PMC * const size = pmc_new(INTERP, enum_class_Integer); + VTABLE_set_integer_native(INTERP, size, attrs->cols * attrs->rows); + return size; + } + return PMCNULL; + } METHOD initialize_from_array(INTVAL rows_size, INTVAL cols_size, PMC * values) { Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF); @@ -166,8 +184,8 @@ pmclass PMCMatrix2D dynpmc auto_attrs provides matrix { const INTVAL init_elems = VTABLE_elements(interp, values); resize_matrix(interp, SELF, rows_size - 1, cols_size - 1); - self_rows = attrs->y; - self_cols = attrs->x; + self_rows = attrs->rows; + self_cols = attrs->cols; s = attrs->storage; for (i = 0; i < cols_size; i++) { @@ -181,3 +199,4 @@ pmclass PMCMatrix2D dynpmc auto_attrs provides matrix { } } } + diff --git a/t/pmc/nummatrix2d.t b/t/pmc/nummatrix2d.t index 305c143..84c9d57 100644 --- a/t/pmc/nummatrix2d.t +++ b/t/pmc/nummatrix2d.t @@ -132,22 +132,22 @@ sub vtable_get_attr_str() { Q:PIR { $P0 = new 'NumMatrix2D' $P0[2;5] = 1.0 - $P1 = getattribute $P0, "X" - is($P1, 3, "get first X") - $P2 = getattribute $P0, "Y" - is($P2, 6, "get first Y") + $P1 = getattribute $P0, "cols" + is($P1, 6, "get first cols") + $P2 = getattribute $P0, "rows" + is($P2, 3, "get first rows") $P0[2;7] = 2.0 - $P1 = getattribute $P0, "X" - is($P1, 3, "get first X again") - $P2 = getattribute $P0, "Y" - is($P2, 8, "get resized Y") + $P1 = getattribute $P0, "cols" + is($P1, 8, "get first cols again") + $P2 = getattribute $P0, "rows" + is($P2, 3, "get resized rows") $P0[10;7] = 3.0 - $P1 = getattribute $P0, "X" - is($P1, 11, "get resized X") - $P2 = getattribute $P0, "Y" - is($P2, 8, "get resized Y again") + $P1 = getattribute $P0, "cols" + is($P1, 8, "get resized cols") + $P2 = getattribute $P0, "rows" + is($P2, 11, "get resized rows again") } } @@ -448,22 +448,22 @@ sub vtable_clone() { sub method_resize() { Q:PIR { $P0 = new ['NumMatrix2D'] - $P1 = getattribute $P0, "X" - $P2 = getattribute $P0, "Y" - is($P1, 0, "new matrices are empty, X") - is($P2, 0, "new matrices are empty, Y") + $P1 = getattribute $P0, "cols" + $P2 = getattribute $P0, "rows" + is($P1, 0, "new matrices are empty, cols") + is($P2, 0, "new matrices are empty, rows") $P0.'resize'(3, 3) - $P1 = getattribute $P0, "X" - $P2 = getattribute $P0, "Y" - is($P1, 3, "matrices can grow on resize, X") - is($P2, 3, "matrices can grow on resize, Y") + $P1 = getattribute $P0, "cols" + $P2 = getattribute $P0, "rows" + is($P1, 3, "matrices can grow on resize, cols") + is($P2, 3, "matrices can grow on resize, rows") $P0.'resize'(1, 1) - $P1 = getattribute $P0, "X" - $P2 = getattribute $P0, "Y" - is($P1, 3, "matrices do not shrink, X") - is($P2, 3, "matrices do not shrink, Y") + $P1 = getattribute $P0, "cols" + $P2 = getattribute $P0, "rows" + is($P1, 3, "matrices do not shrink, cols") + is($P2, 3, "matrices do not shrink, rows") } }