Changeset 2032:18c08f9129e4 in lemon-0.x
- Timestamp:
- 04/03/06 18:03:37 (19 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2671
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/maps.h
r1993 r2032 352 352 Value operator[](Key k) const {return m[k] + v;} 353 353 }; 354 355 ///Shift a map with a constant. 356 357 ///This \ref concept::ReadWriteMap "read-write map" returns the sum of the 358 ///given map and a constant value. It makes also possible to write the map. 359 ///Its \c Key and \c Value is inherited from \c M. 360 /// 361 ///Actually, 362 ///\code 363 /// ShiftMap<X> sh(x,v); 364 ///\endcode 365 ///is equivalent with 366 ///\code 367 /// ConstMap<X::Key, X::Value> c_tmp(v); 368 /// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); 369 ///\endcode 370 template<typename M, typename C = typename M::Value> 371 class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { 372 M& m; 373 C v; 374 public: 375 typedef MapBase<typename M::Key, typename M::Value> Parent; 376 typedef typename Parent::Key Key; 377 typedef typename Parent::Value Value; 378 379 ///Constructor 380 381 ///Constructor 382 ///\param _m is the undelying map 383 ///\param _v is the shift value 384 ShiftWriteMap(const M &_m, const C &_v ) : m(_m), v(_v) {}; 385 Value operator[](Key k) const {return m[k] + v;} 386 void set(Key k, const Value& c) { m.set(k, c - v); } 387 }; 354 388 355 389 ///Returns an \ref ShiftMap class … … 361 395 inline ShiftMap<M, C> shiftMap(const M &m,const C &v) { 362 396 return ShiftMap<M, C>(m,v); 397 } 398 399 template<typename M, typename C> 400 inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) { 401 return ShiftWriteMap<M, C>(m,v); 363 402 } 364 403 … … 457 496 Value operator[](Key k) const {return v * m[k];} 458 497 }; 498 499 ///Scales a maps with a constant. 500 501 ///This \ref concept::ReadWriteMap "read-write map" returns the value of the 502 ///given map multiplied from the left side with a constant value. It can 503 ///be used as write map also if the given multiplier is not zero. 504 ///Its \c Key and \c Value is inherited from \c M. 505 template<typename M, typename C = typename M::Value> 506 class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> { 507 M& m; 508 C v; 509 public: 510 typedef MapBase<typename M::Key, typename M::Value> Parent; 511 typedef typename Parent::Key Key; 512 typedef typename Parent::Value Value; 513 514 ///Constructor 515 516 ///Constructor 517 ///\param _m is the undelying map 518 ///\param _v is the scaling value 519 ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {}; 520 Value operator[](Key k) const {return v * m[k];} 521 void set(Key k, const Value& c) { m.set(k, c / v);} 522 }; 459 523 460 524 ///Returns an \ref ScaleMap class … … 466 530 inline ScaleMap<M, C> scaleMap(const M &m,const C &v) { 467 531 return ScaleMap<M, C>(m,v); 532 } 533 534 template<typename M, typename C> 535 inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) { 536 return ScaleWriteMap<M, C>(m,v); 468 537 } 469 538 … … 636 705 }; 637 706 707 ///Negative value of a map 708 709 ///This \ref concept::ReadWriteMap "read-write map" returns the negative 710 ///value of the value returned by the 711 ///given map. Its \c Key and \c Value will be inherited from \c M. 712 ///The unary \c - operator must be defined for \c Value, of course. 713 714 template<typename M> 715 class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { 716 M& m; 717 public: 718 typedef MapBase<typename M::Key, typename M::Value> Parent; 719 typedef typename Parent::Key Key; 720 typedef typename Parent::Value Value; 721 722 ///Constructor 723 NegWriteMap(M &_m) : m(_m) {}; 724 Value operator[](Key k) const {return -m[k];} 725 void set(Key k, const Value& v) { m.set(k, -v); } 726 }; 727 638 728 ///Returns a \ref NegMap class 639 729 … … 645 735 } 646 736 737 template <typename M> 738 inline NegWriteMap<M> negMap(M &m) { 739 return NegWriteMap<M>(m); 740 } 647 741 648 742 ///Absolute value of a map … … 788 882 } 789 883 884 ///Applies all map setting operations to two maps 885 886 ///This map has two \ref concept::ReadMap "readable map" 887 ///parameters and each read request will be passed just to the 888 ///first map. This class is the just readable map type of the ForkWriteMap. 889 /// 890 ///The \c Key and \c Value will be inherited from \c M1. 891 ///The \c Key and \c Value of M2 must be convertible from those of \c M1. 892 893 template<typename M1, typename M2> 894 class ForkMap : public MapBase<typename M1::Key, typename M1::Value> { 895 const M1& m1; 896 const M2& m2; 897 public: 898 typedef MapBase<typename M1::Key, typename M1::Value> Parent; 899 typedef typename Parent::Key Key; 900 typedef typename Parent::Value Value; 901 902 ///Constructor 903 ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {}; 904 Value operator[](Key k) const {return m1[k];} 905 }; 906 790 907 791 908 ///Applies all map setting operations to two maps … … 801 918 802 919 template<typename M1, typename M2> 803 class Fork Map : public MapBase<typename M1::Key, typename M1::Value> {804 constM1& m1;805 constM2& m2;920 class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> { 921 M1& m1; 922 M2& m2; 806 923 public: 807 924 typedef MapBase<typename M1::Key, typename M1::Value> Parent; … … 810 927 811 928 ///Constructor 812 Fork Map(const M1 &_m1,constM2 &_m2) : m1(_m1), m2(_m2) {};929 ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {}; 813 930 Value operator[](Key k) const {return m1[k];} 814 //void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}931 void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);} 815 932 }; 816 933 … … 823 940 ///\todo Wrong scope in Doxygen when \c \\relates is used 824 941 template <typename M1, typename M2> 825 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {942 inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) { 826 943 return ForkMap<M1, M2>(m1,m2); 944 } 945 946 template <typename M1, typename M2> 947 inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) { 948 return ForkWriteMap<M1, M2>(m1,m2); 827 949 } 828 950 … … 851 973 Value operator[](Key k) const {return !m[k];} 852 974 }; 975 976 ///Logical 'not' of a map with writing possibility 977 978 ///This bool \ref concept::ReadWriteMap "read-write map" returns the 979 ///logical negation of value returned by the given map. It is setted 980 ///then the negation of the value be setted to the original map. 981 ///Its \c Key and will be inherited from \c M, 982 ///its Value is <tt>bool</tt>. 983 template <typename M> 984 class NotWriteMap : public MapBase<typename M::Key, bool> { 985 M& m; 986 public: 987 typedef MapBase<typename M::Key, bool> Parent; 988 typedef typename Parent::Key Key; 989 typedef typename Parent::Value Value; 990 991 /// Constructor 992 NotWriteMap(M &_m) : m(_m) {}; 993 Value operator[](Key k) const {return !m[k];} 994 void set(Key k, bool v) { m.set(k, !v); } 995 }; 853 996 854 997 ///Returns a \ref NotMap class … … 861 1004 } 862 1005 1006 template <typename M> 1007 inline NotWriteMap<M> notMap(M &m) { 1008 return NotWriteMap<M>(m); 1009 } 1010 863 1011 /// \brief Writable bool map for store each true assigned elements. 864 1012 /// -
test/maps_test.cc
r1956 r2032 17 17 */ 18 18 19 #include <deque> 20 #include <set> 21 19 22 #include <lemon/concept_check.h> 20 23 #include <lemon/concept/maps.h> … … 27 30 28 31 struct A {}; 32 inline bool operator<(A, A) { return true; } 29 33 struct B {}; 30 34 … … 42 46 43 47 typedef ReadMap<A,double> DoubleMap; 48 typedef ReadWriteMap<A, double> WriteDoubleMap; 49 50 typedef ReadMap<A,bool> BoolMap; 51 typedef ReadWriteMap<A, bool> BoolWriteMap; 44 52 45 53 int main() … … 56 64 checkConcept<ReadMap<A,double>, DivMap<DoubleMap,DoubleMap> >(); 57 65 checkConcept<ReadMap<A,double>, NegMap<DoubleMap> >(); 66 checkConcept<ReadWriteMap<A,double>, NegWriteMap<WriteDoubleMap> >(); 58 67 checkConcept<ReadMap<A,double>, AbsMap<DoubleMap> >(); 59 68 checkConcept<ReadMap<A,double>, ShiftMap<DoubleMap> >(); 69 checkConcept<ReadWriteMap<A,double>, ShiftWriteMap<WriteDoubleMap> >(); 60 70 checkConcept<ReadMap<A,double>, ScaleMap<DoubleMap> >(); 71 checkConcept<ReadWriteMap<A,double>, ScaleWriteMap<WriteDoubleMap> >(); 72 checkConcept<ReadMap<A,double>, ForkMap<DoubleMap, DoubleMap> >(); 73 checkConcept<ReadWriteMap<A,double>, 74 ForkWriteMap<WriteDoubleMap, WriteDoubleMap> >(); 61 75 62 76 checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >(); 63 77 64 78 checkConcept<ReadMap<A,B>, FunctorMap<F, A, B> >(); 79 80 checkConcept<ReadMap<A, bool>, NotMap<BoolMap> >(); 81 checkConcept<ReadWriteMap<A, bool>, NotWriteMap<BoolWriteMap> >(); 82 83 checkConcept<WriteMap<A, bool>, StoreBoolMap<A*> >(); 84 checkConcept<WriteMap<A, bool>, BackInserterBoolMap<std::deque<A> > >(); 85 checkConcept<WriteMap<A, bool>, FrontInserterBoolMap<std::deque<A> > >(); 86 checkConcept<WriteMap<A, bool>, InserterBoolMap<std::set<A> > >(); 87 checkConcept<WriteMap<A, bool>, FillBoolMap<WriteMap<A, B> > >(); 88 checkConcept<WriteMap<A, bool>, SettingOrderBoolMap<WriteMap<A, int> > >(); 65 89 66 90 int a;
Note: See TracChangeset
for help on using the changeset viewer.