]> git.ipfire.org Git - thirdparty/git.git/blob - ewah/bitmap.c
send-email: move validation code below process_address_list
[thirdparty/git.git] / ewah / bitmap.c
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 "alloc.h"
21 #include "ewok.h"
22
23 #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
24 #define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
25
26 struct bitmap *bitmap_word_alloc(size_t word_alloc)
27 {
28 struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
29 CALLOC_ARRAY(bitmap->words, word_alloc);
30 bitmap->word_alloc = word_alloc;
31 return bitmap;
32 }
33
34 struct bitmap *bitmap_new(void)
35 {
36 return bitmap_word_alloc(32);
37 }
38
39 struct bitmap *bitmap_dup(const struct bitmap *src)
40 {
41 struct bitmap *dst = bitmap_word_alloc(src->word_alloc);
42 COPY_ARRAY(dst->words, src->words, src->word_alloc);
43 return dst;
44 }
45
46 static void bitmap_grow(struct bitmap *self, size_t word_alloc)
47 {
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));
52 }
53
54 void bitmap_set(struct bitmap *self, size_t pos)
55 {
56 size_t block = EWAH_BLOCK(pos);
57
58 bitmap_grow(self, block + 1);
59 self->words[block] |= EWAH_MASK(pos);
60 }
61
62 void bitmap_unset(struct bitmap *self, size_t pos)
63 {
64 size_t block = EWAH_BLOCK(pos);
65
66 if (block < self->word_alloc)
67 self->words[block] &= ~EWAH_MASK(pos);
68 }
69
70 int bitmap_get(struct bitmap *self, size_t pos)
71 {
72 size_t block = EWAH_BLOCK(pos);
73 return block < self->word_alloc &&
74 (self->words[block] & EWAH_MASK(pos)) != 0;
75 }
76
77 struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
78 {
79 struct ewah_bitmap *ewah = ewah_new();
80 size_t i, running_empty_words = 0;
81 eword_t last_word = 0;
82
83 for (i = 0; i < bitmap->word_alloc; ++i) {
84 if (bitmap->words[i] == 0) {
85 running_empty_words++;
86 continue;
87 }
88
89 if (last_word != 0)
90 ewah_add(ewah, last_word);
91
92 if (running_empty_words > 0) {
93 ewah_add_empty_words(ewah, 0, running_empty_words);
94 running_empty_words = 0;
95 }
96
97 last_word = bitmap->words[i];
98 }
99
100 ewah_add(ewah, last_word);
101 return ewah;
102 }
103
104 struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
105 {
106 struct bitmap *bitmap = bitmap_new();
107 struct ewah_iterator it;
108 eword_t blowup;
109 size_t i = 0;
110
111 ewah_iterator_init(&it, ewah);
112
113 while (ewah_iterator_next(&blowup, &it)) {
114 ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
115 bitmap->words[i++] = blowup;
116 }
117
118 bitmap->word_alloc = i;
119 return bitmap;
120 }
121
122 void bitmap_and_not(struct bitmap *self, struct bitmap *other)
123 {
124 const size_t count = (self->word_alloc < other->word_alloc) ?
125 self->word_alloc : other->word_alloc;
126
127 size_t i;
128
129 for (i = 0; i < count; ++i)
130 self->words[i] &= ~other->words[i];
131 }
132
133 void bitmap_or(struct bitmap *self, const struct bitmap *other)
134 {
135 size_t i;
136
137 bitmap_grow(self, other->word_alloc);
138 for (i = 0; i < other->word_alloc; i++)
139 self->words[i] |= other->words[i];
140 }
141
142 void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
143 {
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;
149
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));
155 }
156
157 ewah_iterator_init(&it, other);
158
159 while (ewah_iterator_next(&word, &it))
160 self->words[i++] |= word;
161 }
162
163 size_t bitmap_popcount(struct bitmap *self)
164 {
165 size_t i, count = 0;
166
167 for (i = 0; i < self->word_alloc; ++i)
168 count += ewah_bit_popcount64(self->words[i]);
169
170 return count;
171 }
172
173 int bitmap_equals(struct bitmap *self, struct bitmap *other)
174 {
175 struct bitmap *big, *small;
176 size_t i;
177
178 if (self->word_alloc < other->word_alloc) {
179 small = self;
180 big = other;
181 } else {
182 small = other;
183 big = self;
184 }
185
186 for (i = 0; i < small->word_alloc; ++i) {
187 if (small->words[i] != big->words[i])
188 return 0;
189 }
190
191 for (; i < big->word_alloc; ++i) {
192 if (big->words[i] != 0)
193 return 0;
194 }
195
196 return 1;
197 }
198
199 int bitmap_is_subset(struct bitmap *self, struct bitmap *other)
200 {
201 size_t common_size, i;
202
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;
210 }
211 }
212
213 for (i = 0; i < common_size; i++) {
214 if (self->words[i] & ~other->words[i])
215 return 1;
216 }
217 return 0;
218 }
219
220 void bitmap_free(struct bitmap *bitmap)
221 {
222 if (!bitmap)
223 return;
224
225 free(bitmap->words);
226 free(bitmap);
227 }