Belle II Software  release-05-02-19
State.h
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Filename : TRGState.h
5 // Section : TRG
6 // Owner : Yoshihito Iwasaki
7 // Email : yoshihito.iwasaki@kek.jp
8 //-----------------------------------------------------------------------------
9 // Description : A class to represent a state of multi bits
10 //-----------------------------------------------------------------------------
11 // $Log$
12 //-----------------------------------------------------------------------------
13 
14 #ifndef TRGState_FLAG_
15 #define TRGState_FLAG_
16 
17 #include <vector>
18 #include <iomanip>
19 #include <iostream>
20 #include <sstream>
21 
22 namespace Belle2 {
28  class TRGState {
30 
31  public:
32 
34  TRGState(unsigned bitSize = 0);
35 
37  TRGState(unsigned bitSize, unsigned value);
38 
40  TRGState(std::vector<bool> states);
41 
43  TRGState(unsigned bitSize, const bool* const states);
44 
46  TRGState(std::vector<unsigned>& states, unsigned order);
47 
49  TRGState(const char*, unsigned type);
50 
51  // /// Copy constructor.
52  // TRGState(const TRGState &);
53 
55  virtual ~TRGState();
56 
57  public:// Selectors
58 
60  unsigned size(void) const;
61 
63  bool active(void) const;
64 
66  bool active(unsigned bitPosition) const;
67 
69  void copy2bool(bool* array) const;
70 
72  TRGState subset(unsigned i, unsigned n) const;
73 
76  void dump(const std::string& message = "",
77  const std::string& pre = "") const;
78 
79  public:// Modifiers
80 
82  void clear(void);
83 
85  const TRGState& set(unsigned position, bool state = true);
86 
88  const TRGState& set(unsigned position,
89  unsigned size,
90  const bool* const array);
91 
93  const TRGState& set(unsigned position,
94  unsigned size,
95  unsigned value);
96 
98  const TRGState& set(unsigned position, const TRGState&);
99 
100  public:// Operators
101 
103  operator unsigned() const;
104 
106  operator unsigned long long() const;
107 
109  operator std::string() const;
110 
112  TRGState& operator+=(const TRGState&);
113 
115  bool operator[](unsigned i) const;
116 
118  TRGState& operator=(const TRGState&);
119 
121  bool operator<(const TRGState&) const;
122 
124  TRGState& shift(unsigned i);
125 
126  public:// Utilities
127 
129  static unsigned toUnsigned(unsigned n, const bool* array);
130 
131  private:
132 
134  static const unsigned _su;
135 
137  static const unsigned _bsu;
138 
140  unsigned _size;
141 
143  unsigned _n;
144 
146  unsigned* _state;
147  };
148 
149 //-----------------------------------------------------------------------------
150 
151  inline
152  unsigned
153  TRGState::size(void) const
154  {
155  return _size;
156  }
157 
158  inline
159  bool
160  TRGState::active(void) const
161  {
162  for (unsigned i = 0; i < _n; i++)
163  if (_state[i])
164  return true;
165  return false;
166  }
167 
168  inline
169  bool
170  TRGState::active(unsigned a) const
171  {
172  const unsigned wp = a / _bsu;
173  const unsigned bp = a % _bsu;
174  if (_state[wp] & (1 << bp))
175  return true;
176  else
177  return false;
178  }
179 
180  inline
181  bool
182  TRGState::operator[](unsigned a) const
183  {
184 #ifdef TRG_DEBUG
185  if (a >= _size)
186  std::cout << "TRGState::operator[] !!! bit size overflow"
187  << ":bit size=" << _size << ",specified position=" << a
188  << std::endl;
189 #endif
190 
191  const unsigned wp = a / _bsu;
192  const unsigned bp = a % _bsu;
193  if (_state[wp] & (1 << bp))
194  return true;
195  else
196  return false;
197  }
198 
199  inline
200  TRGState::operator unsigned() const
201  {
202 #ifdef TRG_DEBUG
203  unsigned n = _size / _bsu;
204  if (_size % _bsu) ++n;
205  if (n > 1)
206  std::cout << "TRGState::operator unsigned() !!! bit size overflow"
207  << ":bit size=" << _size << ",max bit size with unsigned="
208  << _bsu << std::endl;
209 #endif
210  return _state[0];
211 
212  }
213 
214  inline
215  TRGState::operator unsigned long long() const
216  {
217  unsigned n = _size / _bsu;
218  if (_size % _bsu) ++n;
219  const unsigned c = sizeof(unsigned long long);
220  if (n > c)
221 #ifdef TRG_DEBUG
222  std::cout << "TRGState::operator unsigned long long() "
223  << "!!! bit size overflow"
224  << ":bit size=" << _size
225  << ",max bit size with unsigned long long="
226  << c << std::endl;
227 #endif
228  n = c;
229  unsigned long long a = 0;
230  const unsigned s = _bsu;
231  for (unsigned i = 0; i < n; i++) {
232  const unsigned long long b = _state[i];
233  a += b << (i * s);
234  }
235  return a;
236  }
237 
238  inline
239  TRGState::operator std::string() const
240  {
241  unsigned nWords = _size / _bsu;
242  if (_size % _bsu) ++nWords;
243  unsigned lastNHex = (_size % _bsu);
244  if (lastNHex == 0) lastNHex = 8;
245  else {
246  lastNHex = lastNHex / 4;
247  if ((_size % _bsu) % 4) ++lastNHex;
248  }
249  std::stringstream t_stringstream;
250  t_stringstream << std::setw(lastNHex) << std::setfill('0') << std::hex << _state[nWords - 1];
251  for (unsigned iWord = 1; iWord < nWords; iWord++) {
252  t_stringstream << std::setw(8) << std::setfill('0') << std::hex << _state[nWords - 1 - iWord];
253  }
254  return t_stringstream.str();
255  }
256 
257  inline
258  void
259  TRGState::copy2bool(bool* a) const
260  {
261  for (unsigned i = 0; i < _size; i++) {
262  const unsigned wp = i / _bsu;
263  const unsigned bp = i % _bsu;
264  if (_state[wp] & (1 << bp))
265  a[i] = true;
266  else
267  a[i] = false;
268  }
269  }
270 
271  inline
272  const TRGState&
273  TRGState::set(unsigned p, unsigned n, const bool* const a)
274  {
275  if (a) {
276  for (unsigned i = 0; i < n; i++) {
277  const unsigned wp = (p + i) / _bsu;
278  const unsigned bp = (p + i) % _bsu;
279  if (a[i])
280  _state[wp] |= (1 << bp);
281  else
282  _state[wp] &= ~(1 << bp);
283  }
284  }
285 #ifdef TRG_DEBUG
286  else std::cout << "NULL pointer found in TRGState::set"
287  << std::endl;
288 #endif
289  return * this;
290  }
291 
292  inline
293  const TRGState&
294  TRGState::set(unsigned p, unsigned n, unsigned a)
295  {
296  for (unsigned i = 0; i < n; i++) {
297  const unsigned wp = (p + i) / _bsu;
298  const unsigned bp = (p + i) % _bsu;
299  if (a & (1 << i))
300  _state[wp] |= (1 << bp);
301  else
302  _state[wp] &= ~(1 << bp);
303  }
304  return * this;
305  }
306 
307  inline
308  const TRGState&
309  TRGState::set(unsigned n, bool a)
310  {
311  const unsigned wp = n / _bsu;
312  const unsigned bp = n % _bsu;
313  if (a)
314  _state[wp] |= (1 << bp);
315  else
316  _state[wp] &= ~(1 << bp);
317  return * this;
318  }
319 
320  inline
321  const TRGState&
322  TRGState::set(unsigned p, const TRGState& s)
323  {
324  const unsigned n = s.size();
325  for (unsigned i = 0; i < n; i++) {
326  const unsigned wp = (p + i) / _bsu;
327  const unsigned bp = (p + i) % _bsu;
328  if (s[i])
329  _state[wp] |= (1 << bp);
330  else
331  _state[wp] &= ~(1 << bp);
332  }
333  return * this;
334  }
335 
336  inline
337  unsigned
338  TRGState::toUnsigned(unsigned n, const bool* a)
339  {
340 #ifdef TRG_DEBUG
341  if (n > 8) std::cout << "given array size(" << n << ") is too big"
342  << std::endl;
343 #endif
344 
345  unsigned b = 0;
346  for (unsigned i = 0; i < n; i++) {
347  if (a[i]) {
348  const unsigned bp = i % _bsu;
349  b |= (1 << bp);
350  }
351  }
352  return b;
353  }
354 
355  inline
356  TRGState&
357  TRGState::shift(unsigned a)
358  {
359 
360  if (! active()) return * this;
361 
362  if (a == 0) return * this;
363 
364  for (int i = _size - 1; i >= 0; --i) {
365  if (i < int(a))
366  set(i, false);
367  else
368  set(i, active(i - a));
369  }
370 
371  return * this;
372  }
373 
374  inline
375  void
377  {
378  for (unsigned i = 0; i < _n; i++)
379  _state[i] = 0;
380  }
381 
383 } // namespace Belle2
384 
385 //-----------------------------------------------------------------------------
386 
387 namespace std {
388 
389  ostream& operator<<(ostream&, const Belle2::TRGState&);
390 
391 }
392 
393 #endif /* TRGState_FLAG_ */
Belle2::TRGState::TRGState
TRGState(unsigned bitSize=0)
Default constructor.
Definition: State.cc:35
Belle2::TRGState::_size
unsigned _size
bit size.
Definition: State.h:140
Belle2::operator<<
std::ostream & operator<<(std::ostream &output, const IntervalOfValidity &iov)
Definition: IntervalOfValidity.cc:196
Belle2::TRGState::_n
unsigned _n
bit storage size.
Definition: State.h:143
Belle2::TRGState::subset
TRGState subset(unsigned i, unsigned n) const
returns subset from i with n bits.
Definition: State.cc:362
Belle2::TRGState
A class to represent a state of multi bits.
Definition: State.h:29
Belle2::TRGState::_su
static const unsigned _su
size of unsigned.
Definition: State.h:134
Belle2::TRGState::active
bool active(void) const
returns true if there are active bits.
Definition: State.h:160
Belle2::TRGState::dump
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Definition: State.cc:204
Belle2::TRGState::set
const TRGState & set(unsigned position, bool state=true)
sets state at bit i.
Definition: State.h:309
Belle2::TRGState::shift
TRGState & shift(unsigned i)
shifts bits.
Definition: State.h:357
Belle2::TRGState::_bsu
static const unsigned _bsu
bit size of unsigned.
Definition: State.h:137
Belle2::TRGState::~TRGState
virtual ~TRGState()
Destructor.
Definition: State.cc:197
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TRGState::operator=
TRGState & operator=(const TRGState &)
Copy operator.
Definition: State.cc:375
Belle2::TRGState::toUnsigned
static unsigned toUnsigned(unsigned n, const bool *array)
Coverts from bool array to unsigned.
Definition: State.h:338
Belle2::TRGState::size
unsigned size(void) const
returns bit size.
Definition: State.h:153
Belle2::TRGState::operator<
bool operator<(const TRGState &) const
Copy operator.
Definition: State.cc:391
Belle2::TRGState::operator[]
bool operator[](unsigned i) const
returns state of i'th bit.
Definition: State.h:182
Belle2::TRGState::clear
void clear(void)
clears state.
Definition: State.h:376
Belle2::TRGState::_state
unsigned * _state
bit state.
Definition: State.h:146
Belle2::TRGState::copy2bool
void copy2bool(bool *array) const
returns a filled array.
Definition: State.h:259
Belle2::TRGState::operator+=
TRGState & operator+=(const TRGState &)
appends TRGState (as MSB).
Definition: State.cc:321