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, 14 years ago

New graph copy interface

File size: 22.6 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>
43  class MapBase
44  {
45  public:
46    ///\e
47    typedef K Key;
48    ///\e
49    typedef T Value;
50  };
51
52  /// Null map. (a.k.a. DoNothingMap)
53
54  /// If you have to provide a map only for its type definitions,
55  /// or if you have to provide a writable map, but
56  /// data written to it will sent to <tt>/dev/null</tt>...
57  template<typename K, typename T>
58  class NullMap : public MapBase<K,T>
59  {
60  public:
61   
62    typedef True NeedCopy;
63
64    /// Gives back a default constructed element.
65    T operator[](const K&) const { return T(); }
66    /// Absorbs the value.
67    void set(const K&, const T&) {}
68  };
69
70  template <typename K, typename V>
71  NullMap<K, V> nullMap() {
72    return NullMap<K, V>();
73  }
74
75
76  /// Constant map.
77
78  /// This is a readable map which assigns a specified value to each key.
79  /// In other aspects it is equivalent to the \ref NullMap.
80  /// \todo set could be used to set the value.
81  template<typename K, typename T>
82  class ConstMap : public MapBase<K,T>
83  {
84    T v;
85  public:
86
87    typedef True NeedCopy;
88
89    /// Default constructor
90
91    /// The value of the map will be uninitialized.
92    /// (More exactly it will be default constructed.)
93    ConstMap() {}
94    ///\e
95
96    /// \param _v The initial value of the map.
97    ///
98    ConstMap(const T &_v) : v(_v) {}
99
100    T operator[](const K&) const { return v; }
101    void set(const K&, const T&) {}
102
103    template<typename T1>
104    struct rebind {
105      typedef ConstMap<K,T1> other;
106    };
107
108    template<typename T1>
109    ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
110  };
111
112  ///Returns a \ref ConstMap class
113
114  ///This function just returns a \ref ConstMap class.
115  ///\relates ConstMap
116  template<class V,class K>
117  inline ConstMap<V,K> constMap(const K &k)
118  {
119    return ConstMap<V,K>(k);
120  }
121
122
123  //to document later
124  template<typename T, T v>
125  struct Const { };
126  //to document later
127  template<typename K, typename V, V v>
128  class ConstMap<K, Const<V, v> > : public MapBase<K, V>
129  {
130  public:
131    ConstMap() { }
132    V operator[](const K&) const { return v; }
133    void set(const K&, const V&) { }
134  };
135
136  /// \c std::map wrapper
137
138  /// This is essentially a wrapper for \c std::map. With addition that
139  /// you can specify a default value different from \c Value() .
140  ///
141  /// \todo Provide allocator parameter...
142  template <typename K, typename T, typename Compare = std::less<K> >
143  class StdMap : public std::map<K,T,Compare> {
144    typedef std::map<K,T,Compare> parent;
145    T v;
146    typedef typename parent::value_type PairType;
147
148  public:
149    ///\e
150    typedef K Key;
151    ///\e
152    typedef T Value;
153    ///\e
154    typedef T& Reference;
155    ///\e
156    typedef const T& ConstReference;
157
158
159    StdMap() : v() {}
160    /// Constructor with specified default value
161    StdMap(const T& _v) : v(_v) {}
162
163    /// \brief Constructs the map from an appropriate std::map.
164    ///
165    /// \warning Inefficient: copies the content of \c m !
166    StdMap(const parent &m) : parent(m) {}
167    /// \brief Constructs the map from an appropriate std::map, and explicitly
168    /// specifies a default value.
169    ///
170    /// \warning Inefficient: copies the content of \c m !
171    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
172   
173    template<typename T1, typename Comp1>
174    StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
175      //FIXME;
176    }
177
178    Reference operator[](const Key &k) {
179      return insert(PairType(k,v)).first -> second;
180    }
181    ConstReference operator[](const Key &k) const {
182      typename parent::iterator i = lower_bound(k);
183      if (i == parent::end() || parent::key_comp()(k, (*i).first))
184        return v;
185      return (*i).second;
186    }
187    void set(const Key &k, const T &t) {
188      parent::operator[](k) = t;
189    }
190
191    /// Changes the default value of the map.
192    /// \return Returns the previous default value.
193    ///
194    /// \warning The value of some keys (which has already been queried, but
195    /// the value has been unchanged from the default) may change!
196    T setDefault(const T &_v) { T old=v; v=_v; return old; }
197
198    template<typename T1>
199    struct rebind {
200      typedef StdMap<Key,T1,Compare> other;
201    };
202  };
203
204  /// @}
205
206  /// \addtogroup map_adaptors
207  /// @{
208
209  /// \brief Identity mapping.
210  ///
211  /// This mapping gives back the given key as value without any
212  /// modification.
213  template <typename T>
214  class IdentityMap {
215  public:
216    typedef T Key;
217    typedef T Value;
218
219    const Value& operator[](const Key& t) const {
220      return t;
221    }
222  };
223
224  ///Convert the \c Value of a maps to another type.
225
226  ///This \ref concept::ReadMap "read only map"
227  ///converts the \c Value of a maps to type \c T.
228  ///Its \c Value is inherited from \c M.
229  ///
230  ///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>
241  class ConvertMap {
242    typename SmartConstReference<M>::Type m;
243  public:
244
245    typedef True NeedCopy;
246
247    ///\e
248    typedef typename M::Key Key;
249    ///\e
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) {};
258
259    /// \brief The subscript operator.
260    ///
261    /// The subscript operator.
262    /// \param edge The edge
263    /// \return The target of the edge
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  }
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  {
287    typename SmartConstReference<M1>::Type m1;
288    typename SmartConstReference<M2>::Type m2;
289
290  public:
291
292    typedef True NeedCopy;
293
294    ///\e
295    typedef typename M1::Key Key;
296    ///\e
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) {};
304    Value operator[](Key k) const {return m1[k]+m2[k];}
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
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  {
338    typename SmartConstReference<M>::Type m;
339    typename M::Value v;
340  public:
341
342    typedef True NeedCopy;
343    ///\e
344    typedef typename M::Key Key;
345    ///\e
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
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  {
378    typename SmartConstReference<M1>::Type m1;
379    typename SmartConstReference<M2>::Type m2;
380  public:
381
382    typedef True NeedCopy;
383    ///\e
384    typedef typename M1::Key Key;
385    ///\e
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) {};
393    Value operator[](Key k) const {return m1[k]-m2[k];}
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  {
418    typename SmartConstReference<M1>::Type m1;
419    typename SmartConstReference<M2>::Type m2;
420  public:
421
422    typedef True NeedCopy;
423    ///\e
424    typedef typename M1::Key Key;
425    ///\e
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) {};
433    Value operator[](Key k) const {return m1[k]*m2[k];}
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 
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  {
464    typename SmartConstReference<M>::Type m;
465    typename M::Value v;
466  public:
467
468    typedef True NeedCopy;
469    ///\e
470    typedef typename M::Key Key;
471    ///\e
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
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  {
504    typename SmartConstReference<M1>::Type m1;
505    typename SmartConstReference<M2>::Type m2;
506  public:
507
508    typedef True NeedCopy;
509    ///\e
510    typedef typename M1::Key Key;
511    ///\e
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) {};
519    Value operator[](Key k) const {return m1[k]/m2[k];}
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
542  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
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  {
552    typename SmartConstReference<M1>::Type m1;
553    typename SmartConstReference<M2>::Type m2;
554  public:
555
556    typedef True NeedCopy;
557    ///\e
558    typedef typename M2::Key Key;
559    ///\e
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) {};
567    Value operator[](Key k) const {return m1[m2[k]];}
568  };
569  ///Returns a \ref ComposeMap class
570
571  ///This function just returns a \ref ComposeMap class.
572  ///
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  }
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
603  template<class M1,class M2,class F,class V = typename F::result_type>
604  class CombineMap
605  {
606    typename SmartConstReference<M1>::Type m1;
607    typename SmartConstReference<M2>::Type m2;
608    F f;
609  public:
610
611    typedef True NeedCopy;
612    ///\e
613    typedef typename M1::Key Key;
614    ///\e
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
642  template<class M1,class M2,class F>
643  inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f)
644  {
645    return CombineMap<M1,M2,F>(m1,m2,f);
646  }
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  {
659    typename SmartConstReference<M>::Type m;
660  public:
661
662    typedef True NeedCopy;
663    ///\e
664    typedef typename M::Key Key;
665    ///\e
666    typedef typename M::Value Value;
667
668    ///Constructor
669
670    ///\e
671    ///
672    NegMap(const M &_m) : m(_m) {};
673    Value operator[](Key k) const {return -m[k];}
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
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 
709
710  template<class M>
711  class AbsMap
712  {
713    typename SmartConstReference<M>::Type m;
714  public:
715
716    typedef True NeedCopy;
717    ///\e
718    typedef typename M::Key Key;
719    ///\e
720    typedef typename M::Value Value;
721
722    ///Constructor
723
724    ///\e
725    ///
726    AbsMap(const M &_m) : m(_m) {};
727    Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
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
740  ///Converts an STL style functor to a map
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:
758
759    typedef True NeedCopy;
760    ///\e
761    typedef K Key;
762    ///\e
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
785  ///Converts a map to an STL style (unary) functor
786
787  ///This class Converts a map to an STL style (unary) functor.
788  ///that is it provides an <tt>operator()</tt> to read its values.
789  ///
790  ///For the sake of convenience it also works as
791  ///a ususal \ref concept::ReadMap "readable map", i.e
792  ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
793
794  template<class M>
795  class MapFunctor
796  {
797    typename SmartConstReference<M>::Type m;
798  public:
799
800    typedef True NeedCopy;
801    ///\e
802    typedef typename M::Key argument_type;
803    ///\e
804    typedef typename M::Value result_type;
805    ///\e
806    typedef typename M::Key Key;
807    ///\e
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
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
842  ///corresponding values of \c M1.
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  {
850    typename SmartConstReference<M1>::Type m1;
851    typename SmartConstReference<M2>::Type m2;
852  public:
853
854    typedef True NeedCopy;
855    ///\e
856    typedef typename M1::Key Key;
857    ///\e
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
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
934  /// @}
935}
936
937#endif // LEMON_MAPS_H
Note: See TracBrowser for help on using the repository browser.