Skip to content

Commit

Permalink
add item_at methods to PMC- and ComplexMatrix2D types. Fix the last l…
Browse files Browse the repository at this point in the history
…ingering test failure with complex matrices. All important tests pass
  • Loading branch information
Whiteknight committed Aug 17, 2010
1 parent 2c2eaf1 commit 9e126c6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
20 changes: 17 additions & 3 deletions src/pmc/complexmatrix2d.pmc
Expand Up @@ -547,9 +547,23 @@ sizes, growing the matrix if needed.

*/

METHOD item_at(INTVAL row, INTVAL col) {
PMC * const p = get_complex_pmc_at_xy(INTERP, SELF, row, col);
RETURN(PMC * p);
METHOD item_at(INTVAL row, INTVAL col,
PMC * value :optional, INTVAL has_value :opt_flag) {
DECLATTRS(SELF, attrs);
const INTVAL rows = attrs->rows;
const INTVAL cols = attrs->cols;
if (row < 0 || col < 0 || row >= rows || col >= cols) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": Index out of bounds.");
}
if (has_value) {
set_complex_pmc_at_xy(INTERP, SELF, value, row, col);
RETURN(PMC * value);
}
else {
PMC * const p = get_complex_pmc_at_xy(INTERP, SELF, row, col);
RETURN(PMC * p);
}
}

/*
Expand Down
19 changes: 13 additions & 6 deletions src/pmc/pmcmatrix2d.pmc
Expand Up @@ -413,16 +413,21 @@ sizes, growing the matrix if needed.

*/

METHOD item_at(INTVAL row, INTVAL col) {
METHOD item_at(INTVAL row, INTVAL col,
PMC * value :optional, INTVAL has_value :opt_flag) {
DECLATTRS(SELF, attrs);
const INTVAL rows = attrs->rows;
const INTVAL cols = attrs->cols;
const INTVAL flags = attrs->flags;
PMC ** const s = attrs->storage;
PMC * p;
if (row >= rows || col >= cols || row < 0 || col < 0) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": indices out of bounds in item_at");
}
p = ITEM_XY(attrs->storage, attrs->flags, rows, cols, row, col);
if (has_value)
ITEM_XY(s, flags, rows, cols, row, col) = value;
p = ITEM_XY(s, flags, rows, cols, row, col);
RETURN(PMC * p);
}

Expand Down Expand Up @@ -510,7 +515,7 @@ value with the return value of the called function.
for (j = 0; j < cols_size; j++) {
PMC * const value = ITEM_XY_ROWMAJOR(old_s, rows_size, cols_size, i, j);
PMC * result = PMCNULL;
Parrot_ext_call(INTERP, func, "PPIIPf->N", SELF, value, i, j, args, &result);
Parrot_ext_call(INTERP, func, "PPIIPf->P", SELF, value, i, j, args, &result);
ITEM_XY_ROWMAJOR(new_s, rows_size, cols_size, i, j) = result;
}
}
Expand All @@ -526,6 +531,7 @@ value with the return value of the called function.
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
const INTVAL newsize = rows_size * cols_size;
const INTVAL flags = attrs->flags;
PMC ** const self_s = attrs->storage;
PMC ** new_s;
INTVAL i, j;
Expand All @@ -535,13 +541,14 @@ value with the return value of the called function.

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

for (i = 0; i < rows_size; i++) {
for (j = 0; j < cols_size; j++) {
PMC * const value = ITEM_XY_ROWMAJOR(self_s, rows_size, cols_size, i, j);
PMC * const value = ITEM_XY(self_s, flags, rows_size, cols_size, i, j);
PMC * result = PMCNULL;
Parrot_ext_call(INTERP, func, "PPIIPf->N", SELF, value, i, j, args, &result);
ITEM_XY_ROWMAJOR(new_s, rows_size, cols_size, i, j) = result;
Parrot_ext_call(INTERP, func, "PPIIPf->P", SELF, value, i, j, args, &result);
ITEM_XY(new_s, flags, rows_size, cols_size, i, j) = result;
}
}
RETURN(PMC * new_matrix);
Expand Down
4 changes: 2 additions & 2 deletions t/pmc/complexmatrix2d.t
Expand Up @@ -119,8 +119,8 @@ method test_METHOD_conjugate() {

method test_METHOD_iterate_function_inplace() {
my $m := self.matrix2x2("1+1i", "2+2i", "3+3i", "4+4i");
my $n := self.matrix2x2("3.5+1i", "4.5+1i", "5.5+1i", "6.5+1i");
$m.iterate_function_inplace(-> $matrix, $value, $row, $col {
my $n := self.matrix2x2("3.5+1i", "4.5+2i", "5.5+3i", "6.5+4i");
$m.iterate_function_inplace(sub ($matrix, $value, $row, $col) {
return ($value + 2.5);
});
assert_equal($m, $n, "Cannot iterate function in place");
Expand Down
3 changes: 1 addition & 2 deletions t/run_test
Expand Up @@ -11,8 +11,7 @@ INIT {
#class MyProgram is Program {
method main(*@args) {
#for @args {
my $test := "pmcmatrix2d.t";
my $sub := Nqp::compile_file("t/pmc/" ~ $test);
my $sub := Nqp::compile_file("t/pmc/nummatrix2d.t");
$sub[0]();
#}
}
Expand Down

0 comments on commit 9e126c6

Please sign in to comment.