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