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