d61dc6114a852ae2b622421143394cba29076078
   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
  25struct 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
  33void 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
  48int bitmap_get(struct bitmap *self, size_t pos)
  49{
  50        size_t block = EWAH_BLOCK(pos);
  51        return block < self->word_alloc &&
  52                (self->words[block] & EWAH_MASK(pos)) != 0;
  53}
  54
  55struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)
  56{
  57        struct ewah_bitmap *ewah = ewah_new();
  58        size_t i, running_empty_words = 0;
  59        eword_t last_word = 0;
  60
  61        for (i = 0; i < bitmap->word_alloc; ++i) {
  62                if (bitmap->words[i] == 0) {
  63                        running_empty_words++;
  64                        continue;
  65                }
  66
  67                if (last_word != 0)
  68                        ewah_add(ewah, last_word);
  69
  70                if (running_empty_words > 0) {
  71                        ewah_add_empty_words(ewah, 0, running_empty_words);
  72                        running_empty_words = 0;
  73                }
  74
  75                last_word = bitmap->words[i];
  76        }
  77
  78        ewah_add(ewah, last_word);
  79        return ewah;
  80}
  81
  82struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
  83{
  84        struct bitmap *bitmap = bitmap_new();
  85        struct ewah_iterator it;
  86        eword_t blowup;
  87        size_t i = 0;
  88
  89        ewah_iterator_init(&it, ewah);
  90
  91        while (ewah_iterator_next(&blowup, &it)) {
  92                ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
  93                bitmap->words[i++] = blowup;
  94        }
  95
  96        bitmap->word_alloc = i;
  97        return bitmap;
  98}
  99
 100void bitmap_and_not(struct bitmap *self, struct bitmap *other)
 101{
 102        const size_t count = (self->word_alloc < other->word_alloc) ?
 103                self->word_alloc : other->word_alloc;
 104
 105        size_t i;
 106
 107        for (i = 0; i < count; ++i)
 108                self->words[i] &= ~other->words[i];
 109}
 110
 111void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
 112{
 113        size_t original_size = self->word_alloc;
 114        size_t other_final = (other->bit_size / BITS_IN_EWORD) + 1;
 115        size_t i = 0;
 116        struct ewah_iterator it;
 117        eword_t word;
 118
 119        if (self->word_alloc < other_final) {
 120                self->word_alloc = other_final;
 121                REALLOC_ARRAY(self->words, self->word_alloc);
 122                memset(self->words + original_size, 0x0,
 123                        (self->word_alloc - original_size) * sizeof(eword_t));
 124        }
 125
 126        ewah_iterator_init(&it, other);
 127
 128        while (ewah_iterator_next(&word, &it))
 129                self->words[i++] |= word;
 130}
 131
 132void bitmap_each_bit(struct bitmap *self, ewah_callback callback, void *data)
 133{
 134        size_t pos = 0, i;
 135
 136        for (i = 0; i < self->word_alloc; ++i) {
 137                eword_t word = self->words[i];
 138                uint32_t offset;
 139
 140                if (word == (eword_t)~0) {
 141                        for (offset = 0; offset < BITS_IN_EWORD; ++offset)
 142                                callback(pos++, data);
 143                } else {
 144                        for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
 145                                if ((word >> offset) == 0)
 146                                        break;
 147
 148                                offset += ewah_bit_ctz64(word >> offset);
 149                                callback(pos + offset, data);
 150                        }
 151                        pos += BITS_IN_EWORD;
 152                }
 153        }
 154}
 155
 156size_t bitmap_popcount(struct bitmap *self)
 157{
 158        size_t i, count = 0;
 159
 160        for (i = 0; i < self->word_alloc; ++i)
 161                count += ewah_bit_popcount64(self->words[i]);
 162
 163        return count;
 164}
 165
 166int bitmap_equals(struct bitmap *self, struct bitmap *other)
 167{
 168        struct bitmap *big, *small;
 169        size_t i;
 170
 171        if (self->word_alloc < other->word_alloc) {
 172                small = self;
 173                big = other;
 174        } else {
 175                small = other;
 176                big = self;
 177        }
 178
 179        for (i = 0; i < small->word_alloc; ++i) {
 180                if (small->words[i] != big->words[i])
 181                        return 0;
 182        }
 183
 184        for (; i < big->word_alloc; ++i) {
 185                if (big->words[i] != 0)
 186                        return 0;
 187        }
 188
 189        return 1;
 190}
 191
 192void bitmap_reset(struct bitmap *bitmap)
 193{
 194        memset(bitmap->words, 0x0, bitmap->word_alloc * sizeof(eword_t));
 195}
 196
 197void bitmap_free(struct bitmap *bitmap)
 198{
 199        if (bitmap == NULL)
 200                return;
 201
 202        free(bitmap->words);
 203        free(bitmap);
 204}