/* * Mask.hpp * * Created on: Sep 7, 2012 * Author: heber */ #ifndef MASK_HPP_ #define MASK_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include "CodePatterns/Assert.hpp" #include "types.hpp" /** A Mask_t is just a set of ids such that one can use the set as a filter * for any type by looking them up in the map. * * The interface is such that we have a bitmask for every given id. Only we * store only ids associated to true. */ template struct Mask_t : public std::set { /** Checks whether the bit for the given \a id is true. * * @param id id to check * @return true - bit set, false - bit not set */ bool isTrue(const T id) const { const typename std::set::const_iterator iter = std::set::find(id); return (iter != std::set::end()); } /** Checks whether the bit for the given \a id is true. * * @param id id to check * @return true - bit set, false - bit not set */ bool isFalse(const T id) const { return (!isTrue(id)); } /** Setter for the bit associated to \a id. * * @param id id to set to true */ void setTrue(const T id) { insert(id); } /** Unsetter for the bit associated to \a id. * * @param id id to set to false */ void setFalse(const T id) { typename std::set::iterator iter = std::set::find(id); if (iter != std::set::end()) erase(iter); } /** Sets bit associated to \a id to a specific boolean value. * * @param id id to set or unset * @param value bit giving whether to set or unset */ void setValue(const T id, bool value) { if (value) setTrue(id); else setFalse(id); } /** Use in printing the bit mask bit by bit. * * @param id bit to print * @return "t" or "f" whether bit is set or not. */ const char* printBit(const T id) const { const typename std::set::const_iterator iter = std::set::find(id); return (iter != std::set::end() ? "t" : "f"); } }; #endif /* MASK_HPP_ */