Debian Codegraph @

easel/esl_bitfield.c

raw symbols SCIP
1 /* esl_bitfield : compact bit flags
2 *
3 * Contents:
4 * 1. ESL_BITFIELD
5 * 2. Unit tests
6 * 3. Test driver
7 */
8 #include <esl_config.h>
9
10 #include <string.h>
11
12 #include "easel.h"
13 #include "esl_bitfield.h"
14
15
16 /*****************************************************************
17 * 1. ESL_BITFIELD
18 *****************************************************************/
19
20 /* Function: esl_bitfield_Create()
21 * Synopsis: Create a new bitfield.
22 * Incept: SRE, Sun 14 Apr 2019 [DL4378 LHR-BOS]
23 *
24 * Purpose: Create a new bitfield of size <nb> bits, with all bits
25 * initialized to off.
26 */
27 ESL_BITFIELD *
28 esl_bitfield_Create(int nb)
29 {
30 ESL_BITFIELD *b = NULL;
31 int nu = (nb + 63) / 64;
32 int status;
33
34 ESL_DASSERT1(( nb >= 1 ));
35
36 ESL_ALLOC(b, sizeof(ESL_BITFIELD));
37 b->b = NULL;
38
39 ESL_ALLOC(b->b, sizeof(uint64_t) * nu);
40 memset((void *) b->b, 0, sizeof(uint64_t) * nu);
41 b->nb = nb;
42 return b;
43
44 ERROR:
45 esl_bitfield_Destroy(b);
46 return NULL;
47 }
48
49
50 /* Function: esl_bitfield_Count()
51 * Synopsis: Return the number of bits that are set.
52 * Incept: SRE, Thu 18 Apr 2019
53 */
54 int
55 esl_bitfield_Count(const ESL_BITFIELD *b)
56 {
57 int n = 0;
58 int i;
59
60 for (i = 0; i < b->nb; i++)
61 if (esl_bitfield_IsSet(b, i)) n++;
62 return n;
63 }
64
65
66 /* Function: esl_bitfield_Destroy()
67 * Synopsis: Frees an <ESL_BITFIELD>
68 */
69 void
70 esl_bitfield_Destroy(ESL_BITFIELD *b)
71 {
72 if (b) free(b->b);
73 free(b);
74 }
75
76
77 /*****************************************************************
78 * 2. Unit tests
79 *****************************************************************/
80 #ifdef eslBITFIELD_TESTDRIVE
81
82 #include "esl_random.h"
83 #include "esl_vectorops.h"
84
85 static void
86 utest_randpattern(ESL_RANDOMNESS *rng)
87 {
88 char msg[] = "bitfield randpattern utest failed";
89 int nb = 1 + esl_rnd_Roll(rng, 192); // 1..192; 1-3 uint64_t's. Keep it small to exercise edge cases frequently.
90 int nu = (nb + 63) / 64;
91 int nset = esl_rnd_Roll(rng, nb+1); // we'll set between 0 and <nb> of the <nb> bits.
92 int *deal = NULL; // sample of <nset> elements out of <nb> that are initially set TRUE
93 int *bigflags = NULL; // bigflags[] are 1/0 for set/unset bits
94 ESL_BITFIELD *b = esl_bitfield_Create(nb);
95 int i;
96 int status;
97
98 if (nset > 0) ESL_ALLOC(deal, sizeof(int) * nset); // watch out for nset = 0 case; avoid zero malloc.
99 ESL_ALLOC(bigflags, sizeof(int) * nb);
100 esl_vec_ISet(bigflags, nb, FALSE);
101
102 if (nset > 0) esl_rnd_Deal(rng, nset, nb, deal);
103 for (i = 0; i < nset; i++) bigflags[deal[i]] = TRUE;
104 for (i = 0; i < nset; i++) esl_bitfield_Set(b, deal[i]);
105
106 if (esl_bitfield_Count(b) != nset) esl_fatal(msg);
107
108 for (i = 0; i < nb; i++) if (bigflags[i] != esl_bitfield_IsSet(b, i)) esl_fatal(msg);
109 for (i = 0; i < nb; i++) esl_bitfield_Toggle(b, i);
110
111 if (esl_bitfield_Count(b) != nb - nset) esl_fatal(msg);
112
113 for (i = 0; i < nb; i++) if (bigflags[i] == esl_bitfield_IsSet(b, i)) esl_fatal(msg);
114 for (i = 0; i < nb; i++) esl_bitfield_Clear(b, i); // do all bits, to test that clearing 0 bits leaves them 0
115
116 if (esl_bitfield_Count(b) != 0) esl_fatal(msg);
117 for (i = 0; i < nu; i++) if (b->b[i]) esl_fatal(msg); // note <nu>. this reaches inside the "opaque" ESL_BITFIELD and can break if that structure changes.
118
119 free(deal);
120 free(bigflags);
121 esl_bitfield_Destroy(b);
122 return;
123
124 ERROR:
125 esl_fatal(msg);
126 }
127 #endif // eslBITFIELD_TESTDRIVE
128
129 /*****************************************************************
130 * 3. Test driver
131 *****************************************************************/
132 #ifdef eslBITFIELD_TESTDRIVE
133
134 #include "esl_getopts.h"
135
136 static ESL_OPTIONS options[] = {
137 /* name type default env range toggles reqs incomp help docgroup*/
138 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
139 { "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
140 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
141 };
142 static char usage[] = "[-options]";
143 static char banner[] = "test driver for bitfield module";
144
145 int
146 main(int argc, char **argv)
147 {
148 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
149 ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));
150
151 fprintf(stderr, "## %s\n", argv[0]);
152 fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng));
153
154 utest_randpattern(rng);
155
156 fprintf(stderr, "# status = ok\n");
157
158 esl_randomness_Destroy(rng);
159 esl_getopts_Destroy(go);
160 return eslOK;
161 }
162 #endif // eslBITFIELD_TESTDRIVE