Line | Count | Source |
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 | MEMZERO_ARRAY(self->words + old_size, (self->word_alloc - old_size)); |
50 | 0 | } |
51 | | |
52 | | void bitmap_set(struct bitmap *self, size_t pos) |
53 | 0 | { |
54 | 0 | size_t block = EWAH_BLOCK(pos); |
55 | |
|
56 | 0 | bitmap_grow(self, block + 1); |
57 | 0 | self->words[block] |= EWAH_MASK(pos); |
58 | 0 | } |
59 | | |
60 | | void bitmap_unset(struct bitmap *self, size_t pos) |
61 | 0 | { |
62 | 0 | size_t block = EWAH_BLOCK(pos); |
63 | |
|
64 | 0 | if (block < self->word_alloc) |
65 | 0 | self->words[block] &= ~EWAH_MASK(pos); |
66 | 0 | } |
67 | | |
68 | | int bitmap_get(struct bitmap *self, size_t pos) |
69 | 0 | { |
70 | 0 | size_t block = EWAH_BLOCK(pos); |
71 | 0 | return block < self->word_alloc && |
72 | 0 | (self->words[block] & EWAH_MASK(pos)) != 0; |
73 | 0 | } |
74 | | |
75 | | struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap) |
76 | 0 | { |
77 | 0 | struct ewah_bitmap *ewah = ewah_new(); |
78 | 0 | size_t i, running_empty_words = 0; |
79 | 0 | eword_t last_word = 0; |
80 | |
|
81 | 0 | for (i = 0; i < bitmap->word_alloc; ++i) { |
82 | 0 | if (bitmap->words[i] == 0) { |
83 | 0 | running_empty_words++; |
84 | 0 | continue; |
85 | 0 | } |
86 | | |
87 | 0 | if (last_word != 0) |
88 | 0 | ewah_add(ewah, last_word); |
89 | |
|
90 | 0 | if (running_empty_words > 0) { |
91 | 0 | ewah_add_empty_words(ewah, 0, running_empty_words); |
92 | 0 | running_empty_words = 0; |
93 | 0 | } |
94 | |
|
95 | 0 | last_word = bitmap->words[i]; |
96 | 0 | } |
97 | |
|
98 | 0 | ewah_add(ewah, last_word); |
99 | 0 | return ewah; |
100 | 0 | } |
101 | | |
102 | | struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah) |
103 | 0 | { |
104 | 0 | struct bitmap *bitmap = bitmap_new(); |
105 | 0 | struct ewah_iterator it; |
106 | 0 | eword_t blowup; |
107 | 0 | size_t i = 0; |
108 | |
|
109 | 0 | ewah_iterator_init(&it, ewah); |
110 | |
|
111 | 0 | while (ewah_iterator_next(&blowup, &it)) { |
112 | 0 | ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc); |
113 | 0 | bitmap->words[i++] = blowup; |
114 | 0 | } |
115 | |
|
116 | 0 | bitmap->word_alloc = i; |
117 | 0 | return bitmap; |
118 | 0 | } |
119 | | |
120 | | void bitmap_and_not(struct bitmap *self, struct bitmap *other) |
121 | 0 | { |
122 | 0 | const size_t count = (self->word_alloc < other->word_alloc) ? |
123 | 0 | self->word_alloc : other->word_alloc; |
124 | |
|
125 | 0 | size_t i; |
126 | |
|
127 | 0 | for (i = 0; i < count; ++i) |
128 | 0 | self->words[i] &= ~other->words[i]; |
129 | 0 | } |
130 | | |
131 | | void bitmap_or(struct bitmap *self, const struct bitmap *other) |
132 | 0 | { |
133 | 0 | size_t i; |
134 | |
|
135 | 0 | bitmap_grow(self, other->word_alloc); |
136 | 0 | for (i = 0; i < other->word_alloc; i++) |
137 | 0 | self->words[i] |= other->words[i]; |
138 | 0 | } |
139 | | |
140 | | int ewah_bitmap_is_subset(struct ewah_bitmap *self, struct bitmap *other) |
141 | 0 | { |
142 | 0 | struct ewah_iterator it; |
143 | 0 | eword_t word; |
144 | 0 | size_t i; |
145 | |
|
146 | 0 | ewah_iterator_init(&it, self); |
147 | |
|
148 | 0 | for (i = 0; i < other->word_alloc; i++) { |
149 | 0 | 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 | 0 | return 1; |
157 | 0 | } |
158 | 0 | 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 | 0 | return 0; |
166 | 0 | } |
167 | 0 | } |
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 | 0 | while (ewah_iterator_next(&word, &it)) |
176 | 0 | if (word) |
177 | 0 | return 0; |
178 | | |
179 | | /* `self` is definitely a subset of `other` */ |
180 | 0 | return 1; |
181 | 0 | } |
182 | | |
183 | | void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other) |
184 | 0 | { |
185 | 0 | size_t original_size = self->word_alloc; |
186 | 0 | size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1; |
187 | 0 | size_t i = 0; |
188 | 0 | struct ewah_iterator it; |
189 | 0 | eword_t word; |
190 | |
|
191 | 0 | if (self->word_alloc < other_final) { |
192 | 0 | self->word_alloc = other_final; |
193 | 0 | REALLOC_ARRAY(self->words, self->word_alloc); |
194 | 0 | MEMZERO_ARRAY(self->words + original_size, |
195 | 0 | (self->word_alloc - original_size)); |
196 | 0 | } |
197 | |
|
198 | 0 | ewah_iterator_init(&it, other); |
199 | |
|
200 | 0 | while (ewah_iterator_next(&word, &it)) |
201 | 0 | self->words[i++] |= word; |
202 | 0 | } |
203 | | |
204 | | size_t bitmap_popcount(struct bitmap *self) |
205 | 0 | { |
206 | 0 | size_t i, count = 0; |
207 | |
|
208 | 0 | for (i = 0; i < self->word_alloc; ++i) |
209 | 0 | count += ewah_bit_popcount64(self->words[i]); |
210 | |
|
211 | 0 | return count; |
212 | 0 | } |
213 | | |
214 | | size_t ewah_bitmap_popcount(struct ewah_bitmap *self) |
215 | 0 | { |
216 | 0 | struct ewah_iterator it; |
217 | 0 | eword_t word; |
218 | 0 | size_t count = 0; |
219 | |
|
220 | 0 | ewah_iterator_init(&it, self); |
221 | |
|
222 | 0 | while (ewah_iterator_next(&word, &it)) |
223 | 0 | count += ewah_bit_popcount64(word); |
224 | |
|
225 | 0 | return count; |
226 | 0 | } |
227 | | |
228 | | int bitmap_is_empty(struct bitmap *self) |
229 | 0 | { |
230 | 0 | size_t i; |
231 | 0 | for (i = 0; i < self->word_alloc; i++) |
232 | 0 | if (self->words[i]) |
233 | 0 | return 0; |
234 | 0 | return 1; |
235 | 0 | } |
236 | | |
237 | | int bitmap_equals(struct bitmap *self, struct bitmap *other) |
238 | 0 | { |
239 | 0 | struct bitmap *big, *small; |
240 | 0 | size_t i; |
241 | |
|
242 | 0 | if (self->word_alloc < other->word_alloc) { |
243 | 0 | small = self; |
244 | 0 | big = other; |
245 | 0 | } else { |
246 | 0 | small = other; |
247 | 0 | big = self; |
248 | 0 | } |
249 | |
|
250 | 0 | for (i = 0; i < small->word_alloc; ++i) { |
251 | 0 | if (small->words[i] != big->words[i]) |
252 | 0 | return 0; |
253 | 0 | } |
254 | | |
255 | 0 | for (; i < big->word_alloc; ++i) { |
256 | 0 | if (big->words[i] != 0) |
257 | 0 | return 0; |
258 | 0 | } |
259 | | |
260 | 0 | return 1; |
261 | 0 | } |
262 | | |
263 | | int bitmap_equals_ewah(struct bitmap *self, struct ewah_bitmap *other) |
264 | 0 | { |
265 | 0 | struct ewah_iterator it; |
266 | 0 | eword_t word; |
267 | 0 | size_t i = 0; |
268 | |
|
269 | 0 | ewah_iterator_init(&it, other); |
270 | |
|
271 | 0 | while (ewah_iterator_next(&word, &it)) |
272 | 0 | if (word != (i < self->word_alloc ? self->words[i++] : 0)) |
273 | 0 | return 0; |
274 | | |
275 | 0 | for (; i < self->word_alloc; i++) |
276 | 0 | if (self->words[i]) |
277 | 0 | return 0; |
278 | | |
279 | 0 | return 1; |
280 | 0 | } |
281 | | |
282 | | int bitmap_is_subset(struct bitmap *self, struct bitmap *other) |
283 | 0 | { |
284 | 0 | size_t common_size, i; |
285 | |
|
286 | 0 | if (self->word_alloc < other->word_alloc) |
287 | 0 | common_size = self->word_alloc; |
288 | 0 | else { |
289 | 0 | common_size = other->word_alloc; |
290 | 0 | for (i = common_size; i < self->word_alloc; i++) { |
291 | 0 | if (self->words[i]) |
292 | 0 | return 1; |
293 | 0 | } |
294 | 0 | } |
295 | | |
296 | 0 | for (i = 0; i < common_size; i++) { |
297 | 0 | if (self->words[i] & ~other->words[i]) |
298 | 0 | return 1; |
299 | 0 | } |
300 | 0 | return 0; |
301 | 0 | } |
302 | | |
303 | | void bitmap_free(struct bitmap *bitmap) |
304 | 0 | { |
305 | 0 | if (!bitmap) |
306 | 0 | return; |
307 | | |
308 | 0 | free(bitmap->words); |
309 | 0 | free(bitmap); |
310 | 0 | } |