int main(int argc, char *argv[]) { (void)argc; (void)argv; UArray2_T test_array; bool OK = true; test_array = UArray2_new(DIM1, DIM2, ELEMENT_SIZE); //These functions check that the dimension has been set correctly OK &= (UArray2_width(test_array) == DIM1); OK &= (UArray2_height(test_array) == DIM2); OK &= (UArray2_size(test_array) == ELEMENT_SIZE); /* Note: we are only setting a value on the corner of the array */ *((number *)UArray2_at(test_array, DIM1-1, DIM2-1)) = MARKER; printf("Trying column major\n"); UArray2_map_col_major(test_array, check_and_print, &OK); printf("Trying row major\n"); UArray2_map_row_major(test_array, check_and_print, &OK); printf(" width = %d\n", UArray2_width(test_array)); printf("height %d\n", UArray2_height(test_array)); UArray2_free(&test_array); printf("The array is %sOK!\n", (OK ? "" : "NOT ")); }
/* reset_to_zero() * * Function is mapped to a 2-Dimensional array and replaces each element of the * array with a 0. */ void reset_to_zero(int col, int row, UArray2_T array, void *element, void *cl) { (void) col; (void) row; (void) cl; (void) array; assert(sizeof(*(int *)element) == UArray2_size(array)); *(int *)element = 0; }
/* box_check() * * Function is passed the upper right hand corner of one of the 3x3 subsections * of the sudoku puzzle and loops through each element of the section using the * frequency array to determine if there are any repeated elements. Calls a * helper function to perform the actual duplicate check. */ void box_check(UArray2_T array, int col, int row, UArray2_T freq_arr) { UArray2_map_row_major(freq_arr, reset_to_zero, NULL); for (int i = row; i < (row + 3); i++) { for (int j = col; j < (col + 3); j++) { int *curr_num = UArray2_at(array, j, i); assert(sizeof(*curr_num) == UArray2_size(array)); check_for_duplicate(array, freq_arr, (void *)curr_num); } } }
/* fill_array() * * Function is mapped to each element of the 2-Dimensional array storing the * sudoku solution. At each index of the array, it gets the next unread integer * from the puzzle using Pnmrdr and stores it in the array. */ void fill_array(int col, int row, UArray2_T array, void *element, void *cl) { (void) col; (void) row; (void) array; assert(sizeof(*(int *)element) == UArray2_size(array)); int temp = Pnmrdr_get(cl); assert(temp > 0 && temp < 10); *(int *)element = temp; }
/* col_check() * * Function is mapped to each element of the array in column-major order. For * each element it uses a helper function to check if that number has already * been seen in the current column. Reesets the 9x1 frequency array to all 0's * each time a new column is examined. */ void col_check(int col, int row, UArray2_T array, void *element, void *cl) { (void) col; (void) array; assert(sizeof(*(int *)element) == UArray2_size(array)); if (row == 0) { UArray2_map_row_major(*(UArray2_T *)cl, reset_to_zero, NULL); } check_for_duplicate(array, *(UArray2_T *)cl, element); }
/* check_for_duplicate() * * Function takes the 2-Dimensional array containing the solution being checked, * the 9x1 frequency array and a pointer to the current element in the solution * puzzle being checked. Looks at index (current element - 1) of the frequency * array to determine if the current element has already been seen in the * current portion of the puzzle. Calls exit(1) if a duplicate is found. */ void check_for_duplicate(UArray2_T sol_arr, UArray2_T freq_arr, void *element) { int curr_num = *(int *)element; int *already_seen = NULL; already_seen = (int *)(UArray2_at(freq_arr, (curr_num - 1), 0)); assert(sizeof(*already_seen) == UArray2_size(sol_arr)); if (*already_seen == 0) { *already_seen = 1; } else { UArray2_free(&sol_arr); UArray2_free(&freq_arr); exit(1); } }
int UArray2b_size(T array2b) { assert(array2b); return UArray2_size(array2b->blocks); }
extern int UArray2b_size (T array2b) { assert(array2b); return UArray2_size(array2b->blockarray); }