Skip to content

Commit

Permalink
Adding a few count functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Mar 24, 2017
1 parent ca889a4 commit 40d74c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ size_t bitset_intersection_count(const bitset_t * restrict b1, const bitset_t *
/* compute the difference in-place (to b1), to generate a new bitset first call bitset_copy */
void bitset_inplace_difference(bitset_t * restrict b1, const bitset_t * restrict b2);

/* compute the size of the difference */
size_t bitset_difference_count(const bitset_t *restrict b1, const bitset_t * restrict b2) ;

/* compute the symmetric difference in-place (to b1), return true if successful, to generate a new bitset first call bitset_copy */
bool bitset_inplace_symmetric_difference(bitset_t * restrict b1, const bitset_t * restrict b2);

/* compute the size of the symmetric difference */
size_t bitset_symmetric_difference_count(const bitset_t *restrict b1, const bitset_t * restrict b2);

/* iterate over the set bits
like so :
Expand Down
33 changes: 33 additions & 0 deletions src/bitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ void bitset_inplace_difference(bitset_t *restrict b1, const bitset_t * restrict
}
}


size_t bitset_difference_count(const bitset_t *restrict b1, const bitset_t * restrict b2) {
size_t minlength = b1->arraysize < b2->arraysize ? b1->arraysize : b2->arraysize;
size_t k = 0;
size_t answer = 0;
for( ; k < minlength; ++k) {
answer += __builtin_popcountll (b1->array[k] & ~ (b2->array[k]));
}
for( ; k < b1->arraysize ; ++k) {
answer += __builtin_popcountll (b1->array[k]);
}
return answer;
}

bool bitset_inplace_symmetric_difference(bitset_t *restrict b1, const bitset_t * restrict b2) {
size_t minlength = b1->arraysize < b2->arraysize ? b1->arraysize : b2->arraysize;
size_t k = 0;
Expand All @@ -222,3 +236,22 @@ bool bitset_inplace_symmetric_difference(bitset_t *restrict b1, const bitset_t *
}
return true;
}

size_t bitset_symmetric_difference_count(const bitset_t *restrict b1, const bitset_t * restrict b2) {
size_t minlength = b1->arraysize < b2->arraysize ? b1->arraysize : b2->arraysize;
size_t k = 0;
size_t answer = 0;
for( ; k < minlength; ++k) {
answer += __builtin_popcountll(b1->array[k] ^ b2->array[k]);
}
if(b2->arraysize > b1->arraysize) {
for( ; k < b2->arraysize; ++k) {
answer += __builtin_popcountll(b2->array[k]);
}
} else {
for( ; k < b1->arraysize; ++k) {
answer += __builtin_popcountll(b1->array[k]);
}
}
return true;
}

0 comments on commit 40d74c4

Please sign in to comment.