Belle II Software development
State.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#ifndef TRGState_FLAG_
10#define TRGState_FLAG_
11
12#include <vector>
13#include <iomanip>
14#include <iostream>
15#include <sstream>
16
17namespace Belle2 {
24 class TRGState {
25
26 public:
27
29 TRGState(unsigned bitSize = 0);
30
32 TRGState(unsigned bitSize, unsigned value);
33
35 // cppcheck-suppress noExplicitConstructor
36 TRGState(std::vector<bool> states);
37
39 TRGState(unsigned bitSize, const bool* const states);
40
42 TRGState(std::vector<unsigned>& states, unsigned order);
43
45 TRGState(const char*, unsigned type);
46
48 TRGState(const TRGState&) = default;
49
51 virtual ~TRGState();
52
53 public:// Selectors
54
56 unsigned size(void) const;
57
59 bool active(void) const;
60
62 bool active(unsigned bitPosition) const;
63
65 void copy2bool(bool* array) const;
66
68 TRGState subset(unsigned i, unsigned n) const;
69
72 void dump(const std::string& message = "",
73 const std::string& pre = "") const;
74
75 public:// Modifiers
76
78 void clear(void);
79
81 const TRGState& set(unsigned position, bool state = true);
82
84 const TRGState& set(unsigned position,
85 unsigned size,
86 const bool* const array);
87
89 const TRGState& set(unsigned position,
90 unsigned size,
91 unsigned value);
92
94 const TRGState& set(unsigned position, const TRGState&);
95
96 public:// Operators
97
99 operator unsigned() const;
100
102 operator unsigned long long() const;
103
105 operator std::string() const;
106
109
111 bool operator[](unsigned i) const;
112
114 TRGState& operator=(const TRGState&);
115
117 bool operator<(const TRGState&) const;
118
120 TRGState& shift(unsigned i);
121
122 public:// Utilities
123
125 static unsigned toUnsigned(unsigned n, const bool* array);
126
127 private:
128
130 static const unsigned _su;
131
133 static const unsigned _bsu;
134
136 unsigned _size;
137
139 unsigned _n;
140
142 unsigned* _state;
143 };
144
145//-----------------------------------------------------------------------------
146
147 inline
148 unsigned
149 TRGState::size(void) const
150 {
151 return _size;
152 }
153
154 inline
155 bool
157 {
158 for (unsigned i = 0; i < _n; i++)
159 if (_state[i])
160 return true;
161 return false;
162 }
163
164 inline
165 bool
166 TRGState::active(unsigned a) const
167 {
168 const unsigned wp = a / _bsu;
169 const unsigned bp = a % _bsu;
170 if (_state[wp] & (1 << bp))
171 return true;
172 else
173 return false;
174 }
175
176 inline
177 bool
178 TRGState::operator[](unsigned a) const
179 {
180#ifdef TRG_DEBUG
181 if (a >= _size)
182 std::cout << "TRGState::operator[] !!! bit size overflow"
183 << ":bit size=" << _size << ",specified position=" << a
184 << std::endl;
185#endif
186
187 const unsigned wp = a / _bsu;
188 const unsigned bp = a % _bsu;
189 if (_state[wp] & (1 << bp))
190 return true;
191 else
192 return false;
193 }
194
195 inline
196 TRGState::operator unsigned() const
197 {
198#ifdef TRG_DEBUG
199 unsigned n = _size / _bsu;
200 if (_size % _bsu) ++n;
201 if (n > 1)
202 std::cout << "TRGState::operator unsigned() !!! bit size overflow"
203 << ":bit size=" << _size << ",max bit size with unsigned="
204 << _bsu << std::endl;
205#endif
206 return _state[0];
207
208 }
209
210 inline
211 TRGState::operator unsigned long long() const
212 {
213 unsigned n = _size / _bsu;
214 if (_size % _bsu) ++n;
215 const unsigned c = sizeof(unsigned long long);
216 if (n > c)
217#ifdef TRG_DEBUG
218 std::cout << "TRGState::operator unsigned long long() "
219 << "!!! bit size overflow"
220 << ":bit size=" << _size
221 << ",max bit size with unsigned long long="
222 << c << std::endl;
223#endif
224 n = c;
225 unsigned long long a = 0;
226 const unsigned s = _bsu;
227 for (unsigned i = 0; i < n; i++) {
228 const unsigned long long b = _state[i];
229 a += b << (i * s);
230 }
231 return a;
232 }
233
234 inline
235 TRGState::operator std::string() const
236 {
237 unsigned nWords = _size / _bsu;
238 if (_size % _bsu) ++nWords;
239 unsigned lastNHex = (_size % _bsu);
240 if (lastNHex == 0) lastNHex = 8;
241 else {
242 lastNHex = lastNHex / 4;
243 if ((_size % _bsu) % 4) ++lastNHex;
244 }
245 std::stringstream t_stringstream;
246 t_stringstream << std::setw(lastNHex) << std::setfill('0') << std::hex << _state[nWords - 1];
247 for (unsigned iWord = 1; iWord < nWords; iWord++) {
248 t_stringstream << std::setw(8) << std::setfill('0') << std::hex << _state[nWords - 1 - iWord];
249 }
250 return t_stringstream.str();
251 }
252
253 inline
254 void
255 TRGState::copy2bool(bool* a) const
256 {
257 for (unsigned i = 0; i < _size; i++) {
258 const unsigned wp = i / _bsu;
259 const unsigned bp = i % _bsu;
260 if (_state[wp] & (1 << bp))
261 a[i] = true;
262 else
263 a[i] = false;
264 }
265 }
266
267 inline
268 const TRGState&
269 TRGState::set(unsigned p, unsigned n, const bool* const a)
270 {
271 if (a) {
272 for (unsigned i = 0; i < n; i++) {
273 const unsigned wp = (p + i) / _bsu;
274 const unsigned bp = (p + i) % _bsu;
275 if (a[i])
276 _state[wp] |= (1 << bp);
277 else
278 _state[wp] &= ~(1 << bp);
279 }
280 }
281#ifdef TRG_DEBUG
282 else std::cout << "NULL pointer found in TRGState::set"
283 << std::endl;
284#endif
285 return * this;
286 }
287
288 inline
289 const TRGState&
290 TRGState::set(unsigned p, unsigned n, unsigned a)
291 {
292 for (unsigned i = 0; i < n; i++) {
293 const unsigned wp = (p + i) / _bsu;
294 const unsigned bp = (p + i) % _bsu;
295 if (a & (1 << i))
296 _state[wp] |= (1 << bp);
297 else
298 _state[wp] &= ~(1 << bp);
299 }
300 return * this;
301 }
302
303 inline
304 const TRGState&
305 TRGState::set(unsigned n, bool a)
306 {
307 const unsigned wp = n / _bsu;
308 const unsigned bp = n % _bsu;
309 if (a)
310 _state[wp] |= (1 << bp);
311 else
312 _state[wp] &= ~(1 << bp);
313 return * this;
314 }
315
316 inline
317 const TRGState&
318 TRGState::set(unsigned p, const TRGState& s)
319 {
320 const unsigned n = s.size();
321 for (unsigned i = 0; i < n; i++) {
322 const unsigned wp = (p + i) / _bsu;
323 const unsigned bp = (p + i) % _bsu;
324 if (s[i])
325 _state[wp] |= (1 << bp);
326 else
327 _state[wp] &= ~(1 << bp);
328 }
329 return * this;
330 }
331
332 inline
333 unsigned
334 TRGState::toUnsigned(unsigned n, const bool* a)
335 {
336#ifdef TRG_DEBUG
337 if (n > 8) std::cout << "given array size(" << n << ") is too big"
338 << std::endl;
339#endif
340
341 unsigned b = 0;
342 for (unsigned i = 0; i < n; i++) {
343 if (a[i]) {
344 const unsigned bp = i % _bsu;
345 b |= (1 << bp);
346 }
347 }
348 return b;
349 }
350
351 inline
352 TRGState&
353 TRGState::shift(unsigned a)
354 {
355
356 if (! active()) return * this;
357
358 if (a == 0) return * this;
359
360 for (int i = _size - 1; i >= 0; --i) {
361 if (i < int(a))
362 set(i, false);
363 else
364 set(i, active(i - a));
365 }
366
367 return * this;
368 }
369
370 inline
371 void
373 {
374 for (unsigned i = 0; i < _n; i++)
375 _state[i] = 0;
376 }
377
379} // namespace Belle2
380
381//-----------------------------------------------------------------------------
382
383namespace std {
384
385 ostream& operator<<(ostream&, const Belle2::TRGState&);
386
387}
388
389#endif /* TRGState_FLAG_ */
A class to represent a state of multi bits.
Definition: State.h:24
unsigned _size
bit size.
Definition: State.h:136
TRGState(const TRGState &)=default
Default copy constructor.
unsigned _n
bit storage size.
Definition: State.h:139
unsigned * _state
bit state.
Definition: State.h:142
TRGState & shift(unsigned i)
shifts bits.
Definition: State.h:353
unsigned size(void) const
returns bit size.
Definition: State.h:149
virtual ~TRGState()
Destructor.
Definition: State.cc:192
static unsigned toUnsigned(unsigned n, const bool *array)
Coverts from bool array to unsigned.
Definition: State.h:334
TRGState subset(unsigned i, unsigned n) const
returns subset from i with n bits.
Definition: State.cc:356
void copy2bool(bool *array) const
returns a filled array.
Definition: State.h:255
static const unsigned _bsu
bit size of unsigned.
Definition: State.h:133
bool operator<(const TRGState &) const
Copy operator.
Definition: State.cc:384
static const unsigned _su
size of unsigned.
Definition: State.h:130
const TRGState & set(unsigned position, bool state=true)
sets state at bit i.
Definition: State.h:305
TRGState & operator+=(const TRGState &)
appends TRGState (as MSB).
Definition: State.cc:315
bool operator[](unsigned i) const
returns state of i'th bit.
Definition: State.h:178
TRGState & operator=(const TRGState &)
Copy operator.
Definition: State.cc:368
bool active(void) const
returns true if there are active bits.
Definition: State.h:156
void clear(void)
clears state.
Definition: State.h:372
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Definition: State.cc:199
Abstract base class for different kinds of events.
STL namespace.