/* puts each element into bit vector to check digits */ void check_setof9(int col, int row, UArray2_T a, void *elem, void *bit) { int digit = *(int *)elem; Bit_T b = bit; if (digit < 1 || digit > 9) { Bit_free(&b); UArray2_free(&a); exit(1); /* digit is already in vector */ } if (Bit_get(bit, digit) == 1) { Bit_free(&b); UArray2_free(&a); exit(1); /* if digit isn't in vector, change bit to 1 */ } else { Bit_put(bit, digit, 1); /*all nine digits are present, reset bit vector*/ } if (Bit_count(bit) == 9) { Bit_clear(bit, 1, 9); } (void) col; (void) row; (void) a; }
/*Wrapper function for all of the intermediary image decompression functions. This function stores the intermediary components between the compress format and the RGB format. It also calls the free-ing functions, which are universal to compress and decompress.*/ void decompress40(FILE *input) { unsigned height, width; int read = fscanf(input, "COMP40 Compressed image format 2\n%u %u", &width, &height); assert(read == 2); int c = getc(input); assert(c == '\n'); UArray2_T words = UArray2_new(height/2, width/2, sizeof(uint64_t)); UArray2_map_row_major(words, store_compressed, &input); Pnm_components wordparts = decomp_word_array(words); UArray2_free(&words); Pnm_cv compvid = decomp_components_array(wordparts); wordparts_free(wordparts); Pnm_ppm decompressed = decomp_cv_array(compvid); compvid_free(compvid); Pnm_ppmwrite(stdout, decompressed); Pnm_ppmfree(&decompressed); }
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 ")); }
/*takes in a pointer for a Word_image and frees the image*/ void Word_image_free(Word_image *image) { Word_image tempImage = *image; UArray2_T tempWords = tempImage->words; UArray2_free(&tempWords); free(tempImage); }
/* Free array2b */ extern void UArray2b_free (T *array2b) { assert(array2b && *array2b); /* Loop through outer UArray2 to free every inner UArray2 */ for (int i = 0; i < (*array2b)->WidInBlocks; i++) { for (int j = 0; j < (*array2b)->HgtInBlocks; j++) { UArray2_free(UArray2_at((*array2b)->grid, i, j)); } } /* Free outer UArray2 and the whole structure */ UArray2_free(&((*array2b)->grid)); FREE(*array2b); }
/* 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); } }
/* Takes in a pointer to an array2b and frees all of its allocated memory */ void UArray2b_free(T *array2b) { assert(array2b); assert(*array2b); UArray2_map_row_major((*array2b)->shell, free_blocks, NULL); UArray2_free(&((*array2b)->shell)); FREE(*array2b); }
extern void UArray2b_free (T *array2b) { assert(array2b); UArray2_map_row_major((*array2b)->blockarray, free_sub_arrays, NULL); UArray2_free(&(*array2b)->blockarray); FREE(*array2b); }
/* Frees each block in the shell array */ void free_blocks(int col, int row, UArray2_T a2, void *elem, void *cl) { (void)col; (void)a2; (void)row; (void)elem; (void)cl; UArray2_T *array2 = elem; UArray2_free(array2); }
void free_sub_arrays(int col, int row, UArray2_T blockarray, void *elem, void *cl) { assert(elem); UArray2_free((UArray2_T *)elem); (void) col; (void) row; (void) blockarray; (void) cl; }
void UArray2b_free(T *array2b) { assert(array2b && *array2b); T a2b = *array2b; int bwidth = UArray2_width(a2b->blocks); int bheight = UArray2_height(a2b->blocks); for (int i=0; i<bwidth; i++) { for (int j=0; j<bheight; j++) { UArray_free(UArray2_at(a2b->blocks,i,j)); } } UArray2_free(&(a2b->blocks)); free(*array2b); *array2b = NULL; }
/* check_solution() * * Function coordinates the solution checking process by calling helper * functions that check each portion of the puzzle. Creates a new 9x1 array that * contains ones and zeros. If a 1 is stored in element i of the array, this * indicates that the number i+1 has already been seen in that portion of the * puzzle - and thus the solution is incorrect. The function also frees this * array at the end of the checking process to avoid memory leaks. */ void check_solution(UArray2_T array) { UArray2_T curr_line_arr = UArray2_new(9, 1, sizeof(int)); UArray2_map_row_major(array, row_check, &curr_line_arr); UArray2_map_col_major(array, col_check, &curr_line_arr); for (int i = 0; i < 9; i += 3) { for (int j = 0; j < 9; j += 3) { box_check(array, j, i, curr_line_arr); } } UArray2_free(&curr_line_arr); }
/* main calls helper functions to get and process data from pgm file or stdin*/ int main(int argc, char *argv[]) { Pnmrdr_T reader = NULL; FILE *fp = NULL; if (argc == 1) { reader = Pnmrdr_new(stdin); } else if (argc == 2) { reader = file_handler(argv, &fp); } else if (argc > 2) { exit(1); } Pnmrdr_mapdata data = check_data(reader, fp); UArray2_T uarray2 = pixels_to_array(reader, data, fp); correct_sudoku(uarray2); UArray2_free(&uarray2); exit(0); }
/* main() * * Main function for the program. Takes a file containing the puzzle to be * checked as an argument or from stdin and creates a new 9x9 2-Dimensional * array to store the solution while it is being examined. Calls functions to * read in the solution and check the puzzle. Calls exit(0) if the program * returns from the check function successfully to indicate that the puzzle was * indeed correct. */ int main(int argc, char *argv[]) { FILE *srcfile; UArray2_T solution = UArray2_new(9, 9, sizeof(int)); assert(argc <= 2); if (argc == 1) { srcfile = stdin; assert(srcfile != NULL); } else { srcfile = fopen (argv[1], "rb"); assert(srcfile != NULL); } read_in_solution(solution, srcfile); fclose(srcfile); check_solution(solution); UArray2_free(&solution); exit(0); }