COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/maps.h @ 1692:a34203867181

Last change on this file since 1692:a34203867181 was 1691:6be54bcc14ec, checked in by Balazs Dezso, 19 years ago

Handling C x A -> A multiplication

File size: 26.0 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.
[1675]42  template<typename K, typename T, typename _NeedCopy = False>
43  class MapBase {
[720]44  public:
[1675]45    /// \e
46    typedef _NeedCopy NeedCopy;
[911]47    ///\e
[987]48    typedef K Key;
[911]49    ///\e
[987]50    typedef T Value;
[720]51  };
52
[805]53  /// Null map. (a.k.a. DoNothingMap)
[286]54
55  /// If you have to provide a map only for its type definitions,
[805]56  /// or if you have to provide a writable map, but
57  /// data written to it will sent to <tt>/dev/null</tt>...
[1675]58  template<typename K, typename T, typename NC = False>
59  class NullMap : public MapBase<K, T, NC> {
[286]60  public:
[1675]61    typedef MapBase<K, T, NC> Parent;
62    typedef typename Parent::Key Key;
63    typedef typename Parent::Value Value;
[1420]64   
[805]65    /// Gives back a default constructed element.
[286]66    T operator[](const K&) const { return T(); }
[805]67    /// Absorbs the value.
[286]68    void set(const K&, const T&) {}
69  };
70
[1420]71  template <typename K, typename V>
[1675]72  NullMap<K, V, True> nullMap() {
73    return NullMap<K, V, True>();
[1420]74  }
75
[286]76
77  /// Constant map.
78
[805]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.
[1675]82  template<typename K, typename T, typename NC = False>
83  class ConstMap : public MapBase<K, T, NC> {
84  private:
[286]85    T v;
86  public:
87
[1675]88    typedef MapBase<K, T, NC> Parent;
89    typedef typename Parent::Key Key;
90    typedef typename Parent::Value Value;
[1420]91
[805]92    /// Default constructor
93
94    /// The value of the map will be uninitialized.
95    /// (More exactly it will be default constructed.)
[286]96    ConstMap() {}
[911]97    ///\e
[805]98
99    /// \param _v The initial value of the map.
[911]100    ///
[286]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 {
[1675]108      typedef ConstMap<K, T1> other;
[286]109    };
110
111    template<typename T1>
[1675]112    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
[286]113  };
114
[1076]115  ///Returns a \ref ConstMap class
116
117  ///This function just returns a \ref ConstMap class.
118  ///\relates ConstMap
[1675]119  template<typename K, typename V>
120  inline ConstMap<K, V, True> constMap(const V &v) {
121    return ConstMap<K, V, True>(v);
[1076]122  }
123
124
[1660]125  //\todo to document later
[890]126  template<typename T, T v>
127  struct Const { };
[1675]128
[1660]129  //\todo to document later
[1675]130  template<typename K, typename V, V v, typename NC>
131  class ConstMap<K, Const<V, v>, NC > : public MapBase<K, V, NC> {
[890]132  public:
[1675]133    typedef MapBase<K, V, False> Parent;
134    typedef typename Parent::Key Key;
135    typedef typename Parent::Value Value;
136
[890]137    ConstMap() { }
138    V operator[](const K&) const { return v; }
139    void set(const K&, const V&) { }
140  };
[286]141
[1675]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
[286]151  /// \c std::map wrapper
152
153  /// This is essentially a wrapper for \c std::map. With addition that
[987]154  /// you can specify a default value different from \c Value() .
[286]155  ///
156  /// \todo Provide allocator parameter...
[987]157  template <typename K, typename T, typename Compare = std::less<K> >
[1675]158  class StdMap : public std::map<K, T, Compare> {
159    typedef std::map<K, T, Compare> parent;
[286]160    T v;
161    typedef typename parent::value_type PairType;
162
163  public:
[1456]164    ///\e
[987]165    typedef K Key;
[1456]166    ///\e
[987]167    typedef T Value;
[1456]168    ///\e
[987]169    typedef T& Reference;
[1456]170    ///\e
[987]171    typedef const T& ConstReference;
[286]172
173
[345]174    StdMap() : v() {}
[286]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>
[1675]189    StdMap(const StdMap<Key, T1,Comp1> &m, const T &_v) {
[389]190      //FIXME;
191    }
[286]192
[987]193    Reference operator[](const Key &k) {
[346]194      return insert(PairType(k,v)).first -> second;
[286]195    }
[1675]196
[987]197    ConstReference operator[](const Key &k) const {
[389]198      typename parent::iterator i = lower_bound(k);
[391]199      if (i == parent::end() || parent::key_comp()(k, (*i).first))
[286]200        return v;
201      return (*i).second;
202    }
[345]203    void set(const Key &k, const T &t) {
[346]204      parent::operator[](k) = t;
[345]205    }
[286]206
207    /// Changes the default value of the map.
208    /// \return Returns the previous default value.
209    ///
[805]210    /// \warning The value of some keys (which has already been queried, but
[286]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 {
[1675]216      typedef StdMap<Key, T1,Compare> other;
[286]217    };
218  };
[1041]219
[1402]220  /// @}
221
222  /// \addtogroup map_adaptors
223  /// @{
224
[1531]225  /// \brief Identity mapping.
226  ///
227  /// This mapping gives back the given key as value without any
228  /// modification.
[1675]229  template <typename T, typename NC = False>
230  class IdentityMap : public MapBase<T, T, NC> {
[1531]231  public:
[1675]232    typedef MapBase<T, T, NC> Parent;
233    typedef typename Parent::Key Key;
234    typedef typename Parent::Value Value;
[1531]235
[1675]236    const T& operator[](const T& t) const {
[1531]237      return t;
238    }
239  };
[1402]240
[1675]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
[1547]251  ///Convert the \c Value of a map to another type.
[1178]252
253  ///This \ref concept::ReadMap "read only map"
254  ///converts the \c Value of a maps to type \c T.
[1547]255  ///Its \c Key is inherited from \c M.
[1675]256  template <typename M, typename T, typename NC = False>
257  class ConvertMap : public MapBase<typename M::Key, T, NC> {
[1420]258    typename SmartConstReference<M>::Type m;
[1178]259  public:
[1675]260    typedef MapBase<typename M::Key, T, NC> Parent;
261    typedef typename Parent::Key Key;
262    typedef typename Parent::Value Value;
[1178]263
264    ///Constructor
265
266    ///Constructor
[1536]267    ///\param _m is the underlying map
[1178]268    ConvertMap(const M &_m) : m(_m) {};
[1346]269
270    /// \brief The subscript operator.
271    ///
272    /// The subscript operator.
[1536]273    /// \param k The key
[1346]274    /// \return The target of the edge
[1675]275    Value operator[](const Key& k) const {return m[k];}
[1178]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.
[1675]283  template<typename T, typename M>
284  inline ConvertMap<M, T, True> convertMap(const M &m) {
285    return ConvertMap<M, T, True>(m);
[1178]286  }
[1041]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
[1675]294  template<typename M1, typename M2, typename NC = False>
295  class AddMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
[1420]296    typename SmartConstReference<M1>::Type m1;
297    typename SmartConstReference<M2>::Type m2;
298
[1041]299  public:
[1675]300    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
301    typedef typename Parent::Key Key;
302    typedef typename Parent::Value Value;
[1041]303
304    ///Constructor
305    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]306    Value operator[](Key k) const {return m1[k]+m2[k];}
[1041]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
[1675]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);
[1041]319  }
320
[1547]321  ///Shift a map with a constant.
[1070]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
[1547]331  ///is equivalent with
[1070]332  ///\code
333  ///  ConstMap<X::Key, X::Value> c_tmp(v);
334  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
335  ///\endcode
[1691]336  template<typename M, typename C = typename M::Value, typename NC = False>
[1675]337  class ShiftMap : public MapBase<typename M::Key, typename M::Value, NC> {
[1420]338    typename SmartConstReference<M>::Type m;
[1691]339    C v;
[1070]340  public:
[1675]341    typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
342    typedef typename Parent::Key Key;
343    typedef typename Parent::Value Value;
[1070]344
345    ///Constructor
346
347    ///Constructor
348    ///\param _m is the undelying map
349    ///\param _v is the shift value
[1691]350    ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
351    Value operator[](Key k) const {return m[k] + v;}
[1070]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.
[1691]359  template<typename M, typename C>
360  inline ShiftMap<M, C, True> shiftMap(const M &m,const C &v) {
361    return ShiftMap<M, C, True>(m,v);
[1070]362  }
363
[1041]364  ///Difference of two maps
365
366  ///This \ref concept::ReadMap "read only map" returns the difference
[1547]367  ///of the values of the two
[1041]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
[1675]371  template<typename M1, typename M2, typename NC = False>
372  class SubMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
[1420]373    typename SmartConstReference<M1>::Type m1;
374    typename SmartConstReference<M2>::Type m2;
[1041]375  public:
[1675]376    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
377    typedef typename Parent::Key Key;
378    typedef typename Parent::Value Value;
[1041]379
380    ///Constructor
381    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]382    Value operator[](Key k) const {return m1[k]-m2[k];}
[1041]383  };
384 
385  ///Returns a \ref SubMap class
386
387  ///This function just returns a \ref SubMap class.
388  ///
389  ///\relates SubMap
[1675]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);
[1041]393  }
394
395  ///Product of two maps
396
397  ///This \ref concept::ReadMap "read only map" returns the product of the
[1547]398  ///values of the two
[1041]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
[1675]403  template<typename M1, typename M2, typename NC = False>
404  class MulMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
[1420]405    typename SmartConstReference<M1>::Type m1;
406    typename SmartConstReference<M2>::Type m2;
[1041]407  public:
[1675]408    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
409    typedef typename Parent::Key Key;
410    typedef typename Parent::Value Value;
[1041]411
412    ///Constructor
413    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]414    Value operator[](Key k) const {return m1[k]*m2[k];}
[1041]415  };
416 
417  ///Returns a \ref MulMap class
418
419  ///This function just returns a \ref MulMap class.
420  ///\relates MulMap
[1675]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);
[1041]424  }
425 
[1547]426  ///Scales a maps with a constant.
[1070]427
428  ///This \ref concept::ReadMap "read only map" returns the value of the
[1691]429  ///given map multiplied from the left side with a constant value.
[1070]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
[1547]436  ///is equivalent with
[1070]437  ///\code
438  ///  ConstMap<X::Key, X::Value> c_tmp(v);
439  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
440  ///\endcode
[1691]441  template<typename M, typename C = typename M::Value, typename NC = False>
[1675]442  class ScaleMap : public MapBase<typename M::Key, typename M::Value, NC> {
[1420]443    typename SmartConstReference<M>::Type m;
[1691]444    C v;
[1070]445  public:
[1675]446    typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
447    typedef typename Parent::Key Key;
448    typedef typename Parent::Value Value;
[1070]449
450    ///Constructor
451
452    ///Constructor
453    ///\param _m is the undelying map
454    ///\param _v is the scaling value
[1691]455    ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
456    Value operator[](Key k) const {return v * m[k];}
[1070]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.
[1691]464  template<typename M, typename C>
465  inline ScaleMap<M, C, True> scaleMap(const M &m,const C &v) {
466    return ScaleMap<M, C, True>(m,v);
[1070]467  }
468
[1041]469  ///Quotient of two maps
470
471  ///This \ref concept::ReadMap "read only map" returns the quotient of the
[1547]472  ///values of the two
[1041]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
[1675]476  template<typename M1, typename M2, typename NC = False>
477  class DivMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
[1420]478    typename SmartConstReference<M1>::Type m1;
479    typename SmartConstReference<M2>::Type m2;
[1041]480  public:
[1675]481    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
482    typedef typename Parent::Key Key;
483    typedef typename Parent::Value Value;
[1041]484
485    ///Constructor
486    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]487    Value operator[](Key k) const {return m1[k]/m2[k];}
[1041]488  };
489 
490  ///Returns a \ref DivMap class
491
492  ///This function just returns a \ref DivMap class.
493  ///\relates DivMap
[1675]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);
[1041]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
[1675]507  ///  ComposeMap<M1, M2> cm(m1,m2);
[1041]508  ///\endcode
[1044]509  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
[1041]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
[1675]516  template <typename M1, typename M2, typename NC = False>
517  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value, NC> {
[1420]518    typename SmartConstReference<M1>::Type m1;
519    typename SmartConstReference<M2>::Type m2;
[1041]520  public:
[1675]521    typedef MapBase<typename M2::Key, typename M1::Value, NC> Parent;
522    typedef typename Parent::Key Key;
523    typedef typename Parent::Value Value;
[1041]524
525    ///Constructor
526    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
[1044]527    Value operator[](Key k) const {return m1[m2[k]];}
[1041]528  };
529  ///Returns a \ref ComposeMap class
530
531  ///This function just returns a \ref ComposeMap class.
[1219]532  ///
[1041]533  ///\relates ComposeMap
[1675]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);
[1041]537  }
[1219]538 
[1547]539  ///Combines of two maps using an STL (binary) functor.
[1219]540
[1547]541  ///Combines of two maps using an STL (binary) functor.
[1219]542  ///
543  ///
[1547]544  ///This \ref concept::ReadMap "read only map" takes two maps and a
[1219]545  ///binary functor and returns the composition of
[1547]546  ///the two
[1219]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
[1675]552  ///  CombineMap<M1, M2,F,V> cm(m1,m2,f);
[1219]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
[1675]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> {
[1420]566    typename SmartConstReference<M1>::Type m1;
567    typename SmartConstReference<M2>::Type m2;
568    F f;
[1219]569  public:
[1675]570    typedef MapBase<typename M1::Key, V, NC> Parent;
571    typedef typename Parent::Key Key;
572    typedef typename Parent::Value Value;
[1219]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
[1675]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);
[1219]612  }
[1041]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
[1675]622  template<typename M, typename NC = False>
623  class NegMap : public MapBase<typename M::Key, typename M::Value, NC> {
[1420]624    typename SmartConstReference<M>::Type m;
[1041]625  public:
[1675]626    typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
627    typedef typename Parent::Key Key;
628    typedef typename Parent::Value Value;
[1041]629
630    ///Constructor
631    NegMap(const M &_m) : m(_m) {};
[1044]632    Value operator[](Key k) const {return -m[k];}
[1041]633  };
634 
635  ///Returns a \ref NegMap class
636
637  ///This function just returns a \ref NegMap class.
638  ///\relates NegMap
[1675]639  template <typename M>
640  inline NegMap<M, True> negMap(const M &m) {
641    return NegMap<M, True>(m);
[1041]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
[1044]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 
[1041]667
[1675]668  template<typename M, typename NC = False>
669  class AbsMap : public MapBase<typename M::Key, typename M::Value, NC> {
[1420]670    typename SmartConstReference<M>::Type m;
[1041]671  public:
[1675]672    typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
673    typedef typename Parent::Key Key;
674    typedef typename Parent::Value Value;
[1041]675
676    ///Constructor
677    AbsMap(const M &_m) : m(_m) {};
[1675]678    Value operator[](Key k) const {
679      Value tmp = m[k];
680      return tmp >= 0 ? tmp : -tmp;
681    }
682
[1041]683  };
684 
685  ///Returns a \ref AbsMap class
686
687  ///This function just returns a \ref AbsMap class.
688  ///\relates AbsMap
[1675]689  template<typename M>
690  inline AbsMap<M, True> absMap(const M &m) {
691    return AbsMap<M, True>(m);
[1041]692  }
693
[1402]694  ///Converts an STL style functor to a map
[1076]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
[1675]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> {
[1679]712    F f;
[1076]713  public:
[1675]714    typedef MapBase<K, V, NC> Parent;
715    typedef typename Parent::Key Key;
716    typedef typename Parent::Value Value;
[1076]717
718    ///Constructor
[1679]719    FunctorMap(const F &_f) : f(_f) {}
720
721    Value operator[](Key k) const { return f(k);}
[1076]722  };
723 
724  ///Returns a \ref FunctorMap class
725
726  ///This function just returns a \ref FunctorMap class.
727  ///
728  ///The third template parameter isn't necessary to be given.
729  ///\relates FunctorMap
[1675]730  template<typename K, typename V, typename F> inline
731  FunctorMap<F, K, V, True> functorMap(const F &f) {
732    return FunctorMap<F, K, V, True>(f);
[1076]733  }
734
[1675]735  template <typename F> inline
736  FunctorMap<F, typename F::argument_type, typename F::result_type, True>
737  functorMap(const F &f) {
[1679]738    return FunctorMap<F, typename F::argument_type,
739      typename F::result_type, True>(f);
[1675]740  }
741
742  template <typename K, typename V> inline
743  FunctorMap<V (*)(K), K, V, True> functorMap(V (*f)(K)) {
[1679]744    return FunctorMap<V (*)(K), K, V, True>(f);
[1675]745  }
746
747
[1219]748  ///Converts a map to an STL style (unary) functor
[1076]749
[1219]750  ///This class Converts a map to an STL style (unary) functor.
[1076]751  ///that is it provides an <tt>operator()</tt> to read its values.
752  ///
[1223]753  ///For the sake of convenience it also works as
[1537]754  ///a ususal \ref concept::ReadMap "readable map",
755  ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
[1076]756
[1675]757  template <typename M, typename NC = False>
758  class MapFunctor : public MapBase<typename M::Key, typename M::Value, NC> {
[1420]759    typename SmartConstReference<M>::Type m;
[1076]760  public:
[1675]761    typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
762    typedef typename Parent::Key Key;
763    typedef typename Parent::Value Value;
[1420]764
[1456]765    ///\e
[1223]766    typedef typename M::Key argument_type;
[1456]767    ///\e
[1223]768    typedef typename M::Value result_type;
[1076]769
770    ///Constructor
771    MapFunctor(const M &_m) : m(_m) {};
772    ///Returns a value of the map
773    Value operator()(Key k) const {return m[k];}
774    ///\e
775    Value operator[](Key k) const {return m[k];}
776  };
777 
778  ///Returns a \ref MapFunctor class
779
780  ///This function just returns a \ref MapFunctor class.
781  ///\relates MapFunctor
[1675]782  template<typename M>
783  inline MapFunctor<M, True> mapFunctor(const M &m) {
784    return MapFunctor<M, True>(m);
[1076]785  }
786
787
[1547]788  ///Applies all map setting operations to two maps
[1219]789
790  ///This map has two \ref concept::WriteMap "writable map"
791  ///parameters and each write request will be passed to both of them.
792  ///If \c M1 is also \ref concept::ReadMap "readable",
793  ///then the read operations will return the
[1317]794  ///corresponding values of \c M1.
[1219]795  ///
796  ///The \c Key and \c Value will be inherited from \c M1.
797  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
798
[1675]799  template<typename  M1, typename M2, typename NC = False>
800  class ForkMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
[1420]801    typename SmartConstReference<M1>::Type m1;
802    typename SmartConstReference<M2>::Type m2;
[1219]803  public:
[1675]804    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
805    typedef typename Parent::Key Key;
806    typedef typename Parent::Value Value;
[1219]807
808    ///Constructor
809    ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
810    Value operator[](Key k) const {return m1[k];}
[1675]811    //    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
[1219]812  };
813 
814  ///Returns an \ref ForkMap class
815
816  ///This function just returns an \ref ForkMap class.
817  ///\todo How to call these type of functions?
818  ///
819  ///\relates ForkMap
820  ///\todo Wrong scope in Doxygen when \c \\relates is used
[1675]821  template <typename M1, typename M2>
822  inline ForkMap<M1, M2, True> forkMap(const M1 &m1,const M2 &m2) {
823    return ForkMap<M1, M2, True>(m1,m2);
[1219]824  }
825
[1456]826
827 
828  /* ************* BOOL MAPS ******************* */
829 
830  ///Logical 'not' of a map
831 
832  ///This bool \ref concept::ReadMap "read only map" returns the
833  ///logical negation of
834  ///value returned by the
835  ///given map. Its \c Key and will be inherited from \c M,
836  ///its Value is <tt>bool</tt>.
837
[1675]838  template <typename M, typename NC = False>
839  class NotMap : public MapBase<typename M::Key, bool, NC> {
[1456]840    typename SmartConstReference<M>::Type m;
841  public:
[1675]842    typedef MapBase<typename M::Key, bool, NC> Parent;
843    typedef typename Parent::Key Key;
844    typedef typename Parent::Value Value;
[1456]845
846    ///Constructor
847    NotMap(const M &_m) : m(_m) {};
848    Value operator[](Key k) const {return !m[k];}
849  };
850 
851  ///Returns a \ref NotMap class
852 
853  ///This function just returns a \ref NotMap class.
854  ///\relates NotMap
[1675]855  template <typename M>
856  inline NotMap<M, True> notMap(const M &m) {
857    return NotMap<M, True>(m);
[1456]858  }
859
[1041]860  /// @}
[286]861}
[1041]862
[921]863#endif // LEMON_MAPS_H
Note: See TracBrowser for help on using the repository browser.