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.
20 #include "git-compat-util.h"
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
= ewah_malloc(sizeof(struct bitmap
));
29 bitmap
->words
= ewah_calloc(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 self
->words
= ewah_realloc(self
->words
,
42 self
->word_alloc
* sizeof(eword_t
));
44 memset(self
->words
+ old_size
, 0x0,
45 (self
->word_alloc
- old_size
) * sizeof(eword_t
));
48 self
->words
[block
] |= EWAH_MASK(pos
);
51 void bitmap_clear(struct bitmap
*self
, size_t pos
)
53 size_t block
= EWAH_BLOCK(pos
);
55 if (block
< self
->word_alloc
)
56 self
->words
[block
] &= ~EWAH_MASK(pos
);
59 int bitmap_get(struct bitmap
*self
, size_t pos
)
61 size_t block
= EWAH_BLOCK(pos
);
62 return block
< self
->word_alloc
&&
63 (self
->words
[block
] & EWAH_MASK(pos
)) != 0;
66 struct ewah_bitmap
*bitmap_to_ewah(struct bitmap
*bitmap
)
68 struct ewah_bitmap
*ewah
= ewah_new();
69 size_t i
, running_empty_words
= 0;
70 eword_t last_word
= 0;
72 for (i
= 0; i
< bitmap
->word_alloc
; ++i
) {
73 if (bitmap
->words
[i
] == 0) {
74 running_empty_words
++;
79 ewah_add(ewah
, last_word
);
81 if (running_empty_words
> 0) {
82 ewah_add_empty_words(ewah
, 0, running_empty_words
);
83 running_empty_words
= 0;
86 last_word
= bitmap
->words
[i
];
89 ewah_add(ewah
, last_word
);
93 struct bitmap
*ewah_to_bitmap(struct ewah_bitmap
*ewah
)
95 struct bitmap
*bitmap
= bitmap_new();
96 struct ewah_iterator it
;
100 ewah_iterator_init(&it
, ewah
);
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
));
109 bitmap
->words
[i
++] = blowup
;
112 bitmap
->word_alloc
= i
;
116 void bitmap_and_not(struct bitmap
*self
, struct bitmap
*other
)
118 const size_t count
= (self
->word_alloc
< other
->word_alloc
) ?
119 self
->word_alloc
: other
->word_alloc
;
123 for (i
= 0; i
< count
; ++i
)
124 self
->words
[i
] &= ~other
->words
[i
];
127 void bitmap_or_ewah(struct bitmap
*self
, struct ewah_bitmap
*other
)
129 size_t original_size
= self
->word_alloc
;
130 size_t other_final
= (other
->bit_size
/ BITS_IN_EWORD
) + 1;
132 struct ewah_iterator it
;
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
));
143 ewah_iterator_init(&it
, other
);
145 while (ewah_iterator_next(&word
, &it
))
146 self
->words
[i
++] |= word
;
149 void bitmap_each_bit(struct bitmap
*self
, ewah_callback callback
, void *data
)
153 for (i
= 0; i
< self
->word_alloc
; ++i
) {
154 eword_t word
= self
->words
[i
];
157 if (word
== (eword_t
)~0) {
158 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
)
159 callback(pos
++, data
);
161 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
162 if ((word
>> offset
) == 0)
165 offset
+= ewah_bit_ctz64(word
>> offset
);
166 callback(pos
+ offset
, data
);
168 pos
+= BITS_IN_EWORD
;
173 size_t bitmap_popcount(struct bitmap
*self
)
177 for (i
= 0; i
< self
->word_alloc
; ++i
)
178 count
+= ewah_bit_popcount64(self
->words
[i
]);
183 int bitmap_equals(struct bitmap
*self
, struct bitmap
*other
)
185 struct bitmap
*big
, *small
;
188 if (self
->word_alloc
< other
->word_alloc
) {
196 for (i
= 0; i
< small
->word_alloc
; ++i
) {
197 if (small
->words
[i
] != big
->words
[i
])
201 for (; i
< big
->word_alloc
; ++i
) {
202 if (big
->words
[i
] != 0)
209 void bitmap_reset(struct bitmap
*bitmap
)
211 memset(bitmap
->words
, 0x0, bitmap
->word_alloc
* sizeof(eword_t
));
214 void bitmap_free(struct bitmap
*bitmap
)