More added to bitset

This commit is contained in:
Leonardo committed 2020-05-04 18:19:18 -03:00
1 parent 963056c42d
commit c58f3df50a
3 files changed
+131 -6

No files matched your search

+60 -2
View File
@@ -124,10 +124,10 @@ static const char *cmc_string_fmt_bitset = "struct %s<%s> "
\
/* Utility Methods */ \
/* Translates a bit index to a word index */ \
static inline cmc_bitset_word PFX##_bit_to_index(cmc_bitset_word idx) \
static inline size_t PFX##_bit_to_index(size_t idx) \
{ \
/* Calculate how many shifts based on the size of cmc_bitset_word */ \
static const cmc_bitset_word shift = \
static const size_t shift = \
((sizeof(cmc_bitset_word) * 8) >> 6) > 0 \
? 6 \
: ((sizeof(cmc_bitset_word) * 8) >> 5) > 0 \
@@ -310,11 +310,69 @@ static const char *cmc_string_fmt_bitset = "struct %s<%s> "
\
bool PFX##_clear(struct SNAME *_bitset_, size_t bit_index) \
{ \
if (!PFX##_impl_resize(_bitset_, bit_index + 1, false)) \
return false; \
\
/* How many bits in a word */ \
const cmc_bitset_word bits = sizeof(cmc_bitset_word) * CHAR_BIT; \
\
size_t i = PFX##_bit_to_index(bit_index); \
\
_bitset_->buffer[i] &= ~(((cmc_bitset_word)1) << (bit_index % bits)); \
\
_bitset_->flag = cmc_flags.OK; \
\
if (_bitset_->callbacks && _bitset_->callbacks->delete) \
_bitset_->callbacks->delete (); \
\
return true; \
} \
\
bool PFX##_clear_range(struct SNAME *_bitset_, size_t from, size_t to) \
{ \
if (to < from) \
{ \
_bitset_->flag = cmc_flags.INVALID; \
return false; \
} \
\
if (!PFX##_impl_resize(_bitset_, to + 1, false)) \
return false; \
\
size_t start_index = PFX##_bit_to_index(from); \
size_t end_index = PFX##_bit_to_index(to); \
\
/* How many bits in a word */ \
const cmc_bitset_word bits = sizeof(cmc_bitset_word) * CHAR_BIT; \
\
/* 1111 ... 1111 */ \
const cmc_bitset_word ones = ~((cmc_bitset_word)0); \
\
cmc_bitset_word shift_start = from % bits; \
cmc_bitset_word shift_end = (cmc_bitset_word)(-(to + 1)) % bits; \
\
cmc_bitset_word start_mask = ones << shift_start; \
cmc_bitset_word end_mask = ones >> shift_end; \
\
if (start_index == end_index) \
{ \
_bitset_->buffer[end_index] &= ~(start_mask & end_mask); \
} \
else \
{ \
_bitset_->buffer[start_index] &= ~start_mask; \
\
for (size_t i = start_index; i < end_index; i++) \
_bitset_->buffer[i] = 0; \
\
_bitset_->buffer[end_index] &= ~end_mask; \
} \
\
_bitset_->flag = cmc_flags.OK; \
\
if (_bitset_->callbacks && _bitset_->callbacks->delete) \
_bitset_->callbacks->delete (); \
\
return true; \
} \
\