Skip to content

Commit

Permalink
Add a few new accessors to NumMatrix2D. This includes STRING and PMC …
Browse files Browse the repository at this point in the history
…accessors, and linear '_keyed_int' accessors for people who want that too
  • Loading branch information
Whiteknight committed Oct 28, 2009
1 parent ba1ba44 commit 632761f
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/pmc/nummatrix2d.pmc
Expand Up @@ -35,15 +35,42 @@ resize_matrix(PARROT_INTERP, PMC * self, INTVAL x, INTVAL y)
}



pmclass NumMatrix2D dynpmc auto_attrs {
ATTR FLOATVAL * storage;
ATTR INTVAL x;
ATTR INTVAL y;

/*

=head1 VTABLEs

=over 4

=item* init

=cut

*/

VTABLE void init() {
PObj_custom_destroy_SET(SELF);
}

/*

=item* get_number_keyed

=item* get_integer_keyed

=item* get_string_keyed

=item* get_pmc_keyed

=cut

*/

VTABLE FLOATVAL get_number_keyed(PMC * key) {
INTVAL x, y, x_size, y_size;
Parrot_NumMatrix2D_attributes * const attrs
Expand All @@ -62,6 +89,72 @@ pmclass NumMatrix2D dynpmc auto_attrs {
return (INTVAL)f;
}

VTABLE STRING * get_string_keyed(PMC * key) {
FLOATVAL f = VTABLE_get_number_keyed(INTERP, SELF, key);
STRING * const item = Parrot_sprintf_c(INTERP, "\t%f", f);
return item;
}

VTABLE PMC * get_pmc_keyed(PMC * key) {
FLOATVAL f = VTABLE_get_number_keyed(INTERP, SELF, key);
PMC * const item = pmc_new(INTERP, enum_class_Float);
VTABLE_set_number_native(INTERP, item, f);
return item;
}

/*

=item* get_number_keyed_int

=item* get_integer_keyed_int

=item* get_string_keyed_int

=item* get_pmc_keyed_int

=cut

*/

VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
Parrot_NumMatrix2D_attributes * const attrs
= (Parrot_NumMatrix2D_attributes *) PARROT_NUMMATRIX2D(SELF);
const INTVAL total_size = attrs->x * attrs->y;
if (key >= total_size) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"NumMatrix2D: Matrix dimensions must match in add.");
}
return attrs->storage[key];
}

VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
FLOATVAL f = VTABLE_get_number_keyed_int(INTERP, SELF, key);
return (INTVAL)f;
}

VTABLE STRING * get_string_keyed_int(INTVAL key) {
FLOATVAL f = VTABLE_get_number_keyed_int(INTERP, SELF, key);
STRING * const item = Parrot_sprintf_c(INTERP, "\t%f", f);
return item;
}

VTABLE PMC * get_pmc_keyed_int(INTVAL key) {
FLOATVAL f = VTABLE_get_number_keyed_int(INTERP, SELF, key);
PMC * const item = pmc_new(INTERP, enum_class_Float);
VTABLE_set_number_native(INTERP, item, f);
return item;
}

/*

=item* set_number_keyed

=item* set_integer_keyed

=cut

*/

VTABLE void set_number_keyed(PMC * key, FLOATVAL value) {
INTVAL x, y, x_size, y_size;
Parrot_NumMatrix2D_attributes * const attrs
Expand All @@ -81,6 +174,14 @@ pmclass NumMatrix2D dynpmc auto_attrs {
VTABLE_set_number_keyed(INTERP, SELF, key, (FLOATVAL)value);
}

/*

=item* get_string

=cut

*/

VTABLE STRING *get_string() {
INTVAL x, y, x_size, y_size;
STRING *pstr = Parrot_str_new(INTERP, "", 0);
Expand Down

0 comments on commit 632761f

Please sign in to comment.