diff --git a/Property_map/include/CGAL/property_map.h b/Property_map/include/CGAL/property_map.h index f80793a8f8b..ef38bf2f029 100644 --- a/Property_map/include/CGAL/property_map.h +++ b/Property_map/include/CGAL/property_map.h @@ -456,8 +456,52 @@ struct Default_property_map{ 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 +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& pm, const key_type& k) + { + CGAL_assertion(pm.set_ptr!=NULL); + return pm.set_ptr->count(k) != 0; + } + + friend void put(Boolean_property_map& 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_)` +template +Boolean_property_map +make_boolean_property_map(Set& set_) +{ + return Boolean_property_map(set_); +} } // namespace CGAL