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, see <http://www.gnu.org/licenses/>.
19 #include "git-compat-util.h"
22 #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
23 #define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
25 struct bitmap
*bitmap_word_alloc(size_t word_alloc
)
27 struct bitmap
*bitmap
= xmalloc(sizeof(struct bitmap
));
28 CALLOC_ARRAY(bitmap
->words
, word_alloc
);
29 bitmap
->word_alloc
= word_alloc
;
33 struct bitmap
*bitmap_new(void)
35 return bitmap_word_alloc(32);
38 struct bitmap
*bitmap_dup(const struct bitmap
*src
)
40 struct bitmap
*dst
= bitmap_word_alloc(src
->word_alloc
);
41 COPY_ARRAY(dst
->words
, src
->words
, src
->word_alloc
);
45 static void bitmap_grow(struct bitmap
*self
, size_t word_alloc
)
47 size_t old_size
= self
->word_alloc
;
48 ALLOC_GROW(self
->words
, word_alloc
, self
->word_alloc
);
49 memset(self
->words
+ old_size
, 0x0,
50 (self
->word_alloc
- old_size
) * sizeof(eword_t
));
53 void bitmap_set(struct bitmap
*self
, size_t pos
)
55 size_t block
= EWAH_BLOCK(pos
);
57 bitmap_grow(self
, block
+ 1);
58 self
->words
[block
] |= EWAH_MASK(pos
);
61 void bitmap_unset(struct bitmap
*self
, size_t pos
)
63 size_t block
= EWAH_BLOCK(pos
);
65 if (block
< self
->word_alloc
)
66 self
->words
[block
] &= ~EWAH_MASK(pos
);
69 int bitmap_get(struct bitmap
*self
, size_t pos
)
71 size_t block
= EWAH_BLOCK(pos
);
72 return block
< self
->word_alloc
&&
73 (self
->words
[block
] & EWAH_MASK(pos
)) != 0;
76 struct ewah_bitmap
*bitmap_to_ewah(struct bitmap
*bitmap
)
78 struct ewah_bitmap
*ewah
= ewah_new();
79 size_t i
, running_empty_words
= 0;
80 eword_t last_word
= 0;
82 for (i
= 0; i
< bitmap
->word_alloc
; ++i
) {
83 if (bitmap
->words
[i
] == 0) {
84 running_empty_words
++;
89 ewah_add(ewah
, last_word
);
91 if (running_empty_words
> 0) {
92 ewah_add_empty_words(ewah
, 0, running_empty_words
);
93 running_empty_words
= 0;
96 last_word
= bitmap
->words
[i
];
99 ewah_add(ewah
, last_word
);
103 struct bitmap
*ewah_to_bitmap(struct ewah_bitmap
*ewah
)
105 struct bitmap
*bitmap
= bitmap_new();
106 struct ewah_iterator it
;
110 ewah_iterator_init(&it
, ewah
);
112 while (ewah_iterator_next(&blowup
, &it
)) {
113 ALLOC_GROW(bitmap
->words
, i
+ 1, bitmap
->word_alloc
);
114 bitmap
->words
[i
++] = blowup
;
117 bitmap
->word_alloc
= i
;
121 void bitmap_and_not(struct bitmap
*self
, struct bitmap
*other
)
123 const size_t count
= (self
->word_alloc
< other
->word_alloc
) ?
124 self
->word_alloc
: other
->word_alloc
;
128 for (i
= 0; i
< count
; ++i
)
129 self
->words
[i
] &= ~other
->words
[i
];
132 void bitmap_or(struct bitmap
*self
, const struct bitmap
*other
)
136 bitmap_grow(self
, other
->word_alloc
);
137 for (i
= 0; i
< other
->word_alloc
; i
++)
138 self
->words
[i
] |= other
->words
[i
];
141 void bitmap_or_ewah(struct bitmap
*self
, struct ewah_bitmap
*other
)
143 size_t original_size
= self
->word_alloc
;
144 size_t other_final
= (other
->bit_size
/ BITS_IN_EWORD
) + 1;
146 struct ewah_iterator it
;
149 if (self
->word_alloc
< other_final
) {
150 self
->word_alloc
= other_final
;
151 REALLOC_ARRAY(self
->words
, self
->word_alloc
);
152 memset(self
->words
+ original_size
, 0x0,
153 (self
->word_alloc
- original_size
) * sizeof(eword_t
));
156 ewah_iterator_init(&it
, other
);
158 while (ewah_iterator_next(&word
, &it
))
159 self
->words
[i
++] |= word
;
162 size_t bitmap_popcount(struct bitmap
*self
)
166 for (i
= 0; i
< self
->word_alloc
; ++i
)
167 count
+= ewah_bit_popcount64(self
->words
[i
]);
172 int bitmap_is_empty(struct bitmap
*self
)
175 for (i
= 0; i
< self
->word_alloc
; i
++)
181 int bitmap_equals(struct bitmap
*self
, struct bitmap
*other
)
183 struct bitmap
*big
, *small
;
186 if (self
->word_alloc
< other
->word_alloc
) {
194 for (i
= 0; i
< small
->word_alloc
; ++i
) {
195 if (small
->words
[i
] != big
->words
[i
])
199 for (; i
< big
->word_alloc
; ++i
) {
200 if (big
->words
[i
] != 0)
207 int bitmap_is_subset(struct bitmap
*self
, struct bitmap
*other
)
209 size_t common_size
, i
;
211 if (self
->word_alloc
< other
->word_alloc
)
212 common_size
= self
->word_alloc
;
214 common_size
= other
->word_alloc
;
215 for (i
= common_size
; i
< self
->word_alloc
; i
++) {
221 for (i
= 0; i
< common_size
; i
++) {
222 if (self
->words
[i
] & ~other
->words
[i
])
228 void bitmap_free(struct bitmap
*bitmap
)