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_SET_HPP_JOFA_100921 Chris@16: #define BOOST_ICL_CONCEPT_ELEMENT_SET_HPP_JOFA_100921 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost{ namespace icl Chris@16: { Chris@16: Chris@16: //============================================================================== Chris@16: //= Addition Chris@16: //============================================================================== Chris@16: /** \c add inserts \c operand into the map if it's key does Chris@16: not exist in the map. Chris@16: If \c operands'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& operand) Chris@16: { Chris@16: object.insert(operand); Chris@16: return object; Chris@16: } Chris@16: Chris@16: /** \c add add \c operand into the map using \c prior as a hint to Chris@16: insert \c operand 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& operand) Chris@16: { Chris@16: return object.insert(prior, operand); Chris@16: } Chris@16: Chris@16: //============================================================================== Chris@16: //= Subtraction Chris@16: //============================================================================== Chris@16: /** If the \c operand's key value is in the map, it's data value is Chris@16: subtraced from the data value stored in the map. */ Chris@16: template Chris@16: typename enable_if, Type>::type& Chris@16: subtract(Type& object, const typename Type::value_type& operand) Chris@16: { Chris@16: object.erase(operand); 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, bool>::type Chris@16: intersects(const Type& object, const typename Type::key_type& operand) Chris@16: { Chris@16: return !(object.find(operand) == object.end()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename enable_if, bool>::type Chris@16: intersects(const Type& object, const Type& operand) Chris@16: { Chris@16: if(iterative_size(object) < iterative_size(operand)) Chris@16: return Set::intersects(object, operand); Chris@16: else Chris@16: return Set::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::value_type& operand) Chris@16: { Chris@16: typedef typename Type::iterator iterator; Chris@16: std::pair insertion = object.insert(operand); Chris@16: if(!insertion.second) Chris@16: object.erase(insertion.first); 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::element_type& operand) Chris@16: { Chris@16: return icl::flip(object, operand); Chris@16: } Chris@16: Chris@16: /** Symmetric subtract map \c x2 and \c *this. Chris@16: So \c *this becomes the symmetric difference of \c *this and \c x2 */ Chris@16: template Chris@16: inline typename enable_if, 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: //= 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) << " "; 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: