Skip to content

Commit

Permalink
Add freeze/thaw to Complex- and PMCMatrix2D. Add tests for freeze/tha…
Browse files Browse the repository at this point in the history
…w that the primary types pass
  • Loading branch information
Whiteknight committed Jun 23, 2010
1 parent 63787f8 commit f5a1faa
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
53 changes: 53 additions & 0 deletions src/pmc/complexmatrix2d.pmc
Expand Up @@ -384,6 +384,59 @@ pmclass ComplexMatrix2D dynpmc auto_attrs provides matrix {
return 0;
}

/*

=item* freeze

=item* thaw

=cut

*/

VTABLE void freeze(PMC *info) {
Parrot_ComplexMatrix2D_attributes * const attrs = PARROT_COMPLEXMATRIX2D(SELF);
INTVAL const rows = attrs->rows;
INTVAL const cols = attrs->cols;
INTVAL const flags = attrs->flags;
INTVAL i, j;
FLOATVAL * const s = attrs->storage;
VTABLE_push_integer(INTERP, info, rows);
VTABLE_push_integer(INTERP, info, cols);
VTABLE_push_integer(INTERP, info, flags);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
FLOATVAL f = R_ITEM_XY(s, flags, rows, cols, i, j);
VTABLE_push_float(INTERP, info, f);
f = I_ITEM_XY(s, flags, rows, cols, i, j);
VTABLE_push_float(INTERP, info, f);
}
}
}

VTABLE void thaw(PMC *info) {
Parrot_ComplexMatrix2D_attributes * const attrs = PARROT_COMPLEXMATRIX2D(SELF);
INTVAL const rows = VTABLE_shift_integer(INTERP, info);
INTVAL const cols = VTABLE_shift_integer(INTERP, info);
INTVAL const flags = VTABLE_shift_integer(INTERP, info);
INTVAL i, j;
FLOATVAL * s;
attrs->rows = 0;
attrs->cols = 0;
attrs->storage = NULL;
attrs->flags = 0;
resize_matrix(INTERP, SELF, rows - 1, cols - 1);
s = attrs->storage;
attrs->flags = flags;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
FLOATVAL f = VTABLE_shift_float(INTERP, info);
R_ITEM_XY(s, flags, rows, cols, i, j) = f;
f = VTABLE_shift_float(INTERP, info);
I_ITEM_XY(s, flags, rows, cols, i, j) = f;
}
}
}

/*

Expand Down
50 changes: 50 additions & 0 deletions src/pmc/pmcmatrix2d.pmc
Expand Up @@ -295,6 +295,56 @@ pmclass PMCMatrix2D dynpmc auto_attrs provides matrix {
return other;
}

/*

=item* freeze

=item* thaw

=cut

*/

VTABLE void freeze(PMC *info) {
Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF);
INTVAL const rows = attrs->rows;
INTVAL const cols = attrs->cols;
INTVAL const flags = attrs->flags;
INTVAL i, j;
PMC ** const s = attrs->storage;
VTABLE_push_integer(INTERP, info, rows);
VTABLE_push_integer(INTERP, info, cols);
VTABLE_push_integer(INTERP, info, flags);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
PMC * const p = ITEM_XY(s, flags, rows, cols, i, j);
VTABLE_push_pmc(INTERP, info, p);
}
}
}

VTABLE void thaw(PMC *info) {
Parrot_PMCMatrix2D_attributes * const attrs = PARROT_PMCMATRIX2D(SELF);
INTVAL const rows = VTABLE_shift_integer(INTERP, info);
INTVAL const cols = VTABLE_shift_integer(INTERP, info);
INTVAL const flags = VTABLE_shift_integer(INTERP, info);
INTVAL i, j;
PMC ** s;
attrs->rows = 0;
attrs->cols = 0;
attrs->storage = NULL;
attrs->flags = 0;
resize_matrix(INTERP, SELF, rows - 1, cols - 1);
s = attrs->storage;
attrs->flags = flags;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
PMC * const p = VTABLE_shift_pmc(INTERP, info);
ITEM_XY(s, flags, rows, cols, i, j) = p;
}
}
}

METHOD initialize_from_array(INTVAL rows_size, INTVAL cols_size, PMC *values) {
init_from_pmc_array(INTERP, SELF, rows_size, cols_size, values);
}
Expand Down
11 changes: 9 additions & 2 deletions t/testlib/matrixtest.nqp
Expand Up @@ -179,11 +179,18 @@ class Pla::Matrix::Testcase is UnitTest::Testcase {
}

method test_VTABLE_freeze() {
todo("Tests Needed!");
assert_throws_nothing("Cannot set_pmc_keyed", {
my $m := self.fancymatrix2x2();
my $s := pir::freeze__SP($m);
})
}

method test_VTABLE_thaw() {
todo("Tests Needed!");
my $m := self.fancymatrix2x2();
my $s := pir::freeze__SP($m);
my $n := pir::thaw__PS($s);
assert_equal($m, $n, "Freeze/thaw does not create equal PMCs");
assert_not_same($m, $n, "Freeze/thaw returns original");
}

# Test to show that autoresizing behavior of the type is consistent.
Expand Down

0 comments on commit f5a1faa

Please sign in to comment.