Fixed some typos.
[libfirm] / ir / adt / bitset.h
index f7efa15..5d9aada 100644 (file)
-/**
- * Bitsets.
+/*
+ * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
-#ifndef __FIRM_BITSET_H
-#define __FIRM_BITSET_H
+/**
+ * @file
+ * @brief   convenience layer over raw_bitsets (stores number of bits
+ *          with the bitfield)
+ * @author  Matthias Braun
+ */
+#ifndef FIRM_ADT_BITSET_H
+#define FIRM_ADT_BITSET_H
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
 #include <string.h>
+#include "irprintf.h"
 
+#include "xmalloc.h"
+#include "bitfiddle.h"
+#include "raw_bitset.h"
 
-#define INLINE inline
-
-#define BITSET_USE_STD
-
-#include "bitset_std.h"
-
-typedef struct _bitset_t {
-       unsigned units;
-       bitset_unit_t *data;
+typedef struct bitset_t {
+       size_t size;       /**< size of the bitset in bits */
+       unsigned data[1];  /**< data (should be declared data[] but this is only
+                               allowed in C99) */
 } bitset_t;
 
-#define BS_UNIT_SIZE sizeof(bitset_unit_t)
-#define BS_UNIT_SIZE_BITS (BS_UNIT_SIZE * 8)
-#define BS_UNIT_MASK (BS_UNIT_SIZE_BITS - 1)
-
-
-/**
- * Units needed for a given highest bit.
- * @param highest_bit The highest bit that should be storable.
- * @return The number of units needed.
- */
-#define _bitset_units(highest_bit) (round_up2(highest_bit, BS_UNIT_SIZE_BITS) / BS_UNIT_SIZE_BITS)
-
 /**
- * Compute the size in bytes needed for a bitset.
- * This also include the size for the bitset data structure.
- * @param highest_bit The highest bit that shall be storable.
- * @return The overall amount of bytes needed for that bitset.
+ * return the number of bytes a bitset would need
  */
-#define _bitset_overall_size(highest_bit) \
-       (sizeof(bitset_t) + _bitset_units(highest_bit) * BS_UNIT_SIZE)
+static inline size_t bitset_total_size(size_t n_bits)
+{
+       return sizeof(bitset_t) - sizeof(((bitset_t*)0)->data)
+               + BITSET_SIZE_BYTES(n_bits);
+}
 
 /**
- * Initialize a bitset.
- * This functions should not be called.
- * @param area A pointer to memory reserved for the bitset.
- * @param units The number of units that are allocated for the bitset.
- * @return A pointer to the initialized bitset.
+ * initialize a bitset for bitsize size (bitset should point to memory
+ * with a size calculated by bitset_total_size)
  */
-static INLINE bitset_t *_bitset_prepare(void *area, unsigned units)
+static inline bitset_t *bitset_init(void *memory, size_t size)
 {
-       bitset_t *ptr = area;
-       ptr->units = units;
-       ptr->data = (bitset_unit_t *) (ptr + 1);
-       memset(ptr->data, 0, BS_UNIT_SIZE * units);
-       return ptr;
+       bitset_t *result = (bitset_t*) memory;
+       result->size = size;
+       rbitset_clear_all(result->data, size);
+       return result;
 }
 
 /**
  * Allocate a bitset on an obstack.
  * @param obst The obstack.
- * @param highest_bit The greatest bit that shall be stored in the set.
+ * @param size The greatest bit that shall be stored in the set.
  * @return A pointer to an empty initialized bitset.
  */
-#define bitset_obstack_alloc(obst,highest_bit) \
-  _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(highest_bit)), _bitset_units(highest_bit))
+static inline bitset_t *bitset_obstack_alloc(struct obstack *obst,
+                                             size_t n_bits)
+{
+       size_t size   = bitset_total_size(n_bits);
+       void  *memory = obstack_alloc(obst, size);
+       return bitset_init(memory, n_bits);
+}
 
 /**
  * Allocate a bitset via malloc.
- * @param highest_bit The greatest bit that shall be stored in the set.
+ * @param size The greatest bit that shall be stored in the set.
  * @return A pointer to an empty initialized bitset.
  */
-#define bitset_malloc(highest_bit) \
-       _bitset_prepare(malloc(_bitset_overall_size(highest_bit)), _bitset_units(highest_bit))
+static inline bitset_t *bitset_malloc(size_t n_bits)
+{
+       size_t  size   = bitset_total_size(n_bits);
+       void   *memory = xmalloc(size);
+       return bitset_init(memory, n_bits);
+}
 
 /**
  * Free a bitset allocated with bitset_malloc().
  * @param bs The bitset.
  */
