jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.5KB

  1. /*
  2. * bitset.h -- some simple bit vector set operations.
  3. *
  4. * This is useful for sets of small non-negative integers. There are
  5. * some obvious set operations that are not implemented because I
  6. * don't need them right now.
  7. *
  8. * These functions represent sets as arrays of unsigned 32-bit
  9. * integers allocated on the heap. The first entry contains the set
  10. * cardinality (number of elements allowed), followed by one or more
  11. * words containing bit vectors.
  12. *
  13. * $Id: bitset.h,v 1.2 2005/11/23 11:24:29 letz Exp $
  14. */
  15. /*
  16. * Copyright (C) 2005 Jack O'Quin
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. */
  32. #ifndef __bitset_h__
  33. #define __bitset_h__
  34. #include <inttypes.h> /* POSIX standard fixed-size types */
  35. #include <assert.h> /* `#define NDEBUG' to disable */
  36. /* On some 64-bit machines, this implementation may be slightly
  37. * inefficient, depending on how compilers allocate space for
  38. * uint32_t. For the set sizes I currently need, this is acceptable.
  39. * It should not be hard to pack the bits better, if that becomes
  40. * worthwhile.
  41. */
  42. typedef uint32_t _bitset_word_t;
  43. typedef _bitset_word_t *bitset_t;
  44. #define WORD_SIZE(cardinality) (1+((cardinality)+31)/32)
  45. #define BYTE_SIZE(cardinality) (WORD_SIZE(cardinality)*sizeof(_bitset_word_t))
  46. #define WORD_INDEX(element) (1+(element)/32)
  47. #define BIT_INDEX(element) ((element)&037)
  48. static inline void
  49. bitset_add(bitset_t set
  50. , unsigned int element)
  51. {
  52. assert(element < set
  53. [0]);
  54. set
  55. [WORD_INDEX(element)] |= (1 << BIT_INDEX(element));
  56. }
  57. static inline void
  58. bitset_copy(bitset_t to_set, bitset_t from_set)
  59. {
  60. assert(to_set[0] == from_set[0]);
  61. memcpy(to_set, from_set, BYTE_SIZE(to_set[0]));
  62. }
  63. static inline void
  64. bitset_create(bitset_t *set
  65. , unsigned int cardinality)
  66. {
  67. *set
  68. = (bitset_t) calloc(WORD_SIZE(cardinality),
  69. sizeof(_bitset_word_t));
  70. assert(*set
  71. );
  72. *set
  73. [0] = cardinality;
  74. }
  75. static inline void
  76. bitset_destroy(bitset_t *set
  77. )
  78. {
  79. if (*set
  80. ) {
  81. free(*set
  82. );
  83. *set
  84. = (bitset_t) 0;
  85. }
  86. }
  87. static inline int
  88. bitset_empty(bitset_t set
  89. )
  90. {
  91. int i;
  92. _bitset_word_t result = 0;
  93. int nwords = WORD_SIZE(set
  94. [0]);
  95. for (i = 1; i < nwords; i++) {
  96. result |= set
  97. [i];
  98. }
  99. return (result == 0);
  100. }
  101. static inline int
  102. bitset_contains(bitset_t set
  103. , unsigned int element)
  104. {
  105. assert(element < set
  106. [0]);
  107. return (0 != (set
  108. [WORD_INDEX(element)] & (1 << BIT_INDEX(element))));
  109. }
  110. static inline void
  111. bitset_remove(bitset_t set
  112. , unsigned int element)
  113. {
  114. assert(element < set
  115. [0]);
  116. set
  117. [WORD_INDEX(element)] &= ~(1 << BIT_INDEX(element));
  118. }
  119. #endif /* __bitset_h__ */