CMS 3D CMS Logo

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
pat::strbitset::operator[]
bit_vector::const_reference operator[](index_type const &i) const
Definition: strbitset.h:115
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:367
pat::strbitset::bits
const bit_vector & bits() const
give access to the ordered bits
Definition: strbitset.h:292
pat::strbitset::index_type::operator<=
bool operator<=(index_type const &r) const
Definition: strbitset.h:45
pat::strbitset::any
size_t any() const
returns true if any are set
Definition: strbitset.h:275
electrons_cff.bool
bool
Definition: electrons_cff.py:393
pat::operator&
strbitset operator&(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:7
mps_fire.i
i
Definition: mps_fire.py:428
pat::strbitset::index_type::operator>
bool operator>(index_type const &r) const
Definition: strbitset.h:44
pat::strbitset::count
size_t count() const
returns number of bits set
Definition: strbitset.h:265
pat::strbitset::set
strbitset & set(index_type const &i, bool val=true)
Definition: strbitset.h:147
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
pat::strbitset::operator==
bool operator==(bool b) const
equality operator to bool
Definition: strbitset.h:250
pat::strbitset::index_type::bitset_
strbitset const * bitset_
Definition: strbitset.h:52
pat::strbitset::set
strbitset & set(bool val=true)
set method of all bits
Definition: strbitset.h:126
gather_cfg.cout
cout
Definition: gather_cfg.py:144
pat::strbitset::clear
void clear()
clear the bitset and map
Definition: strbitset.h:67
pat::strbitset::index_type
Definition: strbitset.h:25
pat::strbitset::flip
strbitset & flip(index_type const &i)
Definition: strbitset.h:158
pat::strbitset::index_type::str
std::string const & str() const
Definition: strbitset.h:41
pat::strbitset::dummy_
static const std::string dummy_
Definition: strbitset.h:330
pat::strbitset::test
bool test(index_type const &i) const
Definition: strbitset.h:289
pat::strbitset::push_back
void push_back(std::string s)
Definition: strbitset.h:90
pat::strbitset::operator==
bool operator==(const strbitset &r) const
equality operator
Definition: strbitset.h:230
pat::strbitset::operator|=
strbitset & operator|=(const strbitset &r)
bitwise or
Definition: strbitset.h:192
pat::strbitset::none
size_t none() const
returns true if none are set
Definition: strbitset.h:284
pat::strbitset::bits_
bit_vector bits_
the actual bits, indexed by the index in "map_"
Definition: strbitset.h:332
alignCSCRings.s
s
Definition: alignCSCRings.py:92
pat::strbitset::map_
str_index_map map_
map that holds the string-->index map
Definition: strbitset.h:331
pat::strbitset::operator^=
strbitset & operator^=(const strbitset &r)
bitwise xor
Definition: strbitset.h:211
pat::strbitset::index_type::operator<
bool operator<(index_type const &r) const
Definition: strbitset.h:43
pat::strbitset::set
strbitset & set(std::string s, bool val=true)
set method of one bit
Definition: strbitset.h:142
pat::strbitset::index_type::index_type
index_type(strbitset const *b, std::string const &s)
Definition: strbitset.h:33
mps_fire.end
end
Definition: mps_fire.py:242
pat::strbitset::bit_vector
std::vector< bool > bit_vector
Definition: strbitset.h:61
b
double b
Definition: hdecay.h:118
pat::strbitset::index_type::operator==
bool operator==(index_type const &r) const
Definition: strbitset.h:47
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
pat::strbitset::operator^
friend strbitset operator^(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:19
pat::strbitset::operator!=
bool operator!=(bool b) const
inequality operator to bool
Definition: strbitset.h:262
pat::strbitset::operator!=
bool operator!=(const strbitset &r) const
inequality operator
Definition: strbitset.h:259
pat::strbitset::index_type::i_
size_t i_
Definition: strbitset.h:53
RecoTauValidation_cfi.reference
reference
Definition: RecoTauValidation_cfi.py:234
createfilelist.int
int
Definition: createfilelist.py:10
pat::strbitset::index_type::operator>=
bool operator>=(index_type const &r) const
Definition: strbitset.h:46
pat::strbitset::flip
strbitset & flip()
flip method of all bits
Definition: strbitset.h:134
pat::strbitset::operator&=
strbitset & operator&=(const strbitset &r)
bitwise and
Definition: strbitset.h:173
pat::strbitset::str_index_map
std::map< std::string, size_t > str_index_map
Definition: strbitset.h:60
pat
Definition: HeavyIon.h:7
pat::strbitset::test
bool test(std::string s) const
test
Definition: strbitset.h:287
pat::strbitset::operator[]
bit_vector::reference operator[](index_type const &i)
Definition: strbitset.h:123
pat::strbitset::index_type::index_type
index_type()
Definition: strbitset.h:31
pat::strbitset::print
void print(std::ostream &out) const
print method
Definition: strbitset.h:101
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
alignCSCRings.r
r
Definition: alignCSCRings.py:93
pat::strbitset::operator&
friend strbitset operator&(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:7
pat::strbitset::strings
const std::vector< std::string > strings() const
give access to the ordered strings
Definition: strbitset.h:295
pat::strbitset
Definition: strbitset.h:23
heppy_batch.val
val
Definition: heppy_batch.py:351
pat::operator^
strbitset operator^(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:19
pat::operator|
strbitset operator|(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:13
pat::strbitset::size_t
unsigned int size_t
Definition: strbitset.h:59
pat::strbitset::flip
strbitset & flip(std::string s)
flip method of one bit
Definition: strbitset.h:153
pat::strbitset::strbitset
strbitset()
constructor: just clears the bitset and map
Definition: strbitset.h:64
pat::strbitset::index
size_t index(std::string s) const
Definition: strbitset.h:311
pat::strbitset::operator|
friend strbitset operator|(const strbitset &l, const strbitset &r)
Definition: strbitset.cc:13
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
mps_fire.result
result
Definition: mps_fire.py:311
pat::strbitset::operator[]
bit_vector::reference operator[](const std::string s)
access method non-const
Definition: strbitset.h:118
pat::strbitset::operator~
strbitset operator~()
logical negation
Definition: strbitset.h:164
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
pat::strbitset::index_type::size_t
unsigned int size_t
Definition: strbitset.h:27
crabWrapper.key
key
Definition: crabWrapper.py:19
pat::strbitset::index
std::string const & index(size_t i) const
Definition: strbitset.h:321
pat::strbitset::operator[]
bit_vector::const_reference operator[](const std::string s) const
access method const
Definition: strbitset.h:110
pat::strbitset::operator!
bool operator!() const
! Logical negation of bool()
Definition: strbitset.h:85
pat::strbitset::index_type::operator<<
friend std::ostream & operator<<(std::ostream &out, const index_type &r)
Definition: strbitset.cc:25