Merge commit 'refs/top-bases/t/extra-actions/refs' into t/extra-actions/refs
[git/gitweb.git] / ewah / bitmap.c
blob710e58c1bfc652d5f01b47cba5016d2e2f91f590
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "git-compat-util.h"
21 #include "ewok.h"
23 #define MASK(x) ((eword_t)1 << (x % BITS_IN_WORD))
24 #define BLOCK(x) (x / BITS_IN_WORD)
26 struct bitmap *bitmap_new(void)
28 struct bitmap *bitmap = ewah_malloc(sizeof(struct bitmap));
29 bitmap->words = ewah_calloc(32, sizeof(eword_t));
30 bitmap->word_alloc = 32;
31 return bitmap;
34 void bitmap_set(struct bitmap *self, size_t pos)
36 size_t block = BLOCK(pos);
38 if (block >= self->word_alloc) {
39 size_t old_size = self->word_alloc;
40 self->word_alloc = block * 2;
41 self->words = ewah_realloc(self->words,
42 self->word_alloc * sizeof(eword_t));
44 memset(self->words + old_size, 0x0,
45 (self->word_alloc - old_size) * sizeof(eword_t));
48 self->words[block] |= MASK(pos);
51 void bitmap_clear(struct bitmap *self, size_t pos)
53 size_t block = BLOCK(pos);
55 if (block < self->word_alloc)
56 self->words[block] &= ~MASK(pos);
59 int bitmap_get(struct bitmap *self, size_t pos)
61 size_t block = BLOCK(pos);
62 return block < self->word_alloc &&
63 (self->words[block] & MASK(pos)) != 0;
66 struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
68 struct ewah_bitmap *ewah = ewah_new();
69 size_t i, running_empty_words = 0;
70 eword_t last_word = 0;
72 for (i = 0; i < bitmap->word_alloc; ++i) {
73 if (bitmap->words[i] == 0) {
74 running_empty_words++;
75 continue;
78 if (last_word != 0)
79 ewah_add(ewah, last_word);
81 if (running_empty_words > 0) {
82 ewah_add_empty_words(ewah, 0, running_empty_words);
83 running_empty_words = 0;
86 last_word = bitmap->words[i];
89 ewah_add(ewah, last_word);
90 return ewah;
93 struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
95 struct bitmap *bitmap = bitmap_new();
96 struct ewah_iterator it;
97 eword_t blowup;
98 size_t i = 0;
100 ewah_iterator_init(&it, ewah);
102 while (ewah_iterator_next(&blowup, &it)) {
103 if (i >= bitmap->word_alloc) {
104 bitmap->word_alloc *= 1.5;
105 bitmap->words = ewah_realloc(
106 bitmap->words, bitmap->word_alloc * sizeof(eword_t));
109 bitmap->words[i++] = blowup;
112 bitmap->word_alloc = i;
113 return bitmap;
116 void bitmap_and_not(struct bitmap *self, struct bitmap *other)
118 const size_t count = (self->word_alloc < other->word_alloc) ?
119 self->word_alloc : other->word_alloc;
121 size_t i;
123 for (i = 0; i < count; ++i)
124 self->words[i] &= ~other->words[i];
127 void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
129 size_t original_size = self->word_alloc;
130 size_t other_final = (other->bit_size / BITS_IN_WORD) + 1;
131 size_t i = 0;
132 struct ewah_iterator it;
133 eword_t word;
135 if (self->word_alloc < other_final) {
136 self->word_alloc = other_final;
137 self->words = ewah_realloc(self->words,
138 self->word_alloc * sizeof(eword_t));
139 memset(self->words + original_size, 0x0,
140 (self->word_alloc - original_size) * sizeof(eword_t));
143 ewah_iterator_init(&it, other);
145 while (ewah_iterator_next(&word, &it))
146 self->words[i++] |= word;
149 void bitmap_each_bit(struct bitmap *self, ewah_callback callback, void *data)
151 size_t pos = 0, i;
153 for (i = 0; i < self->word_alloc; ++i) {
154 eword_t word = self->words[i];
155 uint32_t offset;
157 if (word == (eword_t)~0) {
158 for (offset = 0; offset < BITS_IN_WORD; ++offset)
159 callback(pos++, data);
160 } else {
161 for (offset = 0; offset < BITS_IN_WORD; ++offset) {
162 if ((word >> offset) == 0)
163 break;
165 offset += ewah_bit_ctz64(word >> offset);
166 callback(pos + offset, data);
168 pos += BITS_IN_WORD;
173 size_t bitmap_popcount(struct bitmap *self)
175 size_t i, count = 0;
177 for (i = 0; i < self->word_alloc; ++i)
178 count += ewah_bit_popcount64(self->words[i]);
180 return count;
183 int bitmap_equals(struct bitmap *self, struct bitmap *other)
185 struct bitmap *big, *small;
186 size_t i;
188 if (self->word_alloc < other->word_alloc) {
189 small = self;
190 big = other;
191 } else {
192 small = other;
193 big = self;
196 for (i = 0; i < small->word_alloc; ++i) {
197 if (small->words[i] != big->words[i])
198 return 0;
201 for (; i < big->word_alloc; ++i) {
202 if (big->words[i] != 0)
203 return 0;
206 return 1;
209 void bitmap_reset(struct bitmap *bitmap)
211 memset(bitmap->words, 0x0, bitmap->word_alloc * sizeof(eword_t));
214 void bitmap_free(struct bitmap *bitmap)
216 if (bitmap == NULL)
217 return;
219 free(bitmap->words);
220 free(bitmap);