Chris@16
|
1 /*-----------------------------------------------------------------------------+
|
Chris@16
|
2 Copyright (c) 2007-2011: 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_MAP_HPP_JOFA_070519
|
Chris@16
|
9 #define BOOST_ICL_MAP_HPP_JOFA_070519
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/icl/impl_config.hpp>
|
Chris@16
|
12
|
Chris@16
|
13 #if defined(ICL_USE_BOOST_MOVE_IMPLEMENTATION)
|
Chris@16
|
14 # include <boost/container/map.hpp>
|
Chris@16
|
15 # include <boost/container/set.hpp>
|
Chris@16
|
16 #elif defined(ICL_USE_STD_IMPLEMENTATION)
|
Chris@16
|
17 # include <map>
|
Chris@16
|
18 # include <set>
|
Chris@16
|
19 #else // Default for implementing containers
|
Chris@16
|
20 # include <map>
|
Chris@16
|
21 # include <set>
|
Chris@16
|
22 #endif
|
Chris@16
|
23
|
Chris@16
|
24 #include <string>
|
Chris@16
|
25 #include <boost/type_traits/ice.hpp>
|
Chris@16
|
26 #include <boost/call_traits.hpp>
|
Chris@16
|
27 #include <boost/icl/detail/notate.hpp>
|
Chris@16
|
28 #include <boost/icl/detail/design_config.hpp>
|
Chris@16
|
29 #include <boost/icl/detail/concept_check.hpp>
|
Chris@16
|
30 #include <boost/icl/detail/on_absorbtion.hpp>
|
Chris@16
|
31 #include <boost/icl/type_traits/is_map.hpp>
|
Chris@16
|
32 #include <boost/icl/type_traits/absorbs_identities.hpp>
|
Chris@16
|
33 #include <boost/icl/type_traits/is_total.hpp>
|
Chris@16
|
34 #include <boost/icl/type_traits/is_element_container.hpp>
|
Chris@16
|
35 #include <boost/icl/type_traits/has_inverse.hpp>
|
Chris@16
|
36 #include <boost/icl/type_traits/to_string.hpp>
|
Chris@16
|
37
|
Chris@16
|
38 #include <boost/icl/associative_element_container.hpp>
|
Chris@16
|
39 #include <boost/icl/functors.hpp>
|
Chris@16
|
40
|
Chris@16
|
41 namespace boost{namespace icl
|
Chris@16
|
42 {
|
Chris@16
|
43
|
Chris@16
|
44 struct partial_absorber
|
Chris@16
|
45 {
|
Chris@16
|
46 enum { absorbs_identities = true };
|
Chris@16
|
47 enum { is_total = false };
|
Chris@16
|
48 };
|
Chris@16
|
49
|
Chris@16
|
50 template<>
|
Chris@16
|
51 inline std::string type_to_string<partial_absorber>::apply() { return "@0"; }
|
Chris@16
|
52
|
Chris@16
|
53 struct partial_enricher
|
Chris@16
|
54 {
|
Chris@16
|
55 enum { absorbs_identities = false };
|
Chris@16
|
56 enum { is_total = false };
|
Chris@16
|
57 };
|
Chris@16
|
58
|
Chris@16
|
59 template<>
|
Chris@16
|
60 inline std::string type_to_string<partial_enricher>::apply() { return "e0"; }
|
Chris@16
|
61
|
Chris@16
|
62 struct total_absorber
|
Chris@16
|
63 {
|
Chris@16
|
64 enum { absorbs_identities = true };
|
Chris@16
|
65 enum { is_total = true };
|
Chris@16
|
66 };
|
Chris@16
|
67
|
Chris@16
|
68 template<>
|
Chris@16
|
69 inline std::string type_to_string<total_absorber>::apply() { return "^0"; }
|
Chris@16
|
70
|
Chris@16
|
71 struct total_enricher
|
Chris@16
|
72 {
|
Chris@16
|
73 enum { absorbs_identities = false };
|
Chris@16
|
74 enum { is_total = true };
|
Chris@16
|
75 };
|
Chris@16
|
76
|
Chris@16
|
77 template<>
|
Chris@16
|
78 inline std::string type_to_string<total_enricher>::apply() { return "e^0"; }
|
Chris@16
|
79
|
Chris@16
|
80
|
Chris@16
|
81
|
Chris@16
|
82 /** \brief Addable, subractable and intersectable maps */
|
Chris@16
|
83 template
|
Chris@16
|
84 <
|
Chris@16
|
85 typename DomainT,
|
Chris@16
|
86 typename CodomainT,
|
Chris@16
|
87 class Traits = icl::partial_absorber,
|
Chris@16
|
88 ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
|
Chris@16
|
89 ICL_COMBINE Combine = ICL_COMBINE_INSTANCE(icl::inplace_plus, CodomainT),
|
Chris@16
|
90 ICL_SECTION Section = ICL_SECTION_INSTANCE(icl::inter_section, CodomainT),
|
Chris@16
|
91 ICL_ALLOC Alloc = std::allocator
|
Chris@16
|
92 >
|
Chris@16
|
93 class map: private ICL_IMPL_SPACE::map<DomainT, CodomainT, ICL_COMPARE_DOMAIN(Compare,DomainT),
|
Chris@16
|
94 Alloc<std::pair<const DomainT, CodomainT> > >
|
Chris@16
|
95 {
|
Chris@16
|
96 public:
|
Chris@16
|
97 typedef Alloc<typename std::pair<const DomainT, CodomainT> > allocator_type;
|
Chris@16
|
98
|
Chris@16
|
99 typedef typename icl::map<DomainT,CodomainT,Traits, Compare,Combine,Section,Alloc> type;
|
Chris@16
|
100 typedef typename ICL_IMPL_SPACE::map<DomainT, CodomainT, ICL_COMPARE_DOMAIN(Compare,DomainT),
|
Chris@16
|
101 allocator_type> base_type;
|
Chris@16
|
102
|
Chris@16
|
103 typedef Traits traits;
|
Chris@16
|
104
|
Chris@16
|
105 public:
|
Chris@16
|
106 typedef DomainT domain_type;
|
Chris@16
|
107 typedef typename boost::call_traits<DomainT>::param_type domain_param;
|
Chris@16
|
108 typedef DomainT key_type;
|
Chris@16
|
109 typedef CodomainT codomain_type;
|
Chris@16
|
110 typedef CodomainT mapped_type;
|
Chris@16
|
111 typedef CodomainT data_type;
|
Chris@16
|
112 typedef std::pair<const DomainT, CodomainT> element_type;
|
Chris@16
|
113 typedef std::pair<const DomainT, CodomainT> value_type;
|
Chris@16
|
114 typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
|
Chris@16
|
115 typedef ICL_COMBINE_CODOMAIN(Combine,CodomainT) codomain_combine;
|
Chris@16
|
116 typedef domain_compare key_compare;
|
Chris@16
|
117 typedef ICL_COMPARE_DOMAIN(Compare,element_type) element_compare;
|
Chris@16
|
118 typedef typename inverse<codomain_combine >::type inverse_codomain_combine;
|
Chris@16
|
119 typedef typename mpl::if_
|
Chris@16
|
120 <has_set_semantics<codomain_type>
|
Chris@16
|
121 , ICL_SECTION_CODOMAIN(Section,CodomainT)
|
Chris@16
|
122 , codomain_combine
|
Chris@16
|
123 >::type codomain_intersect;
|
Chris@16
|
124 typedef typename inverse<codomain_intersect>::type inverse_codomain_intersect;
|
Chris@16
|
125 typedef typename base_type::value_compare value_compare;
|
Chris@16
|
126
|
Chris@16
|
127 typedef typename ICL_IMPL_SPACE::set<DomainT, domain_compare, Alloc<DomainT> > set_type;
|
Chris@16
|
128 typedef set_type key_object_type;
|
Chris@16
|
129
|
Chris@16
|
130
|
Chris@16
|
131 BOOST_STATIC_CONSTANT(bool, _total = (Traits::is_total));
|
Chris@16
|
132 BOOST_STATIC_CONSTANT(bool, _absorbs = (Traits::absorbs_identities));
|
Chris@16
|
133 BOOST_STATIC_CONSTANT(bool,
|
Chris@16
|
134 total_invertible = (mpl::and_<is_total<type>, has_inverse<codomain_type> >::value));
|
Chris@16
|
135
|
Chris@16
|
136 typedef on_absorbtion<type,codomain_combine,Traits::absorbs_identities>
|
Chris@16
|
137 on_identity_absorbtion;
|
Chris@16
|
138
|
Chris@16
|
139 public:
|
Chris@16
|
140 typedef typename base_type::pointer pointer;
|
Chris@16
|
141 typedef typename base_type::const_pointer const_pointer;
|
Chris@16
|
142 typedef typename base_type::reference reference;
|
Chris@16
|
143 typedef typename base_type::const_reference const_reference;
|
Chris@16
|
144 typedef typename base_type::iterator iterator;
|
Chris@16
|
145 typedef typename base_type::const_iterator const_iterator;
|
Chris@16
|
146 typedef typename base_type::size_type size_type;
|
Chris@16
|
147 typedef typename base_type::difference_type difference_type;
|
Chris@16
|
148 typedef typename base_type::reverse_iterator reverse_iterator;
|
Chris@16
|
149 typedef typename base_type::const_reverse_iterator const_reverse_iterator;
|
Chris@16
|
150
|
Chris@16
|
151 public:
|
Chris@16
|
152 BOOST_STATIC_CONSTANT(bool,
|
Chris@16
|
153 is_total_invertible = ( Traits::is_total
|
Chris@16
|
154 && has_inverse<codomain_type>::value));
|
Chris@16
|
155
|
Chris@16
|
156 BOOST_STATIC_CONSTANT(int, fineness = 4);
|
Chris@16
|
157
|
Chris@16
|
158 public:
|
Chris@16
|
159 //==========================================================================
|
Chris@16
|
160 //= Construct, copy, destruct
|
Chris@16
|
161 //==========================================================================
|
Chris@16
|
162 map()
|
Chris@16
|
163 {
|
Chris@16
|
164 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
|
Chris@16
|
165 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
|
Chris@16
|
166 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
|
Chris@16
|
167 BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
|
Chris@16
|
168 }
|
Chris@16
|
169
|
Chris@16
|
170 map(const key_compare& comp): base_type(comp){}
|
Chris@16
|
171
|
Chris@16
|
172 template <class InputIterator>
|
Chris@16
|
173 map(InputIterator first, InputIterator past)
|
Chris@16
|
174 : base_type(first,past){}
|
Chris@16
|
175
|
Chris@16
|
176 template <class InputIterator>
|
Chris@16
|
177 map(InputIterator first, InputIterator past, const key_compare& comp)
|
Chris@16
|
178 : base_type(first,past,comp)
|
Chris@16
|
179 {}
|
Chris@16
|
180
|
Chris@16
|
181 map(const map& src)
|
Chris@16
|
182 : base_type(src)
|
Chris@16
|
183 {
|
Chris@16
|
184 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
|
Chris@16
|
185 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
|
Chris@16
|
186 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
|
Chris@16
|
187 BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
|
Chris@16
|
188 }
|
Chris@16
|
189
|
Chris@16
|
190 explicit map(const element_type& key_value_pair): base_type::map()
|
Chris@16
|
191 {
|
Chris@16
|
192 insert(key_value_pair);
|
Chris@16
|
193 }
|
Chris@16
|
194
|
Chris@16
|
195 # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
196 //==========================================================================
|
Chris@16
|
197 //= Move semantics
|
Chris@16
|
198 //==========================================================================
|
Chris@16
|
199
|
Chris@16
|
200 map(map&& src)
|
Chris@16
|
201 : base_type(boost::move(src))
|
Chris@16
|
202 {
|
Chris@16
|
203 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<DomainT>));
|
Chris@16
|
204 BOOST_CONCEPT_ASSERT((LessThanComparableConcept<DomainT>));
|
Chris@16
|
205 BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept<CodomainT>));
|
Chris@16
|
206 BOOST_CONCEPT_ASSERT((EqualComparableConcept<CodomainT>));
|
Chris@16
|
207 }
|
Chris@16
|
208
|
Chris@101
|
209 map& operator = (map src)
|
Chris@101
|
210 {
|
Chris@101
|
211 base_type::operator=(boost::move(src));
|
Chris@101
|
212 return *this;
|
Chris@101
|
213 }
|
Chris@101
|
214 //==========================================================================
|
Chris@101
|
215 # else
|
Chris@101
|
216
|
Chris@101
|
217 map& operator = (const map& src)
|
Chris@16
|
218 {
|
Chris@16
|
219 base_type::operator=(src);
|
Chris@16
|
220 return *this;
|
Chris@16
|
221 }
|
Chris@101
|
222
|
Chris@16
|
223 # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
224
|
Chris@16
|
225 void swap(map& src) { base_type::swap(src); }
|
Chris@16
|
226
|
Chris@16
|
227 //==========================================================================
|
Chris@16
|
228 using base_type::empty;
|
Chris@16
|
229 using base_type::clear;
|
Chris@16
|
230
|
Chris@16
|
231 using base_type::begin;
|
Chris@16
|
232 using base_type::end;
|
Chris@16
|
233 using base_type::rbegin;
|
Chris@16
|
234 using base_type::rend;
|
Chris@16
|
235
|
Chris@16
|
236 using base_type::size;
|
Chris@16
|
237 using base_type::max_size;
|
Chris@16
|
238
|
Chris@16
|
239 using base_type::key_comp;
|
Chris@16
|
240 using base_type::value_comp;
|
Chris@16
|
241
|
Chris@16
|
242 using base_type::erase;
|
Chris@16
|
243 using base_type::find;
|
Chris@16
|
244 using base_type::count;
|
Chris@16
|
245
|
Chris@16
|
246 using base_type::lower_bound;
|
Chris@16
|
247 using base_type::upper_bound;
|
Chris@16
|
248 using base_type::equal_range;
|
Chris@16
|
249
|
Chris@16
|
250 using base_type::operator[];
|
Chris@16
|
251
|
Chris@16
|
252 public:
|
Chris@16
|
253 //==========================================================================
|
Chris@16
|
254 //= Containedness
|
Chris@16
|
255 //==========================================================================
|
Chris@16
|
256
|
Chris@16
|
257 template<class SubObject>
|
Chris@16
|
258 bool contains(const SubObject& sub)const
|
Chris@16
|
259 { return icl::contains(*this, sub); }
|
Chris@16
|
260
|
Chris@16
|
261 bool within(const map& super)const
|
Chris@16
|
262 { return icl::contains(super, *this); }
|
Chris@16
|
263
|
Chris@16
|
264 //==========================================================================
|
Chris@16
|
265 //= Size
|
Chris@16
|
266 //==========================================================================
|
Chris@16
|
267 /** \c iterative_size() yields the number of elements that is visited
|
Chris@16
|
268 throu complete iteration. For interval sets \c iterative_size() is
|
Chris@16
|
269 different from \c size(). */
|
Chris@16
|
270 std::size_t iterative_size()const { return base_type::size(); }
|
Chris@16
|
271
|
Chris@16
|
272 //==========================================================================
|
Chris@16
|
273 //= Selection
|
Chris@16
|
274 //==========================================================================
|
Chris@16
|
275
|
Chris@16
|
276 /** Total select function. */
|
Chris@16
|
277 codomain_type operator()(const domain_type& key)const
|
Chris@16
|
278 {
|
Chris@16
|
279 const_iterator it = find(key);
|
Chris@16
|
280 return it==end() ? identity_element<codomain_type>::value()
|
Chris@16
|
281 : it->second;
|
Chris@16
|
282 }
|
Chris@16
|
283
|
Chris@16
|
284 //==========================================================================
|
Chris@16
|
285 //= Addition
|
Chris@16
|
286 //==========================================================================
|
Chris@16
|
287 /** \c add inserts \c value_pair into the map if it's key does
|
Chris@16
|
288 not exist in the map.
|
Chris@16
|
289 If \c value_pairs's key value exists in the map, it's data
|
Chris@16
|
290 value is added to the data value already found in the map. */
|
Chris@16
|
291 map& add(const value_type& value_pair)
|
Chris@16
|
292 {
|
Chris@16
|
293 return _add<codomain_combine>(value_pair);
|
Chris@16
|
294 }
|
Chris@16
|
295
|
Chris@16
|
296 /** \c add add \c value_pair into the map using \c prior as a hint to
|
Chris@16
|
297 insert \c value_pair after the position \c prior is pointing to. */
|
Chris@16
|
298 iterator add(iterator prior, const value_type& value_pair)
|
Chris@16
|
299 {
|
Chris@16
|
300 return _add<codomain_combine>(prior, value_pair);
|
Chris@16
|
301 }
|
Chris@16
|
302
|
Chris@16
|
303 //==========================================================================
|
Chris@16
|
304 //= Subtraction
|
Chris@16
|
305 //==========================================================================
|
Chris@16
|
306 /** If the \c value_pair's key value is in the map, it's data value is
|
Chris@16
|
307 subtraced from the data value stored in the map. */
|
Chris@16
|
308 map& subtract(const element_type& value_pair)
|
Chris@16
|
309 {
|
Chris@16
|
310 on_invertible<type, is_total_invertible>
|
Chris@16
|
311 ::subtract(*this, value_pair);
|
Chris@16
|
312 return *this;
|
Chris@16
|
313 }
|
Chris@16
|
314
|
Chris@16
|
315 map& subtract(const domain_type& key)
|
Chris@16
|
316 {
|
Chris@16
|
317 icl::erase(*this, key);
|
Chris@16
|
318 return *this;
|
Chris@16
|
319 }
|
Chris@16
|
320
|
Chris@16
|
321 //==========================================================================
|
Chris@16
|
322 //= Insertion, erasure
|
Chris@16
|
323 //==========================================================================
|
Chris@16
|
324 std::pair<iterator,bool> insert(const value_type& value_pair)
|
Chris@16
|
325 {
|
Chris@16
|
326 if(on_identity_absorbtion::is_absorbable(value_pair.second))
|
Chris@16
|
327 return std::pair<iterator,bool>(end(),true);
|
Chris@16
|
328 else
|
Chris@16
|
329 return base_type::insert(value_pair);
|
Chris@16
|
330 }
|
Chris@16
|
331
|
Chris@16
|
332 iterator insert(iterator prior, const value_type& value_pair)
|
Chris@16
|
333 {
|
Chris@16
|
334 if(on_identity_absorbtion::is_absorbable(value_pair.second))
|
Chris@16
|
335 return end();
|
Chris@16
|
336 else
|
Chris@16
|
337 return base_type::insert(prior, value_pair);
|
Chris@16
|
338 }
|
Chris@16
|
339
|
Chris@16
|
340 template<class Iterator>
|
Chris@16
|
341 iterator insert(Iterator first, Iterator last)
|
Chris@16
|
342 {
|
Chris@16
|
343 iterator prior = end(), it = first;
|
Chris@16
|
344 while(it != last)
|
Chris@16
|
345 prior = this->insert(prior, *it++);
|
Chris@16
|
346 }
|
Chris@16
|
347
|
Chris@16
|
348 /** With <tt>key_value_pair = (k,v)</tt> set value \c v for key \c k */
|
Chris@16
|
349 map& set(const element_type& key_value_pair)
|
Chris@16
|
350 {
|
Chris@16
|
351 return icl::set_at(*this, key_value_pair);
|
Chris@16
|
352 }
|
Chris@16
|
353
|
Chris@16
|
354 /** erase \c key_value_pair from the map.
|
Chris@16
|
355 Erase only if, the exact value content \c val is stored for the given key. */
|
Chris@16
|
356 size_type erase(const element_type& key_value_pair)
|
Chris@16
|
357 {
|
Chris@16
|
358 return icl::erase(*this, key_value_pair);
|
Chris@16
|
359 }
|
Chris@16
|
360
|
Chris@16
|
361 //==========================================================================
|
Chris@16
|
362 //= Intersection
|
Chris@16
|
363 //==========================================================================
|
Chris@16
|
364 /** The intersection of \c key_value_pair and \c *this map is added to \c section. */
|
Chris@16
|
365 void add_intersection(map& section, const element_type& key_value_pair)const
|
Chris@16
|
366 {
|
Chris@16
|
367 on_definedness<type, Traits::is_total>
|
Chris@16
|
368 ::add_intersection(section, *this, key_value_pair);
|
Chris@16
|
369 }
|
Chris@16
|
370
|
Chris@16
|
371 //==========================================================================
|
Chris@16
|
372 //= Symmetric difference
|
Chris@16
|
373 //==========================================================================
|
Chris@16
|
374
|
Chris@16
|
375 map& flip(const element_type& operand)
|
Chris@16
|
376 {
|
Chris@16
|
377 on_total_absorbable<type,_total,_absorbs>::flip(*this, operand);
|
Chris@16
|
378 return *this;
|
Chris@16
|
379 }
|
Chris@16
|
380
|
Chris@16
|
381 private:
|
Chris@16
|
382 template<class Combiner>
|
Chris@16
|
383 map& _add(const element_type& value_pair);
|
Chris@16
|
384
|
Chris@16
|
385 template<class Combiner>
|
Chris@16
|
386 iterator _add(iterator prior, const element_type& value_pair);
|
Chris@16
|
387
|
Chris@16
|
388 template<class Combiner>
|
Chris@16
|
389 map& _subtract(const element_type& value_pair);
|
Chris@16
|
390
|
Chris@16
|
391 template<class FragmentT>
|
Chris@16
|
392 void total_add_intersection(type& section, const FragmentT& fragment)const
|
Chris@16
|
393 {
|
Chris@16
|
394 section += *this;
|
Chris@16
|
395 section.add(fragment);
|
Chris@16
|
396 }
|
Chris@16
|
397
|
Chris@16
|
398 void partial_add_intersection(type& section, const element_type& operand)const
|
Chris@16
|
399 {
|
Chris@16
|
400 const_iterator it_ = find(operand.first);
|
Chris@16
|
401 if(it_ != end())
|
Chris@16
|
402 {
|
Chris@16
|
403 section.template _add<codomain_combine >(*it_);
|
Chris@16
|
404 section.template _add<codomain_intersect>(operand);
|
Chris@16
|
405 }
|
Chris@16
|
406 }
|
Chris@16
|
407
|
Chris@16
|
408
|
Chris@16
|
409 private:
|
Chris@16
|
410 //--------------------------------------------------------------------------
|
Chris@16
|
411 template<class Type, bool is_total_invertible>
|
Chris@16
|
412 struct on_invertible;
|
Chris@16
|
413
|
Chris@16
|
414 template<class Type>
|
Chris@16
|
415 struct on_invertible<Type, true>
|
Chris@16
|
416 {
|
Chris@16
|
417 typedef typename Type::element_type element_type;
|
Chris@16
|
418 typedef typename Type::inverse_codomain_combine inverse_codomain_combine;
|
Chris@16
|
419
|
Chris@16
|
420 static void subtract(Type& object, const element_type& operand)
|
Chris@16
|
421 { object.template _add<inverse_codomain_combine>(operand); }
|
Chris@16
|
422 };
|
Chris@16
|
423
|
Chris@16
|
424 template<class Type>
|
Chris@16
|
425 struct on_invertible<Type, false>
|
Chris@16
|
426 {
|
Chris@16
|
427 typedef typename Type::element_type element_type;
|
Chris@16
|
428 typedef typename Type::inverse_codomain_combine inverse_codomain_combine;
|
Chris@16
|
429
|
Chris@16
|
430 static void subtract(Type& object, const element_type& operand)
|
Chris@16
|
431 { object.template _subtract<inverse_codomain_combine>(operand); }
|
Chris@16
|
432 };
|
Chris@16
|
433
|
Chris@16
|
434 friend struct on_invertible<type, true>;
|
Chris@16
|
435 friend struct on_invertible<type, false>;
|
Chris@16
|
436 //--------------------------------------------------------------------------
|
Chris@16
|
437
|
Chris@16
|
438 //--------------------------------------------------------------------------
|
Chris@16
|
439 template<class Type, bool is_total>
|
Chris@16
|
440 struct on_definedness;
|
Chris@16
|
441
|
Chris@16
|
442 template<class Type>
|
Chris@16
|
443 struct on_definedness<Type, true>
|
Chris@16
|
444 {
|
Chris@16
|
445 static void add_intersection(Type& section, const Type& object,
|
Chris@16
|
446 const element_type& operand)
|
Chris@16
|
447 { object.total_add_intersection(section, operand); }
|
Chris@16
|
448 };
|
Chris@16
|
449
|
Chris@16
|
450 template<class Type>
|
Chris@16
|
451 struct on_definedness<Type, false>
|
Chris@16
|
452 {
|
Chris@16
|
453 static void add_intersection(Type& section, const Type& object,
|
Chris@16
|
454 const element_type& operand)
|
Chris@16
|
455 { object.partial_add_intersection(section, operand); }
|
Chris@16
|
456 };
|
Chris@16
|
457
|
Chris@16
|
458 friend struct on_definedness<type, true>;
|
Chris@16
|
459 friend struct on_definedness<type, false>;
|
Chris@16
|
460 //--------------------------------------------------------------------------
|
Chris@16
|
461
|
Chris@16
|
462 //--------------------------------------------------------------------------
|
Chris@16
|
463 template<class Type, bool has_set_semantics, bool absorbs_identities>
|
Chris@16
|
464 struct on_codomain_model;
|
Chris@16
|
465
|
Chris@16
|
466 template<class Type>
|
Chris@16
|
467 struct on_codomain_model<Type, false, false>
|
Chris@16
|
468 { // !codomain_is_set, !absorbs_identities
|
Chris@16
|
469 static void subtract(Type&, typename Type::iterator it_,
|
Chris@16
|
470 const typename Type::codomain_type& )
|
Chris@16
|
471 { (*it_).second = identity_element<typename Type::codomain_type>::value(); }
|
Chris@16
|
472 };
|
Chris@16
|
473
|
Chris@16
|
474 template<class Type>
|
Chris@16
|
475 struct on_codomain_model<Type, false, true>
|
Chris@16
|
476 { // !codomain_is_set, absorbs_identities
|
Chris@16
|
477 static void subtract(Type& object, typename Type::iterator it_,
|
Chris@16
|
478 const typename Type::codomain_type& )
|
Chris@16
|
479 { object.erase(it_); }
|
Chris@16
|
480 };
|
Chris@16
|
481
|
Chris@16
|
482 template<class Type>
|
Chris@16
|
483 struct on_codomain_model<Type, true, false>
|
Chris@16
|
484 { // !codomain_is_set, !absorbs_identities
|
Chris@16
|
485 typedef typename Type::inverse_codomain_intersect inverse_codomain_intersect;
|
Chris@16
|
486 static void subtract(Type&, typename Type::iterator it_,
|
Chris@16
|
487 const typename Type::codomain_type& co_value)
|
Chris@16
|
488 {
|
Chris@16
|
489 inverse_codomain_intersect()((*it_).second, co_value);
|
Chris@16
|
490 }
|
Chris@16
|
491 };
|
Chris@16
|
492
|
Chris@16
|
493 template<class Type>
|
Chris@16
|
494 struct on_codomain_model<Type, true, true>
|
Chris@16
|
495 { // !codomain_is_set, absorbs_identities
|
Chris@16
|
496 typedef typename Type::inverse_codomain_intersect inverse_codomain_intersect;
|
Chris@16
|
497 static void subtract(Type& object, typename Type::iterator it_,
|
Chris@16
|
498 const typename Type::codomain_type& co_value)
|
Chris@16
|
499 {
|
Chris@16
|
500 inverse_codomain_intersect()((*it_).second, co_value);
|
Chris@16
|
501 if((*it_).second == identity_element<codomain_type>::value())
|
Chris@16
|
502 object.erase(it_);
|
Chris@16
|
503 }
|
Chris@16
|
504 };
|
Chris@16
|
505 //--------------------------------------------------------------------------
|
Chris@16
|
506
|
Chris@16
|
507 //--------------------------------------------------------------------------
|
Chris@16
|
508 template<class Type, bool is_total, bool absorbs_identities>
|
Chris@16
|
509 struct on_total_absorbable;
|
Chris@16
|
510
|
Chris@16
|
511 template<class Type>
|
Chris@16
|
512 struct on_total_absorbable<Type, true, true>
|
Chris@16
|
513 {
|
Chris@16
|
514 typedef typename Type::element_type element_type;
|
Chris@16
|
515 static void flip(Type& object, const typename Type::element_type&)
|
Chris@16
|
516 { icl::clear(object); }
|
Chris@16
|
517 };
|
Chris@16
|
518
|
Chris@16
|
519 template<class Type>
|
Chris@16
|
520 struct on_total_absorbable<Type, true, false>
|
Chris@16
|
521 {
|
Chris@16
|
522 typedef typename Type::element_type element_type;
|
Chris@16
|
523 typedef typename Type::codomain_type codomain_type;
|
Chris@16
|
524
|
Chris@16
|
525 static void flip(Type& object, const element_type& operand)
|
Chris@16
|
526 {
|
Chris@16
|
527 object.add(operand);
|
Chris@16
|
528 ICL_FORALL(typename Type, it_, object)
|
Chris@16
|
529 (*it_).second = identity_element<codomain_type>::value();
|
Chris@16
|
530 }
|
Chris@16
|
531 };
|
Chris@16
|
532
|
Chris@16
|
533 template<class Type>
|
Chris@16
|
534 struct on_total_absorbable<Type, false, true>
|
Chris@16
|
535 { // !is_total, absorbs_identities
|
Chris@16
|
536 typedef typename Type::element_type element_type;
|
Chris@16
|
537 typedef typename Type::codomain_type codomain_type;
|
Chris@16
|
538 typedef typename Type::iterator iterator;
|
Chris@16
|
539 typedef typename Type::inverse_codomain_intersect inverse_codomain_intersect;
|
Chris@16
|
540
|
Chris@16
|
541 static void flip(Type& object, const element_type& operand)
|
Chris@16
|
542 {
|
Chris@16
|
543 std::pair<iterator,bool> insertion = object.insert(operand);
|
Chris@16
|
544 if(!insertion.second)
|
Chris@16
|
545 on_codomain_model<Type, has_set_semantics<codomain_type>::value, true>
|
Chris@16
|
546 ::subtract(object, insertion.first, operand.second);
|
Chris@16
|
547 }
|
Chris@16
|
548 };
|
Chris@16
|
549
|
Chris@16
|
550 template<class Type>
|
Chris@16
|
551 struct on_total_absorbable<Type, false, false>
|
Chris@16
|
552 { // !is_total !absorbs_identities
|
Chris@16
|
553 typedef typename Type::element_type element_type;
|
Chris@16
|
554 typedef typename Type::codomain_type codomain_type;
|
Chris@16
|
555 typedef typename Type::iterator iterator;
|
Chris@16
|
556 typedef typename Type::inverse_codomain_intersect inverse_codomain_intersect;
|
Chris@16
|
557
|
Chris@16
|
558 static void flip(Type& object, const element_type& operand)
|
Chris@16
|
559 {
|
Chris@16
|
560 std::pair<iterator,bool> insertion = object.insert(operand);
|
Chris@16
|
561 if(!insertion.second)
|
Chris@16
|
562 on_codomain_model<Type, has_set_semantics<codomain_type>::value, false>
|
Chris@16
|
563 ::subtract(object, insertion.first, operand.second);
|
Chris@16
|
564 }
|
Chris@16
|
565 };
|
Chris@16
|
566
|
Chris@16
|
567 friend struct on_total_absorbable<type, true, true >;
|
Chris@16
|
568 friend struct on_total_absorbable<type, false, true >;
|
Chris@16
|
569 friend struct on_total_absorbable<type, true, false>;
|
Chris@16
|
570 friend struct on_total_absorbable<type, false, false>;
|
Chris@16
|
571 //--------------------------------------------------------------------------
|
Chris@16
|
572 };
|
Chris@16
|
573
|
Chris@16
|
574
|
Chris@16
|
575
|
Chris@16
|
576 //==============================================================================
|
Chris@16
|
577 //= Addition<ElementMap>
|
Chris@16
|
578 //==============================================================================
|
Chris@16
|
579 template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_ALLOC Alloc>
|
Chris@16
|
580 template <class Combiner>
|
Chris@16
|
581 map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc>&
|
Chris@16
|
582 map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc>
|
Chris@16
|
583 ::_add(const element_type& addend)
|
Chris@16
|
584 {
|
Chris@16
|
585 typedef typename on_absorbtion
|
Chris@16
|
586 <type,Combiner,absorbs_identities<type>::value>::type on_absorbtion_;
|
Chris@16
|
587
|
Chris@16
|
588 const codomain_type& co_val = addend.second;
|
Chris@16
|
589 if(on_absorbtion_::is_absorbable(co_val))
|
Chris@16
|
590 return *this;
|
Chris@16
|
591
|
Chris@16
|
592 std::pair<iterator,bool> insertion
|
Chris@16
|
593 = base_type::insert(value_type(addend.first, version<Combiner>()(co_val)));
|
Chris@16
|
594
|
Chris@16
|
595 if(!insertion.second)
|
Chris@16
|
596 {
|
Chris@16
|
597 iterator it = insertion.first;
|
Chris@16
|
598 Combiner()((*it).second, co_val);
|
Chris@16
|
599
|
Chris@16
|
600 if(on_absorbtion_::is_absorbable((*it).second))
|
Chris@16
|
601 erase(it);
|
Chris@16
|
602 }
|
Chris@16
|
603 return *this;
|
Chris@16
|
604 }
|
Chris@16
|
605
|
Chris@16
|
606
|
Chris@16
|
607 template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_ALLOC Alloc>
|
Chris@16
|
608 template <class Combiner>
|
Chris@16
|
609 typename map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc>::iterator
|
Chris@16
|
610 map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc>
|
Chris@16
|
611 ::_add(iterator prior_, const value_type& addend)
|
Chris@16
|
612 {
|
Chris@16
|
613 typedef typename on_absorbtion
|
Chris@16
|
614 <type,Combiner,absorbs_identities<type>::value>::type on_absorbtion_;
|
Chris@16
|
615
|
Chris@16
|
616 const codomain_type& co_val = addend.second;
|
Chris@16
|
617 if(on_absorbtion_::is_absorbable(co_val))
|
Chris@16
|
618 return end();
|
Chris@16
|
619
|
Chris@16
|
620 iterator inserted_
|
Chris@16
|
621 = base_type::insert(prior_,
|
Chris@16
|
622 value_type(addend.first, Combiner::identity_element()));
|
Chris@16
|
623 Combiner()((*inserted_).second, addend.second);
|
Chris@16
|
624
|
Chris@16
|
625 if(on_absorbtion_::is_absorbable((*inserted_).second))
|
Chris@16
|
626 {
|
Chris@16
|
627 erase(inserted_);
|
Chris@16
|
628 return end();
|
Chris@16
|
629 }
|
Chris@16
|
630 else
|
Chris@16
|
631 return inserted_;
|
Chris@16
|
632 }
|
Chris@16
|
633
|
Chris@16
|
634
|
Chris@16
|
635 //==============================================================================
|
Chris@16
|
636 //= Subtraction<ElementMap>
|
Chris@16
|
637 //==============================================================================
|
Chris@16
|
638 template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_ALLOC Alloc>
|
Chris@16
|
639 template <class Combiner>
|
Chris@16
|
640 map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc>&
|
Chris@16
|
641 map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc>::_subtract(const value_type& minuend)
|
Chris@16
|
642 {
|
Chris@16
|
643 typedef typename on_absorbtion
|
Chris@16
|
644 <type,Combiner,absorbs_identities<type>::value>::type on_absorbtion_;
|
Chris@16
|
645
|
Chris@16
|
646 iterator it_ = find(minuend.first);
|
Chris@16
|
647 if(it_ != end())
|
Chris@16
|
648 {
|
Chris@16
|
649 Combiner()((*it_).second, minuend.second);
|
Chris@16
|
650 if(on_absorbtion_::is_absorbable((*it_).second))
|
Chris@16
|
651 erase(it_);
|
Chris@16
|
652 }
|
Chris@16
|
653 return *this;
|
Chris@16
|
654 }
|
Chris@16
|
655
|
Chris@16
|
656
|
Chris@16
|
657 //-----------------------------------------------------------------------------
|
Chris@16
|
658 // type traits
|
Chris@16
|
659 //-----------------------------------------------------------------------------
|
Chris@16
|
660 template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_ALLOC Alloc>
|
Chris@16
|
661 struct is_map<icl::map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc> >
|
Chris@16
|
662 {
|
Chris@16
|
663 typedef is_map<icl::map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc> > type;
|
Chris@16
|
664 BOOST_STATIC_CONSTANT(bool, value = true);
|
Chris@16
|
665 };
|
Chris@16
|
666
|
Chris@16
|
667 template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_ALLOC Alloc>
|
Chris@16
|
668 struct has_inverse<icl::map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc> >
|
Chris@16
|
669 {
|
Chris@16
|
670 typedef has_inverse<icl::map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc> > type;
|
Chris@16
|
671 BOOST_STATIC_CONSTANT(bool, value = (has_inverse<CodomainT>::value));
|
Chris@16
|
672 };
|
Chris@16
|
673
|
Chris@16
|
674 template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_ALLOC Alloc>
|
Chris@16
|
675 struct absorbs_identities<icl::map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc> >
|
Chris@16
|
676 {
|
Chris@16
|
677 typedef absorbs_identities type;
|
Chris@16
|
678 BOOST_STATIC_CONSTANT(int, value = Traits::absorbs_identities);
|
Chris@16
|
679 };
|
Chris@16
|
680
|
Chris@16
|
681 template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_ALLOC Alloc>
|
Chris@16
|
682 struct is_total<icl::map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc> >
|
Chris@16
|
683 {
|
Chris@16
|
684 typedef is_total type;
|
Chris@16
|
685 BOOST_STATIC_CONSTANT(int, value = Traits::is_total);
|
Chris@16
|
686 };
|
Chris@16
|
687
|
Chris@16
|
688 template <class DomainT, class CodomainT, class Traits, ICL_COMPARE Compare, ICL_COMBINE Combine, ICL_SECTION Section, ICL_ALLOC Alloc>
|
Chris@16
|
689 struct type_to_string<icl::map<DomainT,CodomainT,Traits,Compare,Combine,Section,Alloc> >
|
Chris@16
|
690 {
|
Chris@16
|
691 static std::string apply()
|
Chris@16
|
692 {
|
Chris@16
|
693 return "map<"+ type_to_string<DomainT>::apply() + ","
|
Chris@16
|
694 + type_to_string<CodomainT>::apply() + ","
|
Chris@16
|
695 + type_to_string<Traits>::apply() +">";
|
Chris@16
|
696 }
|
Chris@16
|
697 };
|
Chris@16
|
698
|
Chris@16
|
699
|
Chris@16
|
700
|
Chris@16
|
701 }} // namespace icl boost
|
Chris@16
|
702
|
Chris@16
|
703 #endif // BOOST_ICL_MAP_HPP_JOFA_070519
|
Chris@16
|
704
|