00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef LEMON_MAPS_H
00018 #define LEMON_MAPS_H
00019
00020 #include <lemon/graph_utils.h>
00021 #include <lemon/utility.h>
00022
00023
00030
00031 #include <map>
00032
00033 namespace lemon {
00034
00037
00039
00042 template<typename K, typename T>
00043 class MapBase
00044 {
00045 public:
00047 typedef K Key;
00049 typedef T Value;
00050 };
00051
00053
00057 template<typename K, typename T>
00058 class NullMap : public MapBase<K,T>
00059 {
00060 public:
00061
00062 typedef True NeedCopy;
00063
00065 T operator[](const K&) const { return T(); }
00067 void set(const K&, const T&) {}
00068 };
00069
00070 template <typename K, typename V>
00071 NullMap<K, V> nullMap() {
00072 return NullMap<K, V>();
00073 }
00074
00075
00077
00081 template<typename K, typename T>
00082 class ConstMap : public MapBase<K,T>
00083 {
00084 T v;
00085 public:
00086
00087 typedef True NeedCopy;
00088
00090
00093 ConstMap() {}
00095
00098 ConstMap(const T &_v) : v(_v) {}
00099
00100 T operator[](const K&) const { return v; }
00101 void set(const K&, const T&) {}
00102
00103 template<typename T1>
00104 struct rebind {
00105 typedef ConstMap<K,T1> other;
00106 };
00107
00108 template<typename T1>
00109 ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
00110 };
00111
00113
00116 template<class V,class K>
00117 inline ConstMap<V,K> constMap(const K &k)
00118 {
00119 return ConstMap<V,K>(k);
00120 }
00121
00122
00123
00124 template<typename T, T v>
00125 struct Const { };
00126
00127 template<typename K, typename V, V v>
00128 class ConstMap<K, Const<V, v> > : public MapBase<K, V>
00129 {
00130 public:
00131 ConstMap() { }
00132 V operator[](const K&) const { return v; }
00133 void set(const K&, const V&) { }
00134 };
00135
00137
00142 template <typename K, typename T, typename Compare = std::less<K> >
00143 class StdMap : public std::map<K,T,Compare> {
00144 typedef std::map<K,T,Compare> parent;
00145 T v;
00146 typedef typename parent::value_type PairType;
00147
00148 public:
00150 typedef K Key;
00152 typedef T Value;
00154 typedef T& Reference;
00156 typedef const T& ConstReference;
00157
00158
00159 StdMap() : v() {}
00161 StdMap(const T& _v) : v(_v) {}
00162
00166 StdMap(const parent &m) : parent(m) {}
00171 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
00172
00173 template<typename T1, typename Comp1>
00174 StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
00175
00176 }
00177
00178 Reference operator[](const Key &k) {
00179 return insert(PairType(k,v)).first -> second;
00180 }
00181 ConstReference operator[](const Key &k) const {
00182 typename parent::iterator i = lower_bound(k);
00183 if (i == parent::end() || parent::key_comp()(k, (*i).first))
00184 return v;
00185 return (*i).second;
00186 }
00187 void set(const Key &k, const T &t) {
00188 parent::operator[](k) = t;
00189 }
00190
00196 T setDefault(const T &_v) { T old=v; v=_v; return old; }
00197
00198 template<typename T1>
00199 struct rebind {
00200 typedef StdMap<Key,T1,Compare> other;
00201 };
00202 };
00203
00205
00208
00213 template <typename T>
00214 class IdentityMap {
00215 public:
00216 typedef T Key;
00217 typedef T Value;
00218
00219 const Value& operator[](const Key& t) const {
00220 return t;
00221 }
00222 };
00223
00225
00229 template<class M, class T>
00230 class ConvertMap {
00231 typename SmartConstReference<M>::Type m;
00232 public:
00233
00234 typedef True NeedCopy;
00235
00237 typedef typename M::Key Key;
00239 typedef T Value;
00240
00242
00245 ConvertMap(const M &_m) : m(_m) {};
00246
00252 Value operator[](Key k) const {return m[k];}
00253 };
00254
00256
00260 template<class T, class M>
00261 inline ConvertMap<M,T> convertMap(const M &m)
00262 {
00263 return ConvertMap<M,T>(m);
00264 }
00265
00267
00271
00272 template<class M1,class M2>
00273 class AddMap
00274 {
00275 typename SmartConstReference<M1>::Type m1;
00276 typename SmartConstReference<M2>::Type m2;
00277
00278 public:
00279
00280 typedef True NeedCopy;
00281
00283 typedef typename M1::Key Key;
00285 typedef typename M1::Value Value;
00286
00288 AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
00289 Value operator[](Key k) const {return m1[k]+m2[k];}
00290 };
00291
00293
00299 template<class M1,class M2>
00300 inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
00301 {
00302 return AddMap<M1,M2>(m1,m2);
00303 }
00304
00306
00320 template<class M>
00321 class ShiftMap
00322 {
00323 typename SmartConstReference<M>::Type m;
00324 typename M::Value v;
00325 public:
00326
00327 typedef True NeedCopy;
00329 typedef typename M::Key Key;
00331 typedef typename M::Value Value;
00332
00334
00338 ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
00339 Value operator[](Key k) const {return m[k]+v;}
00340 };
00341
00343
00347 template<class M>
00348 inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v)
00349 {
00350 return ShiftMap<M>(m,v);
00351 }
00352
00354
00359
00360 template<class M1,class M2>
00361 class SubMap
00362 {
00363 typename SmartConstReference<M1>::Type m1;
00364 typename SmartConstReference<M2>::Type m2;
00365 public:
00366
00367 typedef True NeedCopy;
00369 typedef typename M1::Key Key;
00371 typedef typename M1::Value Value;
00372
00374 SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
00375 Value operator[](Key k) const {return m1[k]-m2[k];}
00376 };
00377
00379
00383 template<class M1,class M2>
00384 inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
00385 {
00386 return SubMap<M1,M2>(m1,m2);
00387 }
00388
00390
00396
00397 template<class M1,class M2>
00398 class MulMap
00399 {
00400 typename SmartConstReference<M1>::Type m1;
00401 typename SmartConstReference<M2>::Type m2;
00402 public:
00403
00404 typedef True NeedCopy;
00406 typedef typename M1::Key Key;
00408 typedef typename M1::Value Value;
00409
00411 MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
00412 Value operator[](Key k) const {return m1[k]*m2[k];}
00413 };
00414
00416
00419 template<class M1,class M2>
00420 inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
00421 {
00422 return MulMap<M1,M2>(m1,m2);
00423 }
00424
00426
00440 template<class M>
00441 class ScaleMap
00442 {
00443 typename SmartConstReference<M>::Type m;
00444 typename M::Value v;
00445 public:
00446
00447 typedef True NeedCopy;
00449 typedef typename M::Key Key;
00451 typedef typename M::Value Value;
00452
00454
00458 ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
00459 Value operator[](Key k) const {return m[k]*v;}
00460 };
00461
00463
00467 template<class M>
00468 inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v)
00469 {
00470 return ScaleMap<M>(m,v);
00471 }
00472
00474
00479
00480 template<class M1,class M2>
00481 class DivMap
00482 {
00483 typename SmartConstReference<M1>::Type m1;
00484 typename SmartConstReference<M2>::Type m2;
00485 public:
00486
00487 typedef True NeedCopy;
00489 typedef typename M1::Key Key;
00491 typedef typename M1::Value Value;
00492
00494 DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
00495 Value operator[](Key k) const {return m1[k]/m2[k];}
00496 };
00497
00499
00502 template<class M1,class M2>
00503 inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
00504 {
00505 return DivMap<M1,M2>(m1,m2);
00506 }
00507
00509
00524
00525 template<class M1,class M2>
00526 class ComposeMap
00527 {
00528 typename SmartConstReference<M1>::Type m1;
00529 typename SmartConstReference<M2>::Type m2;
00530 public:
00531
00532 typedef True NeedCopy;
00534 typedef typename M2::Key Key;
00536 typedef typename M1::Value Value;
00537
00539 ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
00540 Value operator[](Key k) const {return m1[m2[k]];}
00541 };
00543
00547 template<class M1,class M2>
00548 inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
00549 {
00550 return ComposeMap<M1,M2>(m1,m2);
00551 }
00552
00554
00575
00576 template<class M1,class M2,class F,class V = typename F::result_type>
00577 class CombineMap
00578 {
00579 typename SmartConstReference<M1>::Type m1;
00580 typename SmartConstReference<M2>::Type m2;
00581 F f;
00582 public:
00583
00584 typedef True NeedCopy;
00586 typedef typename M1::Key Key;
00588 typedef V Value;
00589
00591 CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
00592 : m1(_m1), m2(_m2), f(_f) {};
00593 Value operator[](Key k) const {return f(m1[k],m2[k]);}
00594 };
00595
00597
00612 template<class M1,class M2,class F>
00613 inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f)
00614 {
00615 return CombineMap<M1,M2,F>(m1,m2,f);
00616 }
00617
00619
00625
00626 template<class M>
00627 class NegMap
00628 {
00629 typename SmartConstReference<M>::Type m;
00630 public:
00631
00632 typedef True NeedCopy;
00634 typedef typename M::Key Key;
00636 typedef typename M::Value Value;
00637
00639 NegMap(const M &_m) : m(_m) {};
00640 Value operator[](Key k) const {return -m[k];}
00641 };
00642
00644
00647 template<class M>
00648 inline NegMap<M> negMap(const M &m)
00649 {
00650 return NegMap<M>(m);
00651 }
00652
00653
00655
00675
00676
00677 template<class M>
00678 class AbsMap
00679 {
00680 typename SmartConstReference<M>::Type m;
00681 public:
00682
00683 typedef True NeedCopy;
00685 typedef typename M::Key Key;
00687 typedef typename M::Value Value;
00688
00690 AbsMap(const M &_m) : m(_m) {};
00691 Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
00692 };
00693
00695
00698 template<class M>
00699 inline AbsMap<M> absMap(const M &m)
00700 {
00701 return AbsMap<M>(m);
00702 }
00703
00705
00715
00716
00717 template<class K,class V,class F>
00718 class FunctorMap
00719 {
00720 const F &f;
00721 public:
00722
00723 typedef True NeedCopy;
00725 typedef K Key;
00727 typedef V Value;
00728
00730 FunctorMap(const F &_f) : f(_f) {};
00731 Value operator[](Key k) const {return f(k);}
00732 };
00733
00735
00740 template<class K,class V, class F>
00741 inline FunctorMap<K,V,F> functorMap(const F &f)
00742 {
00743 return FunctorMap<K,V,F>(f);
00744 }
00745
00747
00754
00755 template<class M>
00756 class MapFunctor
00757 {
00758 typename SmartConstReference<M>::Type m;
00759 public:
00760
00761 typedef True NeedCopy;
00763 typedef typename M::Key argument_type;
00765 typedef typename M::Value result_type;
00767 typedef typename M::Key Key;
00769 typedef typename M::Value Value;
00770
00772 MapFunctor(const M &_m) : m(_m) {};
00774 Value operator()(Key k) const {return m[k];}
00776 Value operator[](Key k) const {return m[k];}
00777 };
00778
00780
00783 template<class M>
00784 inline MapFunctor<M> mapFunctor(const M &m)
00785 {
00786 return MapFunctor<M>(m);
00787 }
00788
00789
00791
00800
00801 template<class M1,class M2>
00802 class ForkMap
00803 {
00804 typename SmartConstReference<M1>::Type m1;
00805 typename SmartConstReference<M2>::Type m2;
00806 public:
00807
00808 typedef True NeedCopy;
00810 typedef typename M1::Key Key;
00812 typedef typename M1::Value Value;
00813
00815 ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
00816 Value operator[](Key k) const {return m1[k];}
00817 void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
00818 };
00819
00821
00827 template<class M1,class M2>
00828 inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2)
00829 {
00830 return ForkMap<M1,M2>(m1,m2);
00831 }
00832
00833
00834
00835
00836
00838
00844
00845 template<class M>
00846 class NotMap
00847 {
00848 typename SmartConstReference<M>::Type m;
00849 public:
00850
00851 typedef True NeedCopy;
00853 typedef typename M::Key Key;
00855 typedef bool Value;
00856
00858 NotMap(const M &_m) : m(_m) {};
00859 Value operator[](Key k) const {return !m[k];}
00860 };
00861
00863
00866 template<class M>
00867 inline NotMap<M> notMap(const M &m)
00868 {
00869 return NotMap<M>(m);
00870 }
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00883 }
00884
00885 #endif // LEMON_MAPS_H