1 | /* -*- C++ -*- |
---|
2 | * lemon/maps.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_MAPS_H |
---|
18 | #define LEMON_MAPS_H |
---|
19 | |
---|
20 | #include <lemon/graph_utils.h> |
---|
21 | #include <lemon/utility.h> |
---|
22 | |
---|
23 | |
---|
24 | ///\file |
---|
25 | ///\ingroup maps |
---|
26 | ///\brief Miscellaneous property maps |
---|
27 | /// |
---|
28 | ///\todo This file has the same name as the concept file in concept/, |
---|
29 | /// and this is not easily detectable in docs... |
---|
30 | |
---|
31 | #include <map> |
---|
32 | |
---|
33 | namespace lemon { |
---|
34 | |
---|
35 | /// \addtogroup maps |
---|
36 | /// @{ |
---|
37 | |
---|
38 | /// Base class of maps. |
---|
39 | |
---|
40 | /// Base class of maps. |
---|
41 | /// It provides the necessary <tt>typedef</tt>s required by the map concept. |
---|
42 | template<typename K, typename T> |
---|
43 | class MapBase |
---|
44 | { |
---|
45 | public: |
---|
46 | ///\e |
---|
47 | typedef K Key; |
---|
48 | ///\e |
---|
49 | typedef T Value; |
---|
50 | }; |
---|
51 | |
---|
52 | /// Null map. (a.k.a. DoNothingMap) |
---|
53 | |
---|
54 | /// If you have to provide a map only for its type definitions, |
---|
55 | /// or if you have to provide a writable map, but |
---|
56 | /// data written to it will sent to <tt>/dev/null</tt>... |
---|
57 | template<typename K, typename T> |
---|
58 | class NullMap : public MapBase<K,T> |
---|
59 | { |
---|
60 | public: |
---|
61 | |
---|
62 | typedef True NeedCopy; |
---|
63 | |
---|
64 | /// Gives back a default constructed element. |
---|
65 | T operator[](const K&) const { return T(); } |
---|
66 | /// Absorbs the value. |
---|
67 | void set(const K&, const T&) {} |
---|
68 | }; |
---|
69 | |
---|
70 | template <typename K, typename V> |
---|
71 | NullMap<K, V> nullMap() { |
---|
72 | return NullMap<K, V>(); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /// Constant map. |
---|
77 | |
---|
78 | /// This is a readable map which assigns a specified value to each key. |
---|
79 | /// In other aspects it is equivalent to the \ref NullMap. |
---|
80 | /// \todo set could be used to set the value. |
---|
81 | template<typename K, typename T> |
---|
82 | class ConstMap : public MapBase<K,T> |
---|
83 | { |
---|
84 | T v; |
---|
85 | public: |
---|
86 | |
---|
87 | typedef True NeedCopy; |
---|
88 | |
---|
89 | /// Default constructor |
---|
90 | |
---|
91 | /// The value of the map will be uninitialized. |
---|
92 | /// (More exactly it will be default constructed.) |
---|
93 | ConstMap() {} |
---|
94 | ///\e |
---|
95 | |
---|
96 | /// \param _v The initial value of the map. |
---|
97 | /// |
---|
98 | ConstMap(const T &_v) : v(_v) {} |
---|
99 | |
---|
100 | T operator[](const K&) const { return v; } |
---|
101 | void set(const K&, const T&) {} |
---|
102 | |
---|
103 | template<typename T1> |
---|
104 | struct rebind { |
---|
105 | typedef ConstMap<K,T1> other; |
---|
106 | }; |
---|
107 | |
---|
108 | template<typename T1> |
---|
109 | ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {} |
---|
110 | }; |
---|
111 | |
---|
112 | ///Returns a \ref ConstMap class |
---|
113 | |
---|
114 | ///This function just returns a \ref ConstMap class. |
---|
115 | ///\relates ConstMap |
---|
116 | template<class V,class K> |
---|
117 | inline ConstMap<V,K> constMap(const K &k) |
---|
118 | { |
---|
119 | return ConstMap<V,K>(k); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | //to document later |
---|
124 | template<typename T, T v> |
---|
125 | struct Const { }; |
---|
126 | //to document later |
---|
127 | template<typename K, typename V, V v> |
---|
128 | class ConstMap<K, Const<V, v> > : public MapBase<K, V> |
---|
129 | { |
---|
130 | public: |
---|
131 | ConstMap() { } |
---|
132 | V operator[](const K&) const { return v; } |
---|
133 | void set(const K&, const V&) { } |
---|
134 | }; |
---|
135 | |
---|
136 | /// \c std::map wrapper |
---|
137 | |
---|
138 | /// This is essentially a wrapper for \c std::map. With addition that |
---|
139 | /// you can specify a default value different from \c Value() . |
---|
140 | /// |
---|
141 | /// \todo Provide allocator parameter... |
---|
142 | template <typename K, typename T, typename Compare = std::less<K> > |
---|
143 | class StdMap : public std::map<K,T,Compare> { |
---|
144 | typedef std::map<K,T,Compare> parent; |
---|
145 | T v; |
---|
146 | typedef typename parent::value_type PairType; |
---|
147 | |
---|
148 | public: |
---|
149 | ///\e |
---|
150 | typedef K Key; |
---|
151 | ///\e |
---|
152 | typedef T Value; |
---|
153 | ///\e |
---|
154 | typedef T& Reference; |
---|
155 | ///\e |
---|
156 | typedef const T& ConstReference; |
---|
157 | |
---|
158 | |
---|
159 | StdMap() : v() {} |
---|
160 | /// Constructor with specified default value |
---|
161 | StdMap(const T& _v) : v(_v) {} |
---|
162 | |
---|
163 | /// \brief Constructs the map from an appropriate std::map. |
---|
164 | /// |
---|
165 | /// \warning Inefficient: copies the content of \c m ! |
---|
166 | StdMap(const parent &m) : parent(m) {} |
---|
167 | /// \brief Constructs the map from an appropriate std::map, and explicitly |
---|
168 | /// specifies a default value. |
---|
169 | /// |
---|
170 | /// \warning Inefficient: copies the content of \c m ! |
---|
171 | StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} |
---|
172 | |
---|
173 | template<typename T1, typename Comp1> |
---|
174 | StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { |
---|
175 | //FIXME; |
---|
176 | } |
---|
177 | |
---|
178 | Reference operator[](const Key &k) { |
---|
179 | return insert(PairType(k,v)).first -> second; |
---|
180 | } |
---|
181 | ConstReference operator[](const Key &k) const { |
---|
182 | typename parent::iterator i = lower_bound(k); |
---|
183 | if (i == parent::end() || parent::key_comp()(k, (*i).first)) |
---|
184 | return v; |
---|
185 | return (*i).second; |
---|
186 | } |
---|
187 | void set(const Key &k, const T &t) { |
---|
188 | parent::operator[](k) = t; |
---|
189 | } |
---|
190 | |
---|
191 | /// Changes the default value of the map. |
---|
192 | /// \return Returns the previous default value. |
---|
193 | /// |
---|
194 | /// \warning The value of some keys (which has already been queried, but |
---|
195 | /// the value has been unchanged from the default) may change! |
---|
196 | T setDefault(const T &_v) { T old=v; v=_v; return old; } |
---|
197 | |
---|
198 | template<typename T1> |
---|
199 | struct rebind { |
---|
200 | typedef StdMap<Key,T1,Compare> other; |
---|
201 | }; |
---|
202 | }; |
---|
203 | |
---|
204 | /// @} |
---|
205 | |
---|
206 | /// \addtogroup map_adaptors |
---|
207 | /// @{ |
---|
208 | |
---|
209 | /// \brief Identity mapping. |
---|
210 | /// |
---|
211 | /// This mapping gives back the given key as value without any |
---|
212 | /// modification. |
---|
213 | template <typename T> |
---|
214 | class IdentityMap { |
---|
215 | public: |
---|
216 | typedef T Key; |
---|
217 | typedef T Value; |
---|
218 | |
---|
219 | const Value& operator[](const Key& t) const { |
---|
220 | return t; |
---|
221 | } |
---|
222 | }; |
---|
223 | |
---|
224 | ///Convert the \c Value of a maps to another type. |
---|
225 | |
---|
226 | ///This \ref concept::ReadMap "read only map" |
---|
227 | ///converts the \c Value of a maps to type \c T. |
---|
228 | ///Its \c Value is inherited from \c M. |
---|
229 | /// |
---|
230 | ///\bug wrong documentation |
---|
231 | template<class M, class T> |
---|
232 | class ConvertMap { |
---|
233 | typename SmartConstReference<M>::Type m; |
---|
234 | public: |
---|
235 | |
---|
236 | typedef True NeedCopy; |
---|
237 | |
---|
238 | ///\e |
---|
239 | typedef typename M::Key Key; |
---|
240 | ///\e |
---|
241 | typedef T Value; |
---|
242 | |
---|
243 | ///Constructor |
---|
244 | |
---|
245 | ///Constructor |
---|
246 | ///\param _m is the underlying map |
---|
247 | ConvertMap(const M &_m) : m(_m) {}; |
---|
248 | |
---|
249 | /// \brief The subscript operator. |
---|
250 | /// |
---|
251 | /// The subscript operator. |
---|
252 | /// \param k The key |
---|
253 | /// \return The target of the edge |
---|
254 | Value operator[](Key k) const {return m[k];} |
---|
255 | }; |
---|
256 | |
---|
257 | ///Returns an \ref ConvertMap class |
---|
258 | |
---|
259 | ///This function just returns an \ref ConvertMap class. |
---|
260 | ///\relates ConvertMap |
---|
261 | ///\todo The order of the template parameters are changed. |
---|
262 | template<class T, class M> |
---|
263 | inline ConvertMap<M,T> convertMap(const M &m) |
---|
264 | { |
---|
265 | return ConvertMap<M,T>(m); |
---|
266 | } |
---|
267 | |
---|
268 | ///Sum of two maps |
---|
269 | |
---|
270 | ///This \ref concept::ReadMap "read only map" returns the sum of the two |
---|
271 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
272 | ///The \c Key and \c Value of M2 must be convertible to those of \c M1. |
---|
273 | |
---|
274 | template<class M1,class M2> |
---|
275 | class AddMap |
---|
276 | { |
---|
277 | typename SmartConstReference<M1>::Type m1; |
---|
278 | typename SmartConstReference<M2>::Type m2; |
---|
279 | |
---|
280 | public: |
---|
281 | |
---|
282 | typedef True NeedCopy; |
---|
283 | |
---|
284 | ///\e |
---|
285 | typedef typename M1::Key Key; |
---|
286 | ///\e |
---|
287 | typedef typename M1::Value Value; |
---|
288 | |
---|
289 | ///Constructor |
---|
290 | AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
291 | Value operator[](Key k) const {return m1[k]+m2[k];} |
---|
292 | }; |
---|
293 | |
---|
294 | ///Returns an \ref AddMap class |
---|
295 | |
---|
296 | ///This function just returns an \ref AddMap class. |
---|
297 | ///\todo How to call these type of functions? |
---|
298 | /// |
---|
299 | ///\relates AddMap |
---|
300 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
301 | template<class M1,class M2> |
---|
302 | inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2) |
---|
303 | { |
---|
304 | return AddMap<M1,M2>(m1,m2); |
---|
305 | } |
---|
306 | |
---|
307 | ///Shift a maps with a constant. |
---|
308 | |
---|
309 | ///This \ref concept::ReadMap "read only map" returns the sum of the |
---|
310 | ///given map and a constant value. |
---|
311 | ///Its \c Key and \c Value is inherited from \c M. |
---|
312 | /// |
---|
313 | ///Actually, |
---|
314 | ///\code |
---|
315 | /// ShiftMap<X> sh(x,v); |
---|
316 | ///\endcode |
---|
317 | ///it is equivalent with |
---|
318 | ///\code |
---|
319 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
320 | /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
---|
321 | ///\endcode |
---|
322 | template<class M> |
---|
323 | class ShiftMap |
---|
324 | { |
---|
325 | typename SmartConstReference<M>::Type m; |
---|
326 | typename M::Value v; |
---|
327 | public: |
---|
328 | |
---|
329 | typedef True NeedCopy; |
---|
330 | ///\e |
---|
331 | typedef typename M::Key Key; |
---|
332 | ///\e |
---|
333 | typedef typename M::Value Value; |
---|
334 | |
---|
335 | ///Constructor |
---|
336 | |
---|
337 | ///Constructor |
---|
338 | ///\param _m is the undelying map |
---|
339 | ///\param _v is the shift value |
---|
340 | ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; |
---|
341 | Value operator[](Key k) const {return m[k]+v;} |
---|
342 | }; |
---|
343 | |
---|
344 | ///Returns an \ref ShiftMap class |
---|
345 | |
---|
346 | ///This function just returns an \ref ShiftMap class. |
---|
347 | ///\relates ShiftMap |
---|
348 | ///\todo A better name is required. |
---|
349 | template<class M> |
---|
350 | inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v) |
---|
351 | { |
---|
352 | return ShiftMap<M>(m,v); |
---|
353 | } |
---|
354 | |
---|
355 | ///Difference of two maps |
---|
356 | |
---|
357 | ///This \ref concept::ReadMap "read only map" returns the difference |
---|
358 | ///of the values returned by the two |
---|
359 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
360 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
361 | |
---|
362 | template<class M1,class M2> |
---|
363 | class SubMap |
---|
364 | { |
---|
365 | typename SmartConstReference<M1>::Type m1; |
---|
366 | typename SmartConstReference<M2>::Type m2; |
---|
367 | public: |
---|
368 | |
---|
369 | typedef True NeedCopy; |
---|
370 | ///\e |
---|
371 | typedef typename M1::Key Key; |
---|
372 | ///\e |
---|
373 | typedef typename M1::Value Value; |
---|
374 | |
---|
375 | ///Constructor |
---|
376 | SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
377 | Value operator[](Key k) const {return m1[k]-m2[k];} |
---|
378 | }; |
---|
379 | |
---|
380 | ///Returns a \ref SubMap class |
---|
381 | |
---|
382 | ///This function just returns a \ref SubMap class. |
---|
383 | /// |
---|
384 | ///\relates SubMap |
---|
385 | template<class M1,class M2> |
---|
386 | inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2) |
---|
387 | { |
---|
388 | return SubMap<M1,M2>(m1,m2); |
---|
389 | } |
---|
390 | |
---|
391 | ///Product of two maps |
---|
392 | |
---|
393 | ///This \ref concept::ReadMap "read only map" returns the product of the |
---|
394 | ///values returned by the two |
---|
395 | ///given |
---|
396 | ///maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
397 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
398 | |
---|
399 | template<class M1,class M2> |
---|
400 | class MulMap |
---|
401 | { |
---|
402 | typename SmartConstReference<M1>::Type m1; |
---|
403 | typename SmartConstReference<M2>::Type m2; |
---|
404 | public: |
---|
405 | |
---|
406 | typedef True NeedCopy; |
---|
407 | ///\e |
---|
408 | typedef typename M1::Key Key; |
---|
409 | ///\e |
---|
410 | typedef typename M1::Value Value; |
---|
411 | |
---|
412 | ///Constructor |
---|
413 | MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
414 | Value operator[](Key k) const {return m1[k]*m2[k];} |
---|
415 | }; |
---|
416 | |
---|
417 | ///Returns a \ref MulMap class |
---|
418 | |
---|
419 | ///This function just returns a \ref MulMap class. |
---|
420 | ///\relates MulMap |
---|
421 | template<class M1,class M2> |
---|
422 | inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2) |
---|
423 | { |
---|
424 | return MulMap<M1,M2>(m1,m2); |
---|
425 | } |
---|
426 | |
---|
427 | ///Scale a maps with a constant. |
---|
428 | |
---|
429 | ///This \ref concept::ReadMap "read only map" returns the value of the |
---|
430 | ///given map multipied with a constant value. |
---|
431 | ///Its \c Key and \c Value is inherited from \c M. |
---|
432 | /// |
---|
433 | ///Actually, |
---|
434 | ///\code |
---|
435 | /// ScaleMap<X> sc(x,v); |
---|
436 | ///\endcode |
---|
437 | ///it is equivalent with |
---|
438 | ///\code |
---|
439 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
440 | /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
---|
441 | ///\endcode |
---|
442 | template<class M> |
---|
443 | class ScaleMap |
---|
444 | { |
---|
445 | typename SmartConstReference<M>::Type m; |
---|
446 | typename M::Value v; |
---|
447 | public: |
---|
448 | |
---|
449 | typedef True NeedCopy; |
---|
450 | ///\e |
---|
451 | typedef typename M::Key Key; |
---|
452 | ///\e |
---|
453 | typedef typename M::Value Value; |
---|
454 | |
---|
455 | ///Constructor |
---|
456 | |
---|
457 | ///Constructor |
---|
458 | ///\param _m is the undelying map |
---|
459 | ///\param _v is the scaling value |
---|
460 | ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; |
---|
461 | Value operator[](Key k) const {return m[k]*v;} |
---|
462 | }; |
---|
463 | |
---|
464 | ///Returns an \ref ScaleMap class |
---|
465 | |
---|
466 | ///This function just returns an \ref ScaleMap class. |
---|
467 | ///\relates ScaleMap |
---|
468 | ///\todo A better name is required. |
---|
469 | template<class M> |
---|
470 | inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v) |
---|
471 | { |
---|
472 | return ScaleMap<M>(m,v); |
---|
473 | } |
---|
474 | |
---|
475 | ///Quotient of two maps |
---|
476 | |
---|
477 | ///This \ref concept::ReadMap "read only map" returns the quotient of the |
---|
478 | ///values returned by the two |
---|
479 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
480 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
481 | |
---|
482 | template<class M1,class M2> |
---|
483 | class DivMap |
---|
484 | { |
---|
485 | typename SmartConstReference<M1>::Type m1; |
---|
486 | typename SmartConstReference<M2>::Type m2; |
---|
487 | public: |
---|
488 | |
---|
489 | typedef True NeedCopy; |
---|
490 | ///\e |
---|
491 | typedef typename M1::Key Key; |
---|
492 | ///\e |
---|
493 | typedef typename M1::Value Value; |
---|
494 | |
---|
495 | ///Constructor |
---|
496 | DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
497 | Value operator[](Key k) const {return m1[k]/m2[k];} |
---|
498 | }; |
---|
499 | |
---|
500 | ///Returns a \ref DivMap class |
---|
501 | |
---|
502 | ///This function just returns a \ref DivMap class. |
---|
503 | ///\relates DivMap |
---|
504 | template<class M1,class M2> |
---|
505 | inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2) |
---|
506 | { |
---|
507 | return DivMap<M1,M2>(m1,m2); |
---|
508 | } |
---|
509 | |
---|
510 | ///Composition of two maps |
---|
511 | |
---|
512 | ///This \ref concept::ReadMap "read only map" returns the composition of |
---|
513 | ///two |
---|
514 | ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is |
---|
515 | ///of \c M2, |
---|
516 | ///then for |
---|
517 | ///\code |
---|
518 | /// ComposeMap<M1,M2> cm(m1,m2); |
---|
519 | ///\endcode |
---|
520 | /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt> |
---|
521 | /// |
---|
522 | ///Its \c Key is inherited from \c M2 and its \c Value is from |
---|
523 | ///\c M1. |
---|
524 | ///The \c M2::Value must be convertible to \c M1::Key. |
---|
525 | ///\todo Check the requirements. |
---|
526 | |
---|
527 | template<class M1,class M2> |
---|
528 | class ComposeMap |
---|
529 | { |
---|
530 | typename SmartConstReference<M1>::Type m1; |
---|
531 | typename SmartConstReference<M2>::Type m2; |
---|
532 | public: |
---|
533 | |
---|
534 | typedef True NeedCopy; |
---|
535 | ///\e |
---|
536 | typedef typename M2::Key Key; |
---|
537 | ///\e |
---|
538 | typedef typename M1::Value Value; |
---|
539 | |
---|
540 | ///Constructor |
---|
541 | ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
542 | Value operator[](Key k) const {return m1[m2[k]];} |
---|
543 | }; |
---|
544 | ///Returns a \ref ComposeMap class |
---|
545 | |
---|
546 | ///This function just returns a \ref ComposeMap class. |
---|
547 | /// |
---|
548 | ///\relates ComposeMap |
---|
549 | template<class M1,class M2> |
---|
550 | inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2) |
---|
551 | { |
---|
552 | return ComposeMap<M1,M2>(m1,m2); |
---|
553 | } |
---|
554 | |
---|
555 | ///Combine of two maps using an STL (binary) functor. |
---|
556 | |
---|
557 | ///Combine of two maps using an STL (binary) functor. |
---|
558 | /// |
---|
559 | /// |
---|
560 | ///This \ref concept::ReadMap "read only map" takes to maps and a |
---|
561 | ///binary functor and returns the composition of |
---|
562 | ///two |
---|
563 | ///given maps unsing the functor. |
---|
564 | ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
---|
565 | ///and \c f is of \c F, |
---|
566 | ///then for |
---|
567 | ///\code |
---|
568 | /// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
---|
569 | ///\endcode |
---|
570 | /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
---|
571 | /// |
---|
572 | ///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
---|
573 | ///The \c M2::Value and \c M1::Value must be convertible to the corresponding |
---|
574 | ///input parameter of \c F and the return type of \c F must be convertible |
---|
575 | ///to \c V. |
---|
576 | ///\todo Check the requirements. |
---|
577 | |
---|
578 | template<class M1,class M2,class F,class V = typename F::result_type> |
---|
579 | class CombineMap |
---|
580 | { |
---|
581 | typename SmartConstReference<M1>::Type m1; |
---|
582 | typename SmartConstReference<M2>::Type m2; |
---|
583 | F f; |
---|
584 | public: |
---|
585 | |
---|
586 | typedef True NeedCopy; |
---|
587 | ///\e |
---|
588 | typedef typename M1::Key Key; |
---|
589 | ///\e |
---|
590 | typedef V Value; |
---|
591 | |
---|
592 | ///Constructor |
---|
593 | CombineMap(const M1 &_m1,const M2 &_m2,const F &_f) |
---|
594 | : m1(_m1), m2(_m2), f(_f) {}; |
---|
595 | Value operator[](Key k) const {return f(m1[k],m2[k]);} |
---|
596 | }; |
---|
597 | |
---|
598 | ///Returns a \ref CombineMap class |
---|
599 | |
---|
600 | ///This function just returns a \ref CombineMap class. |
---|
601 | /// |
---|
602 | ///Only the first template parameter (the value type) must be given. |
---|
603 | /// |
---|
604 | ///For example if \c m1 and \c m2 are both \c double valued maps, then |
---|
605 | ///\code |
---|
606 | ///combineMap<double>(m1,m2,std::plus<double>) |
---|
607 | ///\endcode |
---|
608 | ///is equivalent with |
---|
609 | ///\code |
---|
610 | ///addMap(m1,m2) |
---|
611 | ///\endcode |
---|
612 | /// |
---|
613 | ///\relates CombineMap |
---|
614 | template<class M1,class M2,class F> |
---|
615 | inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) |
---|
616 | { |
---|
617 | return CombineMap<M1,M2,F>(m1,m2,f); |
---|
618 | } |
---|
619 | |
---|
620 | ///Negative value of a map |
---|
621 | |
---|
622 | ///This \ref concept::ReadMap "read only map" returns the negative |
---|
623 | ///value of the |
---|
624 | ///value returned by the |
---|
625 | ///given map. Its \c Key and \c Value will be inherited from \c M. |
---|
626 | ///The unary \c - operator must be defined for \c Value, of course. |
---|
627 | |
---|
628 | template<class M> |
---|
629 | class NegMap |
---|
630 | { |
---|
631 | typename SmartConstReference<M>::Type m; |
---|
632 | public: |
---|
633 | |
---|
634 | typedef True NeedCopy; |
---|
635 | ///\e |
---|
636 | typedef typename M::Key Key; |
---|
637 | ///\e |
---|
638 | typedef typename M::Value Value; |
---|
639 | |
---|
640 | ///Constructor |
---|
641 | NegMap(const M &_m) : m(_m) {}; |
---|
642 | Value operator[](Key k) const {return -m[k];} |
---|
643 | }; |
---|
644 | |
---|
645 | ///Returns a \ref NegMap class |
---|
646 | |
---|
647 | ///This function just returns a \ref NegMap class. |
---|
648 | ///\relates NegMap |
---|
649 | template<class M> |
---|
650 | inline NegMap<M> negMap(const M &m) |
---|
651 | { |
---|
652 | return NegMap<M>(m); |
---|
653 | } |
---|
654 | |
---|
655 | |
---|
656 | ///Absolute value of a map |
---|
657 | |
---|
658 | ///This \ref concept::ReadMap "read only map" returns the absolute value |
---|
659 | ///of the |
---|
660 | ///value returned by the |
---|
661 | ///given map. Its \c Key and \c Value will be inherited |
---|
662 | ///from <tt>M</tt>. <tt>Value</tt> |
---|
663 | ///must be comparable to <tt>0</tt> and the unary <tt>-</tt> |
---|
664 | ///operator must be defined for it, of course. |
---|
665 | /// |
---|
666 | ///\bug We need a unified way to handle the situation below: |
---|
667 | ///\code |
---|
668 | /// struct _UnConvertible {}; |
---|
669 | /// template<class A> inline A t_abs(A a) {return _UnConvertible();} |
---|
670 | /// template<> inline int t_abs<>(int n) {return abs(n);} |
---|
671 | /// template<> inline long int t_abs<>(long int n) {return labs(n);} |
---|
672 | /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} |
---|
673 | /// template<> inline float t_abs<>(float n) {return fabsf(n);} |
---|
674 | /// template<> inline double t_abs<>(double n) {return fabs(n);} |
---|
675 | /// template<> inline long double t_abs<>(long double n) {return fabsl(n);} |
---|
676 | ///\endcode |
---|
677 | |
---|
678 | |
---|
679 | template<class M> |
---|
680 | class AbsMap |
---|
681 | { |
---|
682 | typename SmartConstReference<M>::Type m; |
---|
683 | public: |
---|
684 | |
---|
685 | typedef True NeedCopy; |
---|
686 | ///\e |
---|
687 | typedef typename M::Key Key; |
---|
688 | ///\e |
---|
689 | typedef typename M::Value Value; |
---|
690 | |
---|
691 | ///Constructor |
---|
692 | AbsMap(const M &_m) : m(_m) {}; |
---|
693 | Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;} |
---|
694 | }; |
---|
695 | |
---|
696 | ///Returns a \ref AbsMap class |
---|
697 | |
---|
698 | ///This function just returns a \ref AbsMap class. |
---|
699 | ///\relates AbsMap |
---|
700 | template<class M> |
---|
701 | inline AbsMap<M> absMap(const M &m) |
---|
702 | { |
---|
703 | return AbsMap<M>(m); |
---|
704 | } |
---|
705 | |
---|
706 | ///Converts an STL style functor to a map |
---|
707 | |
---|
708 | ///This \ref concept::ReadMap "read only map" returns the value |
---|
709 | ///of a |
---|
710 | ///given map. |
---|
711 | /// |
---|
712 | ///Template parameters \c K and \c V will become its |
---|
713 | ///\c Key and \c Value. They must be given explicitely |
---|
714 | ///because a functor does not provide such typedefs. |
---|
715 | /// |
---|
716 | ///Parameter \c F is the type of the used functor. |
---|
717 | |
---|
718 | |
---|
719 | template<class K,class V,class F> |
---|
720 | class FunctorMap |
---|
721 | { |
---|
722 | const F &f; |
---|
723 | public: |
---|
724 | |
---|
725 | typedef True NeedCopy; |
---|
726 | ///\e |
---|
727 | typedef K Key; |
---|
728 | ///\e |
---|
729 | typedef V Value; |
---|
730 | |
---|
731 | ///Constructor |
---|
732 | FunctorMap(const F &_f) : f(_f) {}; |
---|
733 | Value operator[](Key k) const {return f(k);} |
---|
734 | }; |
---|
735 | |
---|
736 | ///Returns a \ref FunctorMap class |
---|
737 | |
---|
738 | ///This function just returns a \ref FunctorMap class. |
---|
739 | /// |
---|
740 | ///The third template parameter isn't necessary to be given. |
---|
741 | ///\relates FunctorMap |
---|
742 | template<class K,class V, class F> |
---|
743 | inline FunctorMap<K,V,F> functorMap(const F &f) |
---|
744 | { |
---|
745 | return FunctorMap<K,V,F>(f); |
---|
746 | } |
---|
747 | |
---|
748 | ///Converts a map to an STL style (unary) functor |
---|
749 | |
---|
750 | ///This class Converts a map to an STL style (unary) functor. |
---|
751 | ///that is it provides an <tt>operator()</tt> to read its values. |
---|
752 | /// |
---|
753 | ///For the sake of convenience it also works as |
---|
754 | ///a ususal \ref concept::ReadMap "readable map", i.e |
---|
755 | ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
---|
756 | |
---|
757 | template<class M> |
---|
758 | class MapFunctor |
---|
759 | { |
---|
760 | typename SmartConstReference<M>::Type m; |
---|
761 | public: |
---|
762 | |
---|
763 | typedef True NeedCopy; |
---|
764 | ///\e |
---|
765 | typedef typename M::Key argument_type; |
---|
766 | ///\e |
---|
767 | typedef typename M::Value result_type; |
---|
768 | ///\e |
---|
769 | typedef typename M::Key Key; |
---|
770 | ///\e |
---|
771 | typedef typename M::Value Value; |
---|
772 | |
---|
773 | ///Constructor |
---|
774 | |
---|
775 | ///\e |
---|
776 | /// |
---|
777 | MapFunctor(const M &_m) : m(_m) {}; |
---|
778 | ///Returns a value of the map |
---|
779 | |
---|
780 | ///\e |
---|
781 | /// |
---|
782 | Value operator()(Key k) const {return m[k];} |
---|
783 | ///\e |
---|
784 | /// |
---|
785 | Value operator[](Key k) const {return m[k];} |
---|
786 | }; |
---|
787 | |
---|
788 | ///Returns a \ref MapFunctor class |
---|
789 | |
---|
790 | ///This function just returns a \ref MapFunctor class. |
---|
791 | ///\relates MapFunctor |
---|
792 | template<class M> |
---|
793 | inline MapFunctor<M> mapFunctor(const M &m) |
---|
794 | { |
---|
795 | return MapFunctor<M>(m); |
---|
796 | } |
---|
797 | |
---|
798 | |
---|
799 | ///Apply all map setting operations to two maps |
---|
800 | |
---|
801 | ///This map has two \ref concept::WriteMap "writable map" |
---|
802 | ///parameters and each write request will be passed to both of them. |
---|
803 | ///If \c M1 is also \ref concept::ReadMap "readable", |
---|
804 | ///then the read operations will return the |
---|
805 | ///corresponding values of \c M1. |
---|
806 | /// |
---|
807 | ///The \c Key and \c Value will be inherited from \c M1. |
---|
808 | ///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
---|
809 | |
---|
810 | template<class M1,class M2> |
---|
811 | class ForkMap |
---|
812 | { |
---|
813 | typename SmartConstReference<M1>::Type m1; |
---|
814 | typename SmartConstReference<M2>::Type m2; |
---|
815 | public: |
---|
816 | |
---|
817 | typedef True NeedCopy; |
---|
818 | ///\e |
---|
819 | typedef typename M1::Key Key; |
---|
820 | ///\e |
---|
821 | typedef typename M1::Value Value; |
---|
822 | |
---|
823 | ///Constructor |
---|
824 | |
---|
825 | ///\e |
---|
826 | /// |
---|
827 | ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
828 | Value operator[](Key k) const {return m1[k];} |
---|
829 | void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);} |
---|
830 | }; |
---|
831 | |
---|
832 | ///Returns an \ref ForkMap class |
---|
833 | |
---|
834 | ///This function just returns an \ref ForkMap class. |
---|
835 | ///\todo How to call these type of functions? |
---|
836 | /// |
---|
837 | ///\relates ForkMap |
---|
838 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
839 | template<class M1,class M2> |
---|
840 | inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2) |
---|
841 | { |
---|
842 | return ForkMap<M1,M2>(m1,m2); |
---|
843 | } |
---|
844 | |
---|
845 | |
---|
846 | |
---|
847 | /* ************* BOOL MAPS ******************* */ |
---|
848 | |
---|
849 | ///Logical 'not' of a map |
---|
850 | |
---|
851 | ///This bool \ref concept::ReadMap "read only map" returns the |
---|
852 | ///logical negation of |
---|
853 | ///value returned by the |
---|
854 | ///given map. Its \c Key and will be inherited from \c M, |
---|
855 | ///its Value is <tt>bool</tt>. |
---|
856 | |
---|
857 | template<class M> |
---|
858 | class NotMap |
---|
859 | { |
---|
860 | typename SmartConstReference<M>::Type m; |
---|
861 | public: |
---|
862 | |
---|
863 | typedef True NeedCopy; |
---|
864 | ///\e |
---|
865 | typedef typename M::Key Key; |
---|
866 | ///\e |
---|
867 | typedef bool Value; |
---|
868 | |
---|
869 | ///Constructor |
---|
870 | |
---|
871 | ///\e |
---|
872 | /// |
---|
873 | NotMap(const M &_m) : m(_m) {}; |
---|
874 | Value operator[](Key k) const {return !m[k];} |
---|
875 | }; |
---|
876 | |
---|
877 | ///Returns a \ref NotMap class |
---|
878 | |
---|
879 | ///This function just returns a \ref NotMap class. |
---|
880 | ///\relates NotMap |
---|
881 | template<class M> |
---|
882 | inline NotMap<M> notMap(const M &m) |
---|
883 | { |
---|
884 | return NotMap<M>(m); |
---|
885 | } |
---|
886 | |
---|
887 | |
---|
888 | |
---|
889 | |
---|
890 | |
---|
891 | |
---|
892 | |
---|
893 | |
---|
894 | |
---|
895 | |
---|
896 | |
---|
897 | /// @} |
---|
898 | } |
---|
899 | |
---|
900 | #endif // LEMON_MAPS_H |
---|