1 /* bitset - a set of bits
2  * Copyright (c) 2003 Michael B. Allen <mba2000 ioplex.com>
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <limits.h>
26 #include <errno.h>
27 
28 #include "mba/msgno.h"
29 #include "mba/bitset.h"
30 
31 #define bitset_elem(ptr,bit) ((unsigned char *)(ptr))[(bit)/CHAR_BIT]
32 #define bitset_mask(bit) (1U << ((bit) % CHAR_BIT))
33 
34 int
bitset_isset(void * ptr,int bit)35 bitset_isset(void *ptr, int bit)
36 {
37 	return (bitset_elem(ptr,bit) & bitset_mask(bit)) != 0;
38 }
39 int
bitset_set(void * ptr,int bit)40 bitset_set(void *ptr, int bit)
41 {
42 	unsigned char *b = (unsigned char *)ptr + (bit / CHAR_BIT);
43 	unsigned char m = bitset_mask(bit);
44 
45 	if ((*b & m)) return 0;
46 	*b |= m;
47 
48 	return 1;
49 }
50 int
bitset_unset(void * ptr,int bit)51 bitset_unset(void *ptr, int bit)
52 {
53 	unsigned char *b = (unsigned char *)ptr + (bit / CHAR_BIT);
54 	unsigned char m = bitset_mask(bit);
55 
56 	if ((*b & m) == 0) return 0;
57 	*b &= ~m;
58 
59 	return 1;
60 }
61 void
bitset_toggle(void * ptr,int bit)62 bitset_toggle(void *ptr, int bit)
63 {
64 	bitset_elem(ptr,bit) ^= bitset_mask(bit);
65 }
66 int
bitset_find_first(void * ptr,void * plim,int val)67 bitset_find_first(void *ptr, void *plim, int val)
68 {
69 	unsigned char *start = ptr;
70 	unsigned char *bs;
71 	int cond = val ? 0x00 : 0xFF;
72 
73 	for (bs = start; bs < (unsigned char *)plim; bs++) {
74 		if ((*bs & 0xFF) != cond) {
75 			int b = *bs & 0xFF;
76 
77 				/* Index of the first bit that is 0 if
78 				 * val is 0 or 1 if val is 1.
79 				 */
80 			if (!val) {
81 				b = ~b;
82 			}
83 			b = b & -b;
84 			switch (b) {
85 				case 1: b = 0; break;
86 				case 2: b = 1; break;
87 				case 4: b = 2; break;
88 				case 8: b = 3; break;
89 				case 16: b = 4; break;
90 				case 32: b = 5; break;
91 				case 64: b = 6; break;
92 				case 128: b = 7; break;
93 			}
94 
95 			return b + (bs - start) * 8;
96 		}
97 	}
98 
99 	PMNO(errno = ENOENT);
100 	return -1;
101 }
102 void
bitset_iterate(iter_t * iter)103 bitset_iterate(iter_t *iter)
104 {
105 	iter->i1 = 0;
106 }
107 int
bitset_next(void * ptr,void * plim,iter_t * iter)108 bitset_next(void *ptr, void *plim, iter_t *iter)
109 {
110 	unsigned char *b = (unsigned char *)ptr + iter->i1 / 8;
111 	int ret = -1;
112 
113 	if (b < (unsigned char *)plim) {
114 		unsigned long mask = 1 << (iter->i1 % 8);
115 		iter->i1++;
116 		ret = (*b & mask) != 0;
117 	}
118 
119 	return ret;
120 }
121 
122