Line | Count | Source (jump to first uncovered line) |
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 | 0 | #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD)) |
23 | 0 | #define EWAH_BLOCK(x) (x / BITS_IN_EWORD) |
24 | | |
25 | | struct bitmap *bitmap_word_alloc(size_t word_alloc) |
26 | 0 | { |
27 | 0 | struct bitmap *bitmap = xmalloc(sizeof(struct bitmap)); |
28 | 0 | CALLOC_ARRAY(bitmap->words, word_alloc); |
29 | 0 | bitmap->word_alloc = word_alloc; |
30 | 0 | return bitmap; |
31 | 0 | } |
32 | | |
33 | | struct bitmap *bitmap_new(void) |
34 | 0 | { |
35 | 0 | return bitmap_word_alloc(32); |
36 | 0 | } |
37 | | |
38 | | struct bitmap *bitmap_dup(const struct bitmap *src) |
39 | 0 | { |
40 | 0 | struct bitmap *dst = bitmap_word_alloc(src->word_alloc); |
41 | 0 | COPY_ARRAY(dst->words, src->words, src->word_alloc); |
42 | 0 | return dst; |
43 | 0 | } |
44 | | |
45 | | static void bitmap_grow(struct bitmap *self, size_t word_alloc) |
46 | 0 | { |
47 | 0 | size_t old_size = self->word_alloc; |
48 | 0 | ALLOC_GROW(self->words, word_alloc, self->word_alloc); |
49 | 0 | memset(self->words + old_size, 0x0, |
50 | 0 | (self->word_alloc - old_size) * sizeof(eword_t)); |
51 | 0 | } |
52 | | |
53 | | void bitmap_set(struct bitmap *self, size_t pos) |
54 | 0 | { |
55 | 0 | size_t block = EWAH_BLOCK(pos); |
56 | |
|
57 | 0 | bitmap_grow(self, block + 1); |
58 | 0 | self->words[block] |= EWAH_MASK(pos); |
59 | 0 | } |
60 | | |
61 | | void bitmap_unset(struct bitmap *self, size_t pos) |
62 | 0 | { |
63 | 0 | size_t block = EWAH_BLOCK(pos); |
64 | |
|
65 | 0 | if (block < self->word_alloc) |
66 | 0 | self->words[block] &= ~EWAH_MASK(pos); |
67 | 0 | } |
68 | | |
69 | | int bitmap_get(struct bitmap *self, size_t pos) |
70 | 0 | { |
71 | 0 | size_t block = EWAH_BLOCK(pos); |
72 | 0 | return block < self->word_alloc && |
73 | 0 | (self->words[block] & EWAH_MASK(pos)) != 0; |
74 | 0 | } |
75 | | |
76 | | struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap) |
77 | 0 | { |
78 | 0 | struct ewah_bitmap *ewah = ewah_new(); |
79 | 0 | size_t i, running_empty_words = 0; |
80 | 0 | eword_t last_word = 0; |
81 | |
|
82 | 0 | for (i = 0; i < bitmap->word_alloc; ++i) { |
83 | 0 | if (bitmap->words[i] == 0) { |
84 | 0 | running_empty_words++; |
85 | 0 | continue; |
86 | 0 | } |
87 | | |
88 | 0 | if (last_word != 0) |
89 | 0 | ewah_add(ewah, last_word); |
90 | |
|
91 | 0 | if (running_empty_words > 0) { |
92 | 0 | ewah_add_empty_words(ewah, 0, running_empty_words); |
93 | 0 | running_empty_words = 0; |
94 | 0 | } |
95 | |
|
96 | 0 | last_word = bitmap->words[i]; |
97 | 0 | } |
98 | |
|
99 | 0 | ewah_add(ewah, last_word); |
100 | 0 | return ewah; |
101 | 0 | } |
102 | | |
103 | | struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah) |
104 | 0 | { |
105 | 0 | struct bitmap *bitmap = bitmap_new(); |
106 | 0 | struct ewah_iterator it; |
107 | 0 | eword_t blowup; |
108 | 0 | size_t i = 0; |
109 | |
|
110 | 0 | ewah_iterator_init(&it, ewah); |
111 | |
|
112 | 0 | while (ewah_iterator_next(&blowup, &it)) { |
113 | 0 | ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc); |
114 | 0 | bitmap->words[i++] = blowup; |
115 | 0 | } |
116 | |
|
117 | 0 | bitmap->word_alloc = i; |
118 | 0 | return bitmap; |
119 | 0 | } |
120 | | |
121 | | void bitmap_and_not(struct bitmap *self, struct bitmap *other) |
122 | 0 | { |
123 | 0 | const size_t count = (self->word_alloc < other->word_alloc) ? |
124 | 0 | self->word_alloc : other->word_alloc; |
125 | |
|
126 | 0 | size_t i; |
127 | |
|
128 | 0 | for (i = 0; i < count; ++i) |
129 | 0 | self->words[i] &= ~other->words[i]; |
130 | 0 | } |
131 | | |
132 | | void bitmap_or(struct bitmap *self, const struct bitmap *other) |
133 | 0 | { |
134 | 0 | size_t i; |
135 | |
|
136 | 0 | bitmap_grow(self, other->word_alloc); |
137 | 0 | for (i = 0; i < other->word_alloc; i++) |
138 | 0 | self->words[i] |= other->words[i]; |
139 | 0 | } |
140 | | |
141 | | int ewah_bitmap_is_subset(struct ewah_bitmap *self, struct bitmap *other) |
142 | 0 | { |
143 | 0 | struct ewah_iterator it; |
144 | 0 | eword_t word; |
145 | 0 | size_t i; |
146 | |
|
147 | 0 | ewah_iterator_init(&it, self); |
148 | |
|
149 | 0 | for (i = 0; i < other->word_alloc; i++) { |
150 | 0 | if (!ewah_iterator_next(&word, &it)) { |
151 | | /* |
152 | | * If we reached the end of `self`, and haven't |
153 | | * rejected `self` as a possible subset of |
154 | | * `other` yet, then we are done and `self` is |
155 | | * indeed a subset of `other`. |
156 | | */ |
157 | 0 | return 1; |
158 | 0 | } |
159 | 0 | if (word & ~other->words[i]) { |
160 | | /* |
161 | | * Otherwise, compare the next two pairs of |
162 | | * words. If the word from `self` has bit(s) not |
163 | | * in the word from `other`, `self` is not a |
164 | | * subset of `other`. |
165 | | */ |
166 | 0 | return 0; |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | | /* |
171 | | * If we got to this point, there may be zero or more words |
172 | | * remaining in `self`, with no remaining words left in `other`. |
173 | | * If there are any bits set in the remaining word(s) in `self`, |
174 | | * then `self` is not a subset of `other`. |
175 | | */ |
176 | 0 | while (ewah_iterator_next(&word, &it)) |
177 | 0 | if (word) |
178 | 0 | return 0; |
179 | | |
180 | | /* `self` is definitely a subset of `other` */ |
181 | 0 | return 1; |
182 | 0 | } |
183 | | |
184 | | void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other) |
185 | 0 | { |
186 | 0 | size_t original_size = self->word_alloc; |
187 | 0 | size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1; |
188 | 0 | size_t i = 0; |
189 | 0 | struct ewah_iterator it; |
190 | 0 | eword_t word; |
191 | |
|
192 | 0 | if (self->word_alloc < other_final) { |
193 | 0 | self->word_alloc = other_final; |
194 | 0 | REALLOC_ARRAY(self->words, self->word_alloc); |
195 | 0 | memset(self->words + original_size, 0x0, |
196 | 0 | (self->word_alloc - original_size) * sizeof(eword_t)); |
197 | 0 | } |
198 | |
|
199 | 0 | ewah_iterator_init(&it, other); |
200 | |
|
201 | 0 | while (ewah_iterator_next(&word, &it)) |
202 | 0 | self->words[i++] |= word; |
203 | 0 | } |
204 | | |
205 | | size_t bitmap_popcount(struct bitmap *self) |
206 | 0 | { |
207 | 0 | size_t i, count = 0; |
208 | |
|
209 | 0 | for (i = 0; i < self->word_alloc; ++i) |
210 | 0 | count += ewah_bit_popcount64(self->words[i]); |
211 | |
|
212 | 0 | return count; |
213 | 0 | } |
214 | | |
215 | | size_t ewah_bitmap_popcount(struct ewah_bitmap *self) |
216 | 0 | { |
217 | 0 | struct ewah_iterator it; |
218 | 0 | eword_t word; |
219 | 0 | size_t count = 0; |
220 | |
|
221 | 0 | ewah_iterator_init(&it, self); |
222 | |
|
223 | 0 | while (ewah_iterator_next(&word, &it)) |
224 | 0 | count += ewah_bit_popcount64(word); |
225 | |
|
226 | 0 | return count; |
227 | 0 | } |
228 | | |
229 | | int bitmap_is_empty(struct bitmap *self) |
230 | 0 | { |
231 | 0 | size_t i; |
232 | 0 | for (i = 0; i < self->word_alloc; i++) |
233 | 0 | if (self->words[i]) |
234 | 0 | return 0; |
235 | 0 | return 1; |
236 | 0 | } |
237 | | |
238 | | int bitmap_equals(struct bitmap *self, struct bitmap *other) |
239 | 0 | { |
240 | 0 | struct bitmap *big, *small; |
241 | 0 | size_t i; |
242 | |
|
243 | 0 | if (self->word_alloc < other->word_alloc) { |
244 | 0 | small = self; |
245 | 0 | big = other; |
246 | 0 | } else { |
247 | 0 | small = other; |
248 | 0 | big = self; |
249 | 0 | } |
250 | |
|
251 | 0 | for (i = 0; i < small->word_alloc; ++i) { |
252 | 0 | if (small->words[i] != big->words[i]) |
253 | 0 | return 0; |
254 | 0 | } |
255 | | |
256 | 0 | for (; i < big->word_alloc; ++i) { |
257 | 0 | if (big->words[i] != 0) |
258 | 0 | return 0; |
259 | 0 | } |
260 | | |
261 | 0 | return 1; |
262 | 0 | } |
263 | | |
264 | | int bitmap_equals_ewah(struct bitmap *self, struct ewah_bitmap *other) |
265 | 0 | { |
266 | 0 | struct ewah_iterator it; |
267 | 0 | eword_t word; |
268 | 0 | size_t i = 0; |
269 | |
|
270 | 0 | ewah_iterator_init(&it, other); |
271 | |
|
272 | 0 | while (ewah_iterator_next(&word, &it)) |
273 | 0 | if (word != (i < self->word_alloc ? self->words[i++] : 0)) |
274 | 0 | return 0; |
275 | | |
276 | 0 | for (; i < self->word_alloc; i++) |
277 | 0 | if (self->words[i]) |
278 | 0 | return 0; |
279 | | |
280 | 0 | return 1; |
281 | 0 | } |
282 | | |
283 | | int bitmap_is_subset(struct bitmap *self, struct bitmap *other) |
284 | 0 | { |
285 | 0 | size_t common_size, i; |
286 | |
|
287 | 0 | if (self->word_alloc < other->word_alloc) |
288 | 0 | common_size = self->word_alloc; |
289 | 0 | else { |
290 | 0 | common_size = other->word_alloc; |
291 | 0 | for (i = common_size; i < self->word_alloc; i++) { |
292 | 0 | if (self->words[i]) |
293 | 0 | return 1; |
294 | 0 | } |
295 | 0 | } |
296 | | |
297 | 0 | for (i = 0; i < common_size; i++) { |
298 | 0 | if (self->words[i] & ~other->words[i]) |
299 | 0 | return 1; |
300 | 0 | } |
301 | 0 | return 0; |
302 | 0 | } |
303 | | |
304 | | void bitmap_free(struct bitmap *bitmap) |
305 | 0 | { |
306 | 0 | if (!bitmap) |
307 | 0 | return; |
308 | | |
309 | 0 | free(bitmap->words); |
310 | 0 | free(bitmap); |
311 | 0 | } |