42 /// Base class of maps. |
42 /// Base class of maps. |
43 /// It provides the necessary <tt>typedef</tt>s required by the map concept. |
43 /// It provides the necessary <tt>typedef</tt>s required by the map concept. |
44 template<typename K, typename T> |
44 template<typename K, typename T> |
45 class MapBase { |
45 class MapBase { |
46 public: |
46 public: |
47 ///\e |
47 /// The key type of the map. |
48 typedef K Key; |
48 typedef K Key; |
49 ///\e |
49 /// The value type of the map. (The type of objects associated with the keys). |
50 typedef T Value; |
50 typedef T Value; |
51 }; |
51 }; |
52 |
52 |
53 /// Null map. (a.k.a. DoNothingMap) |
53 /// Null map. (a.k.a. DoNothingMap) |
54 |
54 |
55 /// If you have to provide a map only for its type definitions, |
55 /// This map can be used if you have to provide a map only for |
56 /// or if you have to provide a writable map, but |
56 /// its type definitions, or if you have to provide a writable map, |
57 /// data written to it will sent to <tt>/dev/null</tt>... |
57 /// but data written to it is not required (i.e. it will be sent to |
|
58 /// <tt>/dev/null</tt>). |
58 template<typename K, typename T> |
59 template<typename K, typename T> |
59 class NullMap : public MapBase<K, T> { |
60 class NullMap : public MapBase<K, T> { |
60 public: |
61 public: |
61 typedef MapBase<K, T> Parent; |
62 typedef MapBase<K, T> Parent; |
62 typedef typename Parent::Key Key; |
63 typedef typename Parent::Key Key; |
66 T operator[](const K&) const { return T(); } |
67 T operator[](const K&) const { return T(); } |
67 /// Absorbs the value. |
68 /// Absorbs the value. |
68 void set(const K&, const T&) {} |
69 void set(const K&, const T&) {} |
69 }; |
70 }; |
70 |
71 |
|
72 ///Returns a \c NullMap class |
|
73 |
|
74 ///This function just returns a \c NullMap class. |
|
75 ///\relates NullMap |
71 template <typename K, typename V> |
76 template <typename K, typename V> |
72 NullMap<K, V> nullMap() { |
77 NullMap<K, V> nullMap() { |
73 return NullMap<K, V>(); |
78 return NullMap<K, V>(); |
74 } |
79 } |
75 |
80 |
76 |
81 |
77 /// Constant map. |
82 /// Constant map. |
78 |
83 |
79 /// This is a readable map which assigns a specified value to each key. |
84 /// This is a \ref concepts::ReadMap "readable" map which assigns a |
80 /// In other aspects it is equivalent to the \c NullMap. |
85 /// specified value to each key. |
|
86 /// In other aspects it is equivalent to \c NullMap. |
81 template<typename K, typename T> |
87 template<typename K, typename T> |
82 class ConstMap : public MapBase<K, T> { |
88 class ConstMap : public MapBase<K, T> { |
83 private: |
89 private: |
84 T v; |
90 T v; |
85 public: |
91 public: |
145 V operator[](const K&) const { return v; } |
154 V operator[](const K&) const { return v; } |
146 ///\e |
155 ///\e |
147 void set(const K&, const V&) { } |
156 void set(const K&, const V&) { } |
148 }; |
157 }; |
149 |
158 |
150 ///Returns a \c ConstMap class |
159 ///Returns a \c ConstMap class with inlined value |
151 |
160 |
152 ///This function just returns a \c ConstMap class with inlined value. |
161 ///This function just returns a \c ConstMap class with inlined value. |
153 ///\relates ConstMap |
162 ///\relates ConstMap |
154 template<typename K, typename V, V v> |
163 template<typename K, typename V, V v> |
155 inline ConstMap<K, Const<V, v> > constMap() { |
164 inline ConstMap<K, Const<V, v> > constMap() { |
156 return ConstMap<K, Const<V, v> >(); |
165 return ConstMap<K, Const<V, v> >(); |
157 } |
166 } |
158 |
167 |
159 ///Map based on std::map |
168 ///Map based on \c std::map |
160 |
169 |
161 ///This is essentially a wrapper for \c std::map. With addition that |
170 ///This is essentially a wrapper for \c std::map with addition that |
162 ///you can specify a default value different from \c Value() . |
171 ///you can specify a default value different from \c Value() . |
|
172 ///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept. |
163 template <typename K, typename T, typename Compare = std::less<K> > |
173 template <typename K, typename T, typename Compare = std::less<K> > |
164 class StdMap { |
174 class StdMap : public MapBase<K, T> { |
165 template <typename K1, typename T1, typename C1> |
175 template <typename K1, typename T1, typename C1> |
166 friend class StdMap; |
176 friend class StdMap; |
167 public: |
177 public: |
168 |
178 |
|
179 typedef MapBase<K, T> Parent; |
|
180 ///Key type |
|
181 typedef typename Parent::Key Key; |
|
182 ///Value type |
|
183 typedef typename Parent::Value Value; |
|
184 ///Reference Type |
|
185 typedef T& Reference; |
|
186 ///Const reference type |
|
187 typedef const T& ConstReference; |
|
188 |
169 typedef True ReferenceMapTag; |
189 typedef True ReferenceMapTag; |
170 ///\e |
|
171 typedef K Key; |
|
172 ///\e |
|
173 typedef T Value; |
|
174 ///\e |
|
175 typedef T& Reference; |
|
176 ///\e |
|
177 typedef const T& ConstReference; |
|
178 |
190 |
179 private: |
191 private: |
180 |
192 |
181 typedef std::map<K, T, Compare> Map; |
193 typedef std::map<K, T, Compare> Map; |
182 Value _value; |
194 Value _value; |
184 |
196 |
185 public: |
197 public: |
186 |
198 |
187 /// Constructor with specified default value |
199 /// Constructor with specified default value |
188 StdMap(const T& value = T()) : _value(value) {} |
200 StdMap(const T& value = T()) : _value(value) {} |
189 /// \brief Constructs the map from an appropriate std::map, and explicitly |
201 /// \brief Constructs the map from an appropriate \c std::map, and |
190 /// specifies a default value. |
202 /// explicitly specifies a default value. |
191 template <typename T1, typename Comp1> |
203 template <typename T1, typename Comp1> |
192 StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) |
204 StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) |
193 : _map(map.begin(), map.end()), _value(value) {} |
205 : _map(map.begin(), map.end()), _value(value) {} |
194 |
206 |
195 /// \brief Constructs a map from an other StdMap. |
207 /// \brief Constructs a map from an other \ref StdMap. |
196 template<typename T1, typename Comp1> |
208 template<typename T1, typename Comp1> |
197 StdMap(const StdMap<Key, T1, Comp1> &c) |
209 StdMap(const StdMap<Key, T1, Comp1> &c) |
198 : _map(c._map.begin(), c._map.end()), _value(c._value) {} |
210 : _map(c._map.begin(), c._map.end()), _value(c._value) {} |
199 |
211 |
200 private: |
212 private: |
240 struct rebind { |
252 struct rebind { |
241 typedef StdMap<Key, T1, C1> other; |
253 typedef StdMap<Key, T1, C1> other; |
242 }; |
254 }; |
243 }; |
255 }; |
244 |
256 |
245 /// \brief Map for storing values for the range \c [0..size-1] range keys |
257 ///Returns a \c StdMap class |
246 /// |
258 |
247 /// The current map has the \c [0..size-1] keyset and the values |
259 ///This function just returns a \c StdMap class with specified |
|
260 ///default value. |
|
261 ///\relates StdMap |
|
262 template<typename K, typename V, typename Compare> |
|
263 inline StdMap<K, V, Compare> stdMap(const V& value = V()) { |
|
264 return StdMap<K, V, Compare>(value); |
|
265 } |
|
266 |
|
267 template<typename K, typename V> |
|
268 inline StdMap<K, V, std::less<K> > stdMap(const V& value = V()) { |
|
269 return StdMap<K, V, std::less<K> >(value); |
|
270 } |
|
271 |
|
272 ///Returns a \c StdMap class created from an appropriate \c std::map |
|
273 |
|
274 ///This function just returns a \c StdMap class created from an |
|
275 ///appropriate \c std::map. |
|
276 ///\relates StdMap |
|
277 template<typename K, typename V, typename Compare> |
|
278 inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map, |
|
279 const V& value = V() ) { |
|
280 return StdMap<K, V, Compare>(map, value); |
|
281 } |
|
282 |
|
283 /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt> |
|
284 /// |
|
285 /// This map has the <tt>[0..size-1]</tt> keyset and the values |
248 /// are stored in a \c std::vector<T> container. It can be used with |
286 /// are stored in a \c std::vector<T> container. It can be used with |
249 /// some data structures, for example \c UnionFind, \c BinHeap, when |
287 /// some data structures, for example \c UnionFind, \c BinHeap, when |
250 /// the used items are small integer numbers. |
288 /// the used items are small integer numbers. |
251 template <typename T> |
289 template <typename T> |
252 class IntegerMap { |
290 class IntegerMap : public MapBase<int, T> { |
253 |
291 |
254 template <typename T1> |
292 template <typename T1> |
255 friend class IntegerMap; |
293 friend class IntegerMap; |
256 |
294 |
257 public: |
295 public: |
258 |
296 |
|
297 typedef MapBase<int, T> Parent; |
|
298 ///\e |
|
299 typedef typename Parent::Key Key; |
|
300 ///\e |
|
301 typedef typename Parent::Value Value; |
|
302 ///\e |
|
303 typedef T& Reference; |
|
304 ///\e |
|
305 typedef const T& ConstReference; |
|
306 |
259 typedef True ReferenceMapTag; |
307 typedef True ReferenceMapTag; |
260 ///\e |
|
261 typedef int Key; |
|
262 ///\e |
|
263 typedef T Value; |
|
264 ///\e |
|
265 typedef T& Reference; |
|
266 ///\e |
|
267 typedef const T& ConstReference; |
|
268 |
308 |
269 private: |
309 private: |
270 |
310 |
271 typedef std::vector<T> Vector; |
311 typedef std::vector<T> Vector; |
272 Vector _vector; |
312 Vector _vector; |
274 public: |
314 public: |
275 |
315 |
276 /// Constructor with specified default value |
316 /// Constructor with specified default value |
277 IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {} |
317 IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {} |
278 |
318 |
279 /// \brief Constructs the map from an appropriate std::vector. |
319 /// \brief Constructs the map from an appropriate \c std::vector. |
280 template <typename T1> |
320 template <typename T1> |
281 IntegerMap(const std::vector<T1>& vector) |
321 IntegerMap(const std::vector<T1>& vector) |
282 : _vector(vector.begin(), vector.end()) {} |
322 : _vector(vector.begin(), vector.end()) {} |
283 |
323 |
284 /// \brief Constructs a map from an other IntegerMap. |
324 /// \brief Constructs a map from an other \ref IntegerMap. |
285 template <typename T1> |
325 template <typename T1> |
286 IntegerMap(const IntegerMap<T1> &c) |
326 IntegerMap(const IntegerMap<T1> &c) |
287 : _vector(c._vector.begin(), c._vector.end()) {} |
327 : _vector(c._vector.begin(), c._vector.end()) {} |
288 |
328 |
289 /// \brief Resize the container |
329 /// \brief Resize the container |
363 |
413 |
364 ///Constructor |
414 ///Constructor |
365 ///\param _m is the underlying map |
415 ///\param _m is the underlying map |
366 ConvertMap(const M &_m) : m(_m) {}; |
416 ConvertMap(const M &_m) : m(_m) {}; |
367 |
417 |
368 /// \brief The subscript operator. |
418 ///\e |
369 /// |
|
370 /// The subscript operator. |
|
371 /// \param k The key |
|
372 /// \return The target of the edge |
|
373 Value operator[](const Key& k) const {return m[k];} |
419 Value operator[](const Key& k) const {return m[k];} |
374 }; |
420 }; |
375 |
421 |
376 ///Returns an \c ConvertMap class |
422 ///Returns a \c ConvertMap class |
377 |
423 |
378 ///This function just returns an \c ConvertMap class. |
424 ///This function just returns a \c ConvertMap class. |
379 ///\relates ConvertMap |
425 ///\relates ConvertMap |
380 template<typename T, typename M> |
426 template<typename T, typename M> |
381 inline ConvertMap<M, T> convertMap(const M &m) { |
427 inline ConvertMap<M, T> convertMap(const M &m) { |
382 return ConvertMap<M, T>(m); |
428 return ConvertMap<M, T>(m); |
383 } |
429 } |
384 |
430 |
385 ///Simple wrapping of the map |
431 ///Simple wrapping of a map |
386 |
432 |
387 ///This \c concepts::ReadMap "read only map" returns the simple |
433 ///This \ref concepts::ReadMap "read only map" returns the simple |
388 ///wrapping of the given map. Sometimes the reference maps cannot be |
434 ///wrapping of the given map. Sometimes the reference maps cannot be |
389 ///combined with simple read maps. This map adaptor wraps the given |
435 ///combined with simple read maps. This map adaptor wraps the given |
390 ///map to simple read map. |
436 ///map to simple read map. |
|
437 /// |
|
438 ///\sa SimpleWriteMap |
|
439 /// |
|
440 /// \todo Revise the misleading name |
391 template<typename M> |
441 template<typename M> |
392 class SimpleMap : public MapBase<typename M::Key, typename M::Value> { |
442 class SimpleMap : public MapBase<typename M::Key, typename M::Value> { |
393 const M& m; |
443 const M& m; |
394 |
444 |
395 public: |
445 public: |
459 return AddMap<M1, M2>(m1,m2); |
531 return AddMap<M1, M2>(m1,m2); |
460 } |
532 } |
461 |
533 |
462 ///Shift a map with a constant. |
534 ///Shift a map with a constant. |
463 |
535 |
464 ///This \c concepts::ReadMap "read only map" returns the sum of the |
536 ///This \ref concepts::ReadMap "read only map" returns the sum of the |
465 ///given map and a constant value. |
537 ///given map and a constant value. |
466 ///Its \c Key and \c Value is inherited from \c M. |
538 ///Its \c Key and \c Value is inherited from \c M. |
467 /// |
539 /// |
468 ///Actually, |
540 ///Actually, |
469 ///\code |
541 ///\code |
470 /// ShiftMap<X> sh(x,v); |
542 /// ShiftMap<X> sh(x,v); |
471 ///\endcode |
543 ///\endcode |
472 ///is equivalent with |
544 ///is equivalent to |
473 ///\code |
545 ///\code |
474 /// ConstMap<X::Key, X::Value> c_tmp(v); |
546 /// ConstMap<X::Key, X::Value> c_tmp(v); |
475 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
547 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
476 ///\endcode |
548 ///\endcode |
|
549 /// |
|
550 ///\sa ShiftWriteMap |
477 template<typename M, typename C = typename M::Value> |
551 template<typename M, typename C = typename M::Value> |
478 class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
552 class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
479 const M& m; |
553 const M& m; |
480 C v; |
554 C v; |
481 public: |
555 public: |
491 ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
565 ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; |
492 ///\e |
566 ///\e |
493 Value operator[](Key k) const {return m[k] + v;} |
567 Value operator[](Key k) const {return m[k] + v;} |
494 }; |
568 }; |
495 |
569 |
496 ///Shift a map with a constant. |
570 ///Shift a map with a constant (ReadWrite version). |
497 |
571 |
498 ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the |
572 ///This \ref concepts::ReadWriteMap "read-write map" returns the sum of the |
499 ///given map and a constant value. It makes also possible to write the map. |
573 ///given map and a constant value. It makes also possible to write the map. |
500 ///Its \c Key and \c Value is inherited from \c M. |
574 ///Its \c Key and \c Value are inherited from \c M. |
501 /// |
575 /// |
502 ///Actually, |
576 ///\sa ShiftMap |
503 ///\code |
|
504 /// ShiftMap<X> sh(x,v); |
|
505 ///\endcode |
|
506 ///is equivalent with |
|
507 ///\code |
|
508 /// ConstMap<X::Key, X::Value> c_tmp(v); |
|
509 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
|
510 ///\endcode |
|
511 template<typename M, typename C = typename M::Value> |
577 template<typename M, typename C = typename M::Value> |
512 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
578 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
513 M& m; |
579 M& m; |
514 C v; |
580 C v; |
515 public: |
581 public: |
527 Value operator[](Key k) const {return m[k] + v;} |
593 Value operator[](Key k) const {return m[k] + v;} |
528 /// \e |
594 /// \e |
529 void set(Key k, const Value& c) { m.set(k, c - v); } |
595 void set(Key k, const Value& c) { m.set(k, c - v); } |
530 }; |
596 }; |
531 |
597 |
532 ///Returns an \c ShiftMap class |
598 ///Returns a \c ShiftMap class |
533 |
599 |
534 ///This function just returns an \c ShiftMap class. |
600 ///This function just returns an \c ShiftMap class. |
535 ///\relates ShiftMap |
601 ///\relates ShiftMap |
536 template<typename M, typename C> |
602 template<typename M, typename C> |
537 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) { |
603 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) { |
538 return ShiftMap<M, C>(m,v); |
604 return ShiftMap<M, C>(m,v); |
539 } |
605 } |
540 |
606 |
|
607 ///Returns a \c ShiftWriteMap class |
|
608 |
|
609 ///This function just returns a \c ShiftWriteMap class. |
|
610 ///\relates ShiftWriteMap |
541 template<typename M, typename C> |
611 template<typename M, typename C> |
542 inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) { |
612 inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) { |
543 return ShiftWriteMap<M, C>(m,v); |
613 return ShiftWriteMap<M, C>(m,v); |
544 } |
614 } |
545 |
615 |
546 ///Difference of two maps |
616 ///Difference of two maps |
547 |
617 |
548 ///This \c concepts::ReadMap "read only map" returns the difference |
618 ///This \ref concepts::ReadMap "read only map" returns the difference |
549 ///of the values of the two |
619 ///of the values of the two given maps. |
550 ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
620 ///Its \c Key and \c Value are inherited from \c M1. |
551 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
621 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
552 |
622 |
553 template<typename M1, typename M2> |
623 template<typename M1, typename M2> |
554 class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
624 class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
555 const M1& m1; |
625 const M1& m1; |
605 template<typename M1, typename M2> |
673 template<typename M1, typename M2> |
606 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) { |
674 inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) { |
607 return MulMap<M1, M2>(m1,m2); |
675 return MulMap<M1, M2>(m1,m2); |
608 } |
676 } |
609 |
677 |
610 ///Scales a maps with a constant. |
678 ///Scales a map with a constant. |
611 |
679 |
612 ///This \c concepts::ReadMap "read only map" returns the value of the |
680 ///This \ref concepts::ReadMap "read only map" returns the value of the |
613 ///given map multiplied from the left side with a constant value. |
681 ///given map multiplied from the left side with a constant value. |
614 ///Its \c Key and \c Value is inherited from \c M. |
682 ///Its \c Key and \c Value are inherited from \c M. |
615 /// |
683 /// |
616 ///Actually, |
684 ///Actually, |
617 ///\code |
685 ///\code |
618 /// ScaleMap<X> sc(x,v); |
686 /// ScaleMap<X> sc(x,v); |
619 ///\endcode |
687 ///\endcode |
620 ///is equivalent with |
688 ///is equivalent to |
621 ///\code |
689 ///\code |
622 /// ConstMap<X::Key, X::Value> c_tmp(v); |
690 /// ConstMap<X::Key, X::Value> c_tmp(v); |
623 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
691 /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
624 ///\endcode |
692 ///\endcode |
|
693 /// |
|
694 ///\sa ScaleWriteMap |
625 template<typename M, typename C = typename M::Value> |
695 template<typename M, typename C = typename M::Value> |
626 class ScaleMap : public MapBase<typename M::Key, typename M::Value> { |
696 class ScaleMap : public MapBase<typename M::Key, typename M::Value> { |
627 const M& m; |
697 const M& m; |
628 C v; |
698 C v; |
629 public: |
699 public: |
666 Value operator[](Key k) const {return v * m[k];} |
739 Value operator[](Key k) const {return v * m[k];} |
667 /// \e |
740 /// \e |
668 void set(Key k, const Value& c) { m.set(k, c / v);} |
741 void set(Key k, const Value& c) { m.set(k, c / v);} |
669 }; |
742 }; |
670 |
743 |
671 ///Returns an \c ScaleMap class |
744 ///Returns a \c ScaleMap class |
672 |
745 |
673 ///This function just returns an \c ScaleMap class. |
746 ///This function just returns a \c ScaleMap class. |
674 ///\relates ScaleMap |
747 ///\relates ScaleMap |
675 template<typename M, typename C> |
748 template<typename M, typename C> |
676 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) { |
749 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) { |
677 return ScaleMap<M, C>(m,v); |
750 return ScaleMap<M, C>(m,v); |
678 } |
751 } |
679 |
752 |
|
753 ///Returns a \c ScaleWriteMap class |
|
754 |
|
755 ///This function just returns a \c ScaleWriteMap class. |
|
756 ///\relates ScaleWriteMap |
680 template<typename M, typename C> |
757 template<typename M, typename C> |
681 inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) { |
758 inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) { |
682 return ScaleWriteMap<M, C>(m,v); |
759 return ScaleWriteMap<M, C>(m,v); |
683 } |
760 } |
684 |
761 |
685 ///Quotient of two maps |
762 ///Quotient of two maps |
686 |
763 |
687 ///This \c concepts::ReadMap "read only map" returns the quotient of the |
764 ///This \ref concepts::ReadMap "read only map" returns the quotient of the |
688 ///values of the two |
765 ///values of the two given maps. |
689 ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
766 ///Its \c Key and \c Value are inherited from \c M1. |
690 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
767 ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
691 |
|
692 template<typename M1, typename M2> |
768 template<typename M1, typename M2> |
693 class DivMap : public MapBase<typename M1::Key, typename M1::Value> { |
769 class DivMap : public MapBase<typename M1::Key, typename M1::Value> { |
694 const M1& m1; |
770 const M1& m1; |
695 const M2& m2; |
771 const M2& m2; |
696 public: |
772 public: |
713 return DivMap<M1, M2>(m1,m2); |
789 return DivMap<M1, M2>(m1,m2); |
714 } |
790 } |
715 |
791 |
716 ///Composition of two maps |
792 ///Composition of two maps |
717 |
793 |
718 ///This \c concepts::ReadMap "read only map" returns the composition of |
794 ///This \ref concepts::ReadMap "read only map" returns the composition of |
719 ///two |
795 ///two given maps. |
720 ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is |
796 ///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2, |
721 ///of \c M2, |
|
722 ///then for |
797 ///then for |
723 ///\code |
798 ///\code |
724 /// ComposeMap<M1, M2> cm(m1,m2); |
799 /// ComposeMap<M1, M2> cm(m1,m2); |
725 ///\endcode |
800 ///\endcode |
726 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt> |
801 /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
727 /// |
802 /// |
728 ///Its \c Key is inherited from \c M2 and its \c Value is from |
803 ///Its \c Key is inherited from \c M2 and its \c Value is from \c M1. |
729 ///\c M1. |
804 ///\c M2::Value must be convertible to \c M1::Key. |
730 ///The \c M2::Value must be convertible to \c M1::Key. |
805 /// |
|
806 ///\sa CombineMap |
|
807 /// |
731 ///\todo Check the requirements. |
808 ///\todo Check the requirements. |
732 template <typename M1, typename M2> |
809 template <typename M1, typename M2> |
733 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
810 class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
734 const M1& m1; |
811 const M1& m1; |
735 const M2& m2; |
812 const M2& m2; |
753 template <typename M1, typename M2> |
830 template <typename M1, typename M2> |
754 inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) { |
831 inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) { |
755 return ComposeMap<M1, M2>(m1,m2); |
832 return ComposeMap<M1, M2>(m1,m2); |
756 } |
833 } |
757 |
834 |
758 ///Combines of two maps using an STL (binary) functor. |
835 ///Combine of two maps using an STL (binary) functor. |
759 |
836 |
760 ///Combines of two maps using an STL (binary) functor. |
837 ///Combine of two maps using an STL (binary) functor. |
761 /// |
838 /// |
762 /// |
839 ///This \ref concepts::ReadMap "read only map" takes two maps and a |
763 ///This \c concepts::ReadMap "read only map" takes two maps and a |
840 ///binary functor and returns the composition of the two |
764 ///binary functor and returns the composition of |
|
765 ///the two |
|
766 ///given maps unsing the functor. |
841 ///given maps unsing the functor. |
767 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
842 ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
768 ///and \c f is of \c F, |
843 ///and \c f is of \c F, then for |
769 ///then for |
|
770 ///\code |
844 ///\code |
771 /// CombineMap<M1, M2,F,V> cm(m1,m2,f); |
845 /// CombineMap<M1, M2,F,V> cm(m1,m2,f); |
772 ///\endcode |
846 ///\endcode |
773 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
847 /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
774 /// |
848 /// |
775 ///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
849 ///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
776 ///The \c M2::Value and \c M1::Value must be convertible to the corresponding |
850 ///\c M2::Value and \c M1::Value must be convertible to the corresponding |
777 ///input parameter of \c F and the return type of \c F must be convertible |
851 ///input parameter of \c F and the return type of \c F must be convertible |
778 ///to \c V. |
852 ///to \c V. |
|
853 /// |
|
854 ///\sa ComposeMap |
|
855 /// |
779 ///\todo Check the requirements. |
856 ///\todo Check the requirements. |
780 template<typename M1, typename M2, typename F, |
857 template<typename M1, typename M2, typename F, |
781 typename V = typename F::result_type> |
858 typename V = typename F::result_type> |
782 class CombineMap : public MapBase<typename M1::Key, V> { |
859 class CombineMap : public MapBase<typename M1::Key, V> { |
783 const M1& m1; |
860 const M1& m1; |
799 |
876 |
800 ///This function just returns a \c CombineMap class. |
877 ///This function just returns a \c CombineMap class. |
801 /// |
878 /// |
802 ///For example if \c m1 and \c m2 are both \c double valued maps, then |
879 ///For example if \c m1 and \c m2 are both \c double valued maps, then |
803 ///\code |
880 ///\code |
804 ///combineMap<double>(m1,m2,std::plus<double>()) |
881 ///combineMap(m1,m2,std::plus<double>()) |
805 ///\endcode |
882 ///\endcode |
806 ///is equivalent with |
883 ///is equivalent to |
807 ///\code |
884 ///\code |
808 ///addMap(m1,m2) |
885 ///addMap(m1,m2) |
809 ///\endcode |
886 ///\endcode |
810 /// |
887 /// |
811 ///This function is specialized for adaptable binary function |
888 ///This function is specialized for adaptable binary function |
812 ///classes and c++ functions. |
889 ///classes and C++ functions. |
813 /// |
890 /// |
814 ///\relates CombineMap |
891 ///\relates CombineMap |
815 template<typename M1, typename M2, typename F, typename V> |
892 template<typename M1, typename M2, typename F, typename V> |
816 inline CombineMap<M1, M2, F, V> |
893 inline CombineMap<M1, M2, F, V> |
817 combineMap(const M1& m1,const M2& m2, const F& f) { |
894 combineMap(const M1& m1,const M2& m2, const F& f) { |
882 template <typename M> |
960 template <typename M> |
883 inline NegMap<M> negMap(const M &m) { |
961 inline NegMap<M> negMap(const M &m) { |
884 return NegMap<M>(m); |
962 return NegMap<M>(m); |
885 } |
963 } |
886 |
964 |
|
965 ///Returns a \c NegWriteMap class |
|
966 |
|
967 ///This function just returns a \c NegWriteMap class. |
|
968 ///\relates NegWriteMap |
887 template <typename M> |
969 template <typename M> |
888 inline NegWriteMap<M> negMap(M &m) { |
970 inline NegWriteMap<M> negMap(M &m) { |
889 return NegWriteMap<M>(m); |
971 return NegWriteMap<M>(m); |
890 } |
972 } |
891 |
973 |
892 ///Absolute value of a map |
974 ///Absolute value of a map |
893 |
975 |
894 ///This \c concepts::ReadMap "read only map" returns the absolute value |
976 ///This \ref concepts::ReadMap "read only map" returns the absolute value |
895 ///of the |
977 ///of the value returned by the given map. |
896 ///value returned by the |
978 ///Its \c Key and \c Value are inherited from \c M. |
897 ///given map. Its \c Key and \c Value will be inherited |
979 ///\c Value must be comparable to \c 0 and the unary \c - |
898 ///from <tt>M</tt>. <tt>Value</tt> |
|
899 ///must be comparable to <tt>0</tt> and the unary <tt>-</tt> |
|
900 ///operator must be defined for it, of course. |
980 ///operator must be defined for it, of course. |
901 /// |
981 /// |
902 ///\bug We need a unified way to handle the situation below: |
982 ///\bug We need a unified way to handle the situation below: |
903 ///\code |
983 ///\code |
904 /// struct _UnConvertible {}; |
984 /// struct _UnConvertible {}; |
928 return tmp >= 0 ? tmp : -tmp; |
1008 return tmp >= 0 ? tmp : -tmp; |
929 } |
1009 } |
930 |
1010 |
931 }; |
1011 }; |
932 |
1012 |
933 ///Returns a \c AbsMap class |
1013 ///Returns an \c AbsMap class |
934 |
1014 |
935 ///This function just returns a \c AbsMap class. |
1015 ///This function just returns an \c AbsMap class. |
936 ///\relates AbsMap |
1016 ///\relates AbsMap |
937 template<typename M> |
1017 template<typename M> |
938 inline AbsMap<M> absMap(const M &m) { |
1018 inline AbsMap<M> absMap(const M &m) { |
939 return AbsMap<M>(m); |
1019 return AbsMap<M>(m); |
940 } |
1020 } |
941 |
1021 |
942 ///Converts an STL style functor to a map |
1022 ///Converts an STL style functor to a map |
943 |
1023 |
944 ///This \c concepts::ReadMap "read only map" returns the value |
1024 ///This \ref concepts::ReadMap "read only map" returns the value |
945 ///of a |
1025 ///of a given functor. |
946 ///given map. |
|
947 /// |
1026 /// |
948 ///Template parameters \c K and \c V will become its |
1027 ///Template parameters \c K and \c V will become its |
949 ///\c Key and \c Value. They must be given explicitely |
1028 ///\c Key and \c Value. |
950 ///because a functor does not provide such typedefs. |
1029 ///In most cases they have to be given explicitly because a |
|
1030 ///functor typically does not provide \c argument_type and |
|
1031 ///\c result_type typedefs. |
951 /// |
1032 /// |
952 ///Parameter \c F is the type of the used functor. |
1033 ///Parameter \c F is the type of the used functor. |
|
1034 /// |
|
1035 ///\sa MapFunctor |
953 template<typename F, |
1036 template<typename F, |
954 typename K = typename F::argument_type, |
1037 typename K = typename F::argument_type, |
955 typename V = typename F::result_type> |
1038 typename V = typename F::result_type> |
956 class FunctorMap : public MapBase<K, V> { |
1039 class FunctorMap : public MapBase<K, V> { |
957 F f; |
1040 F f; |
1076 Value operator[](Key k) const {return m1[k];} |
1167 Value operator[](Key k) const {return m1[k];} |
1077 ///\e |
1168 ///\e |
1078 void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} |
1169 void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} |
1079 }; |
1170 }; |
1080 |
1171 |
1081 ///Returns an \c ForkMap class |
1172 ///Returns a \c ForkMap class |
1082 |
1173 |
1083 ///This function just returns an \c ForkMap class. |
1174 ///This function just returns a \c ForkMap class. |
1084 /// |
|
1085 ///\relates ForkMap |
1175 ///\relates ForkMap |
1086 template <typename M1, typename M2> |
1176 template <typename M1, typename M2> |
1087 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) { |
1177 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) { |
1088 return ForkMap<M1, M2>(m1,m2); |
1178 return ForkMap<M1, M2>(m1,m2); |
1089 } |
1179 } |
1090 |
1180 |
|
1181 ///Returns a \c ForkWriteMap class |
|
1182 |
|
1183 ///This function just returns a \c ForkWriteMap class. |
|
1184 ///\relates ForkWriteMap |
1091 template <typename M1, typename M2> |
1185 template <typename M1, typename M2> |
1092 inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) { |
1186 inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) { |
1093 return ForkWriteMap<M1, M2>(m1,m2); |
1187 return ForkWriteMap<M1, M2>(m1,m2); |
1094 } |
1188 } |
1095 |
1189 |
1241 Iterator _begin; |
1345 Iterator _begin; |
1242 mutable Iterator _end; |
1346 mutable Iterator _end; |
1243 Functor _functor; |
1347 Functor _functor; |
1244 }; |
1348 }; |
1245 |
1349 |
1246 /// \brief Writable bool map for store each true assigned elements in |
1350 /// \brief Writable bool map for logging each \c true assigned element in |
1247 /// a back insertable container. |
1351 /// a back insertable container. |
1248 /// |
1352 /// |
1249 /// Writable bool map for store each true assigned elements in a back |
1353 /// Writable bool map for logging each \c true assigned element by pushing |
1250 /// insertable container. It will push back all the keys set to true into |
1354 /// them into a back insertable container. |
1251 /// the container. It can be used to retrieve the items into a standard |
1355 /// It can be used to retrieve the items into a standard |
1252 /// container. The next example shows how can you store the undirected |
1356 /// container. The next example shows how you can store the |
1253 /// edges in a vector with prim algorithm. |
1357 /// edges found by the Prim algorithm in a vector. |
1254 /// |
1358 /// |
1255 ///\code |
1359 ///\code |
1256 /// vector<UEdge> span_tree_uedges; |
1360 /// vector<UEdge> span_tree_uedges; |
1257 /// BackInserterBoolMap<vector<UEdge> > inserter_map(span_tree_uedges); |
1361 /// BackInserterBoolMap<vector<UEdge> > inserter_map(span_tree_uedges); |
1258 /// prim(ugraph, cost, inserter_map); |
1362 /// prim(ugraph, cost, inserter_map); |
1259 ///\endcode |
1363 ///\endcode |
|
1364 /// |
|
1365 ///\sa StoreBoolMap |
|
1366 ///\sa FrontInserterBoolMap |
|
1367 ///\sa InserterBoolMap |
1260 template <typename Container, |
1368 template <typename Container, |
1261 typename Functor = |
1369 typename Functor = |
1262 _maps_bits::Identity<typename Container::value_type> > |
1370 _maps_bits::Identity<typename Container::value_type> > |
1263 class BackInserterBoolMap { |
1371 class BackInserterBoolMap { |
1264 public: |
1372 public: |
1265 typedef typename Container::value_type Key; |
1373 typedef typename Functor::argument_type Key; |
1266 typedef bool Value; |
1374 typedef bool Value; |
1267 |
1375 |
1268 /// Constructor |
1376 /// Constructor |
1269 BackInserterBoolMap(Container& _container, |
1377 BackInserterBoolMap(Container& _container, |
1270 const Functor& _functor = Functor()) |
1378 const Functor& _functor = Functor()) |
1271 : container(_container), functor(_functor) {} |
1379 : container(_container), functor(_functor) {} |
1272 |
1380 |
1273 /// Setter function of the map |
1381 /// The \c set function of the map |
1274 void set(const Key& key, Value value) { |
1382 void set(const Key& key, Value value) { |
1275 if (value) { |
1383 if (value) { |
1276 container.push_back(functor(key)); |
1384 container.push_back(functor(key)); |
1277 } |
1385 } |
1278 } |
1386 } |
1280 private: |
1388 private: |
1281 Container& container; |
1389 Container& container; |
1282 Functor functor; |
1390 Functor functor; |
1283 }; |
1391 }; |
1284 |
1392 |
1285 /// \brief Writable bool map for store each true assigned elements in |
1393 /// \brief Writable bool map for logging each \c true assigned element in |
1286 /// a front insertable container. |
1394 /// a front insertable container. |
1287 /// |
1395 /// |
1288 /// Writable bool map for store each true assigned elements in a front |
1396 /// Writable bool map for logging each \c true assigned element by pushing |
1289 /// insertable container. It will push front all the keys set to \c true into |
1397 /// them into a front insertable container. |
1290 /// the container. For example see the BackInserterBoolMap. |
1398 /// It can be used to retrieve the items into a standard |
|
1399 /// container. For example see \ref BackInserterBoolMap. |
|
1400 /// |
|
1401 ///\sa BackInserterBoolMap |
|
1402 ///\sa InserterBoolMap |
1291 template <typename Container, |
1403 template <typename Container, |
1292 typename Functor = |
1404 typename Functor = |
1293 _maps_bits::Identity<typename Container::value_type> > |
1405 _maps_bits::Identity<typename Container::value_type> > |
1294 class FrontInserterBoolMap { |
1406 class FrontInserterBoolMap { |
1295 public: |
1407 public: |
1296 typedef typename Container::value_type Key; |
1408 typedef typename Functor::argument_type Key; |
1297 typedef bool Value; |
1409 typedef bool Value; |
1298 |
1410 |
1299 /// Constructor |
1411 /// Constructor |
1300 FrontInserterBoolMap(Container& _container, |
1412 FrontInserterBoolMap(Container& _container, |
1301 const Functor& _functor = Functor()) |
1413 const Functor& _functor = Functor()) |
1302 : container(_container), functor(_functor) {} |
1414 : container(_container), functor(_functor) {} |
1303 |
1415 |
1304 /// Setter function of the map |
1416 /// The \c set function of the map |
1305 void set(const Key& key, Value value) { |
1417 void set(const Key& key, Value value) { |
1306 if (value) { |
1418 if (value) { |
1307 container.push_front(key); |
1419 container.push_front(functor(key)); |
1308 } |
1420 } |
1309 } |
1421 } |
1310 |
1422 |
1311 private: |
1423 private: |
1312 Container& container; |
1424 Container& container; |
1313 Functor functor; |
1425 Functor functor; |
1314 }; |
1426 }; |
1315 |
1427 |
1316 /// \brief Writable bool map for store each true assigned elements in |
1428 /// \brief Writable bool map for storing each \c true assigned element in |
1317 /// an insertable container. |
1429 /// an insertable container. |
1318 /// |
1430 /// |
1319 /// Writable bool map for store each true assigned elements in an |
1431 /// Writable bool map for storing each \c true assigned element in an |
1320 /// insertable container. It will insert all the keys set to \c true into |
1432 /// insertable container. It will insert all the keys set to \c true into |
1321 /// the container. If you want to store the cut edges of the strongly |
1433 /// the container. |
|
1434 /// |
|
1435 /// For example, if you want to store the cut arcs of the strongly |
1322 /// connected components in a set you can use the next code: |
1436 /// connected components in a set you can use the next code: |
1323 /// |
1437 /// |
1324 ///\code |
1438 ///\code |
1325 /// set<Edge> cut_edges; |
1439 /// set<Edge> cut_edges; |
1326 /// InserterBoolMap<set<Edge> > inserter_map(cut_edges); |
1440 /// InserterBoolMap<set<Edge> > inserter_map(cut_edges); |
1327 /// stronglyConnectedCutEdges(graph, cost, inserter_map); |
1441 /// stronglyConnectedCutEdges(graph, cost, inserter_map); |
1328 ///\endcode |
1442 ///\endcode |
|
1443 /// |
|
1444 ///\sa BackInserterBoolMap |
|
1445 ///\sa FrontInserterBoolMap |
1329 template <typename Container, |
1446 template <typename Container, |
1330 typename Functor = |
1447 typename Functor = |
1331 _maps_bits::Identity<typename Container::value_type> > |
1448 _maps_bits::Identity<typename Container::value_type> > |
1332 class InserterBoolMap { |
1449 class InserterBoolMap { |
1333 public: |
1450 public: |
1334 typedef typename Container::value_type Key; |
1451 typedef typename Container::value_type Key; |
1335 typedef bool Value; |
1452 typedef bool Value; |
1336 |
1453 |
1337 /// Constructor |
1454 /// Constructor with specified iterator |
|
1455 |
|
1456 /// Constructor with specified iterator. |
|
1457 /// \param _container The container for storing the elements. |
|
1458 /// \param _it The elements will be inserted before this iterator. |
|
1459 /// \param _functor The functor that is used when an element is stored. |
1338 InserterBoolMap(Container& _container, typename Container::iterator _it, |
1460 InserterBoolMap(Container& _container, typename Container::iterator _it, |
1339 const Functor& _functor = Functor()) |
1461 const Functor& _functor = Functor()) |
1340 : container(_container), it(_it), functor(_functor) {} |
1462 : container(_container), it(_it), functor(_functor) {} |
1341 |
1463 |
1342 /// Constructor |
1464 /// Constructor |
|
1465 |
|
1466 /// Constructor without specified iterator. |
|
1467 /// The elements will be inserted before <tt>_container.end()</tt>. |
|
1468 /// \param _container The container for storing the elements. |
|
1469 /// \param _functor The functor that is used when an element is stored. |
1343 InserterBoolMap(Container& _container, const Functor& _functor = Functor()) |
1470 InserterBoolMap(Container& _container, const Functor& _functor = Functor()) |
1344 : container(_container), it(_container.end()), functor(_functor) {} |
1471 : container(_container), it(_container.end()), functor(_functor) {} |
1345 |
1472 |
1346 /// Setter function of the map |
1473 /// The \c set function of the map |
1347 void set(const Key& key, Value value) { |
1474 void set(const Key& key, Value value) { |
1348 if (value) { |
1475 if (value) { |
1349 it = container.insert(it, key); |
1476 it = container.insert(it, functor(key)); |
1350 ++it; |
1477 ++it; |
1351 } |
1478 } |
1352 } |
1479 } |
1353 |
1480 |
1354 private: |
1481 private: |
1355 Container& container; |
1482 Container& container; |
1356 typename Container::iterator it; |
1483 typename Container::iterator it; |
1357 Functor functor; |
1484 Functor functor; |
1358 }; |
1485 }; |
1359 |
1486 |
1360 /// \brief Fill the true set elements with a given value. |
1487 /// \brief Writable bool map for filling each \c true assigned element with a |
1361 /// |
1488 /// given value. |
1362 /// Writable bool map to fill the elements set to \c true with a given value. |
1489 /// |
1363 /// The value can set |
1490 /// Writable bool map for filling each \c true assigned element with a |
1364 /// the container. |
1491 /// given value. The value can set the container. |
1365 /// |
1492 /// |
1366 /// The next code finds the connected components of the undirected graph |
1493 /// The following code finds the connected components of a graph |
1367 /// and stores it in the \c comp map: |
1494 /// and stores it in the \c comp map: |
1368 ///\code |
1495 ///\code |
1369 /// typedef UGraph::NodeMap<int> ComponentMap; |
1496 /// typedef UGraph::NodeMap<int> ComponentMap; |
1370 /// ComponentMap comp(ugraph); |
1497 /// ComponentMap comp(ugraph); |
1371 /// typedef FillBoolMap<UGraph::NodeMap<int> > ComponentFillerMap; |
1498 /// typedef FillBoolMap<UGraph::NodeMap<int> > ComponentFillerMap; |