]> git.ipfire.org Git - thirdparty/git.git/blame - ewah/bitmap.c
ewah: factor out bitmap growth
[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 */
08c95df8 19#include "cache.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));
14fbd260
JK
28 bitmap->words = xcalloc(word_alloc, sizeof(eword_t));
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
d574bf43 38static void bitmap_grow(struct bitmap *self, size_t word_alloc)
e1273106 39{
d574bf43 40 if (word_alloc > self->word_alloc) {
e1273106 41 size_t old_size = self->word_alloc;
d574bf43 42 self->word_alloc = word_alloc * 2;
08c95df8 43 REALLOC_ARRAY(self->words, self->word_alloc);
e1273106
VM
44 memset(self->words + old_size, 0x0,
45 (self->word_alloc - old_size) * sizeof(eword_t));
46 }
d574bf43
JK
47}
48
49void bitmap_set(struct bitmap *self, size_t pos)
50{
51 size_t block = EWAH_BLOCK(pos);
e1273106 52
d574bf43 53 bitmap_grow(self, block + 1);
414382fb 54 self->words[block] |= EWAH_MASK(pos);
e1273106
VM
55}
56
cc4aa285
JK
57void bitmap_unset(struct bitmap *self, size_t pos)
58{
59 size_t block = EWAH_BLOCK(pos);
60
61 if (block < self->word_alloc)
62 self->words[block] &= ~EWAH_MASK(pos);
63}
64
e1273106
VM
65int bitmap_get(struct bitmap *self, size_t pos)
66{
414382fb 67 size_t block = EWAH_BLOCK(pos);
e1273106 68 return block < self->word_alloc &&
414382fb 69 (self->words[block] & EWAH_MASK(pos)) != 0;
e1273106
VM
70}
71
72struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
73{
74 struct ewah_bitmap *ewah = ewah_new();
75 size_t i, running_empty_words = 0;
76 eword_t last_word = 0;
77
78 for (i = 0; i < bitmap->word_alloc; ++i) {
79 if (bitmap->words[i] == 0) {
80 running_empty_words++;
81 continue;
82 }
83
84 if (last_word != 0)
85 ewah_add(ewah, last_word);
86
87 if (running_empty_words > 0) {
88 ewah_add_empty_words(ewah, 0, running_empty_words);
89 running_empty_words = 0;
90 }
91
92 last_word = bitmap->words[i];
93 }
94
95 ewah_add(ewah, last_word);
96 return ewah;
97}
98
99struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
100{
101 struct bitmap *bitmap = bitmap_new();
102 struct ewah_iterator it;
103 eword_t blowup;
104 size_t i = 0;
105
106 ewah_iterator_init(&it, ewah);
107
108 while (ewah_iterator_next(&blowup, &it)) {
08c95df8 109 ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
e1273106
VM
110 bitmap->words[i++] = blowup;
111 }
112
113 bitmap->word_alloc = i;
114 return bitmap;
115}
116
117void bitmap_and_not(struct bitmap *self, struct bitmap *other)
118{
119 const size_t count = (self->word_alloc < other->word_alloc) ?
120 self->word_alloc : other->word_alloc;
121
122 size_t i;
123
124 for (i = 0; i < count; ++i)
125 self->words[i] &= ~other->words[i];
126}
127
128void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
129{
130 size_t original_size = self->word_alloc;
34b935c0 131 size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1;
e1273106
VM
132 size_t i = 0;
133 struct ewah_iterator it;
134 eword_t word;
135
136 if (self->word_alloc < other_final) {
137 self->word_alloc = other_final;
08c95df8 138 REALLOC_ARRAY(self->words, self->word_alloc);
e1273106
VM
139 memset(self->words + original_size, 0x0,
140 (self->word_alloc - original_size) * sizeof(eword_t));
141 }
142
143 ewah_iterator_init(&it, other);
144
145 while (ewah_iterator_next(&word, &it))
146 self->words[i++] |= word;
147}
148
e1273106
VM
149size_t bitmap_popcount(struct bitmap *self)
150{
151 size_t i, count = 0;
152
153 for (i = 0; i < self->word_alloc; ++i)
154 count += ewah_bit_popcount64(self->words[i]);
155
156 return count;
157}
158
159int bitmap_equals(struct bitmap *self, struct bitmap *other)
160{
161 struct bitmap *big, *small;
162 size_t i;
163
164 if (self->word_alloc < other->word_alloc) {
165 small = self;
166 big = other;
167 } else {
168 small = other;
169 big = self;
170 }
171
172 for (i = 0; i < small->word_alloc; ++i) {
173 if (small->words[i] != big->words[i])
174 return 0;
175 }
176
177 for (; i < big->word_alloc; ++i) {
178 if (big->words[i] != 0)
179 return 0;
180 }
181
182 return 1;
183}
184
185void bitmap_reset(struct bitmap *bitmap)
186{
187 memset(bitmap->words, 0x0, bitmap->word_alloc * sizeof(eword_t));
188}
189
190void bitmap_free(struct bitmap *bitmap)
191{
192 if (bitmap == NULL)
193 return;
194
195 free(bitmap->words);
196 free(bitmap);
197}