|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file EMBitFlags.h |
| 13 | +/// \brief Header of bit flag class for particle selection. |
| 14 | +/// \author M. Hemmer, marvin.hemmer@cern.ch |
| 15 | + |
| 16 | +#ifndef PWGEM_PHOTONMESON_CORE_EMBITFLAGS_H_ |
| 17 | +#define PWGEM_PHOTONMESON_CORE_EMBITFLAGS_H_ |
| 18 | + |
| 19 | +#include <cstddef> // size_t |
| 20 | +#include <cstdint> // uint64_t |
| 21 | +#include <vector> // vector |
| 22 | + |
| 23 | +/// \class EMBitFlags |
| 24 | +/// \brief Dynamically-sized bit container with bit-level storage. |
| 25 | +/// |
| 26 | +/// Bits can be set beyond the current size, in which case the container |
| 27 | +/// grows automatically. Access via test() and reset() requires the index |
| 28 | +/// to be within the current size. Bits are all on by default and will be |
| 29 | +/// switched off if particle fails a cut |
| 30 | +class EMBitFlags |
| 31 | +{ |
| 32 | + public: |
| 33 | + explicit EMBitFlags(std::size_t nBits = 0); |
| 34 | + |
| 35 | + /// \brief get number of stored bits |
| 36 | + std::size_t size() const; |
| 37 | + |
| 38 | + /// \brief check bit i |
| 39 | + /// \param i index of bit that should be checked |
| 40 | + bool test(std::size_t i) const; |
| 41 | + |
| 42 | + /// \brief set bit i |
| 43 | + /// \param i index of bit which value should be set |
| 44 | + void set(std::size_t i); |
| 45 | + |
| 46 | + /// \brief reset bit i |
| 47 | + /// \param i index of bit which value should be reset |
| 48 | + void reset(std::size_t i); |
| 49 | + |
| 50 | + /// \brief resetting all flags to false |
| 51 | + void clear(); |
| 52 | + |
| 53 | + /// \brief reserve space in the underlying storage for nBits bits |
| 54 | + /// \param nBits number of bits to reserve capacity for |
| 55 | + void reserve(std::size_t nBits); |
| 56 | + |
| 57 | + /// \brief resize the container to hold nBits bits |
| 58 | + /// \param nBits new number of bits (new bits are initialized to false) |
| 59 | + void resize(std::size_t nBits); |
| 60 | + |
| 61 | + private: |
| 62 | + /// \brief ensure that the container can hold at least nBits bits |
| 63 | + /// \param nBits required number of bits |
| 64 | + void ensureSize(std::size_t nBits); |
| 65 | + |
| 66 | + static constexpr std::size_t word(std::size_t i) { return i >> 6; } |
| 67 | + static constexpr std::uint64_t mask(std::size_t i) { return 1ULL << (i & 63); } |
| 68 | + |
| 69 | + std::vector<std::uint64_t> mBits; |
| 70 | + std::size_t mSize = 0; |
| 71 | +}; |
| 72 | + |
| 73 | +#endif // PWGEM_PHOTONMESON_CORE_EMBITFLAGS_H_ |
0 commit comments