2 * Copyright 2013, GitHub, Inc
3 * Copyright 2009-2013, Daniel Lemire, Cliff Moon,
4 * David McIntosh, Robert Becho, Google Inc. and Veronika Zenz
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.
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.
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.
23 #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
24 #define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
26 struct bitmap
*bitmap_new(void)
28 struct bitmap
*bitmap
= xmalloc(sizeof(struct bitmap
));
29 bitmap
->words
= xcalloc(32, sizeof(eword_t
));
30 bitmap
->word_alloc
= 32;
34 void bitmap_set(struct bitmap
*self
, size_t pos
)
36 size_t block
= EWAH_BLOCK(pos
);
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
));
46 self
->words
[block
] |= EWAH_MASK(pos
);
49 void bitmap_clear(struct bitmap
*self
, size_t pos
)
51 size_t block
= EWAH_BLOCK(pos
);
53 if (block
< self
->word_alloc
)
54 self
->words
[block
] &= ~EWAH_MASK(pos
);
57 int bitmap_get(struct bitmap
*self
, size_t pos
)
59 size_t block
= EWAH_BLOCK(pos
);
60 return block
< self
->word_alloc
&&
61 (self
->words
[block
] & EWAH_MASK(pos
)) != 0;
64 struct ewah_bitmap
*bitmap_to_ewah(struct bitmap
*bitmap
)
66 struct ewah_bitmap
*ewah
= ewah_new();
67 size_t i
, running_empty_words
= 0;
68 eword_t last_word
= 0;
70 for (i
= 0; i
< bitmap
->word_alloc
; ++i
) {
71 if (bitmap
->words
[i
] == 0) {
72 running_empty_words
++;
77 ewah_add(ewah
, last_word
);
79 if (running_empty_words
> 0) {
80 ewah_add_empty_words(ewah
, 0, running_empty_words
);
81 running_empty_words
= 0;
84 last_word
= bitmap
->words
[i
];
87 ewah_add(ewah
, last_word
);
91 struct bitmap
*ewah_to_bitmap(struct ewah_bitmap
*ewah
)
93 struct bitmap
*bitmap
= bitmap_new();
94 struct ewah_iterator it
;
98 ewah_iterator_init(&it
, ewah
);
100 while (ewah_iterator_next(&blowup
, &it
)) {
101 ALLOC_GROW(bitmap
->words
, i
+ 1, bitmap
->word_alloc
);
102 bitmap
->words
[i
++] = blowup
;
105 bitmap
->word_alloc
= i
;
109 void bitmap_and_not(struct bitmap
*self
, struct bitmap
*other
)
111 const size_t count
= (self
->word_alloc
< other
->word_alloc
) ?
112 self
->word_alloc
: other
->word_alloc
;
116 for (i
= 0; i
< count
; ++i
)
117 self
->words
[i
] &= ~other
->words
[i
];
120 void bitmap_or_ewah(struct bitmap
*self
, struct ewah_bitmap
*other
)
122 size_t original_size
= self
->word_alloc
;
123 size_t other_final
= (other
->bit_size
/ BITS_IN_EWORD
) + 1;
125 struct ewah_iterator it
;
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
));
135 ewah_iterator_init(&it
, other
);
137 while (ewah_iterator_next(&word
, &it
))
138 self
->words
[i
++] |= word
;
141 void bitmap_each_bit(struct bitmap
*self
, ewah_callback callback
, void *data
)
145 for (i
= 0; i
< self
->word_alloc
; ++i
) {
146 eword_t word
= self
->words
[i
];
149 if (word
== (eword_t
)~0) {
150 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
)
151 callback(pos
++, data
);
153 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
154 if ((word
>> offset
) == 0)
157 offset
+= ewah_bit_ctz64(word
>> offset
);
158 callback(pos
+ offset
, data
);
160 pos
+= BITS_IN_EWORD
;
165 size_t bitmap_popcount(struct bitmap
*self
)
169 for (i
= 0; i
< self
->word_alloc
; ++i
)
170 count
+= ewah_bit_popcount64(self
->words
[i
]);
175 int bitmap_equals(struct bitmap
*self
, struct bitmap
*other
)
177 struct bitmap
*big
, *small
;
180 if (self
->word_alloc
< other
->word_alloc
) {
188 for (i
= 0; i
< small
->word_alloc
; ++i
) {
189 if (small
->words
[i
] != big
->words
[i
])
193 for (; i
< big
->word_alloc
; ++i
) {
194 if (big
->words
[i
] != 0)
201 void bitmap_reset(struct bitmap
*bitmap
)
203 memset(bitmap
->words
, 0x0, bitmap
->word_alloc
* sizeof(eword_t
));
206 void bitmap_free(struct bitmap
*bitmap
)