COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/maps.h @ 1675:fa89ffb27a6d

Last change on this file since 1675:fa89ffb27a6d was 1675:fa89ffb27a6d, checked in by Balazs Dezso, 14 years ago

Redesign of the map adaptors.
/smart reference handling only used by functions/

Better handling of the function objects and functions.


\todo May we use operators instead of the addMap, subMap...?

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