14 #ifndef LARDATAOBJ_UTILITIES_BITMASK_H 15 #define LARDATAOBJ_UTILITIES_BITMASK_H 65 template <
unsigned int NBits>
69 template <
unsigned int NBits>
73 template <
typename Storage>
77 template <
typename Storage>
80 return Storage(1) << index;
87 template <
typename Storage>
98 constexpr
Index_t index()
const;
126 template <
typename Storage>
129 return left == right.
index();
131 template <
typename Storage>
137 template <
typename Storage>
140 return left != right.
index();
142 template <
typename Storage>
148 template <
typename Storage>
149 constexpr
bool operator<(Index_t left, Flag_t<Storage>
right)
153 template <
typename Storage>
156 return left.index() <
right;
159 template <
typename Storage>
162 return left > right.
index();
164 template <
typename Storage>
170 template <
typename Storage>
171 constexpr
bool operator<=(Index_t left, Flag_t<Storage>
right)
173 return left <=
right.index();
175 template <
typename Storage>
178 return left.index() <=
right;
181 template <
typename Storage>
184 return left >= right.
index();
186 template <
typename Storage>
195 template <
typename Storage>
196 std::ostream& operator<<(std::ostream& out, Flag_t<Storage> flag)
198 out <<
'[' << flag.index() <<
']';
203 template <
typename Storage>
211 template <
typename Storage>
222 constexpr
Bits_t() =
default;
237 constexpr
bool all(
This_t bits)
const;
240 constexpr
bool any(
This_t bits)
const;
243 constexpr
bool none(
This_t bits)
const;
246 constexpr
bool only(
This_t bits)
const;
260 void keepOnly(
This_t bits);
297 explicit constexpr
operator bool()
const {
return bool(data); }
322 template <
typename Storage>
327 template <
typename Storage>
332 template <
typename Storage>
337 template <
typename Storage>
341 template <
typename Storage>
345 template <
typename Storage>
350 template <
typename Storage>
355 template <
typename Storage>
370 virtual const char*
what() const noexcept
override {
return message.c_str(); }
406 template <
typename Storage>
451 explicit constexpr
BitMask() =
default;
513 template <
typename... Others>
514 constexpr
BitMask(
Flag_t first, Others... others) : BitMask(create(first, others...))
526 template <
typename... Others>
527 constexpr
BitMask(
Bits_t first, Others... others) : BitMask(create(first, others...))
544 template <
typename Second,
typename... Others>
546 : BitMask(create(first, second, others...))
567 constexpr
bool isDefined(
Flag_t flag)
const;
579 constexpr
bool isDefined(
Bits_t bits)
const;
589 constexpr
bool isUndefined(
Flag_t flag)
const;
599 constexpr
bool isUndefined(
Bits_t bits)
const;
610 constexpr
bool get(
Flag_t flag)
const;
621 constexpr
bool isSet(
Flag_t flag)
const;
632 constexpr
bool isUnset(
Flag_t flag)
const;
644 constexpr
bool all(
Bits_t bits)
const;
656 constexpr
bool any(
Bits_t bits)
const;
668 constexpr
bool none(
Bits_t bits)
const;
678 constexpr
bool anySet(
Mask_t const& mask)
const;
688 constexpr
bool noneSet(
Mask_t const& mask)
const;
701 constexpr
bool match(
Mask_t const& mask)
const;
723 template <
typename Flag,
typename... OtherFlags>
724 void set(Flag first, OtherFlags... others)
726 setImpl(first, others...);
739 template <
typename BeginIter,
typename EndIter>
740 void rangeSet(BeginIter
begin, EndIter
end);
753 template <
typename Flag,
typename... OtherFlags>
754 void unset(Flag first, OtherFlags... others)
756 unsetImpl(first, others...);
769 template <
typename BeginIter,
typename EndIter>
770 void rangeUnset(BeginIter begin, EndIter end);
792 template <
typename Flag,
typename... OtherFlags>
793 void remove(Flag first, OtherFlags... others)
795 undefineImpl(first, others...);
813 static constexpr
size_t capacity();
833 template <
typename Stream>
834 void dump(Stream&& out,
unsigned int nBits)
const;
837 template <
typename Stream>
840 dump(std::forward<Stream>(out), capacity());
860 template <
typename... Args>
861 static constexpr
Mask_t create(Args... args);
1133 void defineSingle(
Flag_t flag);
1136 void undefineSingle(
Flag_t flag);
1142 template <
typename... Flags>
1143 void undefineImpl(
Flag_t flag, Flags... others);
1149 template <
typename... Flags>
1150 void setImpl(
Flag_t flag, Flags... others);
1153 void setSingle(
Flag_t flag);
1159 constexpr
Bits_t definedOnly()
const;
1162 template <
typename... Flags>
1163 void unsetImpl(
Flag_t flag, Flags... others);
1166 void unsetSingle(
Flag_t flag);
1174 return (data & mask) == mask;
1180 return (data & ~mask) == data;
1186 template <
typename Stream,
typename Storage>
1187 Stream& operator<<(Stream&& out, BitMask<Storage>
const& mask)
1189 mask.dump(std::forward<Stream>(out));
1218 template <
typename Storage>
1223 template <
typename Storage>
1229 template <
typename Storage>
1237 template <
typename Storage>
1242 template <
typename Storage>
1248 template <
typename Storage>
1254 template <
typename Storage>
1259 template <
typename Storage>
1264 template <
typename Storage>
1270 template <
typename Storage>
1278 template <
typename Storage>
1283 template <
typename Storage>
1289 template <
typename Storage>
1295 template <
typename Storage>
1299 template <
typename Storage>
1303 template <
typename Storage>
1307 template <
typename Storage>
1311 template <
typename Storage>
1315 template <
typename Storage>
1325 template <
typename Storage>
1329 template <
typename Storage>
1336 template <
typename Storage>
1351 #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.
constexpr bool operator==(This_t other) const
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
typename SmallestUIntType< NBits >::type smallestUInt_t
The smallest integral type accommodating NBits bits.
constexpr bool operator>(Index_t left, Flag_t< Storage > right)
Base class for exceptions thrown by flag-related utilities.
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.
void message(RunManager *runmanager)
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)
constexpr bool operator>=(This_t other) const
void unsetImpl()
Implementation detail of unset()
constexpr bool operator!=(This_t other) const
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 bool operator>(This_t other) const
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.
constexpr bool operator!=(Mask_t const &other) const
constexpr BitMask< Storage > operator~(BitMask< Storage > mask)
Returns a bit set which unsets the specified bits.
constexpr bool operator<(This_t other) const
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.
constexpr bool operator>=(This_t other) const
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.
constexpr bool operator!=(Index_t left, Flag_t< Storage > right)
Collection of utilities for dumping data on screen.
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 bool operator>(This_t other) const
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
constexpr BitMask(Bits_t first, Others...others)
Constructor: merges all arguments in the argument list.
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
constexpr bool operator==(Mask_t const &other) const
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()
constexpr BitMask< Storage > operator&(BitMask< Storage > left, BitMask< Storage > right)
Exception thrown to convey that an invalid flag index was used.
constexpr bool operator<=(This_t other) const
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 bool operator!=(This_t other) const
constexpr Index_t index() const
Returns the index of the (first) bit set.
constexpr bool operator==(This_t other) const
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<=(This_t other) const
constexpr bool operator<(This_t other) const
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.