gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Small improvements in maps.h - Add a new version of constMap() function. - Fix in FunctorToMap class.
0 2 0
default
2 files changed with 18 insertions and 6 deletions:
↑ Collapse diff ↑
Ignore white space 24 line context
... ...
@@ -107,25 +107,25 @@
107 107
    typedef typename Parent::Key Key;
108 108
    typedef typename Parent::Value Value;
109 109

	
110 110
    /// Default constructor
111 111

	
112 112
    /// Default constructor.
113 113
    /// The value of the map will be default constructed.
114 114
    ConstMap() {}
115 115

	
116 116
    /// Constructor with specified initial value
117 117

	
118 118
    /// Constructor with specified initial value.
119
    /// \param v is the initial value of the map.
119
    /// \param v The initial value of the map.
120 120
    ConstMap(const Value &v) : _value(v) {}
121 121

	
122 122
    /// Gives back the specified value.
123 123
    Value operator[](const Key&) const { return _value; }
124 124

	
125 125
    /// Absorbs the value.
126 126
    void set(const Key&, const Value&) {}
127 127

	
128 128
    /// Sets the value that is assigned to each key.
129 129
    void setAll(const Value &v) {
130 130
      _value = v;
131 131
    }
... ...
@@ -134,24 +134,29 @@
134 134
    ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
135 135
  };
136 136

	
137 137
  /// Returns a \ref ConstMap class
138 138

	
139 139
  /// This function just returns a \ref ConstMap class.
140 140
  /// \relates ConstMap
141 141
  template<typename K, typename V>
142 142
  inline ConstMap<K, V> constMap(const V &v) {
143 143
    return ConstMap<K, V>(v);
144 144
  }
145 145

	
146
  template<typename K, typename V>
147
  inline ConstMap<K, V> constMap() {
148
    return ConstMap<K, V>();
149
  }
150

	
146 151

	
147 152
  template<typename T, T v>
148 153
  struct Const {};
149 154

	
150 155
  /// Constant map with inlined constant value.
151 156

	
152 157
  /// This \ref concepts::ReadMap "readable map" assigns a specified
153 158
  /// value to each key.
154 159
  ///
155 160
  /// In other aspects it is equivalent to \ref NullMap.
156 161
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
157 162
  /// concept, but it absorbs the data written to it.
... ...
@@ -604,25 +609,25 @@
604 609
  /// a functor typically does not provide \c argument_type and
605 610
  /// \c result_type typedefs.
606 611
  /// Parameter \c F is the type of the used functor.
607 612
  ///
608 613
  /// The simplest way of using this map is through the functorToMap()
609 614
  /// function.
610 615
  ///
611 616
  /// \sa MapToFunctor
612 617
  template<typename F,
613 618
	   typename K = typename F::argument_type,
614 619
	   typename V = typename F::result_type>
615 620
  class FunctorToMap : public MapBase<K, V> {
616
    const F &_f;
621
    F _f;
617 622
  public:
618 623
    typedef MapBase<K, V> Parent;
619 624
    typedef typename Parent::Key Key;
620 625
    typedef typename Parent::Value Value;
621 626

	
622 627
    /// Constructor
623 628
    FunctorToMap(const F &f = F()) : _f(f) {}
624 629
    /// \e
625 630
    Value operator[](const Key &k) const { return _f(k); }
626 631
  };
627 632

	
628 633
  /// Returns a \ref FunctorToMap class
Ignore white space 6 line context
... ...
@@ -74,38 +74,45 @@
74 74

	
75 75
  // NullMap
76 76
  {
77 77
    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
78 78
    NullMap<A,B> map1;
79 79
    NullMap<A,B> map2 = map1;
80 80
    map1 = nullMap<A,B>();
81 81
  }
82 82

	
83 83
  // ConstMap
84 84
  {
85 85
    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
86
    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
86 87
    ConstMap<A,B> map1;
87 88
    ConstMap<A,B> map2(B());
88 89
    ConstMap<A,B> map3 = map1;
89 90
    map1 = constMap<A>(B());
91
    map1 = constMap<A,B>();
90 92
    map1.setAll(B());
93
    ConstMap<A,C> map4(C(1));
94
    ConstMap<A,C> map5 = map4;
95
    map4 = constMap<A>(C(2));
96
    map4.setAll(C(3));
91 97

	
92 98
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
93 99
    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
94 100

	
95 101
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
96
    ConstMap<A,Const<int,10> > map4;
97
    ConstMap<A,Const<int,10> > map5 = map4;
98
    map4 = map5;
99
    check(map4[A()] == 10 && map5[A()] == 10, "Something is wrong with ConstMap");
102
    ConstMap<A,Const<int,10> > map6;
103
    ConstMap<A,Const<int,10> > map7 = map6;
104
    map6 = constMap<A,int,10>();
105
    map7 = constMap<A,Const<int,10> >();
106
    check(map6[A()] == 10 && map7[A()] == 10, "Something is wrong with ConstMap");
100 107
  }
101 108

	
102 109
  // IdentityMap
103 110
  {
104 111
    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
105 112
    IdentityMap<A> map1;
106 113
    IdentityMap<A> map2 = map1;
107 114
    map1 = identityMap<A>();
108 115

	
109 116
    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
110 117
    check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
111 118
          "Something is wrong with IdentityMap");
0 comments (0 inline)