// returns 0 on error, 1 if ok. int set_get_test() { bitset_container_t* B = bitset_container_create(); int x; printf("[%s] %s\n", __FILE__, __func__); if (B == NULL) { printf("Bug %s, line %d \n", __FILE__, __LINE__); return 0; } for (x = 0; x < 1 << 16; x++) { assert(!bitset_container_get(B, x)); } for (x = 0; x < 1 << 16; x += 3) { assert(bitset_container_cardinality(B) == x / 3); assert(!bitset_container_get(B, x)); bitset_container_set(B, (uint16_t)x); assert(bitset_container_get(B, x)); assert(bitset_container_cardinality(B) == x / 3 + 1); } for (x = 0; x < 1 << 16; x++) { int isset = bitset_container_get(B, (uint16_t)x); int shouldbeset = (x / 3 * 3 == x); if (isset != shouldbeset) { printf("Bug %s, line %d \n", __FILE__, __LINE__); bitset_container_free(B); return 0; } } if (bitset_container_cardinality(B) != (1 << 16) / 3 + 1) { printf("Bug %s, line %d \n", __FILE__, __LINE__); bitset_container_free(B); return 0; } if (bitset_container_compute_cardinality(B) != (1 << 16) / 3 + 1) { printf("Bug %s, line %d \n", __FILE__, __LINE__); bitset_container_free(B); return 0; } for (x = 0; x < 1 << 16; x += 3) { bitset_container_unset(B, (uint16_t)x); } if (bitset_container_cardinality(B) != 0) { printf("Bug %s, line %d \n", __FILE__, __LINE__); bitset_container_free(B); return 0; } if (bitset_container_compute_cardinality(B) != 0) { printf("Bug %s, line %d \n", __FILE__, __LINE__); bitset_container_free(B); return 0; } bitset_container_free(B); return 1; }
int get_test(bitset_container_t* B) { int card = 0; int x; for (x = 0; x < 1 << 16; x++) { card += bitset_container_get(B, (uint16_t)x); } return card; }
bool run_bitset_container_andnot(const run_container_t *src_1, const bitset_container_t *src_2, void **dst) { // follows the Java implementation as of June 2016 int card = run_container_cardinality(src_1); if (card <= DEFAULT_MAX_SIZE) { // must be an array array_container_t *answer = array_container_create_given_capacity(card); answer->cardinality = 0; for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) { rle16_t rle = src_1->runs[rlepos]; for (int run_value = rle.value; run_value <= rle.value + rle.length; ++run_value) { if (!bitset_container_get(src_2, (uint16_t)run_value)) { answer->array[answer->cardinality++] = (uint16_t)run_value; } } } *dst = answer; return false; } else { // we guess it will be a bitset, though have to check guess when // done bitset_container_t *answer = bitset_container_clone(src_2); uint32_t last_pos = 0; for (int32_t rlepos = 0; rlepos < src_1->n_runs; ++rlepos) { rle16_t rle = src_1->runs[rlepos]; uint32_t start = rle.value; uint32_t end = start + rle.length + 1; bitset_reset_range(answer->array, last_pos, start); bitset_flip_range(answer->array, start, end); last_pos = end; } bitset_reset_range(answer->array, last_pos, (uint32_t)(1 << 16)); answer->cardinality = bitset_container_compute_cardinality(answer); if (answer->cardinality <= DEFAULT_MAX_SIZE) { *dst = array_container_from_bitset(answer); bitset_container_free(answer); return false; // not bitset } *dst = answer; return true; // bitset } }