COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/maps.h @ 1531:a3b20dd847b5

Last change on this file since 1531:a3b20dd847b5 was 1531:a3b20dd847b5, checked in by Balazs Dezso, 19 years ago

New graph copy interface

File size: 22.6 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
[890]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  };
[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
[1178]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  ///Actually,
231  ///\code
232  ///  ConvertMap<X> sh(x,v);
233  ///\endcode
234  ///it is equivalent with
235  ///\code
236  ///  ConstMap<X::Key, X::Value> c_tmp(v);
237  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
238  ///\endcode
239  ///\bug wrong documentation
240  template<class M, class T>
[1420]241  class ConvertMap {
242    typename SmartConstReference<M>::Type m;
[1178]243  public:
[1420]244
245    typedef True NeedCopy;
246
[1456]247    ///\e
[1178]248    typedef typename M::Key Key;
[1456]249    ///\e
[1178]250    typedef T Value;
251
252    ///Constructor
253
254    ///Constructor
255    ///\param _m is the undelying map
256    ///\param _v is the convert value
257    ConvertMap(const M &_m) : m(_m) {};
[1346]258
259    /// \brief The subscript operator.
260    ///
261    /// The subscript operator.
262    /// \param edge The edge
263    /// \return The target of the edge
[1178]264    Value operator[](Key k) const {return m[k];}
265  };
266 
267  ///Returns an \ref ConvertMap class
268
269  ///This function just returns an \ref ConvertMap class.
270  ///\relates ConvertMap
271  ///\todo The order of the template parameters are changed.
272  template<class T, class M>
273  inline ConvertMap<M,T> convertMap(const M &m)
274  {
275    return ConvertMap<M,T>(m);
276  }
[1041]277
278  ///Sum of two maps
279
280  ///This \ref concept::ReadMap "read only map" returns the sum of the two
281  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
282  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
283
284  template<class M1,class M2>
285  class AddMap
286  {
[1420]287    typename SmartConstReference<M1>::Type m1;
288    typename SmartConstReference<M2>::Type m2;
289
[1041]290  public:
[1420]291
292    typedef True NeedCopy;
293
[1456]294    ///\e
[1041]295    typedef typename M1::Key Key;
[1456]296    ///\e
[1041]297    typedef typename M1::Value Value;
298
299    ///Constructor
300
301    ///\e
302    ///
303    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]304    Value operator[](Key k) const {return m1[k]+m2[k];}
[1041]305  };
306 
307  ///Returns an \ref AddMap class
308
309  ///This function just returns an \ref AddMap class.
310  ///\todo How to call these type of functions?
311  ///
312  ///\relates AddMap
313  ///\todo Wrong scope in Doxygen when \c \\relates is used
314  template<class M1,class M2>
315  inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
316  {
317    return AddMap<M1,M2>(m1,m2);
318  }
319
[1070]320  ///Shift a maps with a constant.
321
322  ///This \ref concept::ReadMap "read only map" returns the sum of the
323  ///given map and a constant value.
324  ///Its \c Key and \c Value is inherited from \c M.
325  ///
326  ///Actually,
327  ///\code
328  ///  ShiftMap<X> sh(x,v);
329  ///\endcode
330  ///it is equivalent with
331  ///\code
332  ///  ConstMap<X::Key, X::Value> c_tmp(v);
333  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
334  ///\endcode
335  template<class M>
336  class ShiftMap
337  {
[1420]338    typename SmartConstReference<M>::Type m;
[1070]339    typename M::Value v;
340  public:
[1420]341
342    typedef True NeedCopy;
[1456]343    ///\e
[1070]344    typedef typename M::Key Key;
[1456]345    ///\e
[1070]346    typedef typename M::Value Value;
347
348    ///Constructor
349
350    ///Constructor
351    ///\param _m is the undelying map
352    ///\param _v is the shift value
353    ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
354    Value operator[](Key k) const {return m[k]+v;}
355  };
356 
357  ///Returns an \ref ShiftMap class
358
359  ///This function just returns an \ref ShiftMap class.
360  ///\relates ShiftMap
361  ///\todo A better name is required.
362  template<class M>
363  inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
364  {
365    return ShiftMap<M>(m,v);
366  }
367
[1041]368  ///Difference of two maps
369
370  ///This \ref concept::ReadMap "read only map" returns the difference
371  ///of the values returned by the two
372  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
373  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
374
375  template<class M1,class M2>
376  class SubMap
377  {
[1420]378    typename SmartConstReference<M1>::Type m1;
379    typename SmartConstReference<M2>::Type m2;
[1041]380  public:
[1420]381
382    typedef True NeedCopy;
[1456]383    ///\e
[1041]384    typedef typename M1::Key Key;
[1456]385    ///\e
[1041]386    typedef typename M1::Value Value;
387
388    ///Constructor
389
390    ///\e
391    ///
392    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]393    Value operator[](Key k) const {return m1[k]-m2[k];}
[1041]394  };
395 
396  ///Returns a \ref SubMap class
397
398  ///This function just returns a \ref SubMap class.
399  ///
400  ///\relates SubMap
401  template<class M1,class M2>
402  inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
403  {
404    return SubMap<M1,M2>(m1,m2);
405  }
406
407  ///Product of two maps
408
409  ///This \ref concept::ReadMap "read only map" returns the product of the
410  ///values returned by the two
411  ///given
412  ///maps. Its \c Key and \c Value will be inherited from \c M1.
413  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
414
415  template<class M1,class M2>
416  class MulMap
417  {
[1420]418    typename SmartConstReference<M1>::Type m1;
419    typename SmartConstReference<M2>::Type m2;
[1041]420  public:
[1420]421
422    typedef True NeedCopy;
[1456]423    ///\e
[1041]424    typedef typename M1::Key Key;
[1456]425    ///\e
[1041]426    typedef typename M1::Value Value;
427
428    ///Constructor
429
430    ///\e
431    ///
432    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]433    Value operator[](Key k) const {return m1[k]*m2[k];}
[1041]434  };
435 
436  ///Returns a \ref MulMap class
437
438  ///This function just returns a \ref MulMap class.
439  ///\relates MulMap
440  template<class M1,class M2>
441  inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
442  {
443    return MulMap<M1,M2>(m1,m2);
444  }
445 
[1070]446  ///Scale a maps with a constant.
447
448  ///This \ref concept::ReadMap "read only map" returns the value of the
449  ///given map multipied with a constant value.
450  ///Its \c Key and \c Value is inherited from \c M.
451  ///
452  ///Actually,
453  ///\code
454  ///  ScaleMap<X> sc(x,v);
455  ///\endcode
456  ///it is equivalent with
457  ///\code
458  ///  ConstMap<X::Key, X::Value> c_tmp(v);
459  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
460  ///\endcode
461  template<class M>
462  class ScaleMap
463  {
[1420]464    typename SmartConstReference<M>::Type m;
[1070]465    typename M::Value v;
466  public:
[1420]467
468    typedef True NeedCopy;
[1456]469    ///\e
[1070]470    typedef typename M::Key Key;
[1456]471    ///\e
[1070]472    typedef typename M::Value Value;
473
474    ///Constructor
475
476    ///Constructor
477    ///\param _m is the undelying map
478    ///\param _v is the scaling value
479    ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
480    Value operator[](Key k) const {return m[k]*v;}
481  };
482 
483  ///Returns an \ref ScaleMap class
484
485  ///This function just returns an \ref ScaleMap class.
486  ///\relates ScaleMap
487  ///\todo A better name is required.
488  template<class M>
489  inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
490  {
491    return ScaleMap<M>(m,v);
492  }
493
[1041]494  ///Quotient of two maps
495
496  ///This \ref concept::ReadMap "read only map" returns the quotient of the
497  ///values returned by the two
498  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
499  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
500
501  template<class M1,class M2>
502  class DivMap
503  {
[1420]504    typename SmartConstReference<M1>::Type m1;
505    typename SmartConstReference<M2>::Type m2;
[1041]506  public:
[1420]507
508    typedef True NeedCopy;
[1456]509    ///\e
[1041]510    typedef typename M1::Key Key;
[1456]511    ///\e
[1041]512    typedef typename M1::Value Value;
513
514    ///Constructor
515
516    ///\e
517    ///
518    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]519    Value operator[](Key k) const {return m1[k]/m2[k];}
[1041]520  };
521 
522  ///Returns a \ref DivMap class
523
524  ///This function just returns a \ref DivMap class.
525  ///\relates DivMap
526  template<class M1,class M2>
527  inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
528  {
529    return DivMap<M1,M2>(m1,m2);
530  }
531 
532  ///Composition of two maps
533
534  ///This \ref concept::ReadMap "read only map" returns the composition of
535  ///two
536  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
537  ///of \c M2,
538  ///then for
539  ///\code
540  ///  ComposeMap<M1,M2> cm(m1,m2);
541  ///\endcode
[1044]542  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
[1041]543  ///
544  ///Its \c Key is inherited from \c M2 and its \c Value is from
545  ///\c M1.
546  ///The \c M2::Value must be convertible to \c M1::Key.
547  ///\todo Check the requirements.
548
549  template<class M1,class M2>
550  class ComposeMap
551  {
[1420]552    typename SmartConstReference<M1>::Type m1;
553    typename SmartConstReference<M2>::Type m2;
[1041]554  public:
[1420]555
556    typedef True NeedCopy;
[1456]557    ///\e
[1041]558    typedef typename M2::Key Key;
[1456]559    ///\e
[1041]560    typedef typename M1::Value Value;
561
562    ///Constructor
563
564    ///\e
565    ///
566    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]567    Value operator[](Key k) const {return m1[m2[k]];}
[1041]568  };
569  ///Returns a \ref ComposeMap class
570
571  ///This function just returns a \ref ComposeMap class.
[1219]572  ///
[1041]573  ///\relates ComposeMap
574  template<class M1,class M2>
575  inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
576  {
577    return ComposeMap<M1,M2>(m1,m2);
578  }
[1219]579 
580  ///Combine of two maps using an STL (binary) functor.
581
582  ///Combine of two maps using an STL (binary) functor.
583  ///
584  ///
585  ///This \ref concept::ReadMap "read only map" takes to maps and a
586  ///binary functor and returns the composition of
587  ///two
588  ///given maps unsing the functor.
589  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
590  ///and \c f is of \c F,
591  ///then for
592  ///\code
593  ///  CombineMap<M1,M2,F,V> cm(m1,m2,f);
594  ///\endcode
595  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
596  ///
597  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
598  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
599  ///input parameter of \c F and the return type of \c F must be convertible
600  ///to \c V.
601  ///\todo Check the requirements.
602
[1420]603  template<class M1,class M2,class F,class V = typename F::result_type>
[1219]604  class CombineMap
605  {
[1420]606    typename SmartConstReference<M1>::Type m1;
607    typename SmartConstReference<M2>::Type m2;
608    F f;
[1219]609  public:
[1420]610
611    typedef True NeedCopy;
[1456]612    ///\e
[1219]613    typedef typename M1::Key Key;
[1456]614    ///\e
[1219]615    typedef V Value;
616
617    ///Constructor
618
619    ///\e
620    ///
621    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
622      : m1(_m1), m2(_m2), f(_f) {};
623    Value operator[](Key k) const {return f(m1[k],m2[k]);}
624  };
625 
626  ///Returns a \ref CombineMap class
627
628  ///This function just returns a \ref CombineMap class.
629  ///
630  ///Only the first template parameter (the value type) must be given.
631  ///
632  ///For example if \c m1 and \c m2 are both \c double valued maps, then
633  ///\code
634  ///combineMap<double>(m1,m2,std::plus<double>)
635  ///\endcode
636  ///is equivalent with
637  ///\code
638  ///addMap(m1,m2)
639  ///\endcode
640  ///
641  ///\relates CombineMap
[1420]642  template<class M1,class M2,class F>
643  inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f)
[1219]644  {
[1420]645    return CombineMap<M1,M2,F>(m1,m2,f);
[1219]646  }
[1041]647
648  ///Negative value of a map
649
650  ///This \ref concept::ReadMap "read only map" returns the negative
651  ///value of the
652  ///value returned by the
653  ///given map. Its \c Key and \c Value will be inherited from \c M.
654  ///The unary \c - operator must be defined for \c Value, of course.
655
656  template<class M>
657  class NegMap
658  {
[1420]659    typename SmartConstReference<M>::Type m;
[1041]660  public:
[1420]661
662    typedef True NeedCopy;
[1456]663    ///\e
[1041]664    typedef typename M::Key Key;
[1456]665    ///\e
[1041]666    typedef typename M::Value Value;
667
668    ///Constructor
669
670    ///\e
671    ///
672    NegMap(const M &_m) : m(_m) {};
[1044]673    Value operator[](Key k) const {return -m[k];}
[1041]674  };
675 
676  ///Returns a \ref NegMap class
677
678  ///This function just returns a \ref NegMap class.
679  ///\relates NegMap
680  template<class M>
681  inline NegMap<M> negMap(const M &m)
682  {
683    return NegMap<M>(m);
684  }
685
686
687  ///Absolute value of a map
688
689  ///This \ref concept::ReadMap "read only map" returns the absolute value
690  ///of the
691  ///value returned by the
[1044]692  ///given map. Its \c Key and \c Value will be inherited
693  ///from <tt>M</tt>. <tt>Value</tt>
694  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
695  ///operator must be defined for it, of course.
696  ///
697  ///\bug We need a unified way to handle the situation below:
698  ///\code
699  ///  struct _UnConvertible {};
700  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
701  ///  template<> inline int t_abs<>(int n) {return abs(n);}
702  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
703  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
704  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
705  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
706  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
707  ///\endcode
708 
[1041]709
710  template<class M>
711  class AbsMap
712  {
[1420]713    typename SmartConstReference<M>::Type m;
[1041]714  public:
[1420]715
716    typedef True NeedCopy;
[1456]717    ///\e
[1041]718    typedef typename M::Key Key;
[1456]719    ///\e
[1041]720    typedef typename M::Value Value;
721
722    ///Constructor
723
724    ///\e
725    ///
726    AbsMap(const M &_m) : m(_m) {};
[1044]727    Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
[1041]728  };
729 
730  ///Returns a \ref AbsMap class
731
732  ///This function just returns a \ref AbsMap class.
733  ///\relates AbsMap
734  template<class M>
735  inline AbsMap<M> absMap(const M &m)
736  {
737    return AbsMap<M>(m);
738  }
739
[1402]740  ///Converts an STL style functor to a map
[1076]741
742  ///This \ref concept::ReadMap "read only map" returns the value
743  ///of a
744  ///given map.
745  ///
746  ///Template parameters \c K and \c V will become its
747  ///\c Key and \c Value. They must be given explicitely
748  ///because a functor does not provide such typedefs.
749  ///
750  ///Parameter \c F is the type of the used functor.
751 
752
753  template<class K,class V,class F>
754  class FunctorMap
755  {
756    const F &f;
757  public:
[1420]758
759    typedef True NeedCopy;
[1456]760    ///\e
[1076]761    typedef K Key;
[1456]762    ///\e
[1076]763    typedef V Value;
764
765    ///Constructor
766
767    ///\e
768    ///
769    FunctorMap(const F &_f) : f(_f) {};
770    Value operator[](Key k) const {return f(k);}
771  };
772 
773  ///Returns a \ref FunctorMap class
774
775  ///This function just returns a \ref FunctorMap class.
776  ///
777  ///The third template parameter isn't necessary to be given.
778  ///\relates FunctorMap
779  template<class K,class V, class F>
780  inline FunctorMap<K,V,F> functorMap(const F &f)
781  {
782    return FunctorMap<K,V,F>(f);
783  }
784
[1219]785  ///Converts a map to an STL style (unary) functor
[1076]786
[1219]787  ///This class Converts a map to an STL style (unary) functor.
[1076]788  ///that is it provides an <tt>operator()</tt> to read its values.
789  ///
[1223]790  ///For the sake of convenience it also works as
791  ///a ususal \ref concept::ReadMap "readable map", i.e
[1172]792  ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
[1076]793
794  template<class M>
795  class MapFunctor
796  {
[1420]797    typename SmartConstReference<M>::Type m;
[1076]798  public:
[1420]799
800    typedef True NeedCopy;
[1456]801    ///\e
[1223]802    typedef typename M::Key argument_type;
[1456]803    ///\e
[1223]804    typedef typename M::Value result_type;
[1456]805    ///\e
[1076]806    typedef typename M::Key Key;
[1456]807    ///\e
[1076]808    typedef typename M::Value Value;
809
810    ///Constructor
811
812    ///\e
813    ///
814    MapFunctor(const M &_m) : m(_m) {};
815    ///Returns a value of the map
816   
817    ///\e
818    ///
819    Value operator()(Key k) const {return m[k];}
820    ///\e
821    ///
822    Value operator[](Key k) const {return m[k];}
823  };
824 
825  ///Returns a \ref MapFunctor class
826
827  ///This function just returns a \ref MapFunctor class.
828  ///\relates MapFunctor
829  template<class M>
830  inline MapFunctor<M> mapFunctor(const M &m)
831  {
832    return MapFunctor<M>(m);
833  }
834
835
[1219]836  ///Apply all map setting operations to two maps
837
838  ///This map has two \ref concept::WriteMap "writable map"
839  ///parameters and each write request will be passed to both of them.
840  ///If \c M1 is also \ref concept::ReadMap "readable",
841  ///then the read operations will return the
[1317]842  ///corresponding values of \c M1.
[1219]843  ///
844  ///The \c Key and \c Value will be inherited from \c M1.
845  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
846
847  template<class M1,class M2>
848  class ForkMap
849  {
[1420]850    typename SmartConstReference<M1>::Type m1;
851    typename SmartConstReference<M2>::Type m2;
[1219]852  public:
[1420]853
854    typedef True NeedCopy;
[1456]855    ///\e
[1219]856    typedef typename M1::Key Key;
[1456]857    ///\e
[1219]858    typedef typename M1::Value Value;
859
860    ///Constructor
861
862    ///\e
863    ///
864    ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
865    Value operator[](Key k) const {return m1[k];}
866    void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
867  };
868 
869  ///Returns an \ref ForkMap class
870
871  ///This function just returns an \ref ForkMap class.
872  ///\todo How to call these type of functions?
873  ///
874  ///\relates ForkMap
875  ///\todo Wrong scope in Doxygen when \c \\relates is used
876  template<class M1,class M2>
877  inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2)
878  {
879    return ForkMap<M1,M2>(m1,m2);
880  }
881
[1456]882
883 
884  /* ************* BOOL MAPS ******************* */
885 
886  ///Logical 'not' of a map
887 
888  ///This bool \ref concept::ReadMap "read only map" returns the
889  ///logical negation of
890  ///value returned by the
891  ///given map. Its \c Key and will be inherited from \c M,
892  ///its Value is <tt>bool</tt>.
893
894  template<class M>
895  class NotMap
896  {
897    typename SmartConstReference<M>::Type m;
898  public:
899
900    typedef True NeedCopy;
901    ///\e
902    typedef typename M::Key Key;
903    ///\e
904    typedef bool Value;
905
906    ///Constructor
907
908    ///\e
909    ///
910    NotMap(const M &_m) : m(_m) {};
911    Value operator[](Key k) const {return !m[k];}
912  };
913 
914  ///Returns a \ref NotMap class
915 
916  ///This function just returns a \ref NotMap class.
917  ///\relates NotMap
918  template<class M>
919  inline NotMap<M> notMap(const M &m)
920  {
921    return NotMap<M>(m);
922  }
923
924
925
926
927
928
929
930
931
932
933
[1041]934  /// @}
[286]935}
[1041]936
[921]937#endif // LEMON_MAPS_H
Note: See TracBrowser for help on using the repository browser.