]> git.ipfire.org Git - thirdparty/git.git/blame - ewah/bitmap.c
Merge branch 'jk/bundle-progress'
[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
EN
19#include "git-compat-util.h"
20#include "alloc.h"
e1273106
VM
21#include "ewok.h"
22
34b935c0
JK
23#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
24#define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
e1273106 25
14fbd260 26struct bitmap *bitmap_word_alloc(size_t word_alloc)
e1273106 27{
fb7dbf3e 28 struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
ca56dadb 29 CALLOC_ARRAY(bitmap->words, word_alloc);
14fbd260 30 bitmap->word_alloc = word_alloc;
e1273106
VM
31 return bitmap;
32}
33
14fbd260
JK
34struct bitmap *bitmap_new(void)
35{
36 return bitmap_word_alloc(32);
37}
38
ccae08e8
JK
39struct 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
d574bf43 46static void bitmap_grow(struct bitmap *self, size_t word_alloc)
e1273106 47{
2e2d141a
JK
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));
d574bf43
JK
52}
53
54void bitmap_set(struct bitmap *self, size_t pos)
55{
56 size_t block = EWAH_BLOCK(pos);
e1273106 57
d574bf43 58 bitmap_grow(self, block + 1);
414382fb 59 self->words[block] |= EWAH_MASK(pos);
e1273106
VM
60}
61
cc4aa285
JK
62void 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
e1273106
VM
70int bitmap_get(struct bitmap *self, size_t pos)
71{
414382fb 72 size_t block = EWAH_BLOCK(pos);
e1273106 73 return block < self->word_alloc &&
414382fb 74 (self->words[block] & EWAH_MASK(pos)) != 0;
e1273106
VM
75}
76
77struct 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
104struct 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)) {
08c95df8 114 ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
e1273106
VM
115 bitmap->words[i++] = blowup;
116 }
117
118 bitmap->word_alloc = i;
119 return bitmap;
120}
121
122void 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
3ed67510
JK
133void 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
e1273106
VM
142void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
143{
144 size_t original_size = self->word_alloc;
34b935c0 145 size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1;
e1273106
VM
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;
08c95df8 152 REALLOC_ARRAY(self->words, self->word_alloc);
e1273106
VM
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
e1273106
VM
163size_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
173int 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
ed03a58b
DS
199int 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
e1273106
VM
220void bitmap_free(struct bitmap *bitmap)
221{
afe8a907 222 if (!bitmap)
e1273106
VM
223 return;
224
225 free(bitmap->words);
226 free(bitmap);
227}