/* * Abstract Data Type Implementation file array2d.c * Array of pointer implementation * Special case for symmetric arrays */ #include <stdlib.h> #include <assert.h> #include "sym.h" struct s_array2d { int **values; /* Value storage */ int rows, cols; /* Dimensions */ }; /* * Given a pointer to a symmetric array and the coordinates of an * an element return a pointer to its location. Elements on the left * and below the diagonal are mapped using the diagonal as the axis of * symmetry */ static int * value_position(array2d a, int row, int col) { assert(row >= 0 && row < a->rows); assert(col >= 0 && col < a->cols); if (row > col) return (&(a->values[col][row])); else return (&(a->values[row][col])); } array2d new_array2d(int rows, int cols) { array2d a; int i; assert(rows == cols); /* Must be symmetric */ a = (array2d)malloc(sizeof(struct s_array2d)); assert(a != NULL); a->values = (int **)malloc(rows * sizeof(int *)); assert(a->values != NULL); for (i = 0; i < rows; i++) { a->values[i] = (int *)malloc((i + 1) * sizeof(int)); assert(a->values[i] != NULL); } a->rows = rows; a->cols = cols; return (a); } void set_val_array2d(array2d a, int row, int col, int val) { *value_position(a, row, col) = val; } int get_val_array2d(array2d a, int row, int col) { return (*value_position(a, row, col)); } void delete_array2d(array2d a) { int i; for (i = 0; i < a->rows; i++) free(a->values[i]); free(a->values); free(a); }