Otawa  0.10
BitSet.h
Go to the documentation of this file.
1 /*
2  * BitSet class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2006-07, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22 #ifndef OTAWA_DFA_BITSET_H
23 #define OTAWA_DFA_BITSET_H
24 
25 #define OTAWA_BITSET_SIZE
26 //#define OTAWA_BITSET_BOTH
27 
28 #if defined(OTAWA_BITSET_WAH) || defined(OTAWA_BITSET_BOTH)
29 # include <elm/util/WAHVector.h>
30 #endif
31 #if defined(OTAWA_BITSET_BOTH) || !defined(OTAWA_BITSET_WAH)
32 # include <elm/util/BitVector.h>
33 #endif
34 #ifdef OTAWA_BITSET_BOTH
35 # include <elm/util/WAHVector.h>
36 # include <elm/util/BitVector.h>
37  class Both {
38  public:
39  Both(void) { }
40  Both(int s, bool v = false): bvec(s, v), wvec(s, v) { }
41  inline int __size(void) const { return bvec.__size() + wvec.__size(); }
42  inline bool isEmpty(void) const { ASSERT(bvec.isEmpty() && wvec.isEmpty()); return bvec.isEmpty(); }
43  inline int countBits(void) const { ASSERT(bvec.countBits() == wvec.countBits()); return bvec.countBits(); };
44  inline int size(void) const { ASSERT(bvec.size() == wvec.size()); return bvec.size(); }
45  inline void set(void) { bvec.set(); wvec.set(); check(); }
46  inline void clear(void) { bvec.clear(); wvec.clear(); check(); }
47  inline bool bit(int i) const { ASSERT(bvec.bit(i) == wvec.bit(i)); return bvec.bit(i); }
48  inline void set(int i) { bvec.set(i); wvec.set(i); check(); }
49  inline void clear(int i) { bvec.clear(i); wvec.clear(i); check(); }
50  inline bool equals(const Both& b) const { ASSERT(bvec.equals(b.bvec) == wvec.equals(b.wvec)); return bvec.equals(b.bvec); }
51  inline bool includes(const Both& b) const { ASSERT(bvec.includes(b.bvec) == wvec.includes(b.wvec)); return bvec.includes(b.bvec); }
52  inline bool includesStrictly(const Both& b) const { ASSERT(bvec.includesStrictly(b.bvec) == wvec.includesStrictly(b.wvec)); return bvec.includesStrictly(b.bvec); }
53  inline void applyNot(void) { bvec.applyNot(); wvec.applyNot(); check(); }
54  inline void applyOr(const Both& b) { bvec.applyOr(b.bvec); wvec.applyOr(b.wvec); check(); }
55  inline void applyAnd(const Both& b) { bvec.applyAnd(b.bvec); wvec.applyAnd(b.wvec); check(); }
56  inline void applyReset(const Both& b) { bvec.applyReset(b.bvec); wvec.applyReset(b.wvec); check(); }
57  inline Both makeNot(void) const { Both b; b.bvec = bvec.makeNot(); b.wvec = wvec.makeNot(); b.check(); return b; }
58  inline Both makeOr(const Both& b) const { Both r; r.bvec = bvec.makeOr(b.bvec); r.wvec = wvec.makeOr(b.wvec); r.check(); return r; }
59  inline Both makeAnd(const Both& b) const { Both r; r.bvec = bvec.makeAnd(b.bvec); r.wvec = wvec.makeAnd(b.wvec); r.check(); return r; }
60  inline Both makeReset(const Both& b) const { Both r; r.bvec = bvec.makeReset(b.bvec); r.wvec = wvec.makeReset(b.wvec); r.check(); return r; }
61 
62  class OneIterator: public elm::BitVector::OneIterator {
63  public:
64  OneIterator(const Both& b): elm::BitVector::OneIterator(b.bvec) { }
65  };
66 
67  private:
68  elm::BitVector bvec;
69  elm::WAHVector wvec;
70 
71  void check(void) {
72  ASSERT(bvec.size() == wvec.size());
73  for(int i = 0; i < bvec.size(); i++)
74  ASSERT(bvec[i] == wvec[i]);
75  }
76  };
77 #endif
78 
79 namespace otawa { namespace dfa {
80 
81 // BitSet class
82 class BitSet {
83 # ifdef OTAWA_BITSET_WAH
84  typedef elm::WAHVector vec_t;
85 # elif defined(OTAWA_BITSET_BOTH)
86  typedef Both vec_t;
87 # else
89 # endif
91 public:
92 # ifdef OTAWA_BITSET_SIZE
94 # define OTAWA_BITSET_ALLOC { __total_size += vec.__size(); __used_size += vec.__size(); if(__used_size >= __max_size) __max_size = __used_size; }
95 # define OTAWA_BITSET_FREE { __used_size -= vec.__size(); }
96 # else
97 # define OTAWA_BITSET_ALLOC
98 # define OTAWA_BITSET_FREE
99 # endif
100 
101  inline BitSet(void) { OTAWA_BITSET_ALLOC }
102  inline BitSet(int size): vec(size) { OTAWA_BITSET_ALLOC }
103  inline BitSet(const BitSet& set): vec(set.vec) { OTAWA_BITSET_ALLOC }
104  inline ~BitSet(void) { OTAWA_BITSET_FREE }
105 
106  inline bool isEmpty(void) const { return vec.isEmpty(); }
107  inline bool isFull(void) const { return vec.countBits() == vec.size(); }
108  inline void fill(void) { vec.set(); }
109  inline void empty(void) { vec.clear(); }
110 
111  inline bool contains(int index) const { return vec.bit(index); }
112  inline void add(int index) { OTAWA_BITSET_FREE vec.set(index); OTAWA_BITSET_ALLOC }
113  inline void remove(int index) { OTAWA_BITSET_FREE vec.clear(index); OTAWA_BITSET_ALLOC }
114 
115  inline bool equals(const BitSet& set) const { return vec.equals(set.vec); }
116  inline bool includes(const BitSet& set) const { return vec.includes(set.vec); }
117  inline bool includesStrictly(const BitSet& set) const { return vec.includesStrictly(set.vec); }
118  inline void complement(void) { vec.applyNot(); }
119  inline int size(void) const { return vec.size(); }
120  inline void add(const BitSet& set) { OTAWA_BITSET_FREE vec.applyOr(set.vec); OTAWA_BITSET_ALLOC }
121  inline void remove(const BitSet& set) { OTAWA_BITSET_FREE vec.applyReset(set.vec); OTAWA_BITSET_ALLOC }
122  inline void mask(const BitSet& set) { OTAWA_BITSET_FREE vec.applyAnd(set.vec); OTAWA_BITSET_ALLOC }
123  inline int count(void) const { return vec.countBits(); }
124 
125  inline BitSet doComp(void) const { return BitSet(vec.makeNot()); }
126  inline BitSet doUnion(const BitSet& set) const { return BitSet(vec.makeOr(set.vec)); }
127  inline BitSet doInter(const BitSet& set) const { return BitSet(vec.makeAnd(set.vec)); }
128  inline BitSet doDiff(const BitSet& set) const { return BitSet(vec.makeReset(set.vec)); }
129 
130  inline BitSet& operator=(const BitSet& set) { OTAWA_BITSET_FREE vec = set.vec; OTAWA_BITSET_ALLOC return *this; }
131 
132  inline BitSet& operator+=(int index) { add(index); return *this; }
133  inline BitSet& operator+=(const BitSet& set) { add(set); return *this; }
134  inline BitSet& operator-=(int index) { remove(index); return *this; }
135  inline BitSet& operator-=(const BitSet& set) { remove(set); return *this; }
136  inline BitSet operator+(const BitSet& set) { return doUnion(set); }
137  inline BitSet operator-(const BitSet& set) { return doDiff(set); }
138 
139  inline BitSet& operator|=(const BitSet& set) { add(set); return *this; }
140  inline BitSet& operator&=(const BitSet& set) { mask(set); return *this; }
141  inline BitSet operator|(const BitSet& set) { return doUnion(set); }
142  inline BitSet operator&(const BitSet& set) { return doInter(set); }
143 
144  inline bool operator==(const BitSet& set) const { return equals(set); }
145  inline bool operator!=(const BitSet& set) const { return !equals(set); }
146  inline bool operator>=(const BitSet& set) const { return includes(set); }
147  inline bool operator<=(const BitSet& set) const { return set.includes(*this); }
148  inline bool operator>(const BitSet& set) const { return includesStrictly(set); }
149  inline bool operator<(const BitSet& set) const { return set.includesStrictly(*this); }
150 
151  // Iterator class
152  class Iterator: public vec_t::OneIterator {
153  public:
154  inline Iterator(const BitSet& set): vec_t::OneIterator(set.vec) { }
155  };
156 
157 private:
158  inline BitSet(const vec_t& ivec): vec(ivec) { }
159 };
160 
161 inline elm::io::Output& operator<<(elm::io::Output& output, const BitSet& bits) {
162  output << "{";
163  bool first = true;
164  for(int i = 0; i < bits.size(); i++)
165  if(bits.contains(i)) {
166  if(first)
167  first = false;
168  else
169  output << ", ";
170  output << i;
171  }
172  output << "}";
173  return output;
174 
175 }
176 
177 } } // otawa::dfa
178 
179 #endif // OTAWA_DFA_BITSET_H
bool operator!=(const BitSet &set) const
Same as !equals().
Definition: BitSet.h:145
bool includes(const BitVector &vec) const
BitSet doComp(void) const
Compute a bit set complement of the current one.
Definition: BitSet.h:125
void set(T *target, int size, const T &v)
static int __total_size
Definition: BitSet.h:93
Iterator(const BitSet &set)
Build an iterator on the items of the given bit set.
Definition: BitSet.h:154
BitVector makeOr(const BitVector &vec) const
void empty(void)
Remove all items from the bit set.
Definition: BitSet.h:109
bool includesStrictly(const BitSet &set) const
Test if the current bit set contains strictly the given one, that is, both bit sets are equals...
Definition: BitSet.h:117
BitSet & operator+=(int index)
Same as add(int).
Definition: BitSet.h:132
BitSet & operator&=(const BitSet &set)
Same as remove(const BitSet&).
Definition: BitSet.h:140
BitVector makeReset(const BitVector &vec) const
bool equals(const BitVector &vec) const
bool operator==(const BitSet &set) const
Same as equals().
Definition: BitSet.h:144
bool operator<=(const BitSet &set) const
Same as !includesStrictly().
Definition: BitSet.h:147
BitVector makeAnd(const BitVector &vec) const
This class implements a set as a bit vector.
Definition: BitSet.h:82
bool isFull(void) const
Test if the bit set contains all existing items.
Definition: BitSet.h:107
BitSet operator&(const BitSet &set)
Same as doInter().
Definition: BitSet.h:142
BitVector makeNot(void) const
elm::io::Output & operator<<(elm::io::Output &output, const BitSet &bits)
Definition: BitSet.h:161
BitSet(const vec_t &ivec)
Definition: BitSet.h:158
BitSet operator-(const BitSet &set)
Same as doDiff().
Definition: BitSet.h:137
BitSet & operator=(const BitSet &set)
Assignment with bit sets.
Definition: BitSet.h:130
BitSet & operator-=(int index)
Same as remove(int).
Definition: BitSet.h:134
#define OTAWA_BITSET_ALLOC
Definition: BitSet.h:94
bool includesStrictly(const BitVector &vec) const
int countBits(void) const
bool contains(int index) const
Test if the bit set contains an item.
Definition: BitSet.h:111
BitSet(void)
Definition: BitSet.h:101
bool bit(int index) const
static int __used_size
Definition: BitSet.h:93
BitSet(int size)
Build a bitset with the given size.
Definition: BitSet.h:102
uint32 size
BitSet operator+(const BitSet &set)
Same as doUnion().
Definition: BitSet.h:136
int size(void) const
Definition: BitSet.h:119
BitSet(const BitSet &set)
Build a bit set from by copying an existing one.
Definition: BitSet.h:103
void mask(const BitSet &set)
Keep only in this set the items also containted in the given one (performs intersection operation)...
Definition: BitSet.h:122
OneIterator(const BitVector &bit_vector)
BitSet operator|(const BitSet &set)
Same as doUnion().
Definition: BitSet.h:141
int count(void) const
Definition: BitSet.h:123
BitSet doInter(const BitSet &set) const
Build a new bit set as intersection between the current and the given one.
Definition: BitSet.h:127
vec_t vec
Definition: BitSet.h:90
void complement(void)
Complement the current bit set.
Definition: BitSet.h:118
BitSet doDiff(const BitSet &set) const
Build a new bit set as difference between the current and the given one.
Definition: BitSet.h:128
BitSet & operator+=(const BitSet &set)
Same as add(const BitSet&).
Definition: BitSet.h:133
void set(int index) const
void clear(int index) const
bool isEmpty(void) const
bool isEmpty(void) const
Test if the bit set is empty.
Definition: BitSet.h:106
bool operator<(const BitSet &set) const
Same as !includes().
Definition: BitSet.h:149
bool operator>(const BitSet &set) const
Same as includesStrictly().
Definition: BitSet.h:148
void clear(T *target, int size)
bool equals(const BitSet &set) const
Test if two bit sets are equals.
Definition: BitSet.h:115
elm::BitVector vec_t
Definition: BitSet.h:88
int size(void) const
BitSet & operator|=(const BitSet &set)
Same as add(const BitSet&).
Definition: BitSet.h:139
void applyReset(const BitVector &vec)
~BitSet(void)
Definition: BitSet.h:104
BitSet doUnion(const BitSet &set) const
Build a new bit set as union between the current and the given one.
Definition: BitSet.h:126
Iterator on the item contained in a bit set.
Definition: BitSet.h:152
bool operator>=(const BitSet &set) const
Same as includes().
Definition: BitSet.h:146
void fill(void)
Fill the bit set with all items.
Definition: BitSet.h:108
void applyNot(void)
void applyOr(const BitVector &vec)
BitSet & operator-=(const BitSet &set)
Same as remove(const BitSet&).
Definition: BitSet.h:135
void add(const BitSet &set)
Add the items of the given bit set to the current one.
Definition: BitSet.h:120
void applyAnd(const BitVector &vec)
bool includes(const BitSet &set) const
Test if the current bit set contains the given one.
Definition: BitSet.h:116
void add(int index)
Add an item to the bit set.
Definition: BitSet.h:112
static int __max_size
Definition: BitSet.h:93
#define OTAWA_BITSET_FREE
Definition: BitSet.h:95