Skip to content

Commit

Permalink
New interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Apr 14, 2017
1 parent 4cbf0f0 commit 9269774
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions benchmarks/lemirebenchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ size_t iterate(bitset_t *b1) {
return sum;
}

bool incr(size_t value, void * param) {
size_t sum;
memcpy(&sum, param, sizeof(size_t));
sum ++;
memcpy(param, &sum, sizeof(size_t));
return true;
}

size_t iterate2(bitset_t *b1) {
size_t sum = 0;
bitset_for_each(b1,incr,&sum);
return sum;
}



int main() {
int repeat = 10;
Expand All @@ -74,6 +89,8 @@ int main() {
assert(bitset_count(b1) == iterate(b1));
BEST_TIME_CHECK(bitset_count(b1),count,repeat);
BEST_TIME_CHECK(iterate(b1),count,repeat);
BEST_TIME_CHECK(iterate2(b1),count,repeat);


bitset_free(b1);

Expand Down
17 changes: 17 additions & 0 deletions include/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ static inline bool nextSetBit(const bitset_t *bitset, size_t *i) {
}
return false;
}
typedef bool (*bitset_iterator)(size_t value, void *param);

// return true if uninterrupted
static inline bool bitset_for_each(const bitset_t *b, bitset_iterator iterator, void *ptr) {
size_t base = 0;
for (size_t i = 0; i < b->arraysize; ++i ) {
uint64_t w = b->array[i];
while (w != 0) {
uint64_t t = w & (~w + 1);
int r = __builtin_ctzll(w);
if(!iterator(r + base, ptr)) return false;
w ^= t;
}
base += 64;
}
return true;
}

static inline void bitset_print(const bitset_t *b) {
printf("{");
Expand Down
22 changes: 22 additions & 0 deletions tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ void test_iterate() {
bitset_free(b);
}

bool increment(size_t value, void *param) {
size_t k;
memcpy(&k, param, sizeof(size_t));
assert(value == k);
k += 3;
memcpy(param, &k, sizeof(size_t));
return true;
}

void test_iterate2() {
bitset_t * b = bitset_create();
for(int k = 0; k < 1000; ++k)
bitset_set(b,3*k);
assert(bitset_count(b) == 1000);
size_t k = 0;
bitset_for_each(b,increment,&k);
assert(k == 3000);
bitset_free(b);
}


void test_construct() {
bitset_t * b = bitset_create();
for(int k = 0; k < 1000; ++k)
Expand Down Expand Up @@ -85,6 +106,7 @@ int main() {
test_construct();
test_union_intersection();
test_iterate();
test_iterate2();
test_max_min();
test_counts();
printf("All asserts passed. Code is probably ok.\n");
Expand Down

0 comments on commit 9269774

Please sign in to comment.