You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.2 KiB
60 lines
1.2 KiB
3 months ago
|
/*
|
||
|
* UCW Library -- Support routines for bitarray
|
||
|
*
|
||
|
* (c) 2012 Pavel Charvat <pchar@ucw.cz>
|
||
|
* (c) 2013 Martin Mares <mj@ucw.cz>
|
||
|
*
|
||
|
* This software may be freely distributed and used according to the terms
|
||
|
* of the GNU Lesser General Public License.
|
||
|
*/
|
||
|
|
||
|
#include <ucw/lib.h>
|
||
|
#include <ucw/bitops.h>
|
||
|
#include <ucw/bitarray.h>
|
||
|
|
||
|
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 <stdio.h>
|
||
|
#include <alloca.h>
|
||
|
|
||
|
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
|