From: Martin Mares Date: Tue, 19 Mar 2013 16:11:44 +0000 (+0100) Subject: Bitarray: Added bit_array_xrealloc() X-Git-Tag: v5.99~86 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=08c2028943a2cf55daf137729d1f8225e8bd8d57;p=libucw.git Bitarray: Added bit_array_xrealloc() As suggested by Martina Balintova and inspired by her code. Also renamed bit-count.c to bit-array.c. --- diff --git a/ucw/Makefile b/ucw/Makefile index d4a055ce..6c47e712 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -21,7 +21,7 @@ LIBUCW_MODS= \ wildmatch regex \ prime primetable random \ time-stamp time-timer time-conf \ - bit-ffs bit-fls bit-count \ + bit-ffs bit-fls bit-array \ url \ mainloop main-block main-rec \ proctitle exitstatus runcmd \ diff --git a/ucw/bit-array.c b/ucw/bit-array.c new file mode 100644 index 00000000..584d752e --- /dev/null +++ b/ucw/bit-array.c @@ -0,0 +1,59 @@ +/* + * 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 + +uns bit_array_count_bits(bitarray_t a, uns n) +{ + uns m = 0; + n = BIT_ARRAY_WORDS(n); + while (n--) + m += bit_count(*a++); + return m; +} + +bitarray_t bit_array_xrealloc(bitarray_t a, uns old_n, uns new_n) +{ + uns old_bytes = BIT_ARRAY_BYTES(old_n); + uns 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; + uns n; + for (n = 0; buf[n] == '0' || buf[n] == '1'; n++); + bit_array_zero(a, n); + for (uns i = 0; i < n; i++) + if (buf[i] == '1') + bit_array_set(a, i); + printf("%u\n", bit_array_count_bits(a, n)); + } +} + +#endif diff --git a/ucw/bit-count.c b/ucw/bit-count.c deleted file mode 100644 index a84a6b7c..00000000 --- a/ucw/bit-count.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * UCW Library -- Counting bits in bitarray - * - * (c) 2012 Pavel Charvat - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include -#include -#include - -uns bit_array_count_bits(bitarray_t a, uns n) -{ - uns m = 0; - n = BIT_ARRAY_WORDS(n); - while (n--) - m += bit_count(*a++); - return m; -} - -#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; - uns n; - for (n = 0; buf[n] == '0' || buf[n] == '1'; n++); - bit_array_zero(a, n); - for (uns i = 0; i < n; i++) - if (buf[i] == '1') - bit_array_set(a, i); - printf("%u\n", bit_array_count_bits(a, n)); - } -} - -#endif diff --git a/ucw/bitarray.h b/ucw/bitarray.h index e2994c64..daf8fa01 100644 --- a/ucw/bitarray.h +++ b/ucw/bitarray.h @@ -25,6 +25,8 @@ bit_array_xmalloc(uns n) return xmalloc(BIT_ARRAY_BYTES(n)); } +bitarray_t bit_array_xrealloc(bitarray_t a, uns old_n, uns new_n); + static inline bitarray_t bit_array_xmalloc_zero(uns n) {