]> git.ipfire.org Git - thirdparty/git.git/blob - ewah/bitmap.c
commit.c: allow lookup_commit_reference to handle arbitrary repositories
[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 "cache.h"
20 #include "ewok.h"
21
22 #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
23 #define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
24
25 struct bitmap *bitmap_new(void)
26 {
27 struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
28 bitmap->words = xcalloc(32, sizeof(eword_t));
29 bitmap->word_alloc = 32;
30 return bitmap;
31 }
32
33 void bitmap_set(struct bitmap *self, size_t pos)
34 {
35 size_t block = EWAH_BLOCK(pos);
36
37 if (block >= self->word_alloc) {
38 size_t old_size = self->word_alloc;
39 self->word_alloc = block * 2;
40 REALLOC_ARRAY(self->words, self->word_alloc);
41 memset(self->words + old_size, 0x0,
42 (self->word_alloc - old_size) * sizeof(eword_t));
43 }
44
45 self->words[block] |= EWAH_MASK(pos);
46 }
47
48 void bitmap_clear(struct bitmap *self, size_t pos)
49 {
50 size_t block = EWAH_BLOCK(pos);
51
52 if (block < self->word_alloc)
53 self->words[block] &= ~EWAH_MASK(pos);
54 }
55
56 int bitmap_get(struct bitmap *self, size_t pos)
57 {
58 size_t block = EWAH_BLOCK(pos);
59 return block < self->word_alloc &&
60 (self->words[block] & EWAH_MASK(pos)) != 0;
61 }
62
63 struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
64 {
65 struct ewah_bitmap *ewah = ewah_new();
66 size_t i, running_empty_words = 0;
67 eword_t last_word = 0;
68
69 for (i = 0; i < bitmap->word_alloc; ++i) {
70 if (bitmap->words[i] == 0) {
71 running_empty_words++;
72 continue;
73 }
74
75 if (last_word != 0)
76 ewah_add(ewah, last_word);
77
78 if (running_empty_words > 0) {
79 ewah_add_empty_words(ewah, 0, running_empty_words);
80 running_empty_words = 0;
81 }
82
83 last_word = bitmap->words[i];
84 }
85
86 ewah_add(ewah, last_word);
87 return ewah;
88 }
89
90 struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
91 {
92 struct bitmap *bitmap = bitmap_new();
93 struct ewah_iterator it;
94 eword_t blowup;
95 size_t i = 0;
96
97 ewah_iterator_init(&it, ewah);
98
99 while (ewah_iterator_next(&blowup, &it)) {
100 ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
101 bitmap->words[i++] = blowup;
102 }
103
104 bitmap->word_alloc = i;
105 return bitmap;
106 }
107
108 void bitmap_and_not(struct bitmap *self, struct bitmap *other)
109 {
110 const size_t count = (self->word_alloc < other->word_alloc) ?
111 self->word_alloc : other->word_alloc;
112
113 size_t i;
114
115 for (i = 0; i < count; ++i)
116 self->words[i] &= ~other->words[i];
117 }
118
119 void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
120 {
121 size_t original_size = self->word_alloc;
122 size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1;
123 size_t i = 0;
124 struct ewah_iterator it;
125 eword_t word;
126
127 if (self->word_alloc < other_final) {
128 self->word_alloc = other_final;
129 REALLOC_ARRAY(self->words, self->word_alloc);
130 memset(self->words + original_size, 0x0,
131 (self->word_alloc - original_size) * sizeof(eword_t));
132 }
133
134 ewah_iterator_init(&it, other);
135
136 while (ewah_iterator_next(&word, &it))
137 self->words[i++] |= word;
138 }
139
140 void bitmap_each_bit(struct bitmap *self, ewah_callback callback, void *data)
141 {
142 size_t pos = 0, i;
143
144 for (i = 0; i < self->word_alloc; ++i) {
145 eword_t word = self->words[i];
146 uint32_t offset;
147
148 if (word == (eword_t)~0) {
149 for (offset = 0; offset < BITS_IN_EWORD; ++offset)
150 callback(pos++, data);
151 } else {
152 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
153 if ((word >> offset) == 0)
154 break;
155
156 offset += ewah_bit_ctz64(word >> offset);
157 callback(pos + offset, data);
158 }
159 pos += BITS_IN_EWORD;
160 }
161 }
162 }
163
164 size_t bitmap_popcount(struct bitmap *self)
165 {
166 size_t i, count = 0;
167
168 for (i = 0; i < self->word_alloc; ++i)
169 count += ewah_bit_popcount64(self->words[i]);
170
171 return count;
172 }
173
174 int bitmap_equals(struct bitmap *self, struct bitmap *other)
175 {
176 struct bitmap *big, *small;
177 size_t i;
178
179 if (self->word_alloc < other->word_alloc) {
180 small = self;
181 big = other;
182 } else {
183 small = other;
184 big = self;
185 }
186
187 for (i = 0; i < small->word_alloc; ++i) {
188 if (small->words[i] != big->words[i])
189 return 0;
190 }
191
192 for (; i < big->word_alloc; ++i) {
193 if (big->words[i] != 0)
194 return 0;
195 }
196
197 return 1;
198 }
199
200 void bitmap_reset(struct bitmap *bitmap)
201 {
202 memset(bitmap->words, 0x0, bitmap->word_alloc * sizeof(eword_t));
203 }
204
205 void bitmap_free(struct bitmap *bitmap)
206 {
207 if (bitmap == NULL)
208 return;
209
210 free(bitmap->words);
211 free(bitmap);
212 }