CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
strbitset.h
Go to the documentation of this file.
1 #ifndef strbitset_h
2 #define strbitset_h
3 
15 #include <string>
16 #include <map>
17 #include <vector>
18 #include <iostream>
19 #include <fstream>
20 
21 namespace pat {
22 
23  class strbitset {
24  public:
25  class index_type {
26  public:
27  typedef unsigned int size_t;
28 
29  friend class strbitset;
30 
31  index_type() : bitset_(nullptr), i_(0) {}
32 
33  index_type(strbitset const* b, std::string const& s) : bitset_(b) {
34  if (bitset_) {
35  i_ = bitset_->index(s);
36  } else {
37  i_ = 0;
38  }
39  }
40 
41  std::string const& str() const { return bitset_->index(i_); }
42 
43  bool operator<(index_type const& r) const { return i_ < r.i_; }
44  bool operator>(index_type const& r) const { return i_ > r.i_; }
45  bool operator<=(index_type const& r) const { return i_ <= r.i_; }
46  bool operator>=(index_type const& r) const { return i_ >= r.i_; }
47  bool operator==(index_type const& r) const { return i_ == r.i_; }
48 
49  friend std::ostream& operator<<(std::ostream& out, const index_type& r);
50 
51  protected:
53  size_t i_;
54  };
55 
56  friend class index_type;
57 
58  // Add typedefs
59  typedef unsigned int size_t;
60  typedef std::map<std::string, size_t> str_index_map;
61  typedef std::vector<bool> bit_vector;
62 
64  strbitset() { clear(); }
65 
67  void clear() {
68  map_.clear();
69  bits_.clear();
70  }
71 
73  operator bool() const {
74  bool b = true;
75  for (bit_vector::const_iterator bitsBegin = bits_.begin(), bitsEnd = bits_.end(), ibit = bitsBegin;
76  ibit != bitsEnd;
77  ++ibit) {
78  if (*ibit == false)
79  b = false;
80  }
81  return b;
82  }
83 
85  bool operator!() const { return !(operator bool()); }
86 
91  if (map_.find(s) == map_.end()) {
92  map_[s] = bits_.size();
93  bits_.resize(bits_.size() + 1);
94  *(bits_.rbegin()) = false;
95  } else {
96  std::cout << "Duplicate entry " << s << ", not added to registry" << std::endl;
97  }
98  }
99 
101  void print(std::ostream& out) const {
102  for (str_index_map::const_iterator mbegin = map_.begin(), mend = map_.end(), mit = mbegin; mit != mend; ++mit) {
103  char buff[100];
104  sprintf(buff, "%10s = %6i", mit->first.c_str(), (int)(bits_.at(mit->second)));
105  out << buff << std::endl;
106  }
107  }
108 
110  bit_vector::const_reference operator[](const std::string s) const {
111  size_t index = this->index(s);
112  return bits_.operator[](index);
113  }
114 
115  bit_vector::const_reference operator[](index_type const& i) const { return bits_.operator[](i.i_); }
116 
119  size_t index = this->index(s);
120  return bits_.operator[](index);
121  }
122 
123  bit_vector::reference operator[](index_type const& i) { return bits_.operator[](i.i_); }
124 
126  strbitset& set(bool val = true) {
127  for (bit_vector::iterator ibegin = bits_.begin(), iend = bits_.end(), i = ibegin; i != iend; ++i) {
128  *i = val;
129  }
130  return *this;
131  }
132 
135  for (bit_vector::iterator ibegin = bits_.begin(), iend = bits_.end(), i = ibegin; i != iend; ++i) {
136  *i = !(*i);
137  }
138  return *this;
139  }
140 
142  strbitset& set(std::string s, bool val = true) {
143  (*this)[s] = val;
144  return *this;
145  }
146 
147  strbitset& set(index_type const& i, bool val = true) {
148  (*this)[i] = val;
149  return *this;
150  }
151 
154  (*this)[s] = !((*this)[s]);
155  return *this;
156  }
157 
159  (*this)[i] = !((*this)[i]);
160  return *this;
161  }
162 
165  strbitset ret(*this);
166  for (bit_vector::iterator ibegin = ret.bits_.begin(), iend = ret.bits_.end(), i = ibegin; i != iend; ++i) {
167  *i = !(*i);
168  }
169  return ret;
170  }
171 
174  if (map_.size() != r.map_.size()) {
175  std::cout << "strbitset operator&= : bitsets not the same size" << std::endl;
176  } else {
177  str_index_map::iterator ibegin = map_.begin(), iend = map_.end(), i = ibegin;
178  for (; i != iend; ++i) {
179  std::string key = i->first;
180  str_index_map::const_iterator j = r.map_.find(key);
181  if (j == r.map_.end()) {
182  std::cout << "strbitset operator&= : cannot find key " << key << std::endl;
183  } else {
184  (*this)[key] = (*this)[key] && r[key];
185  }
186  }
187  }
188  return *this;
189  }
190 
193  if (map_.size() != r.map_.size()) {
194  std::cout << "strbitset operator&= : bitsets not the same size" << std::endl;
195  } else {
196  str_index_map::iterator ibegin = map_.begin(), iend = map_.end(), i = ibegin;
197  for (; i != iend; ++i) {
198  std::string key = i->first;
199  str_index_map::const_iterator j = r.map_.find(key);
200  if (j == r.map_.end()) {
201  std::cout << "strbitset operator&= : cannot find key " << key << std::endl;
202  } else {
203  (*this)[key] = (*this)[key] || r[key];
204  }
205  }
206  }
207  return *this;
208  }
209 
212  if (map_.size() != r.map_.size()) {
213  std::cout << "strbitset operator&= : bitsets not the same size" << std::endl;
214  } else {
215  str_index_map::iterator ibegin = map_.begin(), iend = map_.end(), i = ibegin;
216  for (; i != iend; ++i) {
217  std::string key = i->first;
218  str_index_map::const_iterator j = r.map_.find(key);
219  if (j == r.map_.end()) {
220  std::cout << "strbitset operator&= : cannot find key " << key << std::endl;
221  } else {
222  (*this)[key] = ((*this)[key] || r[key]) && !((*this)[key] && r[key]);
223  }
224  }
225  }
226  return *this;
227  }
228 
230  bool operator==(const strbitset& r) const {
231  if (map_.size() != r.map_.size()) {
232  std::cout << "strbitset operator&= : bitsets not the same size" << std::endl;
233  } else {
234  str_index_map::const_iterator ibegin = map_.begin(), iend = map_.end(), i = ibegin;
235  for (; i != iend; ++i) {
236  std::string key = i->first;
237  str_index_map::const_iterator j = r.map_.find(key);
238  if (j == r.map_.end()) {
239  std::cout << "strbitset operator&= : cannot find key " << key << std::endl;
240  } else {
241  if ((*this)[key] != r[key])
242  return false;
243  }
244  }
245  }
246  return true;
247  }
248 
250  bool operator==(bool b) const {
251  bool result = true;
252  for (bit_vector::const_iterator iBegin = bits_.begin(), iEnd = bits_.end(), ibit = iBegin; ibit != iEnd; ++ibit) {
253  result &= (*ibit == b);
254  }
255  return result;
256  }
257 
259  bool operator!=(const strbitset& r) const { return !(operator==(r)); }
260 
262  bool operator!=(bool b) const { return !(operator==(b)); }
263 
265  size_t count() const {
266  size_t ret = 0;
267  for (bit_vector::const_iterator ibegin = bits_.begin(), iend = bits_.end(), i = ibegin; i != iend; ++i) {
268  if (*i)
269  ++ret;
270  }
271  return ret;
272  }
273 
275  size_t any() const {
276  for (bit_vector::const_iterator ibegin = bits_.begin(), iend = bits_.end(), i = ibegin; i != iend; ++i) {
277  if (*i)
278  return true;
279  }
280  return true;
281  }
282 
284  size_t none() const { return !any(); }
285 
287  bool test(std::string s) const { return (*this)[s] == true; }
288 
289  bool test(index_type const& i) const { return (*this)[i] == true; }
290 
292  const bit_vector& bits() const { return bits_; }
293 
295  const std::vector<std::string> strings() const {
296  std::vector<std::string> strings;
297  strings.resize(bits_.size());
298  for (str_index_map::const_iterator it = map_.begin(), end = map_.end(); it != end; ++it) {
299  strings[it->second] = it->first;
300  }
301  return strings;
302  }
303 
304  friend strbitset operator&(const strbitset& l, const strbitset& r);
305  friend strbitset operator|(const strbitset& l, const strbitset& r);
306  friend strbitset operator^(const strbitset& l, const strbitset& r);
307 
308  private:
311  size_t index(std::string s) const {
312  str_index_map::const_iterator f = map_.find(s);
313  if (f == map_.end()) {
314  std::cout << "Cannot find " << s << ", returning size()" << std::endl;
315  return map_.size();
316  } else {
317  return f->second;
318  }
319  }
320 
321  std::string const& index(size_t i) const {
322  for (str_index_map::const_iterator f = map_.begin(), fEnd = map_.end(); f != fEnd; ++f) {
323  if (f->second == i)
324  return f->first;
325  }
326  std::cout << "Cannot find " << i << ", returning dummy" << std::endl;
327  return dummy_;
328  }
329 
330  static const std::string dummy_;
333  };
334 
335  strbitset operator&(const strbitset& l, const strbitset& r);
336 
337  strbitset operator|(const strbitset& l, const strbitset& r);
338  strbitset operator^(const strbitset& l, const strbitset& r);
339 
340 } // namespace pat
341 
342 #endif
bit_vector::const_reference operator[](index_type const &i) const
Definition: strbitset.h:115
strbitset & set(std::string s, bool val=true)
set method of one bit
Definition: strbitset.h:142
bool operator!() const
! Logical negation of bool()
Definition: strbitset.h:85
tuple ret
prodAgent to be discontinued
bool operator<(index_type const &r) const
Definition: strbitset.h:43
std::map< std::string, size_t > str_index_map
Definition: strbitset.h:60
size_t count() const
returns number of bits set
Definition: strbitset.h:265
friend strbitset operator^(const strbitset &l, const strbitset &r)
strbitset & flip()
flip method of all bits
Definition: strbitset.h:134
strbitset & operator&=(const strbitset &r)
bitwise and
Definition: strbitset.h:173
bool test(index_type const &i) const
Definition: strbitset.h:289
friend strbitset operator|(const strbitset &l, const strbitset &r)
strbitset operator|(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:13
void print(std::ostream &out) const
print method
Definition: strbitset.h:101
friend strbitset operator&(const strbitset &l, const strbitset &r)
bit_vector::reference operator[](index_type const &i)
Definition: strbitset.h:123
std::string const & index(size_t i) const
Definition: strbitset.h:321
bool operator==(const strbitset &r) const
equality operator
Definition: strbitset.h:230
tuple result
Definition: mps_fire.py:311
bool operator!=(const strbitset &r) const
inequality operator
Definition: strbitset.h:259
strbitset operator~()
logical negation
Definition: strbitset.h:164
strbitset & flip(std::string s)
flip method of one bit
Definition: strbitset.h:153
strbitset()
constructor: just clears the bitset and map
Definition: strbitset.h:64
index_type(strbitset const *b, std::string const &s)
Definition: strbitset.h:33
unsigned int size_t
Definition: strbitset.h:59
bool operator>(index_type const &r) const
Definition: strbitset.h:44
strbitset operator^(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:19
bit_vector::reference operator[](const std::string s)
access method non-const
Definition: strbitset.h:118
std::string const & str() const
Definition: strbitset.h:41
const bit_vector & bits() const
give access to the ordered bits
Definition: strbitset.h:292
tuple key
prepare the HTCondor submission files and eventually submit them
bool operator==(bool b) const
equality operator to bool
Definition: strbitset.h:250
strbitset const * bitset_
Definition: strbitset.h:52
strbitset operator&(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:7
void clear()
clear the bitset and map
Definition: strbitset.h:67
size_t none() const
returns true if none are set
Definition: strbitset.h:284
strbitset & set(index_type const &i, bool val=true)
Definition: strbitset.h:147
bool operator>=(index_type const &r) const
Definition: strbitset.h:46
strbitset & set(bool val=true)
set method of all bits
Definition: strbitset.h:126
double b
Definition: hdecay.h:118
size_t index(std::string s) const
Definition: strbitset.h:311
strbitset & flip(index_type const &i)
Definition: strbitset.h:158
bool test(std::string s) const
test
Definition: strbitset.h:287
static const std::string dummy_
Definition: strbitset.h:330
bit_vector bits_
the actual bits, indexed by the index in &quot;map_&quot;
Definition: strbitset.h:332
const std::vector< std::string > strings() const
give access to the ordered strings
Definition: strbitset.h:295
string end
Definition: dataset.py:937
tuple cout
Definition: gather_cfg.py:144
bit_vector::const_reference operator[](const std::string s) const
access method const
Definition: strbitset.h:110
bool operator!=(bool b) const
inequality operator to bool
Definition: strbitset.h:262
str_index_map map_
map that holds the string–&gt;index map
Definition: strbitset.h:331
size_t any() const
returns true if any are set
Definition: strbitset.h:275
friend std::ostream & operator<<(std::ostream &out, const index_type &r)
Definition: strbitset.cc:25
strbitset & operator^=(const strbitset &r)
bitwise xor
Definition: strbitset.h:211
bool operator==(index_type const &r) const
Definition: strbitset.h:47
std::vector< bool > bit_vector
Definition: strbitset.h:61
bool operator<=(index_type const &r) const
Definition: strbitset.h:45
strbitset & operator|=(const strbitset &r)
bitwise or
Definition: strbitset.h:192
void push_back(std::string s)
Definition: strbitset.h:90