Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
WAHVector.h
1 /*
2  * WAHVector class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2013, 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 02110-1301 USA
20  */
21 #ifndef ELM_UTIL_WAHVECTOR_H_
22 #define ELM_UTIL_WAHVECTOR_H_
23 
24 #include <elm/types.h>
25 #include <elm/io.h>
26 
27 namespace elm {
28 
29 class WAHVector {
30 public:
31  typedef t::uint32 word_t;
32 
33  inline WAHVector(void): rem(0), _size(0), words(0) { }
34  WAHVector(int _size, bool init = false);
35  WAHVector(const WAHVector& v);
36  inline ~WAHVector(void) { if(words) delete [] words; }
37 
38  bool bit(int index) const;
39  bool isEmpty(void) const;
40  bool isFull(void) const;
41  bool equals(const WAHVector& vec) const;
42  bool includes(const WAHVector& vec) const;
43  bool includesStrictly(const WAHVector &vec) const;
44  int size(void) const;
45  inline int countBits(void) const { return countOnes(); }
46  int countOnes(void) const;
47  inline int countZeroes(void) const { return size() - countOnes(); }
48 
49  inline void set(int index, bool value) { if(value) set(index); else clear(index); }
50  void set(int index);
51  void clear(int index);
52 
53  void set(void);
54  void clear(void);
55  void copy(const WAHVector& v);
56  inline void applyAnd(const WAHVector& v) { doAnd(*this, *this, v); }
57  inline WAHVector makeAnd(const WAHVector& v) const { WAHVector r; r.doAnd(r, *this, v); return r; }
58  inline void applyOr(const WAHVector& v) { doOr(*this, *this, v); }
59  inline WAHVector makeOr(const WAHVector& v) const { WAHVector r; r.doOr(r, *this, v); return r; }
60  inline void applyReset(const WAHVector& v) { doReset(*this, *this, v); }
61  inline WAHVector makeReset(const WAHVector& v) const { WAHVector r; r.doReset(r, *this, v); return r; }
62  inline void applyNot(void) { doNot(*this, *this); }
63  inline WAHVector makeNot(void) const { WAHVector r; r.doNot(r, *this); return r; }
64 
65  inline bool operator[](int i) const { return bit(i); }
66  class Bit {
67  public:
68  inline Bit(WAHVector& v, int i): _v(&v), _i(i) { }
69  inline Bit& operator=(const Bit& b) { _v = b._v; _i = b._i; return *this; }
70  inline Bit& operator=(bool b) { _v->set(_i, b); return *this; }
71  inline operator bool(void) const { return _v->bit(_i); }
72  private:
73  WAHVector *_v;
74  int _i;
75  };
76  inline Bit operator[](int i) { return Bit(*this, i); }
77 
78  inline WAHVector operator~(void) const { return makeNot(); }
79  inline WAHVector operator|(const WAHVector &vec) const { return makeOr(vec); }
80  inline WAHVector operator&(const WAHVector &vec) const { return makeAnd(vec); }
81  inline WAHVector operator+(const WAHVector &vec) const { return makeOr(vec); }
82  inline WAHVector operator*(const WAHVector &vec) const { return makeAnd(vec); }
83  inline WAHVector operator-(const WAHVector &vec) const { return makeReset(vec); }
84  inline WAHVector &operator=(const WAHVector &vec) { copy(vec); return *this; }
85  inline WAHVector &operator|=(const WAHVector &vec) { applyOr(vec); return *this; }
86  inline WAHVector &operator&=(const WAHVector &vec) { applyAnd(vec); return *this; }
87  inline WAHVector &operator+=(const WAHVector &vec) { applyOr(vec); return *this; }
88  inline WAHVector &operator*=(const WAHVector &vec) { applyAnd(vec); return *this; }
89  inline WAHVector &operator-=(const WAHVector &vec) { applyReset(vec); return *this; }
90  inline bool operator==(const WAHVector& v) { return equals(v); }
91  inline bool operator!=(const WAHVector& v) { return !equals(v); }
92  inline bool operator<(const WAHVector &vec) const { return vec.includesStrictly(*this); }
93  inline bool operator<=(const WAHVector &vec) const { return vec.includes(*this); }
94  inline bool operator>(const WAHVector &vec) const { return includesStrictly(vec); }
95  inline bool operator>=(const WAHVector &vec) const { return includes(vec); }
96 
97 # ifndef NDEBUG
98  void __dump(io::Output& out) const;
99 # endif
100  inline int __size(void) const { return sizeof(WAHVector) + _size * sizeof(word_t); }
101 
102 private:
103  static void doAnd(WAHVector& r, const WAHVector& v1, const WAHVector& v2);
104  static void doOr(WAHVector& r, const WAHVector& v1, const WAHVector& v2);
105  static void doReset(WAHVector& r, const WAHVector& v1, const WAHVector& v2);
106  static void doNot(WAHVector& r, const WAHVector& v);
107  t::uint32 rem;
108  t::uint32 _size;
109  word_t *words;
110 };
111 
112 #ifndef NDEBUG
113  inline io::Output& operator<<(io::Output& out, const WAHVector& v) { v.__dump(out); return out; }
114 #endif
115 
116 }; // elm
117 
118 #endif // ELM_UTIL_WAHVECTOR_H_
elm::WAHVector::isFull
bool isFull(void) const
Definition: util_WAHVector.cpp:288
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::WAHVector::operator*
WAHVector operator*(const WAHVector &vec) const
Definition: WAHVector.h:82
elm::WAHVector::isEmpty
bool isEmpty(void) const
Definition: util_WAHVector.cpp:272
elm::WAHVector
Definition: WAHVector.h:29
elm::WAHVector::Bit::operator=
Bit & operator=(const Bit &b)
Definition: WAHVector.h:69
elm::operator<<
AutoString & operator<<(CString str, const T &value)
Definition: AutoString.h:75
elm::WAHVector::~WAHVector
~WAHVector(void)
Definition: WAHVector.h:36
elm::WAHVector::includesStrictly
bool includesStrictly(const WAHVector &vec) const
Definition: util_WAHVector.cpp:350
elm::WAHVector::operator+=
WAHVector & operator+=(const WAHVector &vec)
Definition: WAHVector.h:87
elm::WAHVector::operator&=
WAHVector & operator&=(const WAHVector &vec)
Definition: WAHVector.h:86
elm::WAHVector::applyOr
void applyOr(const WAHVector &v)
Definition: WAHVector.h:58
value
bool
elm::WAHVector::operator[]
bool operator[](int i) const
Definition: WAHVector.h:65
elm::WAHVector::Bit::Bit
Bit(WAHVector &v, int i)
Definition: WAHVector.h:68
elm::WAHVector::Bit
Definition: WAHVector.h:66
elm
Definition: adapter.h:26
elm::WAHVector::operator!=
bool operator!=(const WAHVector &v)
Definition: WAHVector.h:91
elm::word_t
WAHVector::word_t word_t
Definition: util_WAHVector.cpp:29
elm::WAHVector::operator[]
Bit operator[](int i)
Definition: WAHVector.h:76
elm::WAHVector::applyReset
void applyReset(const WAHVector &v)
Definition: WAHVector.h:60
elm::WAHVector::countBits
int countBits(void) const
Definition: WAHVector.h:45
elm::WAHVector::countOnes
int countOnes(void) const
Definition: util_WAHVector.cpp:374
elm::WAHVector::equals
bool equals(const WAHVector &vec) const
Definition: util_WAHVector.cpp:304
elm::WAHVector::operator~
WAHVector operator~(void) const
Definition: WAHVector.h:78
elm::WAHVector::operator*=
WAHVector & operator*=(const WAHVector &vec)
Definition: WAHVector.h:88
elm::WAHVector::clear
void clear(void)
Definition: util_WAHVector.cpp:513
elm::WAHVector::copy
void copy(const WAHVector &v)
Definition: util_WAHVector.cpp:530
elm::WAHVector::makeOr
WAHVector makeOr(const WAHVector &v) const
Definition: WAHVector.h:59
elm::WAHVector::bit
bool bit(int index) const
Definition: util_WAHVector.cpp:258
elm::WAHVector::operator-=
WAHVector & operator-=(const WAHVector &vec)
Definition: WAHVector.h:89
elm::WAHVector::operator+
WAHVector operator+(const WAHVector &vec) const
Definition: WAHVector.h:81
elm::WAHVector::operator|=
WAHVector & operator|=(const WAHVector &vec)
Definition: WAHVector.h:85
elm::WAHVector::operator<
bool operator<(const WAHVector &vec) const
Definition: WAHVector.h:92
elm::WAHVector::operator==
bool operator==(const WAHVector &v)
Definition: WAHVector.h:90
elm::WAHVector::makeReset
WAHVector makeReset(const WAHVector &v) const
Definition: WAHVector.h:61
elm::WAHVector::applyAnd
void applyAnd(const WAHVector &v)
Definition: WAHVector.h:56
elm::WAHVector::operator>
bool operator>(const WAHVector &vec) const
Definition: WAHVector.h:94
elm::WAHVector::includes
bool includes(const WAHVector &vec) const
Definition: util_WAHVector.cpp:319
elm::WAHVector::size
int size(void) const
Definition: util_WAHVector.cpp:359
elm::t::uint32
unsigned int uint32
Definition: arch.h:31
elm::WAHVector::__dump
void __dump(io::Output &out) const
Definition: util_WAHVector.cpp:690
elm::WAHVector::applyNot
void applyNot(void)
Definition: WAHVector.h:62
elm::WAHVector::makeNot
WAHVector makeNot(void) const
Definition: WAHVector.h:63
elm::WAHVector::Bit::operator=
Bit & operator=(bool b)
Definition: WAHVector.h:70
elm::WAHVector::countZeroes
int countZeroes(void) const
Definition: WAHVector.h:47
elm::WAHVector::set
void set(void)
Definition: util_WAHVector.cpp:521
elm::WAHVector::__size
int __size(void) const
Definition: WAHVector.h:100
elm::WAHVector::operator=
WAHVector & operator=(const WAHVector &vec)
Definition: WAHVector.h:84
elm::WAHVector::operator<=
bool operator<=(const WAHVector &vec) const
Definition: WAHVector.h:93
elm::WAHVector::word_t
t::uint32 word_t
Definition: WAHVector.h:31
elm::WAHVector::WAHVector
WAHVector(void)
Definition: WAHVector.h:33
elm::io::Output
Definition: Output.h:179
elm::WAHVector::operator&
WAHVector operator&(const WAHVector &vec) const
Definition: WAHVector.h:80
elm::WAHVector::operator>=
bool operator>=(const WAHVector &vec) const
Definition: WAHVector.h:95
elm::WAHVector::operator|
WAHVector operator|(const WAHVector &vec) const
Definition: WAHVector.h:79
elm::WAHVector::operator-
WAHVector operator-(const WAHVector &vec) const
Definition: WAHVector.h:83
elm::WAHVector::makeAnd
WAHVector makeAnd(const WAHVector &v) const
Definition: WAHVector.h:57
elm::WAHVector::set
void set(int index, bool value)
Definition: WAHVector.h:49