Chris@16: /*-----------------------------------------------------------------------------+ Chris@16: Copyright (c) 2010-2010: Joachim Faulhaber Chris@16: +------------------------------------------------------------------------------+ Chris@16: Distributed under the Boost Software License, Version 1.0. Chris@16: (See accompanying file LICENCE.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt) Chris@16: +-----------------------------------------------------------------------------*/ Chris@16: #ifndef BOOST_ICL_CONCEPT_ELEMENT_MAP_HPP_JOFA_100921 Chris@16: #define BOOST_ICL_CONCEPT_ELEMENT_MAP_HPP_JOFA_100921 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost{ namespace icl Chris@16: { Chris@16: Chris@16: //NOTE: Some forward declarations are needed by some compilers. Chris@16: template Chris@16: typename enable_if, Type>::type& Chris@16: erase_if(const Predicate& pred, Type& object); Chris@16: Chris@16: Chris@16: //============================================================================== Chris@16: //= Containedness Chris@16: //============================================================================== Chris@16: //------------------------------------------------------------------------------ Chris@16: //- bool within(c P&, c T&) T:{m} P:{b} fragment_types Chris@16: //------------------------------------------------------------------------------ Chris@16: /** Checks if a key-value pair is in the map */ Chris@16: template Chris@16: typename enable_if, bool>::type Chris@16: within(const typename Type::element_type& value_pair, const Type& super) Chris@16: { Chris@16: typedef typename Type::const_iterator const_iterator; Chris@16: const_iterator found_ = super.find(value_pair.first); Chris@16: return found_ != super.end() && (*found_).second == value_pair.second; Chris@16: } Chris@16: Chris@16: //------------------------------------------------------------------------------ Chris@16: //- bool contains(c T&, c P&) T:{m} P:{b} fragment_types Chris@16: //------------------------------------------------------------------------------ Chris@16: template Chris@16: typename enable_if, bool>::type Chris@16: contains(const Type& super, const typename Type::element_type& value_pair) Chris@16: { Chris@16: return icl::within(value_pair, super); Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Equivalences and Orderings Chris@16: //============================================================================== Chris@16: Chris@16: /** Protonic equality is equality on all elements that do not carry an identity element as content. */ Chris@16: template Chris@16: inline typename enable_if, bool>::type Chris@16: is_distinct_equal(const Type& lhs, const Type& rhs) Chris@16: { Chris@16: return Map::lexicographical_distinct_equal(lhs, rhs); Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Addition Chris@16: //============================================================================== Chris@16: /** \c add inserts \c value_pair into the map if it's key does Chris@16: not exist in the map. Chris@16: If \c value_pairs's key value exists in the map, it's data Chris@16: value is added to the data value already found in the map. */ Chris@16: template Chris@16: typename enable_if, Type>::type& Chris@16: add(Type& object, const typename Type::value_type& value_pair) Chris@16: { Chris@16: return object.add(value_pair); Chris@16: } Chris@16: Chris@16: /** \c add add \c value_pair into the map using \c prior as a hint to Chris@16: insert \c value_pair after the position \c prior is pointing to. */ Chris@16: template Chris@16: typename enable_if, typename Type::iterator>::type Chris@16: add(Type& object, typename Type::iterator prior, Chris@16: const typename Type::value_type& value_pair) Chris@16: { Chris@16: return object.add(prior, value_pair); Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Erasure Chris@16: //============================================================================== Chris@16: //------------------------------------------------------------------------------ Chris@16: //- T& erase(T&, c P&) T:{m} P:{b} fragment_type Chris@16: //------------------------------------------------------------------------------ Chris@16: template Chris@16: typename enable_if, typename Type::size_type>::type Chris@16: erase(Type& object, const typename Type::element_type& value_pair) Chris@16: { Chris@16: typedef typename Type::size_type size_type; Chris@16: typedef typename Type::iterator iterator; Chris@16: typedef typename Type::on_identity_absorbtion on_identity_absorbtion; Chris@16: Chris@16: if(on_identity_absorbtion::is_absorbable(value_pair.second)) Chris@16: return identity_element::value(); Chris@16: Chris@16: iterator it_ = object.find(value_pair.first); Chris@16: if(it_ != object.end() && value_pair.second == (*it_).second) Chris@16: { Chris@16: object.erase(it_); Chris@16: return unit_element::value(); Chris@16: } Chris@16: Chris@16: return identity_element::value(); Chris@16: } Chris@16: Chris@16: template Chris@16: typename enable_if, Type>::type& Chris@16: erase(Type& object, const typename Type::set_type& erasure) Chris@16: { Chris@16: typedef typename Type::set_type set_type; Chris@16: ICL_const_FORALL(typename set_type, elem_, erasure) Chris@16: icl::erase(object, *elem_); Chris@16: Chris@16: return object; Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Subtraction Chris@16: //============================================================================== Chris@16: //------------------------------------------------------------------------------ Chris@16: //- T& subtract(T&, c P&) T:{m} P:{b} fragment_type Chris@16: //------------------------------------------------------------------------------ Chris@16: template Chris@16: inline typename enable_if, Type>::type& Chris@16: subtract(Type& object, const typename Type::element_type& operand) Chris@16: { Chris@16: return object.subtract(operand); Chris@16: } Chris@16: Chris@16: //------------------------------------------------------------------------------ Chris@16: //- T& subtract(T&, c P&) T:{m} P:{e} key_type Chris@16: //------------------------------------------------------------------------------ Chris@16: template Chris@16: typename enable_if, Type>::type& Chris@16: subtract(Type& object, const typename Type::domain_type& key_value) Chris@16: { Chris@16: return icl::erase(object, key_value); Chris@16: } Chris@16: Chris@16: //------------------------------------------------------------------------------ Chris@16: //- T& subtract(T&, c P&) T:{m} P:{s} set key_type Chris@16: //------------------------------------------------------------------------------ Chris@16: template Chris@16: inline typename enable_if, Type>::type& Chris@16: operator -= (Type& object, const typename Type::set_type& operand) Chris@16: { Chris@16: typedef typename Type::set_type set_type; Chris@16: typedef typename set_type::const_iterator co_iterator; Chris@16: typedef typename Type::iterator iterator; Chris@16: Chris@16: co_iterator common_lwb_, common_upb_; Chris@16: if(!Set::common_range(common_lwb_, common_upb_, operand, object)) Chris@16: return object; Chris@16: Chris@16: co_iterator it_ = common_lwb_; Chris@16: iterator common_; Chris@16: Chris@16: while(it_ != common_upb_) Chris@16: object.erase(*it_++); Chris@16: Chris@16: return object; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, Type>::type Chris@16: operator - (Type object, const typename Type::set_type& subtrahend) Chris@16: { Chris@16: return object -= subtrahend; Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Selective Update Chris@16: //============================================================================== Chris@16: //------------------------------------------------------------------------------ Chris@16: //- T& set_at(T&, c P&) T:{m} P:{b} Chris@16: //------------------------------------------------------------------------------ Chris@16: template Chris@16: inline typename enable_if, Type>::type& Chris@16: set_at(Type& object, const typename Type::element_type& operand) Chris@16: { Chris@16: typedef typename Type::iterator iterator; Chris@16: typedef typename Type::codomain_combine codomain_combine; Chris@16: typedef on_absorbtion::value> Chris@16: on_identity_absorbtion; Chris@16: Chris@16: if(!on_identity_absorbtion::is_absorbable(operand.second)) Chris@16: { Chris@16: std::pair insertion = object.insert(operand); Chris@16: if(!insertion.second) Chris@16: insertion->second = operand.second; Chris@16: } Chris@16: return object; Chris@16: } Chris@16: Chris@16: Chris@16: //============================================================================== Chris@16: //= Intersection Chris@16: //============================================================================== Chris@16: template Chris@16: inline typename enable_if, void>::type Chris@16: add_intersection(Type& section, const Type& object, Chris@16: const typename Type::element_type& operand) Chris@16: { Chris@16: object.add_intersection(section, operand); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, void>::type Chris@16: add_intersection(Type& section, const Type& object, const Type& operand) Chris@16: { Chris@16: ICL_const_FORALL(typename Type, it_, operand) Chris@16: icl::add_intersection(section, object, *it_); Chris@16: } Chris@16: Chris@16: //------------------------------------------------------------------------------ Chris@16: //- T& op &=(T&, c P&) T:{m} P:{b m} fragment_types Chris@16: //------------------------------------------------------------------------------ Chris@16: Chris@16: template Chris@16: inline typename enable_if, is_total >, Type>::type& Chris@16: operator &=(Type& object, const typename Type::element_type& operand) Chris@16: { Chris@16: object.add(operand); Chris@16: return object; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, mpl::not_ > >, Type>::type& Chris@16: operator &=(Type& object, const typename Type::element_type& operand) Chris@16: { Chris@16: Type section; Chris@16: icl::add_intersection(section, object, operand); Chris@16: object.swap(section); Chris@16: return object; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, Type>::type Chris@16: operator & (Type object, const typename Type::element_type& operand) Chris@16: { Chris@16: return object &= operand; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, Type>::type Chris@16: operator & (const typename Type::element_type& operand, Type object) Chris@16: { Chris@16: return object &= operand; Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@16: inline typename enable_if, is_total >, Type>::type& Chris@16: operator &=(Type& object, const Type& operand) Chris@16: { Chris@16: object += operand; Chris@16: return object; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, mpl::not_ > >, Type>::type& Chris@16: operator &=(Type& object, const Type& operand) Chris@16: { Chris@16: Type section; Chris@16: icl::add_intersection(section, object, operand); Chris@16: object.swap(section); Chris@16: return object; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, Type>::type Chris@16: operator & (Type object, const typename Type::key_object_type& operand) Chris@16: { Chris@16: return object &= operand; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, Type>::type Chris@16: operator & (const typename Type::key_object_type& operand, Type object) Chris@16: { Chris@16: return object &= operand; Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Intersection bool intersects(x,y) Chris@16: //============================================================================== Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , is_total > Chris@16: , bool>::type Chris@16: intersects(const Type&, const CoType&) Chris@16: { Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , mpl::not_ > > Chris@16: , bool>::type Chris@16: intersects(const Type& object, const typename Type::domain_type& operand) Chris@16: { Chris@16: return icl::contains(object, operand); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , mpl::not_ > > Chris@16: , bool>::type Chris@16: intersects(const Type& object, const typename Type::set_type& operand) Chris@16: { Chris@16: if(object.iterative_size() < operand.iterative_size()) Chris@16: return Map::intersects(object, operand); Chris@16: else Chris@16: return Map::intersects(operand, object); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , mpl::not_ > > Chris@16: , bool>::type Chris@16: intersects(const Type& object, const typename Type::element_type& operand) Chris@16: { Chris@16: Type intersection; Chris@16: icl::add_intersection(intersection, object, operand); Chris@16: return !intersection.empty(); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , mpl::not_ > > Chris@16: , bool>::type Chris@16: intersects(const Type& object, const Type& operand) Chris@16: { Chris@16: if(object.iterative_size() < operand.iterative_size()) Chris@16: return Map::intersects(object, operand); Chris@16: else Chris@16: return Map::intersects(operand, object); Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Symmetric difference Chris@16: //============================================================================== Chris@16: template Chris@16: inline typename enable_if, Type>::type& Chris@16: flip(Type& object, const typename Type::element_type& operand) Chris@16: { Chris@16: return object.flip(operand); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , is_total Chris@16: , absorbs_identities > Chris@16: , Type>::type& Chris@16: operator ^= (Type& object, const CoType&) Chris@16: { Chris@16: icl::clear(object); Chris@16: return object; Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , is_total Chris@16: , mpl::not_ > > Chris@16: , Type>::type& Chris@16: operator ^= (Type& object, const typename Type::element_type& operand) Chris@16: { Chris@16: return object.flip(operand); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , is_total Chris@16: , mpl::not_ > > Chris@16: , Type>::type& Chris@16: operator ^= (Type& object, const Type& operand) Chris@16: { Chris@16: ICL_const_FORALL(typename Type, it_, operand) Chris@16: icl::flip(object, *it_); Chris@16: Chris@16: ICL_FORALL(typename Type, it2_, object) Chris@16: (*it2_).second = identity_element::value(); Chris@16: Chris@16: return object; Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , mpl::not_ > > Chris@16: , Type>::type& Chris@16: operator ^= (Type& object, const typename Type::element_type& operand) Chris@16: { Chris@16: return icl::flip(object, operand); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if< mpl::and_< is_element_map Chris@16: , mpl::not_ > > Chris@16: , Type>::type& Chris@16: operator ^= (Type& object, const Type& operand) Chris@16: { Chris@16: typedef typename Type::const_iterator const_iterator; Chris@16: const_iterator it_ = operand.begin(); Chris@16: while(it_ != operand.end()) Chris@16: icl::flip(object, *it_++); Chris@16: Chris@16: return object; Chris@16: } Chris@16: Chris@16: Chris@16: //============================================================================== Chris@16: //= Set selection Chris@16: //============================================================================== Chris@16: template Chris@16: inline typename enable_if, Chris@16: typename Type::set_type>::type& Chris@16: domain(typename Type::set_type& domain_set, const Type& object) Chris@16: { Chris@16: typename Type::set_type::iterator prior_ = domain_set.end(); Chris@16: typename Type::const_iterator it_ = object.begin(); Chris@16: while(it_ != object.end()) Chris@16: prior_ = domain_set.insert(prior_, (*it_++).first); Chris@16: Chris@16: return domain_set; Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Neutron absorbtion Chris@16: //============================================================================== Chris@16: template Chris@16: inline typename enable_if Chris@16: , absorbs_identities >, Type>::type& Chris@16: absorb_identities(Type& object) Chris@16: { Chris@16: typedef typename Type::element_type element_type; Chris@16: return icl::erase_if(content_is_identity_element(), object); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if Chris@16: , mpl::not_ > > Chris@16: , Type>::type& Chris@16: absorb_identities(Type&){} Chris@16: Chris@16: //============================================================================== Chris@16: //= Streaming Chris@16: //============================================================================== Chris@16: template Chris@16: inline typename enable_if, std::basic_ostream >::type& Chris@16: operator << (std::basic_ostream& stream, const Type& object) Chris@16: { Chris@16: stream << "{"; Chris@16: ICL_const_FORALL(typename Type, it, object) Chris@16: stream << "(" << it->first << "->" << it->second << ")"; Chris@16: Chris@16: return stream << "}"; Chris@16: } Chris@16: Chris@16: Chris@16: }} // namespace boost icl Chris@16: Chris@16: #endif Chris@16: Chris@16: