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