]> mj.ucw.cz Git - libucw.git/blobdiff - lib/bitarray.h
Remember size of the input file.
[libucw.git] / lib / bitarray.h
index 77e382588071c89fab6ccc25c8a6425ce9700c4c..724804154e58349b94bca592667edd2d0b200949 100644 (file)
@@ -1,22 +1,44 @@
 /*
 /*
- *     Bit Array Operations
+ *     UCW Library -- Bit Array Operations
  *
  *
- *     (c) 2003 Martin Mares <mj@ucw.cz>
+ *     (c) 2003--2006 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
  */
 
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
  */
 
+#ifndef _UCW_BITARRAY_H
+#define _UCW_BITARRAY_H
+
 #include <string.h>
 
 typedef u32 *bitarray_t;
 #define BIT_ARRAY_WORDS(n) (((n)+31)/32)
 #include <string.h>
 
 typedef u32 *bitarray_t;
 #define BIT_ARRAY_WORDS(n) (((n)+31)/32)
+#define BIT_ARRAY_BYTES(n) (4*BIT_ARRAY_WORDS(n))
 #define BIT_ARRAY(name,size) u32 name[BIT_ARRAY_WORDS(size)]
 
 #define BIT_ARRAY(name,size) u32 name[BIT_ARRAY_WORDS(size)]
 
+static inline bitarray_t
+bit_array_xmalloc(uns n)
+{
+  return xmalloc(BIT_ARRAY_BYTES(n));
+}
+
+static inline bitarray_t
+bit_array_xmalloc_zero(uns n)
+{
+  return xmalloc_zero(BIT_ARRAY_BYTES(n));
+}
+
 static inline void
 bit_array_zero(bitarray_t a, uns n)
 {
 static inline void
 bit_array_zero(bitarray_t a, uns n)
 {
-  bzero(a, 4*BIT_ARRAY_WORDS(n));
+  bzero(a, BIT_ARRAY_BYTES(n));
+}
+
+static inline void
+bit_array_set_all(bitarray_t a, uns n)
+{
+  memset(a, 255, BIT_ARRAY_BYTES(n));
 }
 
 static inline void
 }
 
 static inline void
@@ -31,6 +53,15 @@ bit_array_clear(bitarray_t a, uns i)
   a[i/32] &= ~(1 << (i%32));
 }
 
   a[i/32] &= ~(1 << (i%32));
 }
 
+static inline void
+bit_array_assign(bitarray_t a, uns i, uns x)
+{
+  if (x)
+    bit_array_set(a, i);
+  else
+    bit_array_clear(a, i);
+}
+
 static inline uns
 bit_array_isset(bitarray_t a, uns i)
 {
 static inline uns
 bit_array_isset(bitarray_t a, uns i)
 {
@@ -72,3 +103,5 @@ bit_array_test_and_clear(bitarray_t a, uns i)
 #define BIT_ARRAY_FISH_BITS_END                                                        \
          while (0);                                                            \
        }
 #define BIT_ARRAY_FISH_BITS_END                                                        \
          while (0);                                                            \
        }
+
+#endif