]> git.ipfire.org Git - thirdparty/git.git/blame - ewah/bitmap.c
Merge branch 'jc/test-i18ngrep'
[thirdparty/git.git] / ewah / bitmap.c
CommitLineData
e1273106
VM
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
48425792 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
e1273106 18 */
36bf1958 19#include "git-compat-util.h"
e1273106
VM
20#include "ewok.h"
21
34b935c0
JK
22#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
23#define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
e1273106 24
14fbd260 25struct bitmap *bitmap_word_alloc(size_t word_alloc)
e1273106 26{
fb7dbf3e 27 struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
ca56dadb 28 CALLOC_ARRAY(bitmap->words, word_alloc);
14fbd260 29 bitmap->word_alloc = word_alloc;
e1273106
VM
30 return bitmap;
31}
32
14fbd260
JK
33struct bitmap *bitmap_new(void)
34{
35 return bitmap_word_alloc(32);
36}
37
ccae08e8
JK
38struct 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
d574bf43 45static void bitmap_grow(struct bitmap *self, size_t word_alloc)
e1273106 46{
2e2d141a
JK
47 size_t old_size = self->word_alloc;
48 ALLOC_GROW(self->words, word_alloc, self->word_alloc);
49 memset(self->words + old_size, 0x0,
50 (self->word_alloc - old_size) * sizeof(eword_t));
d574bf43
JK
51}
52
53void bitmap_set(struct bitmap *self, size_t pos)
54{
55 size_t block = EWAH_BLOCK(pos);
e1273106 56
d574bf43 57 bitmap_grow(self, block + 1);
414382fb 58 self->words[block] |= EWAH_MASK(pos);
e1273106
VM
59}
60
cc4aa285
JK
61void bitmap_unset(struct bitmap *self, size_t pos)
62{
63 size_t block = EWAH_BLOCK(pos);
64
65 if (block < self->word_alloc)
66 self->words[block] &= ~EWAH_MASK(pos);
67}
68
e1273106
VM
69int bitmap_get(struct bitmap *self, size_t pos)
70{
414382fb 71 size_t block = EWAH_BLOCK(pos);
e1273106 72 return block < self->word_alloc &&
414382fb 73 (self->words[block] & EWAH_MASK(pos)) != 0;
e1273106
VM
74}
75
76struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
77{
78 struct ewah_bitmap *ewah = ewah_new();
79 size_t i, running_empty_words = 0;
80 eword_t last_word = 0;
81
82 for (i = 0; i < bitmap->word_alloc; ++i) {
83 if (bitmap->words[i] == 0) {
84 running_empty_words++;
85 continue;
86 }
87
88 if (last_word != 0)
89 ewah_add(ewah, last_word);
90
91 if (running_empty_words > 0) {
92 ewah_add_empty_words(ewah, 0, running_empty_words);
93 running_empty_words = 0;
94 }
95
96 last_word = bitmap->words[i];
97 }
98
99 ewah_add(ewah, last_word);
100 return ewah;
101}
102
103struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
104{
105 struct bitmap *bitmap = bitmap_new();
106 struct ewah_iterator it;
107 eword_t blowup;
108 size_t i = 0;
109
110 ewah_iterator_init(&it, ewah);
111
112 while (ewah_iterator_next(&blowup, &it)) {
08c95df8 113 ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
e1273106
VM
114 bitmap->words[i++] = blowup;
115 }
116
117 bitmap->word_alloc = i;
118 return bitmap;
119}
120
121void bitmap_and_not(struct bitmap *self, struct bitmap *other)
122{
123 const size_t count = (self->word_alloc < other->word_alloc) ?
124 self->word_alloc : other->word_alloc;
125
126 size_t i;
127
128 for (i = 0; i < count; ++i)
129 self->words[i] &= ~other->words[i];
130}
131
3ed67510
JK
132void bitmap_or(struct bitmap *self, const struct bitmap *other)
133{
134 size_t i;
135
136 bitmap_grow(self, other->word_alloc);
137 for (i = 0; i < other->word_alloc; i++)
138 self->words[i] |= other->words[i];
139}
140
e1273106
VM
141void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
142{
143 size_t original_size = self->word_alloc;
34b935c0 144 size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1;
e1273106
VM
145 size_t i = 0;
146 struct ewah_iterator it;
147 eword_t word;
148
149 if (self->word_alloc < other_final) {
150 self->word_alloc = other_final;
08c95df8 151 REALLOC_ARRAY(self->words, self->word_alloc);
e1273106
VM
152 memset(self->words + original_size, 0x0,
153 (self->word_alloc - original_size) * sizeof(eword_t));
154 }
155
156 ewah_iterator_init(&it, other);
157
158 while (ewah_iterator_next(&word, &it))
159 self->words[i++] |= word;
160}
161
e1273106
VM
162size_t bitmap_popcount(struct bitmap *self)
163{
164 size_t i, count = 0;
165
166 for (i = 0; i < self->word_alloc; ++i)
167 count += ewah_bit_popcount64(self->words[i]);
168
169 return count;
170}
171
e5d48bf3
TB
172int bitmap_is_empty(struct bitmap *self)
173{
174 size_t i;
175 for (i = 0; i < self->word_alloc; i++)
176 if (self->words[i])
177 return 0;
178 return 1;
179}
180
e1273106
VM
181int bitmap_equals(struct bitmap *self, struct bitmap *other)
182{
183 struct bitmap *big, *small;
184 size_t i;
185
186 if (self->word_alloc < other->word_alloc) {
187 small = self;
188 big = other;
189 } else {
190 small = other;
191 big = self;
192 }
193
194 for (i = 0; i < small->word_alloc; ++i) {
195 if (small->words[i] != big->words[i])
196 return 0;
197 }
198
199 for (; i < big->word_alloc; ++i) {
200 if (big->words[i] != 0)
201 return 0;
202 }
203
204 return 1;
205}
206
ed03a58b
DS
207int bitmap_is_subset(struct bitmap *self, struct bitmap *other)
208{
209 size_t common_size, i;
210
211 if (self->word_alloc < other->word_alloc)
212 common_size = self->word_alloc;
213 else {
214 common_size = other->word_alloc;
215 for (i = common_size; i < self->word_alloc; i++) {
216 if (self->words[i])
217 return 1;
218 }
219 }
220
221 for (i = 0; i < common_size; i++) {
222 if (self->words[i] & ~other->words[i])
223 return 1;
224 }
225 return 0;
226}
227
e1273106
VM
228void bitmap_free(struct bitmap *bitmap)
229{
afe8a907 230 if (!bitmap)
e1273106
VM
231 return;
232
233 free(bitmap->words);
234 free(bitmap);
235}