1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2019 Free Software Foundation, Inc.
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #ifndef BITVECTOR_H
18 #define BITVECTOR_H 1
19 
20 #include <limits.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 
24 enum { BITS_PER_ULONG = CHAR_BIT * sizeof (unsigned long int) };
25 
26 unsigned long int *bitvector_allocate(size_t n);
27 size_t bitvector_count (const unsigned long int *, size_t);
28 
29 static unsigned long int
bitvector_mask(size_t idx)30 bitvector_mask (size_t idx)
31 {
32   return 1UL << (idx % BITS_PER_ULONG);
33 }
34 
35 static const unsigned long int *
bitvector_unit(const unsigned long int * vec,size_t idx)36 bitvector_unit (const unsigned long int *vec, size_t idx)
37 {
38   return &vec[idx / BITS_PER_ULONG];
39 }
40 
41 static unsigned long int *
bitvector_unit_rw(unsigned long int * vec,size_t idx)42 bitvector_unit_rw (unsigned long int *vec, size_t idx)
43 {
44   return &vec[idx / BITS_PER_ULONG];
45 }
46 
47 static inline void
bitvector_set1(unsigned long int * vec,size_t idx)48 bitvector_set1 (unsigned long int *vec, size_t idx)
49 {
50   *bitvector_unit_rw (vec, idx) |= bitvector_mask (idx);
51 }
52 
53 static inline void
bitvector_set0(unsigned long int * vec,size_t idx)54 bitvector_set0 (unsigned long int *vec, size_t idx)
55 {
56   *bitvector_unit_rw (vec, idx) &= ~bitvector_mask (idx);
57 }
58 
59 static inline bool
bitvector_is_set(const unsigned long int * vec,size_t idx)60 bitvector_is_set (const unsigned long int *vec, size_t idx)
61 {
62   return (*bitvector_unit (vec, idx) & bitvector_mask (idx)) != 0;
63 }
64 
65 /* Returns 2**X, 0 <= X < 32. */
66 #define BIT_INDEX(X) (1ul << (X))
67 
68 #endif /* bitvector.h */
69