1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include <helpers/bitmask.h>
6
7
8#define bitsperlong (8 * sizeof(unsigned long))
9
10
11#define howmany(x, y) (((x)+((y)-1))/(y))
12
13
14#define longsperbits(n) howmany(n, bitsperlong)
15
16#define max(a, b) ((a) > (b) ? (a) : (b))
17
18
19
20
21
22
23struct bitmask *bitmask_alloc(unsigned int n)
24{
25 struct bitmask *bmp;
26
27 bmp = malloc(sizeof(*bmp));
28 if (bmp == 0)
29 return 0;
30 bmp->size = n;
31 bmp->maskp = calloc(longsperbits(n), sizeof(unsigned long));
32 if (bmp->maskp == 0) {
33 free(bmp);
34 return 0;
35 }
36 return bmp;
37}
38
39
40void bitmask_free(struct bitmask *bmp)
41{
42 if (bmp == 0)
43 return;
44 free(bmp->maskp);
45 bmp->maskp = (unsigned long *)0xdeadcdef;
46 free(bmp);
47}
48
49
50
51
52
53
54
55
56
57
58
59
60
61static unsigned int _getbit(const struct bitmask *bmp, unsigned int n)
62{
63 if (n < bmp->size)
64 return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1;
65 else
66 return 0;
67}
68
69
70static void _setbit(struct bitmask *bmp, unsigned int n, unsigned int v)
71{
72 if (n < bmp->size) {
73 if (v)
74 bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong);
75 else
76 bmp->maskp[n/bitsperlong] &=
77 ~(1UL << (n % bitsperlong));
78 }
79}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98static int scan_was_ok(int sret, char nextc, const char *ok_next_chars)
99{
100 return sret == 1 ||
101 (sret == 2 && strchr(ok_next_chars, nextc) != NULL);
102}
103
104static const char *nexttoken(const char *q, int sep)
105{
106 if (q)
107 q = strchr(q, sep);
108 if (q)
109 q++;
110 return q;
111}
112
113
114struct bitmask *bitmask_setbit(struct bitmask *bmp, unsigned int i)
115{
116 _setbit(bmp, i, 1);
117 return bmp;
118}
119
120
121struct bitmask *bitmask_setall(struct bitmask *bmp)
122{
123 unsigned int i;
124 for (i = 0; i < bmp->size; i++)
125 _setbit(bmp, i, 1);
126 return bmp;
127}
128
129
130struct bitmask *bitmask_clearall(struct bitmask *bmp)
131{
132 unsigned int i;
133 for (i = 0; i < bmp->size; i++)
134 _setbit(bmp, i, 0);
135 return bmp;
136}
137
138
139int bitmask_isallclear(const struct bitmask *bmp)
140{
141 unsigned int i;
142 for (i = 0; i < bmp->size; i++)
143 if (_getbit(bmp, i))
144 return 0;
145 return 1;
146}
147
148
149int bitmask_isbitset(const struct bitmask *bmp, unsigned int i)
150{
151 return _getbit(bmp, i);
152}
153
154
155unsigned int bitmask_first(const struct bitmask *bmp)
156{
157 return bitmask_next(bmp, 0);
158}
159
160
161unsigned int bitmask_last(const struct bitmask *bmp)
162{
163 unsigned int i;
164 unsigned int m = bmp->size;
165 for (i = 0; i < bmp->size; i++)
166 if (_getbit(bmp, i))
167 m = i;
168 return m;
169}
170
171
172unsigned int bitmask_next(const struct bitmask *bmp, unsigned int i)
173{
174 unsigned int n;
175 for (n = i; n < bmp->size; n++)
176 if (_getbit(bmp, n))
177 break;
178 return n;
179}
180
181
182
183
184
185
186
187
188
189
190
191int bitmask_parselist(const char *buf, struct bitmask *bmp)
192{
193 const char *p, *q;
194
195 bitmask_clearall(bmp);
196
197 q = buf;
198 while (p = q, q = nexttoken(q, ','), p) {
199 unsigned int a;
200 unsigned int b;
201 unsigned int s;
202 const char *c1, *c2;
203 char nextc;
204 int sret;
205
206 sret = sscanf(p, "%u%c", &a, &nextc);
207 if (!scan_was_ok(sret, nextc, ",-"))
208 goto err;
209 b = a;
210 s = 1;
211 c1 = nexttoken(p, '-');
212 c2 = nexttoken(p, ',');
213 if (c1 != NULL && (c2 == NULL || c1 < c2)) {
214 sret = sscanf(c1, "%u%c", &b, &nextc);
215 if (!scan_was_ok(sret, nextc, ",:"))
216 goto err;
217 c1 = nexttoken(c1, ':');
218 if (c1 != NULL && (c2 == NULL || c1 < c2)) {
219 sret = sscanf(c1, "%u%c", &s, &nextc);
220 if (!scan_was_ok(sret, nextc, ","))
221 goto err;
222 }
223 }
224 if (!(a <= b))
225 goto err;
226 if (b >= bmp->size)
227 goto err;
228 while (a <= b) {
229 _setbit(bmp, a, 1);
230 a += s;
231 }
232 }
233 return 0;
234err:
235 bitmask_clearall(bmp);
236 return -1;
237}
238
239
240
241
242
243
244
245
246
247
248static inline int emit(char *buf, int buflen, int rbot, int rtop, int len)
249{
250 if (len > 0)
251 len += snprintf(buf + len, max(buflen - len, 0), ",");
252 if (rbot == rtop)
253 len += snprintf(buf + len, max(buflen - len, 0), "%d", rbot);
254 else
255 len += snprintf(buf + len, max(buflen - len, 0), "%d-%d",
256 rbot, rtop);
257 return len;
258}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274int bitmask_displaylist(char *buf, int buflen, const struct bitmask *bmp)
275{
276 int len = 0;
277
278 unsigned int cur, rbot, rtop;
279
280 if (buflen > 0)
281 *buf = 0;
282 rbot = cur = bitmask_first(bmp);
283 while (cur < bmp->size) {
284 rtop = cur;
285 cur = bitmask_next(bmp, cur+1);
286 if (cur >= bmp->size || cur > rtop + 1) {
287 len = emit(buf, buflen, rbot, rtop, len);
288 rbot = cur;
289 }
290 }
291 return len;
292}
293