14 #ifndef LARDATAOBJ_UTILITIES_BITMASK_H 15 #define LARDATAOBJ_UTILITIES_BITMASK_H 66 template <
unsigned int NBits>
70 template <
unsigned int NBits>
74 template <
typename Storage>
78 template <
typename Storage>
80 {
return Storage(1) <<
index; }
88 template <
typename Storage>
117 {
return bits == other.
bits; }
119 {
return bits != other.
bits; }
121 {
return bits < other.
bits; }
123 {
return bits > other.
bits; }
125 {
return bits <= other.
bits; }
127 {
return bits >= other.
bits; }
136 template <
typename Storage>
138 {
return left == right.
index(); }
139 template <
typename Storage>
143 template <
typename Storage>
145 {
return left != right.
index(); }
146 template <
typename Storage>
150 template <
typename Storage>
151 constexpr
bool operator< (Index_t left, Flag_t<Storage>
right)
153 template <
typename Storage>
155 {
return left.index() <
right; }
157 template <
typename Storage>
159 {
return left > right.
index(); }
160 template <
typename Storage>
164 template <
typename Storage>
165 constexpr
bool operator<= (Index_t left, Flag_t<Storage>
right)
166 {
return left <=
right.index(); }
167 template <
typename Storage>
169 {
return left.index() <=
right; }
171 template <
typename Storage>
173 {
return left >= right.
index(); }
174 template <
typename Storage>
181 template <
typename Storage>
182 std::ostream& operator<< (std::ostream& out, Flag_t<Storage> flag)
183 { out <<
'[' << flag.index() <<
']';
return out; }
186 template <
typename Storage>
193 template <
typename Storage>
204 constexpr
Bits_t() =
default;
227 constexpr
bool any(
This_t bits)
const;
230 constexpr
bool none(
This_t bits)
const;
233 constexpr
bool only(
This_t bits)
const;
248 void keepOnly(
This_t bits);
279 {
return data == other.
data; }
281 {
return data != other.
data; }
283 {
return data < other.
data; }
285 {
return data > other.
data; }
287 {
return data <= other.
data; }
289 {
return data >= other.
data; }
294 explicit constexpr
operator bool()
const {
return bool(data); }
311 { onlyBits(base, ~bits); }
325 template <
typename Storage>
330 template <
typename Storage>
335 template <
typename Storage>
340 template <
typename Storage>
345 template <
typename Storage>
350 template <
typename Storage>
355 template <
typename Storage>
360 template <
typename Storage>
419 template <
typename Storage>
466 explicit constexpr
BitMask() =
default;
535 template <
typename... Others>
537 : BitMask(create(first, others...))
549 template <
typename... Others>
551 : BitMask(create(first, others...))
568 template <
typename Second,
typename... Others>
570 : BitMask(create(first, second, others...))
592 constexpr
bool isDefined(
Flag_t flag)
const;
604 constexpr
bool isDefined(
Bits_t bits)
const;
614 constexpr
bool isUndefined(
Flag_t flag)
const;
624 constexpr
bool isUndefined(
Bits_t bits)
const;
635 constexpr
bool get(
Flag_t flag)
const;
646 constexpr
bool isSet(
Flag_t flag)
const;
657 constexpr
bool isUnset(
Flag_t flag)
const;
681 constexpr
bool any(
Bits_t bits)
const;
693 constexpr
bool none(
Bits_t bits)
const;
704 constexpr
bool anySet(
Mask_t const&
mask)
const;
714 constexpr
bool noneSet(
Mask_t const& mask)
const;
727 constexpr
bool match(
Mask_t const& mask)
const;
750 template <
typename Flag,
typename... OtherFlags>
751 void set(Flag first, OtherFlags... others)
752 { setImpl(first, others...); }
764 template <
typename BeginIter,
typename EndIter>
765 void rangeSet(BeginIter
begin, EndIter
end);
778 template <
typename Flag,
typename... OtherFlags>
779 void unset(Flag first, OtherFlags... others)
780 { unsetImpl(first, others...); }
792 template <
typename BeginIter,
typename EndIter>
793 void rangeUnset(BeginIter begin, EndIter end);
816 template <
typename Flag,
typename... OtherFlags>
817 void remove(Flag first, OtherFlags... others)
818 { undefineImpl(first, others...); }
822 { presence.clear();
values.clear(); }
833 static constexpr
size_t capacity();
850 template <
typename Stream>
851 void dump(Stream&& out,
unsigned int nBits)
const;
854 template <
typename Stream>
856 {
dump(std::forward<Stream>(out), capacity()); }
876 template <
typename... Args>
1150 void defineSingle(
Flag_t flag);
1153 void undefineSingle(
Flag_t flag);
1159 template <
typename... Flags>
1160 void undefineImpl(
Flag_t flag, Flags... others);
1166 template <
typename... Flags>
1167 void setImpl(
Flag_t flag, Flags... others);
1170 void setSingle(
Flag_t flag);
1176 constexpr
Bits_t definedOnly()
const;
1179 template <
typename... Flags>
1180 void unsetImpl(
Flag_t flag, Flags... others);
1183 void unsetSingle(
Flag_t flag);
1187 {
return data & bits; }
1191 {
return (data & mask) ==
mask; }
1195 {
return (data & ~mask) ==
data; }
1202 template <
typename Stream,
typename Storage>
1203 Stream& operator<< (Stream&& out, BitMask<Storage>
const&
mask)
1204 {
mask.dump(std::forward<Stream>(out));
return out; }
1233 template <
typename Storage>
1239 template <
typename Storage>
1245 template <
typename Storage>
1254 template <
typename Storage>
1260 template <
typename Storage>
1266 template <
typename Storage>
1272 template <
typename Storage>
1279 template <
typename Storage>
1285 template <
typename Storage>
1291 template <
typename Storage>
1300 template <
typename Storage>
1306 template <
typename Storage>
1312 template <
typename Storage>
1318 template <
typename Storage>
1324 template <
typename Storage>
1328 template <
typename Storage>
1333 template <
typename Storage>
1337 template <
typename Storage>
1342 template <
typename Storage>
1348 template <typename Storage>
1349 constexpr BitMask<Storage> operator~ (Bits_t<Storage> bits);
1354 template <
typename Storage>
1358 template <
typename Storage>
1366 template <
typename Storage>
1379 #include "lardataobj/Utilities/BitMask.tcc" 1384 #endif // LARDATAOBJ_UTILITIES_BITMASK_H typename Bits_t::Flag_t Flag_t
Type identifying a single flag.
Storage_t bits
The bits representing this flag (only one is set)
constexpr unsigned int computePages(unsigned int bits)
Returns the number of Storage variables needed to hold that many bits.
constexpr Bits_t(Flag_t flag)
Constructs from a single flag.
Namespace for general, non-LArSoft-specific utilities.
void msg(const char *fmt,...)
typename SmallestUIntType< NBits >::type smallestUInt_t
The smallest integral type accommodating NBits bits.
constexpr bool operator<(Index_t left, Flag_t< Storage > right)
constexpr bool operator>(Index_t left, Flag_t< Storage > right)
constexpr bool operator<=(Index_t left, Flag_t< Storage > right)
Base class for exceptions thrown by flag-related utilities.
FlagNotDefined(std::string msg="Flag undefined-flag error")
Type identifying a flag. Operations are implemented as free functions.
static void onlyBits(Storage_t &base, Storage_t bits)
void unsetImpl()
Implementation detail of unset()
constexpr bool operator>=(Index_t left, Flag_t< Storage > right)
unsigned int Index_t
Type to denote the index of the flag.
util::flags::Index_t FlagIndex_t
Type of index of flag.
void dump(Stream &&out) const
Prints into the specified stream all bits.
Type for constructor tag from values.
static constexpr bool testUnsetBitmask(Storage_t data, Storage_t mask)
Returns whether all the specified bits in the mask are set.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
static constexpr bool testBits(Storage_t data, Storage_t bits)
Returns whether any of the specified bits is set.
constexpr BitMask< Storage > operator-(BitMask< Storage > baseMask, BitMask< Storage > mask)
A class containing a set of flags.
constexpr Storage makeBits(Index_t index)
Returns a set of bits with only the one at the specified index set.
constexpr bool empty() const
Returns whether there is no bit set at all.
constexpr Flag_t(Index_t flagIndex)
Constructs from the flag index.
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
constexpr BitMask< Storage > Set(Flag_t< Storage > flag)
Returns a bit mask which sets the specified flag.
static void setBits(Storage_t &base, Storage_t bits)
Returns data with all bits from base and from bits set.
def dump(input_file, output_file)
Storage Storage_t
Type of underlying bit data representation.
void clear()
Unsets all bits.
bool invert(ublas::matrix< T, L, A > &m)
constexpr Bits_t< Storage > operator+(Bits_t< Storage > left, Bits_t< Storage > right)
Returns a new Bits_t with all the bits from both arguments set.
static constexpr bool testBitmask(Storage_t data, Storage_t mask)
Returns whether all the specified bits in the mask are set.
auto select(T const &...t)
constexpr bool operator!=(Index_t left, Flag_t< Storage > right)
Type identifying a set of bits.
constexpr BitMask< Storage > makeMask(Bits_t< Storage > bits)
Constructs a mask from bits.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
constexpr bool operator==(Index_t left, Flag_t< Storage > right)
constexpr BitMask(Bits_t first, Others...others)
Constructor: merges all arguments in the argument list.
static QInternalList< QTextCodec > * all
void unset(Flag first, OtherFlags...others)
Unsets all specified flags.
constexpr BitMask(Flag_t first, Others...others)
Constructor: merges all arguments in the argument list.
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
virtual const char * what() const noexcept override
static void unsetBits(Storage_t &base, Storage_t bits)
Returns data with all bits from base, but the ones from bits unset.
Exception(std::string msg="Flag error")
util::flags::Flag_t< Storage_t > Flag_t
Type of flag matching our storage.
Bits_t values
Storage of value bits.
constexpr Bits_t(Storage_t bits)
Constructs from a set of bits.
void setImpl()
Implementation detail of set()
Exception thrown to convey that an invalid flag index was used.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
constexpr BitMaskFromValuesTag maskFromValues
Value useful for BitMask constructors from value.
Bits_t presence
Storage of definition bits.
void clear()
Undefines all bits.
second_as<> second
Type of time stored in seconds, in double precision.
OutOfRange(std::string msg="Flag out-of-range error")
Storage_t data
The bits representing all set bits.
constexpr Flag_t copy() const
Returns a copy of this object.
constexpr Index_t index() const
Returns the index of the (first) bit set.
Exception thrown to convey that an undefined flag index was tested.
constexpr BitMask< Storage > Unset(Flag_t< Storage > flag)
Returns a bit mask which unsets the specified flag.
cet::coded_exception< error, detail::translate > exception
void undefineImpl()
Implementation detail of remove()
constexpr bool operator!() const
Returns true if there is no bit set.
constexpr BitMask(Mask_t first, Second second, Others...others)
Constructor: merges all arguments in the argument list.