Guest User

Untitled

a guest
Jun 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #ifndef BITSET_H
  2. #define BITSET_H
  3. /**
  4. * @file bitset.h
  5. * @brief Interface to the bitset data structure.
  6. */
  7.  
  8. #include <stdlib.h>
  9. #include <limits.h>
  10.  
  11.  
  12. #define UINT_BITS sizeof(unsigned)*CHAR_BIT
  13.  
  14.  
  15. /**
  16. * Bitset data structure.
  17. */
  18. struct bitset {
  19. size_t length; /**< Length in bits. */
  20. unsigned chunk[]; /**< Storage for the set. */
  21. };
  22.  
  23.  
  24. extern struct bitset *new_bitset(size_t length);
  25.  
  26. extern void delete_bitset(struct bitset *self);
  27.  
  28. static inline void bitset_set(struct bitset *self, int index)
  29. {
  30. div_t d = div(index, UINT_BITS);
  31. self->chunk[d.quot] |= (1 << (unsigned) d.rem);
  32. }
  33.  
  34. static inline int bitset_get(const struct bitset *self, int index)
  35. {
  36. div_t d = div(index, UINT_BITS);
  37. return (self->chunk[d.quot] >> (unsigned) d.rem) & 1;
  38. }
  39.  
  40. static inline void bitset_clear(struct bitset *self, int index)
  41. {
  42. div_t d = div(index, UINT_BITS);
  43. self->chunk[d.quot] &= ~(1 << (unsigned) d.rem);
  44. }
  45. #endif // !BITSET_H
Add Comment
Please, Sign In to add comment