-#define bitset_free(bs) free(bs)
+static inline void bitset_free(bitset_t *bitset)
+{
+       xfree(bitset);
+}
 
 /**
  * Allocate a bitset on the stack via alloca.
- * @param highest_bit The greatest bit that shall be stored in the set.
+ * @param size The greatest bit that shall be stored in the set.
  * @return A pointer to an empty initialized bitset.
  */
-#define bitset_alloca(highest_bit) \
-       _bitset_prepare(alloca(_bitset_overall_size(highest_bit)), _bitset_units(highest_bit))
+#define bitset_alloca(size) \
+       bitset_init(alloca(bitset_total_size(size)), (size))
 
 /**
- * Print a bitset to a stream.
- * The bitset is printed as a comma seperated list of bits set.
- * @param file The stream.
+ * Get the size of the bitset in bits.
+ * @note Note the difference between capacity and size.
  * @param bs The bitset.
+ * @return The highest bit which can be set or cleared plus 1.
  */
-static INLINE void bitset_fprint(FILE *file, bitset_t *bs)
+static inline size_t bitset_size(const bitset_t *bitset)
 {
-       const char *prefix = "";
-       int i;
-       unsigned k = 0;
-
-       putc('[', file);
-       for(i = 0; i < bs->units; i++) {
-               bitset_unit_t j;
-               bitset_unit_t unit = bs->data[i];
-
-#if 0
-               printf("%s%08x", prefix, unit);
-               prefix=":";
-               continue;
-#endif
-               for(j = 1; j != 0; j <<= 1, k++) {
-                       if(unit & j) {
-                               fprintf(file, "%s%u", prefix, k);
-                               prefix = ",";
-                       }
-               }
-       }
-       putc(']', file);
+       return bitset->size;
 }
 
 /**
- * Get the unit which contains a specific bit.
- * This function is internal.
+ * Set a bit in the bitset.
  * @param bs The bitset.
- * @param bit The bit.
- * @return A pointer to the unit containing the bit.
+ * @param bit The bit to set.
  */
-static INLINE bitset_unit_t *_bitset_get_unit(const bitset_t *bs, unsigned bit)
+static inline void bitset_set(bitset_t *bs, size_t bit)
 {
-       assert(bit < bs->units * BS_UNIT_SIZE_BITS && "Bit too large");
-       return bs->data + bit / BS_UNIT_SIZE_BITS;
+       assert(bit < bs->size);
+       rbitset_set(bs->data, bit);
 }
 
 /**
- * Set a bit in the bitset.
+ * Clear a bit in the bitset.
  * @param bs The bitset.
- * @param bit The bit to set.
+ * @param bit The bit to clear.
  */
-static INLINE void bitset_set(bitset_t *bs, unsigned bit)
+static inline void bitset_clear(bitset_t *bs, size_t bit)
 {
-       bitset_unit_t *unit = _bitset_get_unit(bs, bit);
-       _bitset_inside_set(unit, bit & BS_UNIT_MASK);
+       assert(bit < bs->size);
+       rbitset_clear(bs->data, bit);
 }
 
 /**
- * Clear a bit in the bitset.
+ * Check, if a bit is set.
  * @param bs The bitset.
- * @param bit The bit to clear.
+ * @param bit The bit to check for.
+ * @return 1, if the bit was set, 0 if not.
  */
-static INLINE void bitset_clear(bitset_t *bs, unsigned bit)
+static inline bool bitset_is_set(const bitset_t *bs, size_t bit)
 {
-       bitset_unit_t *unit = _bitset_get_unit(bs, bit);
-       _bitset_inside_clear(unit, bit & BS_UNIT_MASK);
+       assert(bit < bs->size);
+       return rbitset_is_set(bs->data, bit);
 }
 
 /**
@@ -162,91 +156,256 @@ static INLINE void bitset_clear(bitset_t *bs, unsigned bit)
  * @param bs The bitset.
  * @param bit The bit to flip.
  */
-static INLINE void bitset_flip(bitset_t *bs, unsigned bit)
+static inline void bitset_flip(bitset_t *bs, size_t bit)
 {
-       bitset_unit_t *unit = _bitset_get_unit(bs, bit);
-       _bitset_inside_flip(unit, bit & BS_UNIT_MASK);
+       assert(bit < bs->size);
+       rbitset_flip(bs->data, bit);
 }
 
 /**
- * Find the smallest bit set in the bitset.
+ * Flip the whole bitset.
  * @param bs The bitset.
- * @return The smallest bit set in the bitset.
  */
-static INLINE unsigned bitset_min(bitset_t *bs)
+static inline void bitset_flip_all(bitset_t *bs)
 {
-       unsigned i, ofs = 0;
-       bitset_unit_t *unit;
+       rbitset_flip_all(bs->data, bs->size);
+}
 
