COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/maps.h @ 1575:438bc5defad1

Last change on this file since 1575:438bc5defad1 was 1555:48769ac7ec32, checked in by Alpar Juttner, 19 years ago

Doc improvement

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