/*-----------------------------------------------------------------------------* | Bit2_get | Purpose: Finds the element stored at the given location of the | specified Bit2 | Arguments: a pointer to the 2D bit array, the matrix location as a pair | of ints | Returns: the value stored at the given matrix location of the 2D bit | array as an int | Fail cases: | - the pointer to the 2D bit array is null | - the given matrix location lies outside the bounds of the 2D | bit array *-----------------------------------------------------------------------------*/ int Bit2_get(Bit2_T bit2, int i, int j) { assert(bit2 != NULL); assert((i >= 0 && i < Bit2_width(bit2)) && (j >= 0 && j < Bit2_height(bit2))); int index = get_index(i, j, Bit2_height(bit2)); return Bit_get(bit2->bitmap, index); }
/* 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; }
/* * Tests whether bit stored in given cordinates is in the array. Returns 1, * if the bit in the column c and row r is one and returns 0 otherwise. */ extern int Bit2_get(T bit2, int col, int row) { assert (bit2->bit_vector); assert (bit2); int index = get_index(col, row, bit2->width); return (Bit_get(bit2->bit_vector, index)); }
/* * Calls the function pointed by apply for each element in the bit2, column * indices vary more rapidly than row indices. */ extern void Bit2_map_row_major(T bit2, void apply(int col, int row, int bit, void *cl), void *cl) { assert (bit2->bit_vector); assert (bit2); int width = bit2->width; int i; for (i = 0; i < bit2->dimensions; i++) { apply(get_column(i, width), get_row(i ,width), Bit_get(bit2->bit_vector, i), cl); } }
/* * Calls the function pointed by apply for each element in the Bit2, row * indices vary more rapidly than colum indices. */ extern void Bit2_map_col_major(T bit2, void apply(int col, int row, int bit, void *cl), void *cl) { assert (bit2->bit_vector); assert (bit2); int width = bit2->width; int index = 0; int j; int i; for(j = 0; j < bit2->width; j++){ index = j; apply(get_column(index, width), get_row(index ,width), Bit_get(bit2->bit_vector, index), cl); for(i = 0; i < ((bit2->height) - 1); i++){ index = index + width; apply(get_column(index, width), get_row(index ,width), Bit_get(bit2->bit_vector, index), cl); } } }
int main(int argc, char *argv[]) { int k, n, i; Bit_T bits; if (argc != 3) usage(); k = atoi(argv[1]); n = atoi(argv[2]); srand(time(0)); bits = Bit_new(n); for (i = 0; i < k; i++) { int j; do { j = rand()%n; } while (Bit_get(bits, j)); Bit_put(bits, j, 1); printf("%d\n", j); } Bit_free(&bits); return 0; }