/* * UCW Library -- Support routines for bitarray * * (c) 2012 Pavel Charvat * (c) 2013 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. */ #include #include #include uint bit_array_count_bits(bitarray_t a, uint n) { uint m = 0; n = BIT_ARRAY_WORDS(n); while (n--) m += bit_count(*a++); return m; } bitarray_t bit_array_xrealloc(bitarray_t a, uint old_n, uint new_n) { uint old_bytes = BIT_ARRAY_BYTES(old_n); uint new_bytes = BIT_ARRAY_BYTES(new_n); if (old_bytes == new_bytes) return a; a = xrealloc(a, new_bytes); if (old_bytes < new_bytes) bzero(a + old_bytes, new_bytes - old_bytes); return a; } #ifdef TEST #include #include int main(void) { char buf[1024]; bitarray_t a = alloca(BIT_ARRAY_BYTES(sizeof(buf))); while (1) { if (!fgets(buf, sizeof(buf), stdin)) return 0; uint n; for (n = 0; buf[n] == '0' || buf[n] == '1'; n++); bit_array_zero(a, n); for (uint i = 0; i < n; i++) if (buf[i] == '1') bit_array_set(a, i); printf("%u\n", bit_array_count_bits(a, n)); } } #endif