CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
strbitset.h
Go to the documentation of this file.
1 #ifndef strbitset_h
2 #define strbitset_h
3 
16 #include <string>
17 #include <map>
18 #include <vector>
19 #include <iostream>
20 #include <fstream>
21 
22 namespace pat {
23 
24 class strbitset {
25  public:
26 
27 
28  class index_type {
29  public:
30  typedef unsigned int size_t;
31 
32  friend class strbitset;
33 
34  index_type() :bitset_(0), i_(0) {}
35 
36  index_type( strbitset const * b, std::string const & s) :
37  bitset_(b)
38  {
39  if ( bitset_ ) {
40  i_ = bitset_->index(s);
41  } else {
42  i_ = 0;
43  }
44  }
45 
46  std::string const & str() const { return bitset_->index(i_);}
47 
48  bool operator< ( index_type const & r) const { return i_ < r.i_;}
49  bool operator> ( index_type const & r) const { return i_ > r.i_;}
50  bool operator<=( index_type const & r) const { return i_ <= r.i_;}
51  bool operator>=( index_type const & r) const { return i_ >= r.i_;}
52  bool operator==( index_type const & r) const { return i_ == r.i_;}
53 
54  friend std::ostream & operator<<(std::ostream & out, const index_type & r);
55 
56  protected:
57  strbitset const * bitset_;
58  size_t i_;
59  };
60 
61 
62  friend class index_type;
63 
64  // Add typedefs
65  typedef unsigned int size_t;
66  typedef std::map<std::string, size_t> str_index_map;
67  typedef std::vector<bool> bit_vector;
68 
71  clear();
72  }
73 
75  void clear() {
76  map_.clear();
77  bits_.clear();
78  }
79 
81  operator bool() const {
82  bool b = true;
83  for ( bit_vector::const_iterator bitsBegin = bits_.begin(),
84  bitsEnd = bits_.end(), ibit = bitsBegin;
85  ibit != bitsEnd; ++ibit ) {
86  if ( *ibit == false ) b = false;
87  }
88  return b;
89  }
90 
92  bool operator!() const {
93  return ! ( operator bool() );
94  }
95 
99  void push_back( std::string s ) {
100  if ( map_.find(s) == map_.end() ) {
101  map_[s] = bits_.size();
102  bits_.resize( bits_.size() + 1 );
103  *(bits_.rbegin()) = false;
104  } else {
105  std::cout << "Duplicate entry " << s << ", not added to registry" << std::endl;
106  }
107  }
108 
109 
111  void print(std::ostream & out) const {
112  for( str_index_map::const_iterator mbegin = map_.begin(),
113  mend = map_.end(),
114  mit = mbegin;
115  mit != mend; ++mit ) {
116  char buff[100];
117  sprintf(buff, "%10s = %6d", mit->first.c_str(), bits_.at(mit->second));
118  out << buff << std::endl;
119  }
120  }
121 
123  bit_vector::const_reference operator[] ( const std::string s) const {
124  size_t index = this->index(s);
125  return bits_.operator[](index);
126  }
127 
128  bit_vector::const_reference operator[] ( index_type const & i) const {
129  return bits_.operator[](i.i_);
130  }
131 
133  bit_vector::reference operator[] ( const std::string s) {
134  size_t index = this->index(s);
135  return bits_.operator[](index);
136  }
137 
139  return bits_.operator[](i.i_);
140  }
141 
142 
144  strbitset & set( bool val = true) {
145  for ( bit_vector::iterator ibegin = bits_.begin(),
146  iend = bits_.end(), i = ibegin;
147  i != iend; ++i ) {
148  *i = val;
149  }
150  return *this;
151  }
152 
155  for ( bit_vector::iterator ibegin = bits_.begin(),
156  iend = bits_.end(), i = ibegin;
157  i != iend; ++i ) {
158  *i = ! (*i);
159  }
160  return *this;
161  }
162 
164  strbitset & set( std::string s, bool val = true) {
165  (*this)[s] = val;
166  return *this;
167  }
168 
169  strbitset & set( index_type const & i, bool val = true) {
170  (*this)[i] = val;
171  return *this;
172  }
173 
174 
176  strbitset & flip( std::string s) {
177  (*this)[s] = !( (*this)[s] );
178  return *this;
179  }
180 
181  strbitset & flip( index_type const & i ) {
182  (*this)[i] = !( (*this)[i] );
183  return *this;
184  }
185 
188  strbitset ret(*this);
189  for ( bit_vector::iterator ibegin = ret.bits_.begin(),
190  iend = ret.bits_.end(), i = ibegin;
191  i != iend; ++i ) {
192  *i = !(*i);
193  }
194  return ret;
195  }
196 
199  if ( map_.size() != r.map_.size() ) {
200  std::cout << "strbitset operator&= : bitsets not the same size" << std::endl;
201  } else {
202  str_index_map::iterator ibegin = map_.begin(), iend = map_.end(), i = ibegin;
203  for ( ; i != iend; ++i ) {
204  std::string key = i->first;
205  str_index_map::const_iterator j = r.map_.find( key );
206  if ( j == r.map_.end() ) {
207  std::cout << "strbitset operator&= : cannot find key " << key << std::endl;
208  } else {
209  (*this)[key] = (*this)[key] && r[key];
210  }
211  }
212  }
213  return *this;
214  }
215 
216 
219  if ( map_.size() != r.map_.size() ) {
220  std::cout << "strbitset operator&= : bitsets not the same size" << std::endl;
221  } else {
222  str_index_map::iterator ibegin = map_.begin(), iend = map_.end(), i = ibegin;
223  for ( ; i != iend; ++i ) {
224  std::string key = i->first;
225  str_index_map::const_iterator j = r.map_.find( key );
226  if ( j == r.map_.end() ) {
227  std::cout << "strbitset operator&= : cannot find key " << key << std::endl;
228  } else {
229  (*this)[key] = (*this)[key] || r[key];
230  }
231  }
232  }
233  return *this;
234  }
235 
236 
237 
240  if ( map_.size() != r.map_.size() ) {
241  std::cout << "strbitset operator&= : bitsets not the same size" << std::endl;
242  } else {
243  str_index_map::iterator ibegin = map_.begin(), iend = map_.end(), i = ibegin;
244  for ( ; i != iend; ++i ) {
245  std::string key = i->first;
246  str_index_map::const_iterator j = r.map_.find( key );
247  if ( j == r.map_.end() ) {
248  std::cout << "strbitset operator&= : cannot find key " << key << std::endl;
249  } else {
250  (*this)[key] = ( (*this)[key] || r[key]) && ! ((*this)[key] && r[key]);
251  }
252  }
253  }
254  return *this;
255  }
256 
257 
258 
259 
260 
262  bool operator==( const strbitset & r) const {
263  if ( map_.size() != r.map_.size() ) {
264  std::cout << "strbitset operator&= : bitsets not the same size" << std::endl;
265  } else {
266  str_index_map::const_iterator ibegin = map_.begin(), iend = map_.end(), i = ibegin;
267  for ( ; i != iend; ++i ) {
268  std::string key = i->first;
269  str_index_map::const_iterator j = r.map_.find( key );
270  if ( j == r.map_.end() ) {
271  std::cout << "strbitset operator&= : cannot find key " << key << std::endl;
272  } else {
273  if ( (*this)[key] != r[key] ) return false;
274  }
275  }
276  }
277  return true;
278  }
279 
281  bool operator==( bool b ) const {
282  bool result = true;
283  for ( bit_vector::const_iterator iBegin = bits_.begin(),
284  iEnd = bits_.end(), ibit = iBegin;
285  ibit != iEnd; ++ibit ) {
286  result &= ( *ibit == b );
287  }
288  return result;
289  }
290 
291 
293  bool operator!=( const strbitset & r) const {
294  return ! (operator==(r));
295  }
296 
298  bool operator!=( bool b ) const {
299  return ! ( operator!=(b));
300  }
301 
303  size_t count() const {
304  size_t ret = 0;
305  for ( bit_vector::const_iterator ibegin = bits_.begin(),
306  iend = bits_.end(), i = ibegin;
307  i != iend; ++i ) {
308  if ( *i ) ++ret;
309  }
310  return ret;
311  }
312 
313 
314 
316  size_t any() const {
317  for ( bit_vector::const_iterator ibegin = bits_.begin(),
318  iend = bits_.end(), i = ibegin;
319  i != iend; ++i ) {
320  if ( *i ) return true;
321  }
322  return true;
323  }
324 
326  size_t none () const {
327  return !any();
328  }
329 
331  bool test(std::string s) const {
332  return (*this)[s] == true;
333  }
334 
335  bool test(index_type const &i) const {
336  return (*this)[i] == true;
337  }
338 
340  const bit_vector& bits() const {
341  return bits_;
342  }
343 
344 
346  const std::vector<std::string> strings() const {
347  std::vector<std::string> strings;
348  strings.resize(bits_.size());
349  for (str_index_map::const_iterator it = map_.begin(),
350  end = map_.end(); it != end; ++it){
351  strings[it->second] = it->first;
352  }
353  return strings;
354  }
355 
356 
357 
358  friend strbitset operator&(const strbitset& l, const strbitset& r);
359  friend strbitset operator|(const strbitset& l, const strbitset& r);
360  friend strbitset operator^(const strbitset& l, const strbitset& r);
361 
362 
363  private:
364 
365 
368  size_t index(std::string s) const {
369  str_index_map::const_iterator f = map_.find(s);
370  if ( f == map_.end() ) {
371  std::cout << "Cannot find " << s << ", returning size()" << std::endl;
372  return map_.size();
373  } else {
374  return f->second;
375  }
376  }
377 
378  std::string const & index( size_t i ) const {
379  for ( str_index_map::const_iterator f = map_.begin(),
380  fEnd = map_.end();
381  f != fEnd; ++f ) {
382  if ( f->second == i ) return f->first;
383  }
384  std::cout << "Cannot find " << i << ", returning dummy" << std::endl;
385  return dummy_;
386 
387  }
388 
389  static const std::string dummy_;
392 };
393 
394  strbitset operator&(const strbitset& l, const strbitset& r);
395 
396  strbitset operator|(const strbitset& l, const strbitset& r);
397  strbitset operator^(const strbitset& l, const strbitset& r);
398 
399 
400 
401 }
402 
403 #endif
strbitset & set(std::string s, bool val=true)
set method of one bit
Definition: strbitset.h:164
int i
Definition: DBlmapReader.cc:9
bool operator!() const
! Logical negation of bool()
Definition: strbitset.h:92
bool operator<(index_type const &r) const
Definition: strbitset.h:48
std::map< std::string, size_t > str_index_map
Definition: strbitset.h:66
size_t count() const
returns number of bits set
Definition: strbitset.h:303
friend strbitset operator^(const strbitset &l, const strbitset &r)
strbitset & flip()
flip method of all bits
Definition: strbitset.h:154
strbitset & operator&=(const strbitset &r)
bitwise and
Definition: strbitset.h:198
bool test(index_type const &i) const
Definition: strbitset.h:335
friend strbitset operator|(const strbitset &l, const strbitset &r)
strbitset operator|(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:15
void print(std::ostream &out) const
print method
Definition: strbitset.h:111
friend strbitset operator&(const strbitset &l, const strbitset &r)
std::string const & index(size_t i) const
Definition: strbitset.h:378
bool operator==(const strbitset &r) const
equality operator
Definition: strbitset.h:262
bool operator!=(const strbitset &r) const
inequality operator
Definition: strbitset.h:293
strbitset operator~()
logical negation
Definition: strbitset.h:187
strbitset & flip(std::string s)
flip method of one bit
Definition: strbitset.h:176
strbitset()
constructor: just clears the bitset and map
Definition: strbitset.h:70
index_type(strbitset const *b, std::string const &s)
Definition: strbitset.h:36
unsigned int size_t
Definition: strbitset.h:65
bool operator>(index_type const &r) const
Definition: strbitset.h:49
strbitset operator^(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:21
std::string const & str() const
Definition: strbitset.h:46
const bit_vector & bits() const
give access to the ordered bits
Definition: strbitset.h:340
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
bool operator==(bool b) const
equality operator to bool
Definition: strbitset.h:281
strbitset const * bitset_
Definition: strbitset.h:57
double f[11][100]
strbitset operator&(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:9
#define end
Definition: vmac.h:38
void clear()
clear the bitset and map
Definition: strbitset.h:75
size_t none() const
returns true if none are set
Definition: strbitset.h:326
tuple out
Definition: dbtoconf.py:99
strbitset & set(index_type const &i, bool val=true)
Definition: strbitset.h:169
bool operator>=(index_type const &r) const
Definition: strbitset.h:51
strbitset & set(bool val=true)
set method of all bits
Definition: strbitset.h:144
double b
Definition: hdecay.h:120
size_t index(std::string s) const
Definition: strbitset.h:368
strbitset & flip(index_type const &i)
Definition: strbitset.h:181
bool test(std::string s) const
test
Definition: strbitset.h:331
static const std::string dummy_
Definition: strbitset.h:389
bit_vector bits_
the actual bits, indexed by the index in &quot;map_&quot;
Definition: strbitset.h:391
list key
Definition: combine.py:13
const std::vector< std::string > strings() const
give access to the ordered strings
Definition: strbitset.h:346
tuple cout
Definition: gather_cfg.py:121
bit_vector::const_reference operator[](const std::string s) const
access method const
Definition: strbitset.h:123
bool operator!=(bool b) const
inequality operator to bool
Definition: strbitset.h:298
str_index_map map_
map that holds the string–&gt;index map
Definition: strbitset.h:390
size_t any() const
returns true if any are set
Definition: strbitset.h:316
friend std::ostream & operator<<(std::ostream &out, const index_type &r)
Definition: strbitset.cc:27
strbitset & operator^=(const strbitset &r)
bitwise xor
Definition: strbitset.h:239
bool operator==(index_type const &r) const
Definition: strbitset.h:52
std::vector< bool > bit_vector
Definition: strbitset.h:67
bool operator<=(index_type const &r) const
Definition: strbitset.h:50
strbitset & operator|=(const strbitset &r)
bitwise or
Definition: strbitset.h:218
void push_back(std::string s)
Definition: strbitset.h:99