Permalink
Browse files

Update GET_KEY_INDICES_* macros to use new pmckey_to_coords()

  • Loading branch information...
1 parent 01e58f5 commit 25966ceadcb3a3def7f4798b591f09a7973e2ad8 @fernandobrito fernandobrito committed Dec 7, 2010
Showing with 51 additions and 6 deletions.
  1. +2 −0 src/include/pla_matrix_library.h
  2. +2 −6 src/include/pla_matrix_types.h
  3. +47 −0 src/lib/matrix_common.c
@@ -8,6 +8,8 @@ void get_complex_value_from_pmc(PARROT_INTERP, PMC * value, FLOATVAL * real,
void intkey_to_coords(PARROT_INTERP, const INTVAL rows, const INTVAL cols,
const INTVAL key, INTVAL * row, INTVAL * col);
+void pmckey_to_coords(PARROT_INTERP, PMC * key, INTVAL * row, INTVAL * col);
+
PMC * get_external_pmc(PARROT_INTERP, const INTVAL type);
PMC * get_external_pmc_init(PARROT_INTERP, const INTVAL type, PMC * const value);
@@ -27,16 +27,12 @@ extern INTVAL __PLA_Type_PMCMatrix2D;
#define GET_KEY_INDICES_ROWMAJOR(i, k, row, col) \
do { \
- (row) = VTABLE_get_integer((i), (k)); \
- (k) = VTABLE_shift_pmc((i), (k)); \
- (col) = VTABLE_get_integer((i), (k)); \
+ pmckey_to_coords((i), (k), (&row), (&col)); \
} while(0);
#define GET_KEY_INDICES_COLMAJOR(i, k, row, col) \
do { \
- (col) = VTABLE_get_integer((i), (k)); \
- (k) = VTABLE_shift_pmc((i), (k)); \
- (row) = VTABLE_get_integer((i), (k)); \
+ pmckey_to_coords((i), (k), (&row), (&col)); \
} while(0);
#define INDEX_MIN(a, b) (((a) <= (b))?(a):(b))
View
@@ -19,6 +19,53 @@ intkey_to_coords(PARROT_INTERP, const INTVAL rows, const INTVAL cols,
}
}
+void
+pmckey_to_coords(PARROT_INTERP, PMC * key, INTVAL * row, INTVAL * col)
+{
+ if (VTABLE_does(interp, key, Parrot_str_new(interp, "array", 5))) {
+ INTVAL size = VTABLE_elements(interp, key);
+
+ if (size == 1) {
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
+ "PLA: 1 element array to get matrix element. NOT IMPLEMENTED");
+ }
+ else if (size == 2) {
+ *row = VTABLE_shift_integer(interp, key);
+ *col = VTABLE_shift_integer(interp, key);
+ }
+ else
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
+ "PLA: array to get matrix element must have 1 or 2 elements");
+ }
+ else if (key->vtable->base_type == enum_class_Key) {
+ INTVAL element1, element2;
+
+ element1 = VTABLE_get_integer(interp, key);
+ key = VTABLE_shift_pmc(interp, key);
+
+ if (!key) {
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
+ "PLA: 1 element PMC key to get matrix element. NOT IMPLEMENTED");
+ }
+ else {
+ element2 = VTABLE_get_integer(interp, key);
+
+ *row = element1;
+ *col = element2;
+
+ key = VTABLE_shift_pmc(interp, key);
+
+ if (key)
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
+ "PLA: PMC key to get matrix element must have only 1 or 2 elements");
+ }
+ }
+ else {
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
+ "PLA: you should provide a PMC Key or array with 1 or 2 elements");
+ }
+}
+
/* Get an instance of an externally-defined (non-PLA) PMC type. Account for
subtypes */
PMC *

0 comments on commit 25966ce

Please sign in to comment.