Add a property map with bool as value_type based on a set-like container

This commit is contained in:
Sébastien Loriot 2017-07-07 17:25:00 +02:00
parent a093cd5e9f
commit 7ef29476ab
1 changed files with 45 additions and 1 deletions

View File

@ -456,8 +456,52 @@ struct Default_property_map{
get (const Default_property_map&, const key_type&){ return ValueType(); } get (const Default_property_map&, const key_type&){ return ValueType(); }
}; };
/// Read-write Property map turning a set (such a `std::set`,
/// `boost::unordered_set`, `std::unordered_set`) into a property map
/// associating a Boolean to the value type of the set. The function `get` will
/// return `true` if the key is inside the set and `false` otherwise. The `put`
/// function will insert an element in the set if `true` is passed and erase it
/// otherwise.
///
/// `operator()(key k)` calling the get function with `k`
template<class Set>
struct Boolean_property_map
{
typedef typename Set::value_type key_type;
typedef bool value_type;
typedef bool reference;
typedef boost::read_write_property_map_tag category;
/// \endcond Set* set_ptr;
/// Constructor taking a copy of the set. Note that `set_` must be valid
/// while the property map is in use.
Boolean_property_map() : set_ptr(NULL) {}
Boolean_property_map(Set& set_) : set_ptr(&set_) {}
friend bool get(const Boolean_property_map<Set>& pm, const key_type& k)
{
CGAL_assertion(pm.set_ptr!=NULL);
return pm.set_ptr->count(k) != 0;
}
friend void put(Boolean_property_map<Set>& pm, const key_type& k, bool v)
{
CGAL_assertion(pm.set_ptr!=NULL);
if (v)
pm.set_ptr->insert(k);
else
pm.set_ptr->erase(k);
}
};
/// \ingroup PkgProperty_map
/// returns `Boolean_property_map<Set>(set_)`
template <class Set>
Boolean_property_map<Set>
make_boolean_property_map(Set& set_)
{
return Boolean_property_map<Set>(set_);
}
} // namespace CGAL } // namespace CGAL