Skip to content

Commit

Permalink
add methods initialize_from_array and initialize_from_args to complex…
Browse files Browse the repository at this point in the history
…matrix2d. Also add set_pmc_keyed_int and get_pmc_keyed_int vtables to it
  • Loading branch information
Whiteknight committed Mar 18, 2010
1 parent cc02185 commit c2e9cdb
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions src/pmc/complexmatrix2d.pmc
Expand Up @@ -48,23 +48,14 @@ resize_matrix(PARROT_INTERP, PMC * self, INTVAL row, INTVAL col)
static void
init_from_pmc_array(PARROT_INTERP, PMC * self, INTVAL rows_size, INTVAL cols_size, PMC * values) {
Parrot_ComplexMatrix2D_attributes * const attrs = PARROT_COMPLEXMATRIX2D(self);
FLOATVAL * s;
INTVAL self_rows, self_cols, i, j, num = 0;
INTVAL num = 0;
const INTVAL init_elems = VTABLE_elements(interp, values);

const INTVAL total_elems = rows_size * cols_size;
resize_matrix(interp, self, rows_size - 1, cols_size - 1);
self_rows = attrs->rows;
self_cols = attrs->cols;
s = attrs->storage;

for (i = 0; i < cols_size; i++) {
for (j = 0; j < rows_size; j++) {
const FLOATVAL value = VTABLE_get_number_keyed_int(interp, values, num);
num++;
R_ITEM_XY_ROWMAJOR(s, self_rows, self_cols, j, i) = value;
if (num >= init_elems)
return;
}
for (; num < init_elems && num < total_elems ; num++) {
PMC * const value = VTABLE_get_pmc_keyed_int(interp, values, num);
VTABLE_set_pmc_keyed_int(interp, self, num, value);
}
}

Expand Down Expand Up @@ -107,6 +98,8 @@ set_complex_pmc_at_xy(PARROT_INTERP, PMC * self, PMC * value, INTVAL row, INTVAL
}
s = attrs->storage;
switch (value->vtable->base_type) {
case enum_class_String:
value = Parrot_pmc_new_init(interp, enum_class_Complex, value);
case enum_class_Complex:
real = VTABLE_get_number_keyed_int(interp, value, 0);
imag = VTABLE_get_number_keyed_int(interp, value, 1);
Expand All @@ -123,6 +116,7 @@ set_complex_pmc_at_xy(PARROT_INTERP, PMC * self, PMC * value, INTVAL row, INTVAL
imag = 0.0;
break;
default:
/* TODO: We should support HLL-mapped types here. */
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ComplexMatrix2d: cannot set unknown PMC type");
}
Expand Down Expand Up @@ -219,6 +213,19 @@ pmclass ComplexMatrix2D dynpmc auto_attrs provides matrix {
return get_complex_pmc_at_xy(INTERP, SELF, rows, cols);
}

VTABLE PMC * get_pmc_keyed_int(INTVAL key) {
Parrot_ComplexMatrix2D_attributes * const attrs = PARROT_COMPLEXMATRIX2D(SELF);
const INTVAL rows = attrs->rows;
const INTVAL cols = attrs->cols;
const INTVAL row = key / rows;
const INTVAL col = key % rows;
if (row > rows || col > cols) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ComplexMatrix2D: Index out of bounds.");
}
return get_complex_pmc_at_xy(INTERP, SELF, row, col);
}

/*

=item* set_pmc_keyed
Expand All @@ -235,6 +242,19 @@ pmclass ComplexMatrix2D dynpmc auto_attrs provides matrix {
set_complex_pmc_at_xy(INTERP, SELF, value, rows, cols);
}

VTABLE void set_pmc_keyed_int(INTVAL key, PMC * value) {
Parrot_ComplexMatrix2D_attributes * const attrs = PARROT_COMPLEXMATRIX2D(SELF);
const INTVAL rows = attrs->rows;
const INTVAL cols = attrs->cols;
const INTVAL row = key / rows;
const INTVAL col = key % rows;
if (row > rows || col > cols) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ComplexMatrix2D: Index out of bounds.");
}
set_complex_pmc_at_xy(INTERP, SELF, value, row, col);
}

VTABLE void set_string_keyed(PMC * key, STRING * value) {
PMC * const item = Parrot_pmc_new(INTERP, enum_class_Complex);
VTABLE_set_string_native(INTERP, item, value);
Expand Down Expand Up @@ -535,6 +555,14 @@ value with the return value of the called function.
}
}

METHOD initialize_from_array(INTVAL rows_size, INTVAL cols_size, PMC *values) {
init_from_pmc_array(INTERP, SELF, rows_size, cols_size, values);
}

METHOD initialize_from_args(INTVAL rows_size, INTVAL cols_size, PMC *values :slurpy) {
init_from_pmc_array(INTERP, SELF, rows_size, cols_size, values);
}

/*

=back
Expand Down

0 comments on commit c2e9cdb

Please sign in to comment.