/* 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; }
/* * Puts the given bit to the index corresponding to given col and row * coordinates in the array. Returns the previous value of that bit. */ extern int Bit2_put(T bit2, int col, int row, int bit) { assert (bit2->bit_vector); assert (bit2); int index = get_index(col, row, bit2->width); return (Bit_put(bit2->bit_vector, index, bit)); }
/*-----------------------------------------------------------------------------* | Bit2_put | Purpose: Inserts the given value into the given Bit2 at the specified | matrix location | Arguments: a pointer to the 2D array, the matrix location as a pair of | ints, the value to be inserted as an int | Returns: returns the value previously stored at the given location 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 | - the value to be inserted is not 0 or 1 *-----------------------------------------------------------------------------*/ int Bit2_put(Bit2_T bit2, int i, int j, int value) { assert(bit2 != NULL); assert((i >= 0 && i < Bit2_width(bit2)) && (j >= 0 && j < Bit2_height(bit2))); assert(value == 0 || value == 1); int index = get_index(i, j, Bit2_height(bit2)); return (Bit_put(bit2->bitmap, index, value)); }
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; }