-       for(i = 0, unit = bs->data; i < bs->units; ++i, ++unit) {
-               unsigned pos = _bitset_inside_ntz(unit);
-               if(pos > 0)
-                       return ofs + pos;
-               ofs += BS_UNIT_SIZE_BITS;
-       }
+/**
+ * Copy a bitset to another. Both bitset must be initialized and have the same
+ * number of bits.
+ * @param tgt The target bitset.
+ * @param src The source bitset.
+ * @return The target bitset.
+ */
+static inline void bitset_copy(bitset_t *tgt, const bitset_t *src)
+{
+       assert(tgt->size == src->size);
+       rbitset_copy(tgt->data, src->data, src->size);
+}
 
-       return 0;
+static inline void bitset_copy_into(bitset_t *tgt, const bitset_t *src)
+{
+       assert(tgt->size >= src->size);
+       rbitset_copy_into(tgt->data, src->data, src->size);
 }
 
 /**
- * Find the greatest bit set in the bitset.
+ * Find the next unset bit from a given bit.
+ * @note Note that if pos is unset, pos is returned.
  * @param bs The bitset.
- * @return The greatest bit set in the bitset.
+ * @param pos The bit from which to search for the next set bit.
+ * @return The next set bit from pos on, or (size_t)-1, if no unset bit was
+ * found after pos.
  */
-static INLINE unsigned bitset_max(bitset_t *bs)
+static inline size_t bitset_next_clear(const bitset_t *bs, size_t pos)
 {
-       unsigned i, max = 0, ofs = 0;
-       bitset_unit_t *unit;
-
-       for(i = 0, unit = bs->data; i < bs->units; ++i, ++unit) {
-               unsigned pos = _bitset_inside_nlz(unit);
-               if(pos > 0)
-                       max = ofs + pos;
-               ofs += BS_UNIT_SIZE_BITS;
-       }
+       return rbitset_next_max(bs->data, pos, bs->size, false);
+}
 
-       return max;
+/**
+ * Find the next set bit from a given bit.
+ * @note Note that if pos is set, pos is returned.
+ * @param bs The bitset.
+ * @param pos The bit from which to search for the next set bit.
+ * @return The next set bit from pos on, or (size_t)-1, if no set bit was
+ * found after pos.
+ */
+static inline size_t bitset_next_set(const bitset_t *bs, size_t pos)
+{
+       return rbitset_next_max(bs->data, pos, bs->size, true);
 }
 
+/**
+ * Convenience macro for bitset iteration.
+ * @param bitset The bitset.
+ * @param elm A size_t variable.
+ */
+#define bitset_foreach(bitset, elm) \
+       for (size_t elm = 0; (elm = bitset_next_set((bitset), elm)) != (size_t)-1; ++elm)
+
+
+#define bitset_foreach_clear(bitset, elm) \
+       for (size_t elm = 0; (elm = bitset_next_clear((bitset), elm)) != (size_t)-1; ++elm)
+
 /**
  * Count the bits set.
  * This can also be seen as the cardinality of the set.
  * @param bs The bitset.
  * @return The number of bits set in the bitset.
  */
-static INLINE unsigned bitset_pop(bitset_t *bs)
+static inline size_t bitset_popcount(const bitset_t *bs)
+{
+       return rbitset_popcount(bs->data, bs->size);
+}
+
+/**
+ * Clear the bitset.
+ * This sets all bits to zero.
+ * @param bs The bitset.
+ */
+static inline void bitset_clear_all(bitset_t *bs)
+{
+       rbitset_clear_all(bs->data, bs->size);
+}
+
+/**
+ * Set the bitset.
+ * This sets all bits to one.
+ * @param bs The bitset.
+ */
+static inline void bitset_set_all(bitset_t *bs)
 {
-       unsigned i, pop = 0;
-       bitset_unit_t *unit;
+       rbitset_set_all(bs->data, bs->size);
+}
 
-       for(i = 0, unit = bs->data; i < bs->units; ++i, ++unit)
-               pop += _bitset_inside_pop(unit);
+/**
+ * Check, if one bitset is contained by another.
+ * That is, each bit set in lhs is also set in rhs.
+ * @param lhs A bitset.
+ * @param rhs Another bitset.
+ * @return 1, if all bits in lhs are also set in rhs, 0 otherwise.
+ */
+static inline bool bitset_contains(const bitset_t *lhs, const bitset_t *rhs)
+{
+       assert(lhs->size == rhs->size);
+       return rbitset_contains(lhs->data, rhs->data, lhs->size);
+}
 
-       return pop;
+/**
+ * Treat the bitset as a number and subtract 1.
+ * @param bs The bitset.
+ * @return The same bitset.
+ */
+static inline void bitset_minus1(bitset_t *bs)
+{
+       rbitset_minus1(bs->data, bs->size);
 }
 
