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