Guest User

Untitled

a guest
Jun 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. /**
  2. * @file bitset.c
  3. * @brief Implementation of a bit set data structure.
  4. */
  5.  
  6. #include <stdlib.h>
  7. #include <assert.h>
  8.  
  9. #include "bitset.h"
  10.  
  11.  
  12. static inline unsigned int num_chunks(size_t length)
  13. {
  14. div_t d = div((int) length, UINT_BITS);
  15. return (unsigned int) (d.rem == 0 ? d.quot : d.quot+1);
  16. }
  17.  
  18. struct bitset *new_bitset(size_t length)
  19. {
  20. struct bitset *b;
  21. size_t total_size;
  22.  
  23. if (length == 0)
  24. return NULL;
  25.  
  26. total_size = sizeof(struct bitset) + num_chunks(length)*sizeof(unsigned);
  27.  
  28. if ((b = calloc(1, total_size)) == NULL)
  29. return NULL;
  30.  
  31. b->length = length;
  32.  
  33. return b;
  34. }
  35.  
  36. void delete_bitset(struct bitset *self)
  37. {
  38. assert(self);
  39.  
  40. free(self);
  41. }
Add Comment
Please, Sign In to add comment