Chris@16
|
1 /*-----------------------------------------------------------------------------+
|
Chris@16
|
2 Copyright (c) 2010-2010: Joachim Faulhaber
|
Chris@16
|
3 +------------------------------------------------------------------------------+
|
Chris@16
|
4 Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
5 (See accompanying file LICENCE.txt or copy at
|
Chris@16
|
6 http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 +-----------------------------------------------------------------------------*/
|
Chris@16
|
8 #ifndef BOOST_ICL_CONCEPT_ELEMENT_SET_HPP_JOFA_100921
|
Chris@16
|
9 #define BOOST_ICL_CONCEPT_ELEMENT_SET_HPP_JOFA_100921
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/icl/type_traits/is_combinable.hpp>
|
Chris@16
|
12 #include <boost/icl/concept/set_value.hpp>
|
Chris@16
|
13 #include <boost/icl/detail/std_set.hpp>
|
Chris@16
|
14 #include <boost/icl/detail/set_algo.hpp>
|
Chris@16
|
15
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost{ namespace icl
|
Chris@16
|
18 {
|
Chris@16
|
19
|
Chris@16
|
20 //==============================================================================
|
Chris@16
|
21 //= Addition<ElementSet>
|
Chris@16
|
22 //==============================================================================
|
Chris@16
|
23 /** \c add inserts \c operand into the map if it's key does
|
Chris@16
|
24 not exist in the map.
|
Chris@16
|
25 If \c operands's key value exists in the map, it's data
|
Chris@16
|
26 value is added to the data value already found in the map. */
|
Chris@16
|
27 template <class Type>
|
Chris@16
|
28 typename enable_if<is_element_set<Type>, Type>::type&
|
Chris@16
|
29 add(Type& object, const typename Type::value_type& operand)
|
Chris@16
|
30 {
|
Chris@16
|
31 object.insert(operand);
|
Chris@16
|
32 return object;
|
Chris@16
|
33 }
|
Chris@16
|
34
|
Chris@16
|
35 /** \c add add \c operand into the map using \c prior as a hint to
|
Chris@16
|
36 insert \c operand after the position \c prior is pointing to. */
|
Chris@16
|
37 template <class Type>
|
Chris@16
|
38 typename enable_if<is_element_set<Type>, typename Type::iterator>::type
|
Chris@16
|
39 add(Type& object, typename Type::iterator prior,
|
Chris@16
|
40 const typename Type::value_type& operand)
|
Chris@16
|
41 {
|
Chris@16
|
42 return object.insert(prior, operand);
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 //==============================================================================
|
Chris@16
|
46 //= Subtraction
|
Chris@16
|
47 //==============================================================================
|
Chris@16
|
48 /** If the \c operand's key value is in the map, it's data value is
|
Chris@16
|
49 subtraced from the data value stored in the map. */
|
Chris@16
|
50 template<class Type>
|
Chris@16
|
51 typename enable_if<is_element_set<Type>, Type>::type&
|
Chris@16
|
52 subtract(Type& object, const typename Type::value_type& operand)
|
Chris@16
|
53 {
|
Chris@16
|
54 object.erase(operand);
|
Chris@16
|
55 return object;
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58
|
Chris@16
|
59 //==============================================================================
|
Chris@16
|
60 //= Intersection
|
Chris@16
|
61 //==============================================================================
|
Chris@16
|
62 template<class Type>
|
Chris@16
|
63 inline typename enable_if<is_element_set<Type>, bool>::type
|
Chris@16
|
64 intersects(const Type& object, const typename Type::key_type& operand)
|
Chris@16
|
65 {
|
Chris@16
|
66 return !(object.find(operand) == object.end());
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 template<class Type>
|
Chris@16
|
70 inline typename enable_if<is_element_set<Type>, bool>::type
|
Chris@16
|
71 intersects(const Type& object, const Type& operand)
|
Chris@16
|
72 {
|
Chris@16
|
73 if(iterative_size(object) < iterative_size(operand))
|
Chris@16
|
74 return Set::intersects(object, operand);
|
Chris@16
|
75 else
|
Chris@16
|
76 return Set::intersects(operand, object);
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 //==============================================================================
|
Chris@16
|
80 //= Symmetric difference
|
Chris@16
|
81 //==============================================================================
|
Chris@16
|
82 template<class Type>
|
Chris@16
|
83 inline typename enable_if<is_element_set<Type>, Type>::type&
|
Chris@16
|
84 flip(Type& object, const typename Type::value_type& operand)
|
Chris@16
|
85 {
|
Chris@16
|
86 typedef typename Type::iterator iterator;
|
Chris@16
|
87 std::pair<iterator,bool> insertion = object.insert(operand);
|
Chris@16
|
88 if(!insertion.second)
|
Chris@16
|
89 object.erase(insertion.first);
|
Chris@16
|
90
|
Chris@16
|
91 return object;
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 template<class Type>
|
Chris@16
|
95 inline typename enable_if<is_element_set<Type>, Type>::type&
|
Chris@16
|
96 operator ^= (Type& object, const typename Type::element_type& operand)
|
Chris@16
|
97 {
|
Chris@16
|
98 return icl::flip(object, operand);
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 /** Symmetric subtract map \c x2 and \c *this.
|
Chris@16
|
102 So \c *this becomes the symmetric difference of \c *this and \c x2 */
|
Chris@16
|
103 template<class Type>
|
Chris@16
|
104 inline typename enable_if<is_element_set<Type>, Type>::type&
|
Chris@16
|
105 operator ^= (Type& object, const Type& operand)
|
Chris@16
|
106 {
|
Chris@16
|
107 typedef typename Type::const_iterator const_iterator;
|
Chris@16
|
108 const_iterator it_ = operand.begin();
|
Chris@16
|
109 while(it_ != operand.end())
|
Chris@16
|
110 icl::flip(object, *it_++);
|
Chris@16
|
111
|
Chris@16
|
112 return object;
|
Chris@16
|
113 }
|
Chris@16
|
114
|
Chris@16
|
115 //==============================================================================
|
Chris@16
|
116 //= Streaming<ElementSet>
|
Chris@16
|
117 //==============================================================================
|
Chris@16
|
118 template<class CharType, class CharTraits, class Type>
|
Chris@16
|
119 inline typename enable_if<is_element_set<Type>, std::basic_ostream<CharType, CharTraits> >::type&
|
Chris@16
|
120 operator << (std::basic_ostream<CharType, CharTraits>& stream, const Type& object)
|
Chris@16
|
121 {
|
Chris@16
|
122 stream << "{";
|
Chris@16
|
123 ICL_const_FORALL(typename Type, it, object)
|
Chris@16
|
124 stream << (*it) << " ";
|
Chris@16
|
125
|
Chris@16
|
126 return stream << "}";
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129
|
Chris@16
|
130 }} // namespace boost icl
|
Chris@16
|
131
|
Chris@16
|
132 #endif
|
Chris@16
|
133
|
Chris@16
|
134
|