Raw
1 /**
2 * Copyright 2013, GitHub, Inc
3 * Copyright 2009-2013, Daniel Lemire, Cliff Moon,
4 * David McIntosh, Robert Becho, Google Inc. and Veronika Zenz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "git-compat-util.h"
20 #include "ewok.h"
21
22 #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
23 #define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
24
25 struct bitmap *bitmap_word_alloc(size_t word_alloc)
26 {
27 struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
28 CALLOC_ARRAY(bitmap->words, word_alloc);
29 bitmap->word_alloc = word_alloc;
30 return bitmap;
31 }
32
33 struct bitmap *bitmap_new(void)
34 {
35 return bitmap_word_alloc(32);
36 }
37
38 struct bitmap *bitmap_dup(const struct bitmap *src)
39 {
40 struct bitmap *dst = bitmap_word_alloc(src->word_alloc);
41 COPY_ARRAY(dst->words, src->words, src->word_alloc);
42 return dst;
43 }
44
45 static void bitmap_grow(struct bitmap *self, size_t word_alloc)
46 {
47 size_t old_size = self->word_alloc;
48 ALLOC_GROW(self->words, word_alloc, self->word_alloc);
49 MEMZERO_ARRAY(self->words + old_size, self->word_alloc - old_size);
50 }
51
52 void bitmap_set(struct bitmap *self, size_t pos)
53 {
54 size_t block = EWAH_BLOCK(pos);
55
56 bitmap_grow(self, block + 1);
57 self->words[block] |= EWAH_MASK(pos);
58 }
59
60 void bitmap_unset(struct bitmap *self, size_t pos)
61 {
62 size_t block = EWAH_BLOCK(pos);
63
64 if (block < self->word_alloc)
65 self->words[block] &= ~EWAH_MASK(pos);
66 }
67
68 int bitmap_get(struct bitmap *self, size_t pos)
69 {
70 size_t block = EWAH_BLOCK(pos);
71 return block < self->word_alloc &&
72 (self->words[block] & EWAH_MASK(pos)) != 0;
73 }
74
75 struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
76 {
77 struct ewah_bitmap *ewah = ewah_new();
78 size_t i, running_empty_words = 0;
79 eword_t last_word = 0;
80
81 for (i = 0; i < bitmap->word_alloc; ++i) {
82 if (bitmap->words[i] == 0) {
83 running_empty_words++;
84 continue;
85 }
86
87 if (last_word != 0)
88 ewah_add(ewah, last_word);
89
90 if (running_empty_words > 0) {
91 ewah_add_empty_words(ewah, 0, running_empty_words);
92 running_empty_words = 0;
93 }
94
95 last_word = bitmap->words[i];
96 }
97
98 ewah_add(ewah, last_word);
99 return ewah;
100 }
101
102 struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
103 {
104 struct bitmap *bitmap = bitmap_new();
105 struct ewah_iterator it;
106 eword_t blowup;
107 size_t i = 0;
108
109 ewah_iterator_init(&it, ewah);
110
111 while (ewah_iterator_next(&blowup, &it)) {
112 ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
113 bitmap->words[i++] = blowup;
114 }
115
116 bitmap->word_alloc = i;
117 return bitmap;
118 }
119
120 void bitmap_and_not(struct bitmap *self, struct bitmap *other)
121 {
122 const size_t count = (self->word_alloc < other->word_alloc) ?
123 self->word_alloc : other->word_alloc;
124
125 size_t i;
126
127 for (i = 0; i < count; ++i)
128 self->words[i] &= ~other->words[i];
129 }
130
131 void bitmap_or(struct bitmap *self, const struct bitmap *other)
132 {
133 size_t i;
134
135 bitmap_grow(self, other->word_alloc);
136 for (i = 0; i < other->word_alloc; i++)
137 self->words[i] |= other->words[i];
138 }
139
140 int ewah_bitmap_is_subset(struct ewah_bitmap *self, struct bitmap *other)
141 {
142 struct ewah_iterator it;
143 eword_t word;
144 size_t i;
145
146 ewah_iterator_init(&it, self);
147
148 for (i = 0; i < other->word_alloc; i++) {
149 if (!ewah_iterator_next(&word, &it)) {
150 /*
151 * If we reached the end of `self`, and haven't
152 * rejected `self` as a possible subset of
153 * `other` yet, then we are done and `self` is
154 * indeed a subset of `other`.
155 */
156 return 1;
157 }
158 if (word & ~other->words[i]) {
159 /*
160 * Otherwise, compare the next two pairs of
161 * words. If the word from `self` has bit(s) not
162 * in the word from `other`, `self` is not a
163 * subset of `other`.
164 */
165 return 0;
166 }
167 }
168
169 /*
170 * If we got to this point, there may be zero or more words
171 * remaining in `self`, with no remaining words left in `other`.
172 * If there are any bits set in the remaining word(s) in `self`,
173 * then `self` is not a subset of `other`.
174 */
175 while (ewah_iterator_next(&word, &it))
176 if (word)
177 return 0;
178
179 /* `self` is definitely a subset of `other` */
180 return 1;
181 }
182
183 void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
184 {
185 size_t original_size = self->word_alloc;
186 size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1;
187 size_t i = 0;
188 struct ewah_iterator it;
189 eword_t word;
190
191 if (self->word_alloc < other_final) {
192 self->word_alloc = other_final;
193 REALLOC_ARRAY(self->words, self->word_alloc);
194 MEMZERO_ARRAY(self->words + original_size,
195 self->word_alloc - original_size);
196 }
197
198 ewah_iterator_init(&it, other);
199
200 while (ewah_iterator_next(&word, &it))
201 self->words[i++] |= word;
202 }
203
204 size_t bitmap_popcount(struct bitmap *self)
205 {
206 size_t i, count = 0;
207
208 for (i = 0; i < self->word_alloc; ++i)
209 count += ewah_bit_popcount64(self->words[i]);
210
211 return count;
212 }
213
214 size_t ewah_bitmap_popcount(struct ewah_bitmap *self)
215 {
216 struct ewah_iterator it;
217 eword_t word;
218 size_t count = 0;
219
220 ewah_iterator_init(&it, self);
221
222 while (ewah_iterator_next(&word, &it))
223 count += ewah_bit_popcount64(word);
224
225 return count;
226 }
227
228 int bitmap_is_empty(struct bitmap *self)
229 {
230 size_t i;
231 for (i = 0; i < self->word_alloc; i++)
232 if (self->words[i])
233 return 0;
234 return 1;
235 }
236
237 int bitmap_equals(struct bitmap *self, struct bitmap *other)
238 {
239 struct bitmap *big, *small;
240 size_t i;
241
242 if (self->word_alloc < other->word_alloc) {
243 small = self;
244 big = other;
245 } else {
246 small = other;
247 big = self;
248 }
249
250 for (i = 0; i < small->word_alloc; ++i) {
251 if (small->words[i] != big->words[i])
252 return 0;
253 }
254
255 for (; i < big->word_alloc; ++i) {
256 if (big->words[i] != 0)
257 return 0;
258 }
259
260 return 1;
261 }
262
263 int bitmap_equals_ewah(struct bitmap *self, struct ewah_bitmap *other)
264 {
265 struct ewah_iterator it;
266 eword_t word;
267 size_t i = 0;
268
269 ewah_iterator_init(&it, other);
270
271 while (ewah_iterator_next(&word, &it))
272 if (word != (i < self->word_alloc ? self->words[i++] : 0))
273 return 0;
274
275 for (; i < self->word_alloc; i++)
276 if (self->words[i])
277 return 0;
278
279 return 1;
280 }
281
282 int bitmap_is_subset(struct bitmap *self, struct bitmap *other)
283 {
284 size_t common_size, i;
285
286 if (self->word_alloc < other->word_alloc)
287 common_size = self->word_alloc;
288 else {
289 common_size = other->word_alloc;
290 for (i = common_size; i < self->word_alloc; i++) {
291 if (self->words[i])
292 return 1;
293 }
294 }
295
296 for (i = 0; i < common_size; i++) {
297 if (self->words[i] & ~other->words[i])
298 return 1;
299 }
300 return 0;
301 }
302
303 void bitmap_free(struct bitmap *bitmap)
304 {
305 if (!bitmap)
306 return;
307
308 free(bitmap->words);
309 free(bitmap);
310 }