Debian Codegraph @

libmowgli-2/src/libmowgli/base/bitvector.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * bitvector.c: Bitvectors.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "mowgli.h"
25 #include "core/bootstrap_internal.h"
26
27 static mowgli_object_class_t klass;
28
29 /*
30 * mowgli_bitvector_init(void)
31 *
32 * Initialization code for the mowgli.bitvector library.
33 *
34 * Inputs:
35 * - none
36 *
37 * Outputs:
38 * - none
39 *
40 * Side Effects:
41 * - the mowgli_bitvector_t object class is registered.
42 */
43 void
44 mowgli_bitvector_bootstrap(void)
45 {
46 mowgli_object_class_init(&klass, "mowgli_bitvector_t", mowgli_free, FALSE);
47 }
48
49 /*
50 * mowgli_bitvector_create(int bits)
51 *
52 * Creates a bitvector.
53 *
54 * Inputs:
55 * - amount of bits that the bitvector should store
56 *
57 * Outputs:
58 * - a mowgli.bitvector object
59 *
60 * Side Effects:
61 * - none
62 */
63 mowgli_bitvector_t *
64 mowgli_bitvector_create(int bits)
65 {
66 mowgli_bitvector_t *bv = mowgli_alloc(sizeof *bv);
67
68 mowgli_object_init(mowgli_object(bv), "mowgli_bitvector_t", &klass, NULL);
69
70 bv->bits = bits;
71 bv->divisor = sizeof(int);
72 bv->vector = (unsigned int *) mowgli_alloc_array(bv->divisor, bv->bits / bv->divisor);
73
74 return bv;
75 }
76
77 /*
78 * mowgli_bitvector_set(mowgli_bitvector_t *bv, int slot, mowgli_boolean_t val)
79 *
80 * Sets a bit either ON or OFF in the bitvector.
81 *
82 * Inputs:
83 * - a mowgli bitvector object
84 * - a slot
85 * - the value for that slot
86 *
87 * Outputs:
88 * - nothing
89 *
90 * Side Effects:
91 * - a bit is either set ON or OFF in the bitvector.
92 */
93 void
94 mowgli_bitvector_set(mowgli_bitvector_t *bv, int slot, mowgli_boolean_t val)
95 {
96 int value = 1 << slot;
97
98 switch (val)
99 {
100 case FALSE:
101 bv->vector[bv->bits / bv->divisor] &= ~value;
102 break;
103 default:
104 case TRUE:
105 bv->vector[bv->bits / bv->divisor] |= value;
106 break;
107 }
108 }
109
110 /*
111 * mowgli_bitvector_get(mowgli_bitvector_t *bv, int slot)
112 *
113 * Returns whether the bit in a given slot is ON or OFF.
114 *
115 * Inputs:
116 * - a mowgli.bitvector object
117 * - a slot to lookup
118 *
119 * Outputs:
120 * - TRUE if the bit is on
121 * - FALSE otherwise
122 *
123 * Side Effects:
124 * - none
125 */
126 mowgli_boolean_t
127 mowgli_bitvector_get(mowgli_bitvector_t *bv, int slot)
128 {
129 int mask = 1 << slot;
130
131 return ((bv->vector[bv->bits / bv->divisor] & mask) != 0) ? TRUE : FALSE;
132 }
133
134 /*
135 * mowgli_bitvector_combine(mowgli_bitvector_t *bv1, mowgli_bitvector_t *bv2)
136 *
137 * Combines two bitvectors together.
138 *
139 * Inputs:
140 * - two bitvectors to be combined
141 *
142 * Outputs:
143 * - a new bitvector containing the combined result.
144 *
145 * Side Effects:
146 * - none
147 */
148 mowgli_bitvector_t *
149 mowgli_bitvector_combine(mowgli_bitvector_t *bv1, mowgli_bitvector_t *bv2)
150 {
151 int bits, iter, bs;
152 mowgli_bitvector_t *out;
153
154 return_val_if_fail(bv1 != NULL, NULL);
155 return_val_if_fail(bv2 != NULL, NULL);
156
157 /* choose the larger bitwidth */
158 bits = bv1->bits > bv2->bits ? bv1->bits : bv2->bits;
159
160 /* create the third bitvector. */
161 out = mowgli_bitvector_create(bits);
162
163 /* cache the size of the bitvector in memory. */
164 bs = out->bits / out->divisor;
165
166 for (iter = 0; iter < bs; iter++)
167 {
168 out->vector[iter] |= bv1->vector[iter];
169 out->vector[iter] |= bv2->vector[iter];
170 }
171
172 return out;
173 }
174
175 /*
176 * mowgli_bitvector_xor(mowgli_bitvector_t *bv1, mowgli_bitvector_t *bv2)
177 *
178 * XORs two bitvectors together.
179 *
180 * Inputs:
181 * - two bitvectors to be XORed
182 *
183 * Outputs:
184 * - a new bitvector containing the XORed result.
185 *
186 * Side Effects:
187 * - none
188 */
189 mowgli_bitvector_t *
190 mowgli_bitvector_xor(mowgli_bitvector_t *bv1, mowgli_bitvector_t *bv2)
191 {
192 int bits, iter, bs;
193 mowgli_bitvector_t *out;
194
195 return_val_if_fail(bv1 != NULL, NULL);
196 return_val_if_fail(bv2 != NULL, NULL);
197
198 /* choose the larger bitwidth */
199 bits = bv1->bits > bv2->bits ? bv1->bits : bv2->bits;
200
201 /* create the third bitvector. */
202 out = mowgli_bitvector_create(bits);
203
204 /* cache the size of the bitvector in memory. */
205 bs = out->bits / out->divisor;
206
207 for (iter = 0; iter < bs; iter++)
208 {
209 out->vector[iter] = bv1->vector[iter];
210 out->vector[iter] &= ~bv2->vector[iter];
211 }
212
213 return out;
214 }
215
216 /*
217 * mowgli_bitvector_compare(mowgli_bitvector_t *bv1, mowgli_bitvector_t *bv2)
218 *
219 * Compares two bitvectors to each other.
220 *
221 * Inputs:
222 * - two bitvectors to be compared
223 *
224 * Outputs:
225 * - TRUE if the bitvector is equal
226 * - FALSE otherwise
227 *
228 * Side Effects:
229 * - none
230 */
231 mowgli_boolean_t
232 mowgli_bitvector_compare(mowgli_bitvector_t *bv1, mowgli_bitvector_t *bv2)
233 {
234 int iter, bs;
235 mowgli_boolean_t ret = TRUE;
236
237 /* cache the size of the bitvector in memory. */
238 bs = bv1->bits / bv1->divisor;
239
240 for (iter = 0; iter < bs; iter++)
241 if (!(bv1->vector[iter] & bv2->vector[iter]))
242 ret = FALSE;
243
244 return ret;
245 }