-/*
- * Here, the binary operations follow.
- * And, Or, And Not, Xor are available.
+/**
+ * Check if two bitsets intersect.
+ * @param a The first bitset.
+ * @param b The second bitset.
+ * @return 1 if they have a bit in common, 0 if not.
+ */
+static inline bool bitset_intersect(const bitset_t *a, const bitset_t *b)
+{
+       assert(a->size == b->size);
+       return rbitsets_have_common(a->data, b->data, a->size);
+}
+
+/**
+ * set or clear all bits in the range [from;to[.
+ * @param a      The bitset.
+ * @param from   The first index to set to one.
+ * @param to     The last index plus one to set to one.
+ * @param do_set If 1 the bits are set, if 0, they are cleared.
+ */
+static inline void bitset_mod_range(bitset_t *a, size_t from, size_t to,
+                                    bool do_set)
+{
+       if (from == to)
+           return;
+
+       if (to < from) {
+               size_t tmp = from;
+               from = to;
+               to = tmp;
+       }
+
+       if (to > a->size)
+               to = a->size;
+
+       rbitset_set_range(a->data, from, to, do_set);
+}
+
+#define bitset_set_range(bs, from, to)   bitset_mod_range((bs), (from), (to), 1)
+#define bitset_clear_range(bs, from, to) bitset_mod_range((bs), (from), (to), 0)
+
+/**
+ * Check, if a bitset is empty.
+ * @param a The bitset.
+ * @return 1, if the bitset is empty, 0 if not.
+ */
+static inline bool bitset_is_empty(const bitset_t *bs)
+{
+       return rbitset_is_empty(bs->data, bs->size);
+}
+
+/**
+ * Print a bitset to a stream.
+ * The bitset is printed as a comma separated list of bits set.
+ * @param file The stream.
+ * @param bs The bitset.
  */
+static inline void bitset_fprint(FILE *file, const bitset_t *bs)
+{
+       const char *prefix = "";
+       size_t i;
 
-#define BINARY_OP(op) \
-static INLINE bitset_t *bitset_ ## op(bitset_t *tgt, bitset_t *src) \
-{ \
-       int i; \
-       int n = tgt->units > src->units ? src->units : tgt->units; \
-       bitset_unit_t *tgt_unit, *src_unit; \
-       src_unit = src->data; \
-       tgt_unit = tgt->data; \
-       for(i = 0; i < n; ++i) \
-               _bitset_inside_ ## op(tgt_unit++, src_unit++); \
-       return tgt; \
+       putc('{', file);
+       for(i = bitset_next_set(bs, 0); i != (size_t)-1; i = bitset_next_set(bs, i + 1)) {
+               ir_fprintf(file, "%s%zu", prefix, i);
+               prefix = ",";
+       }
+       putc('}', file);
 }
 
-BINARY_OP(and)
-BINARY_OP(andnot)
-BINARY_OP(or)
-BINARY_OP(xor)
+/**
+ * Perform tgt = tgt & src operation.
+ * @param tgt  The target bitset.
+ * @param src  The source bitset.
+ * @return the tgt set.
+ */
+static inline void bitset_and(bitset_t *tgt, const bitset_t *src)
+{
+       assert(tgt->size == src->size);
+       rbitset_and(tgt->data, src->data, src->size);
+}
 
+/**
+ * Perform tgt = tgt & ~src operation.
+ * @param tgt  The target bitset.
+ * @param src  The source bitset.
+ * @return the tgt set.
+ */
+static inline void bitset_andnot(bitset_t *tgt, const bitset_t *src)
+{
+       assert(tgt->size == src->size);
+       rbitset_andnot(tgt->data, src->data, src->size);
+}
+
+/**
+ * Perform Union, tgt = tgt u src operation.
+ * @param tgt  The target bitset.
+ * @param src  The source bitset.
+ * @return the tgt set.
+ */
+static inline void bitset_or(bitset_t *tgt, const bitset_t *src)
+{
+       assert(tgt->size == src->size);
+       rbitset_or(tgt->data, src->data, src->size);
+}
+
+/**
+ * Perform tgt = tgt ^ src operation.
+ * @param tgt  The target bitset.
+ * @param src  The source bitset.
+ * @return the tgt set.
+ */
+static inline void bitset_xor(bitset_t *tgt, const bitset_t *src)
+{
+       assert(tgt->size == src->size);
+       rbitset_xor(tgt->data, src->data, src->size);
+}
+
+/**
+ * Copy a raw bitset into an bitset.
+ */
+static inline void rbitset_copy_to_bitset(const unsigned *rbitset,
+                                          bitset_t *bitset)
+{
+       rbitset_copy(bitset->data, rbitset, bitset->size);
+}
 
 #endif