hash.h: move some oid-related declarations from cache.h
[git/debian.git] / ewah / bitmap.c
blob12d6aa398e907f83eeaee9e9635ccbf11220ce35
1 /**
2 * Copyright 2013, GitHub, Inc
3 * Copyright 2009-2013, Daniel Lemire, Cliff Moon,
4 * David McIntosh, Robert Becho, Google Inc. and Veronika Zenz
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.
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.
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/>.
19 #include "git-compat-util.h"
20 #include "alloc.h"
21 #include "ewok.h"
23 #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
24 #define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
26 struct bitmap *bitmap_word_alloc(size_t word_alloc)
28 struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
29 CALLOC_ARRAY(bitmap->words, word_alloc);
30 bitmap->word_alloc = word_alloc;
31 return bitmap;
34 struct bitmap *bitmap_new(void)
36 return bitmap_word_alloc(32);
39 struct bitmap *bitmap_dup(const struct bitmap *src)
41 struct bitmap *dst = bitmap_word_alloc(src->word_alloc);
42 COPY_ARRAY(dst->words, src->words, src->word_alloc);
43 return dst;
46 static void bitmap_grow(struct bitmap *self, size_t word_alloc)
48 size_t old_size = self->word_alloc;
49 ALLOC_GROW(self->words, word_alloc, self->word_alloc);
50 memset(self->words + old_size, 0x0,
51 (self->word_alloc - old_size) * sizeof(eword_t));
54 void bitmap_set(struct bitmap *self, size_t pos)
56 size_t block = EWAH_BLOCK(pos);
58 bitmap_grow(self, block + 1);
59 self->words[block] |= EWAH_MASK(pos);
62 void bitmap_unset(struct bitmap *self, size_t pos)
64 size_t block = EWAH_BLOCK(pos);
66 if (block < self->word_alloc)
67 self->words[block] &= ~EWAH_MASK(pos);
70 int bitmap_get(struct bitmap *self, size_t pos)
72 size_t block = EWAH_BLOCK(pos);
73 return block < self->word_alloc &&
74 (self->words[block] & EWAH_MASK(pos)) != 0;
77 struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
79 struct ewah_bitmap *ewah = ewah_new();
80 size_t i, running_empty_words = 0;
81 eword_t last_word = 0;
83 for (i = 0; i < bitmap->word_alloc; ++i) {
84 if (bitmap->words[i] == 0) {
85 running_empty_words++;
86 continue;
89 if (last_word != 0)
90 ewah_add(ewah, last_word);
92 if (running_empty_words > 0) {
93 ewah_add_empty_words(ewah, 0, running_empty_words);
94 running_empty_words = 0;
97 last_word = bitmap->words[i];
100 ewah_add(ewah, last_word);
101 return ewah;
104 struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
106 struct bitmap *bitmap = bitmap_new();
107 struct ewah_iterator it;
108 eword_t blowup;
109 size_t i = 0;
111 ewah_iterator_init(&it, ewah);
113 while (ewah_iterator_next(&blowup, &it)) {
114 ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
115 bitmap->words[i++] = blowup;
118 bitmap->word_alloc = i;
119 return bitmap;
122 void bitmap_and_not(struct bitmap *self, struct bitmap *other)
124 const size_t count = (self->word_alloc < other->word_alloc) ?
125 self->word_alloc : other->word_alloc;
127 size_t i;
129 for (i = 0; i < count; ++i)
130 self->words[i] &= ~other->words[i];
133 void bitmap_or(struct bitmap *self, const struct bitmap *other)
135 size_t i;
137 bitmap_grow(self, other->word_alloc);
138 for (i = 0; i < other->word_alloc; i++)
139 self->words[i] |= other->words[i];
142 void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
144 size_t original_size = self->word_alloc;
145 size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1;
146 size_t i = 0;
147 struct ewah_iterator it;
148 eword_t word;
150 if (self->word_alloc < other_final) {
151 self->word_alloc = other_final;
152 REALLOC_ARRAY(self->words, self->word_alloc);
153 memset(self->words + original_size, 0x0,
154 (self->word_alloc - original_size) * sizeof(eword_t));
157 ewah_iterator_init(&it, other);
159 while (ewah_iterator_next(&word, &it))
160 self->words[i++] |= word;
163 size_t bitmap_popcount(struct bitmap *self)
165 size_t i, count = 0;
167 for (i = 0; i < self->word_alloc; ++i)
168 count += ewah_bit_popcount64(self->words[i]);
170 return count;
173 int bitmap_equals(struct bitmap *self, struct bitmap *other)
175 struct bitmap *big, *small;
176 size_t i;
178 if (self->word_alloc < other->word_alloc) {
179 small = self;
180 big = other;
181 } else {
182 small = other;
183 big = self;
186 for (i = 0; i < small->word_alloc; ++i) {
187 if (small->words[i] != big->words[i])
188 return 0;
191 for (; i < big->word_alloc; ++i) {
192 if (big->words[i] != 0)
193 return 0;
196 return 1;
199 int bitmap_is_subset(struct bitmap *self, struct bitmap *other)
201 size_t common_size, i;
203 if (self->word_alloc < other->word_alloc)
204 common_size = self->word_alloc;
205 else {
206 common_size = other->word_alloc;
207 for (i = common_size; i < self->word_alloc; i++) {
208 if (self->words[i])
209 return 1;
213 for (i = 0; i < common_size; i++) {
214 if (self->words[i] & ~other->words[i])
215 return 1;
217 return 0;
220 void bitmap_free(struct bitmap *bitmap)
222 if (!bitmap)
223 return;
225 free(bitmap->words);
226 free(bitmap);