]> mj.ucw.cz Git - libucw.git/commitdiff
Bitarray: Added bit_array_xrealloc()
authorMartin Mares <mj@ucw.cz>
Tue, 19 Mar 2013 16:11:44 +0000 (17:11 +0100)
committerMartin Mares <mj@ucw.cz>
Tue, 19 Mar 2013 16:11:44 +0000 (17:11 +0100)
As suggested by Martina Balintova and inspired by her code.

Also renamed bit-count.c to bit-array.c.

ucw/Makefile
ucw/bit-array.c [new file with mode: 0644]
ucw/bit-count.c [deleted file]
ucw/bitarray.h

index d4a055ce1982cf0310088871a8c7aac49e90b237..6c47e7126410c234952fd1f46659aa024890224a 100644 (file)
@@ -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 (file)
index 0000000..584d752
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ *     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>
+
+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 <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;
+      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 (file)
index a84a6b7..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- *     UCW Library -- Counting bits in bitarray
- *
- *     (c) 2012 Pavel Charvat <pchar@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>
-
-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 <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;
-      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
index e2994c64784d3708a45052484114635bb946def8..daf8fa01035ffaaf6c402e0786fa7398c8d128d3 100644 (file)
@@ -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)
 {