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 | typedef K Key; |
---|
150 | typedef T Value; |
---|
151 | typedef T& Reference; |
---|
152 | typedef const T& ConstReference; |
---|
153 | |
---|
154 | |
---|
155 | StdMap() : v() {} |
---|
156 | /// Constructor with specified default value |
---|
157 | StdMap(const T& _v) : v(_v) {} |
---|
158 | |
---|
159 | /// \brief Constructs the map from an appropriate std::map. |
---|
160 | /// |
---|
161 | /// \warning Inefficient: copies the content of \c m ! |
---|
162 | StdMap(const parent &m) : parent(m) {} |
---|
163 | /// \brief Constructs the map from an appropriate std::map, and explicitly |
---|
164 | /// specifies a default value. |
---|
165 | /// |
---|
166 | /// \warning Inefficient: copies the content of \c m ! |
---|
167 | StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} |
---|
168 | |
---|
169 | template<typename T1, typename Comp1> |
---|
170 | StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { |
---|
171 | //FIXME; |
---|
172 | } |
---|
173 | |
---|
174 | Reference operator[](const Key &k) { |
---|
175 | return insert(PairType(k,v)).first -> second; |
---|
176 | } |
---|
177 | ConstReference operator[](const Key &k) const { |
---|
178 | typename parent::iterator i = lower_bound(k); |
---|
179 | if (i == parent::end() || parent::key_comp()(k, (*i).first)) |
---|
180 | return v; |
---|
181 | return (*i).second; |
---|
182 | } |
---|
183 | void set(const Key &k, const T &t) { |
---|
184 | parent::operator[](k) = t; |
---|
185 | } |
---|
186 | |
---|
187 | /// Changes the default value of the map. |
---|
188 | /// \return Returns the previous default value. |
---|
189 | /// |
---|
190 | /// \warning The value of some keys (which has already been queried, but |
---|
191 | /// the value has been unchanged from the default) may change! |
---|
192 | T setDefault(const T &_v) { T old=v; v=_v; return old; } |
---|
193 | |
---|
194 | template<typename T1> |
---|
195 | struct rebind { |
---|
196 | typedef StdMap<Key,T1,Compare> other; |
---|
197 | }; |
---|
198 | }; |
---|
199 | |
---|
200 | /// @} |
---|
201 | |
---|
202 | /// \addtogroup map_adaptors |
---|
203 | /// @{ |
---|
204 | |
---|
205 | |
---|
206 | ///Convert the \c Value of a maps to another type. |
---|
207 | |
---|
208 | ///This \ref concept::ReadMap "read only map" |
---|
209 | ///converts the \c Value of a maps to type \c T. |
---|
210 | ///Its \c Value is inherited from \c M. |
---|
211 | /// |
---|
212 | ///Actually, |
---|
213 | ///\code |
---|
214 | /// ConvertMap<X> sh(x,v); |
---|
215 | ///\endcode |
---|
216 | ///it is equivalent with |
---|
217 | ///\code |
---|
218 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
219 | /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
---|
220 | ///\endcode |
---|
221 | ///\bug wrong documentation |
---|
222 | template<class M, class T> |
---|
223 | class ConvertMap { |
---|
224 | typename SmartConstReference<M>::Type m; |
---|
225 | public: |
---|
226 | |
---|
227 | typedef True NeedCopy; |
---|
228 | |
---|
229 | typedef typename M::Key Key; |
---|
230 | typedef T Value; |
---|
231 | |
---|
232 | ///Constructor |
---|
233 | |
---|
234 | ///Constructor |
---|
235 | ///\param _m is the undelying map |
---|
236 | ///\param _v is the convert value |
---|
237 | ConvertMap(const M &_m) : m(_m) {}; |
---|
238 | |
---|
239 | /// \brief The subscript operator. |
---|
240 | /// |
---|
241 | /// The subscript operator. |
---|
242 | /// \param edge The edge |
---|
243 | /// \return The target of the edge |
---|
244 | Value operator[](Key k) const {return m[k];} |
---|
245 | }; |
---|
246 | |
---|
247 | ///Returns an \ref ConvertMap class |
---|
248 | |
---|
249 | ///This function just returns an \ref ConvertMap class. |
---|
250 | ///\relates ConvertMap |
---|
251 | ///\todo The order of the template parameters are changed. |
---|
252 | template<class T, class M> |
---|
253 | inline ConvertMap<M,T> convertMap(const M &m) |
---|
254 | { |
---|
255 | return ConvertMap<M,T>(m); |
---|
256 | } |
---|
257 | |
---|
258 | ///Sum of two maps |
---|
259 | |
---|
260 | ///This \ref concept::ReadMap "read only map" returns the sum of the two |
---|
261 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
262 | ///The \c Key and \c Value of M2 must be convertible to those of \c M1. |
---|
263 | |
---|
264 | template<class M1,class M2> |
---|
265 | class AddMap |
---|
266 | { |
---|
267 | typename SmartConstReference<M1>::Type m1; |
---|
268 | typename SmartConstReference<M2>::Type m2; |
---|
269 | |
---|
270 | public: |
---|
271 | |
---|
272 | typedef True NeedCopy; |
---|
273 | |
---|
274 | typedef typename M1::Key Key; |
---|
275 | typedef typename M1::Value Value; |
---|
276 | |
---|
277 | ///Constructor |
---|
278 | |
---|
279 | ///\e |
---|
280 | /// |
---|
281 | AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
282 | Value operator[](Key k) const {return m1[k]+m2[k];} |
---|
283 | }; |
---|
284 | |
---|
285 | ///Returns an \ref AddMap class |
---|
286 | |
---|
287 | ///This function just returns an \ref AddMap class. |
---|
288 | ///\todo How to call these type of functions? |
---|
289 | /// |
---|
290 | ///\relates AddMap |
---|
291 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
292 | template<class M1,class M2> |
---|
293 | inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2) |
---|
294 | { |
---|
295 | return AddMap<M1,M2>(m1,m2); |
---|
296 | } |
---|
297 | |
---|
298 | ///Shift a maps with a constant. |
---|
299 | |
---|
300 | ///This \ref concept::ReadMap "read only map" returns the sum of the |
---|
301 | ///given map and a constant value. |
---|
302 | ///Its \c Key and \c Value is inherited from \c M. |
---|
303 | /// |
---|
304 | ///Actually, |
---|
305 | ///\code |
---|
306 | /// ShiftMap<X> sh(x,v); |
---|
307 | ///\endcode |
---|
308 | ///it is equivalent with |
---|
309 | ///\code |
---|
310 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
311 | /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
---|
312 | ///\endcode |
---|
313 | template<class M> |
---|
314 | class ShiftMap |
---|
315 | { |
---|
316 | typename SmartConstReference<M>::Type m; |
---|
317 | typename M::Value v; |
---|
318 | public: |
---|
319 | |
---|
320 | typedef True NeedCopy; |
---|
321 | typedef typename M::Key Key; |
---|
322 | typedef typename M::Value Value; |
---|
323 | |
---|
324 | ///Constructor |
---|
325 | |
---|
326 | ///Constructor |
---|
327 | ///\param _m is the undelying map |
---|
328 | ///\param _v is the shift value |
---|
329 | ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; |
---|
330 | Value operator[](Key k) const {return m[k]+v;} |
---|
331 | }; |
---|
332 | |
---|
333 | ///Returns an \ref ShiftMap class |
---|
334 | |
---|
335 | ///This function just returns an \ref ShiftMap class. |
---|
336 | ///\relates ShiftMap |
---|
337 | ///\todo A better name is required. |
---|
338 | template<class M> |
---|
339 | inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v) |
---|
340 | { |
---|
341 | return ShiftMap<M>(m,v); |
---|
342 | } |
---|
343 | |
---|
344 | ///Difference of two maps |
---|
345 | |
---|
346 | ///This \ref concept::ReadMap "read only map" returns the difference |
---|
347 | ///of the values returned by the two |
---|
348 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
349 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
350 | |
---|
351 | template<class M1,class M2> |
---|
352 | class SubMap |
---|
353 | { |
---|
354 | typename SmartConstReference<M1>::Type m1; |
---|
355 | typename SmartConstReference<M2>::Type m2; |
---|
356 | public: |
---|
357 | |
---|
358 | typedef True NeedCopy; |
---|
359 | typedef typename M1::Key Key; |
---|
360 | typedef typename M1::Value Value; |
---|
361 | |
---|
362 | ///Constructor |
---|
363 | |
---|
364 | ///\e |
---|
365 | /// |
---|
366 | SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
367 | Value operator[](Key k) const {return m1[k]-m2[k];} |
---|
368 | }; |
---|
369 | |
---|
370 | ///Returns a \ref SubMap class |
---|
371 | |
---|
372 | ///This function just returns a \ref SubMap class. |
---|
373 | /// |
---|
374 | ///\relates SubMap |
---|
375 | template<class M1,class M2> |
---|
376 | inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2) |
---|
377 | { |
---|
378 | return SubMap<M1,M2>(m1,m2); |
---|
379 | } |
---|
380 | |
---|
381 | ///Product of two maps |
---|
382 | |
---|
383 | ///This \ref concept::ReadMap "read only map" returns the product of the |
---|
384 | ///values returned by the two |
---|
385 | ///given |
---|
386 | ///maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
387 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
388 | |
---|
389 | template<class M1,class M2> |
---|
390 | class MulMap |
---|
391 | { |
---|
392 | typename SmartConstReference<M1>::Type m1; |
---|
393 | typename SmartConstReference<M2>::Type m2; |
---|
394 | public: |
---|
395 | |
---|
396 | typedef True NeedCopy; |
---|
397 | typedef typename M1::Key Key; |
---|
398 | typedef typename M1::Value Value; |
---|
399 | |
---|
400 | ///Constructor |
---|
401 | |
---|
402 | ///\e |
---|
403 | /// |
---|
404 | MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
405 | Value operator[](Key k) const {return m1[k]*m2[k];} |
---|
406 | }; |
---|
407 | |
---|
408 | ///Returns a \ref MulMap class |
---|
409 | |
---|
410 | ///This function just returns a \ref MulMap class. |
---|
411 | ///\relates MulMap |
---|
412 | template<class M1,class M2> |
---|
413 | inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2) |
---|
414 | { |
---|
415 | return MulMap<M1,M2>(m1,m2); |
---|
416 | } |
---|
417 | |
---|
418 | ///Scale a maps with a constant. |
---|
419 | |
---|
420 | ///This \ref concept::ReadMap "read only map" returns the value of the |
---|
421 | ///given map multipied with a constant value. |
---|
422 | ///Its \c Key and \c Value is inherited from \c M. |
---|
423 | /// |
---|
424 | ///Actually, |
---|
425 | ///\code |
---|
426 | /// ScaleMap<X> sc(x,v); |
---|
427 | ///\endcode |
---|
428 | ///it is equivalent with |
---|
429 | ///\code |
---|
430 | /// ConstMap<X::Key, X::Value> c_tmp(v); |
---|
431 | /// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
---|
432 | ///\endcode |
---|
433 | template<class M> |
---|
434 | class ScaleMap |
---|
435 | { |
---|
436 | typename SmartConstReference<M>::Type m; |
---|
437 | typename M::Value v; |
---|
438 | public: |
---|
439 | |
---|
440 | typedef True NeedCopy; |
---|
441 | typedef typename M::Key Key; |
---|
442 | typedef typename M::Value Value; |
---|
443 | |
---|
444 | ///Constructor |
---|
445 | |
---|
446 | ///Constructor |
---|
447 | ///\param _m is the undelying map |
---|
448 | ///\param _v is the scaling value |
---|
449 | ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {}; |
---|
450 | Value operator[](Key k) const {return m[k]*v;} |
---|
451 | }; |
---|
452 | |
---|
453 | ///Returns an \ref ScaleMap class |
---|
454 | |
---|
455 | ///This function just returns an \ref ScaleMap class. |
---|
456 | ///\relates ScaleMap |
---|
457 | ///\todo A better name is required. |
---|
458 | template<class M> |
---|
459 | inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v) |
---|
460 | { |
---|
461 | return ScaleMap<M>(m,v); |
---|
462 | } |
---|
463 | |
---|
464 | ///Quotient of two maps |
---|
465 | |
---|
466 | ///This \ref concept::ReadMap "read only map" returns the quotient of the |
---|
467 | ///values returned by the two |
---|
468 | ///given maps. Its \c Key and \c Value will be inherited from \c M1. |
---|
469 | ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
---|
470 | |
---|
471 | template<class M1,class M2> |
---|
472 | class DivMap |
---|
473 | { |
---|
474 | typename SmartConstReference<M1>::Type m1; |
---|
475 | typename SmartConstReference<M2>::Type m2; |
---|
476 | public: |
---|
477 | |
---|
478 | typedef True NeedCopy; |
---|
479 | typedef typename M1::Key Key; |
---|
480 | typedef typename M1::Value Value; |
---|
481 | |
---|
482 | ///Constructor |
---|
483 | |
---|
484 | ///\e |
---|
485 | /// |
---|
486 | DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
487 | Value operator[](Key k) const {return m1[k]/m2[k];} |
---|
488 | }; |
---|
489 | |
---|
490 | ///Returns a \ref DivMap class |
---|
491 | |
---|
492 | ///This function just returns a \ref DivMap class. |
---|
493 | ///\relates DivMap |
---|
494 | template<class M1,class M2> |
---|
495 | inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2) |
---|
496 | { |
---|
497 | return DivMap<M1,M2>(m1,m2); |
---|
498 | } |
---|
499 | |
---|
500 | ///Composition of two maps |
---|
501 | |
---|
502 | ///This \ref concept::ReadMap "read only map" returns the composition of |
---|
503 | ///two |
---|
504 | ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is |
---|
505 | ///of \c M2, |
---|
506 | ///then for |
---|
507 | ///\code |
---|
508 | /// ComposeMap<M1,M2> cm(m1,m2); |
---|
509 | ///\endcode |
---|
510 | /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt> |
---|
511 | /// |
---|
512 | ///Its \c Key is inherited from \c M2 and its \c Value is from |
---|
513 | ///\c M1. |
---|
514 | ///The \c M2::Value must be convertible to \c M1::Key. |
---|
515 | ///\todo Check the requirements. |
---|
516 | |
---|
517 | template<class M1,class M2> |
---|
518 | class ComposeMap |
---|
519 | { |
---|
520 | typename SmartConstReference<M1>::Type m1; |
---|
521 | typename SmartConstReference<M2>::Type m2; |
---|
522 | public: |
---|
523 | |
---|
524 | typedef True NeedCopy; |
---|
525 | typedef typename M2::Key Key; |
---|
526 | typedef typename M1::Value Value; |
---|
527 | |
---|
528 | ///Constructor |
---|
529 | |
---|
530 | ///\e |
---|
531 | /// |
---|
532 | ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
533 | Value operator[](Key k) const {return m1[m2[k]];} |
---|
534 | }; |
---|
535 | ///Returns a \ref ComposeMap class |
---|
536 | |
---|
537 | ///This function just returns a \ref ComposeMap class. |
---|
538 | /// |
---|
539 | ///\relates ComposeMap |
---|
540 | template<class M1,class M2> |
---|
541 | inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2) |
---|
542 | { |
---|
543 | return ComposeMap<M1,M2>(m1,m2); |
---|
544 | } |
---|
545 | |
---|
546 | ///Combine of two maps using an STL (binary) functor. |
---|
547 | |
---|
548 | ///Combine of two maps using an STL (binary) functor. |
---|
549 | /// |
---|
550 | /// |
---|
551 | ///This \ref concept::ReadMap "read only map" takes to maps and a |
---|
552 | ///binary functor and returns the composition of |
---|
553 | ///two |
---|
554 | ///given maps unsing the functor. |
---|
555 | ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
---|
556 | ///and \c f is of \c F, |
---|
557 | ///then for |
---|
558 | ///\code |
---|
559 | /// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
---|
560 | ///\endcode |
---|
561 | /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
---|
562 | /// |
---|
563 | ///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
---|
564 | ///The \c M2::Value and \c M1::Value must be convertible to the corresponding |
---|
565 | ///input parameter of \c F and the return type of \c F must be convertible |
---|
566 | ///to \c V. |
---|
567 | ///\todo Check the requirements. |
---|
568 | |
---|
569 | template<class M1,class M2,class F,class V = typename F::result_type> |
---|
570 | class CombineMap |
---|
571 | { |
---|
572 | typename SmartConstReference<M1>::Type m1; |
---|
573 | typename SmartConstReference<M2>::Type m2; |
---|
574 | F f; |
---|
575 | public: |
---|
576 | |
---|
577 | typedef True NeedCopy; |
---|
578 | typedef typename M1::Key Key; |
---|
579 | typedef V Value; |
---|
580 | |
---|
581 | ///Constructor |
---|
582 | |
---|
583 | ///\e |
---|
584 | /// |
---|
585 | CombineMap(const M1 &_m1,const M2 &_m2,const F &_f) |
---|
586 | : m1(_m1), m2(_m2), f(_f) {}; |
---|
587 | Value operator[](Key k) const {return f(m1[k],m2[k]);} |
---|
588 | }; |
---|
589 | |
---|
590 | ///Returns a \ref CombineMap class |
---|
591 | |
---|
592 | ///This function just returns a \ref CombineMap class. |
---|
593 | /// |
---|
594 | ///Only the first template parameter (the value type) must be given. |
---|
595 | /// |
---|
596 | ///For example if \c m1 and \c m2 are both \c double valued maps, then |
---|
597 | ///\code |
---|
598 | ///combineMap<double>(m1,m2,std::plus<double>) |
---|
599 | ///\endcode |
---|
600 | ///is equivalent with |
---|
601 | ///\code |
---|
602 | ///addMap(m1,m2) |
---|
603 | ///\endcode |
---|
604 | /// |
---|
605 | ///\relates CombineMap |
---|
606 | template<class M1,class M2,class F> |
---|
607 | inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) |
---|
608 | { |
---|
609 | return CombineMap<M1,M2,F>(m1,m2,f); |
---|
610 | } |
---|
611 | |
---|
612 | ///Negative value of a map |
---|
613 | |
---|
614 | ///This \ref concept::ReadMap "read only map" returns the negative |
---|
615 | ///value of the |
---|
616 | ///value returned by the |
---|
617 | ///given map. Its \c Key and \c Value will be inherited from \c M. |
---|
618 | ///The unary \c - operator must be defined for \c Value, of course. |
---|
619 | |
---|
620 | template<class M> |
---|
621 | class NegMap |
---|
622 | { |
---|
623 | typename SmartConstReference<M>::Type m; |
---|
624 | public: |
---|
625 | |
---|
626 | typedef True NeedCopy; |
---|
627 | typedef typename M::Key Key; |
---|
628 | typedef typename M::Value Value; |
---|
629 | |
---|
630 | ///Constructor |
---|
631 | |
---|
632 | ///\e |
---|
633 | /// |
---|
634 | NegMap(const M &_m) : m(_m) {}; |
---|
635 | Value operator[](Key k) const {return -m[k];} |
---|
636 | }; |
---|
637 | |
---|
638 | ///Returns a \ref NegMap class |
---|
639 | |
---|
640 | ///This function just returns a \ref NegMap class. |
---|
641 | ///\relates NegMap |
---|
642 | template<class M> |
---|
643 | inline NegMap<M> negMap(const M &m) |
---|
644 | { |
---|
645 | return NegMap<M>(m); |
---|
646 | } |
---|
647 | |
---|
648 | |
---|
649 | ///Absolute value of a map |
---|
650 | |
---|
651 | ///This \ref concept::ReadMap "read only map" returns the absolute value |
---|
652 | ///of the |
---|
653 | ///value returned by the |
---|
654 | ///given map. Its \c Key and \c Value will be inherited |
---|
655 | ///from <tt>M</tt>. <tt>Value</tt> |
---|
656 | ///must be comparable to <tt>0</tt> and the unary <tt>-</tt> |
---|
657 | ///operator must be defined for it, of course. |
---|
658 | /// |
---|
659 | ///\bug We need a unified way to handle the situation below: |
---|
660 | ///\code |
---|
661 | /// struct _UnConvertible {}; |
---|
662 | /// template<class A> inline A t_abs(A a) {return _UnConvertible();} |
---|
663 | /// template<> inline int t_abs<>(int n) {return abs(n);} |
---|
664 | /// template<> inline long int t_abs<>(long int n) {return labs(n);} |
---|
665 | /// template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);} |
---|
666 | /// template<> inline float t_abs<>(float n) {return fabsf(n);} |
---|
667 | /// template<> inline double t_abs<>(double n) {return fabs(n);} |
---|
668 | /// template<> inline long double t_abs<>(long double n) {return fabsl(n);} |
---|
669 | ///\endcode |
---|
670 | |
---|
671 | |
---|
672 | template<class M> |
---|
673 | class AbsMap |
---|
674 | { |
---|
675 | typename SmartConstReference<M>::Type m; |
---|
676 | public: |
---|
677 | |
---|
678 | typedef True NeedCopy; |
---|
679 | typedef typename M::Key Key; |
---|
680 | typedef typename M::Value Value; |
---|
681 | |
---|
682 | ///Constructor |
---|
683 | |
---|
684 | ///\e |
---|
685 | /// |
---|
686 | AbsMap(const M &_m) : m(_m) {}; |
---|
687 | Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;} |
---|
688 | }; |
---|
689 | |
---|
690 | ///Returns a \ref AbsMap class |
---|
691 | |
---|
692 | ///This function just returns a \ref AbsMap class. |
---|
693 | ///\relates AbsMap |
---|
694 | template<class M> |
---|
695 | inline AbsMap<M> absMap(const M &m) |
---|
696 | { |
---|
697 | return AbsMap<M>(m); |
---|
698 | } |
---|
699 | |
---|
700 | ///Converts an STL style functor to a map |
---|
701 | |
---|
702 | ///This \ref concept::ReadMap "read only map" returns the value |
---|
703 | ///of a |
---|
704 | ///given map. |
---|
705 | /// |
---|
706 | ///Template parameters \c K and \c V will become its |
---|
707 | ///\c Key and \c Value. They must be given explicitely |
---|
708 | ///because a functor does not provide such typedefs. |
---|
709 | /// |
---|
710 | ///Parameter \c F is the type of the used functor. |
---|
711 | |
---|
712 | |
---|
713 | template<class K,class V,class F> |
---|
714 | class FunctorMap |
---|
715 | { |
---|
716 | const F &f; |
---|
717 | public: |
---|
718 | |
---|
719 | typedef True NeedCopy; |
---|
720 | typedef K Key; |
---|
721 | typedef V Value; |
---|
722 | |
---|
723 | ///Constructor |
---|
724 | |
---|
725 | ///\e |
---|
726 | /// |
---|
727 | FunctorMap(const F &_f) : f(_f) {}; |
---|
728 | Value operator[](Key k) const {return f(k);} |
---|
729 | }; |
---|
730 | |
---|
731 | ///Returns a \ref FunctorMap class |
---|
732 | |
---|
733 | ///This function just returns a \ref FunctorMap class. |
---|
734 | /// |
---|
735 | ///The third template parameter isn't necessary to be given. |
---|
736 | ///\relates FunctorMap |
---|
737 | template<class K,class V, class F> |
---|
738 | inline FunctorMap<K,V,F> functorMap(const F &f) |
---|
739 | { |
---|
740 | return FunctorMap<K,V,F>(f); |
---|
741 | } |
---|
742 | |
---|
743 | ///Converts a map to an STL style (unary) functor |
---|
744 | |
---|
745 | ///This class Converts a map to an STL style (unary) functor. |
---|
746 | ///that is it provides an <tt>operator()</tt> to read its values. |
---|
747 | /// |
---|
748 | ///For the sake of convenience it also works as |
---|
749 | ///a ususal \ref concept::ReadMap "readable map", i.e |
---|
750 | ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
---|
751 | |
---|
752 | template<class M> |
---|
753 | class MapFunctor |
---|
754 | { |
---|
755 | typename SmartConstReference<M>::Type m; |
---|
756 | public: |
---|
757 | |
---|
758 | typedef True NeedCopy; |
---|
759 | typedef typename M::Key argument_type; |
---|
760 | typedef typename M::Value result_type; |
---|
761 | typedef typename M::Key Key; |
---|
762 | typedef typename M::Value Value; |
---|
763 | |
---|
764 | ///Constructor |
---|
765 | |
---|
766 | ///\e |
---|
767 | /// |
---|
768 | MapFunctor(const M &_m) : m(_m) {}; |
---|
769 | ///Returns a value of the map |
---|
770 | |
---|
771 | ///\e |
---|
772 | /// |
---|
773 | Value operator()(Key k) const {return m[k];} |
---|
774 | ///\e |
---|
775 | /// |
---|
776 | Value operator[](Key k) const {return m[k];} |
---|
777 | }; |
---|
778 | |
---|
779 | ///Returns a \ref MapFunctor class |
---|
780 | |
---|
781 | ///This function just returns a \ref MapFunctor class. |
---|
782 | ///\relates MapFunctor |
---|
783 | template<class M> |
---|
784 | inline MapFunctor<M> mapFunctor(const M &m) |
---|
785 | { |
---|
786 | return MapFunctor<M>(m); |
---|
787 | } |
---|
788 | |
---|
789 | |
---|
790 | ///Apply all map setting operations to two maps |
---|
791 | |
---|
792 | ///This map has two \ref concept::WriteMap "writable map" |
---|
793 | ///parameters and each write request will be passed to both of them. |
---|
794 | ///If \c M1 is also \ref concept::ReadMap "readable", |
---|
795 | ///then the read operations will return the |
---|
796 | ///corresponding values of \c M1. |
---|
797 | /// |
---|
798 | ///The \c Key and \c Value will be inherited from \c M1. |
---|
799 | ///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
---|
800 | |
---|
801 | template<class M1,class M2> |
---|
802 | class ForkMap |
---|
803 | { |
---|
804 | typename SmartConstReference<M1>::Type m1; |
---|
805 | typename SmartConstReference<M2>::Type m2; |
---|
806 | public: |
---|
807 | |
---|
808 | typedef True NeedCopy; |
---|
809 | typedef typename M1::Key Key; |
---|
810 | typedef typename M1::Value Value; |
---|
811 | |
---|
812 | ///Constructor |
---|
813 | |
---|
814 | ///\e |
---|
815 | /// |
---|
816 | ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
---|
817 | Value operator[](Key k) const {return m1[k];} |
---|
818 | void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);} |
---|
819 | }; |
---|
820 | |
---|
821 | ///Returns an \ref ForkMap class |
---|
822 | |
---|
823 | ///This function just returns an \ref ForkMap class. |
---|
824 | ///\todo How to call these type of functions? |
---|
825 | /// |
---|
826 | ///\relates ForkMap |
---|
827 | ///\todo Wrong scope in Doxygen when \c \\relates is used |
---|
828 | template<class M1,class M2> |
---|
829 | inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2) |
---|
830 | { |
---|
831 | return ForkMap<M1,M2>(m1,m2); |
---|
832 | } |
---|
833 | |
---|
834 | /// @} |
---|
835 | |
---|
836 | } |
---|
837 | |
---|
838 | |
---|
839 | #endif // LEMON_MAPS_H |
---|