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 96 line context
... ...
@@ -71,123 +71,128 @@
71 71
    /// Gives back a default constructed element.
72 72
    Value operator[](const Key&) const { return Value(); }
73 73
    /// Absorbs the value.
74 74
    void set(const Key&, const Value&) {}
75 75
  };
76 76

	
77 77
  /// Returns a \ref NullMap class
78 78

	
79 79
  /// This function just returns a \ref NullMap class.
80 80
  /// \relates NullMap
81 81
  template <typename K, typename V>
82 82
  NullMap<K, V> nullMap() {
83 83
    return NullMap<K, V>();
84 84
  }
85 85

	
86 86

	
87 87
  /// Constant map.
88 88

	
89 89
  /// This \ref concepts::ReadMap "readable map" assigns a specified
90 90
  /// value to each key.
91 91
  ///
92 92
  /// In other aspects it is equivalent to \ref NullMap.
93 93
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
94 94
  /// concept, but it absorbs the data written to it.
95 95
  ///
96 96
  /// The simplest way of using this map is through the constMap()
97 97
  /// function.
98 98
  ///
99 99
  /// \sa NullMap
100 100
  /// \sa IdentityMap
101 101
  template<typename K, typename V>
102 102
  class ConstMap : public MapBase<K, V> {
103 103
  private:
104 104
    V _value;
105 105
  public:
106 106
    typedef MapBase<K, V> Parent;
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
    }
132 132

	
133 133
    template<typename V1>
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.
158 163
  ///
159 164
  /// The simplest way of using this map is through the constMap()
160 165
  /// function.
161 166
  ///
162 167
  /// \sa NullMap
163 168
  /// \sa IdentityMap
164 169
  template<typename K, typename V, V v>
165 170
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
166 171
  public:
167 172
    typedef MapBase<K, V> Parent;
168 173
    typedef typename Parent::Key Key;
169 174
    typedef typename Parent::Value Value;
170 175

	
171 176
    /// Constructor.
172 177
    ConstMap() {}
173 178

	
174 179
    /// Gives back the specified value.
175 180
    Value operator[](const Key&) const { return v; }
176 181

	
177 182
    /// Absorbs the value.
178 183
    void set(const Key&, const Value&) {}
179 184
  };
180 185

	
181 186
  /// Returns a \ref ConstMap class with inlined constant value
182 187

	
183 188
  /// This function just returns a \ref ConstMap class with inlined
184 189
  /// constant value.
185 190
  /// \relates ConstMap
186 191
  template<typename K, typename V, V v>
187 192
  inline ConstMap<K, Const<V, v> > constMap() {
188 193
    return ConstMap<K, Const<V, v> >();
189 194
  }
190 195

	
191 196

	
192 197
  /// Identity map.
193 198

	
... ...
@@ -568,97 +573,97 @@
568 573
  /// is equivalent to
569 574
  /// \code
570 575
  ///   addMap(m1,m2)
571 576
  /// \endcode
572 577
  ///
573 578
  /// This function is specialized for adaptable binary function
574 579
  /// classes and C++ functions.
575 580
  ///
576 581
  /// \relates CombineMap
577 582
  template<typename M1, typename M2, typename F, typename V>
578 583
  inline CombineMap<M1, M2, F, V>
579 584
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
580 585
    return CombineMap<M1, M2, F, V>(m1,m2,f);
581 586
  }
582 587

	
583 588
  template<typename M1, typename M2, typename F>
584 589
  inline CombineMap<M1, M2, F, typename F::result_type>
585 590
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
586 591
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
587 592
  }
588 593

	
589 594
  template<typename M1, typename M2, typename K1, typename K2, typename V>
590 595
  inline CombineMap<M1, M2, V (*)(K1, K2), V>
591 596
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
592 597
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
593 598
  }
594 599

	
595 600

	
596 601
  /// Converts an STL style (unary) functor to a map
597 602

	
598 603
  /// This \ref concepts::ReadMap "read-only map" returns the value
599 604
  /// of a given functor. Actually, it just wraps the functor and
600 605
  /// provides the \c Key and \c Value typedefs.
601 606
  ///
602 607
  /// Template parameters \c K and \c V will become its \c Key and
603 608
  /// \c Value. In most cases they have to be given explicitly because
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
629 634

	
630 635
  /// This function just returns a \ref FunctorToMap class.
631 636
  ///
632 637
  /// This function is specialized for adaptable binary function
633 638
  /// classes and C++ functions.
634 639
  ///
635 640
  /// \relates FunctorToMap
636 641
  template<typename K, typename V, typename F>
637 642
  inline FunctorToMap<F, K, V> functorToMap(const F &f) {
638 643
    return FunctorToMap<F, K, V>(f);
639 644
  }
640 645

	
641 646
  template <typename F>
642 647
  inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
643 648
    functorToMap(const F &f)
644 649
  {
645 650
    return FunctorToMap<F, typename F::argument_type,
646 651
      typename F::result_type>(f);
647 652
  }
648 653

	
649 654
  template <typename K, typename V>
650 655
  inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
651 656
    return FunctorToMap<V (*)(K), K, V>(f);
652 657
  }
653 658

	
654 659

	
655 660
  /// Converts a map to an STL style (unary) functor
656 661

	
657 662
  /// This class converts a map to an STL style (unary) functor.
658 663
  /// That is it provides an <tt>operator()</tt> to read its values.
659 664
  ///
660 665
  /// For the sake of convenience it also works as a usual
661 666
  /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
662 667
  /// and the \c Key and \c Value typedefs also exist.
663 668
  ///
664 669
  /// The simplest way of using this map is through the mapToFunctor()
Ignore white space 96 line context
... ...
@@ -38,110 +38,117 @@
38 38
  C(int _x) : x(_x) {}
39 39
};
40 40

	
41 41
class F {
42 42
public:
43 43
  typedef A argument_type;
44 44
  typedef B result_type;
45 45

	
46 46
  B operator()(const A&) const { return B(); }
47 47
private:
48 48
  F& operator=(const F&);
49 49
};
50 50

	
51 51
int func(A) { return 3; }
52 52

	
53 53
int binc(int a, B) { return a+1; }
54 54

	
55 55
typedef ReadMap<A, double> DoubleMap;
56 56
typedef ReadWriteMap<A, double> DoubleWriteMap;
57 57
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
58 58

	
59 59
typedef ReadMap<A, bool> BoolMap;
60 60
typedef ReadWriteMap<A, bool> BoolWriteMap;
61 61
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
62 62

	
63 63
int main()
64 64
{
65 65
  // Map concepts
66 66
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
67 67
  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
68 68
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
69 69
  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
70 70
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
71 71
  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
72 72
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
73 73
  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
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");
112 119
  }
113 120

	
114 121
  // RangeMap
115 122
  {
116 123
    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
117 124
    RangeMap<B> map1;
118 125
    RangeMap<B> map2(10);
119 126
    RangeMap<B> map3(10,B());
120 127
    RangeMap<B> map4 = map1;
121 128
    RangeMap<B> map5 = rangeMap<B>();
122 129
    RangeMap<B> map6 = rangeMap<B>(10);
123 130
    RangeMap<B> map7 = rangeMap(10,B());
124 131

	
125 132
    checkConcept< ReferenceMap<int, double, double&, const double&>,
126 133
                  RangeMap<double> >();
127 134
    std::vector<double> v(10, 0);
128 135
    v[5] = 100;
129 136
    RangeMap<double> map8(v);
130 137
    RangeMap<double> map9 = rangeMap(v);
131 138
    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
132 139
          "Something is wrong with RangeMap");
133 140
  }
134 141

	
135 142
  // SparseMap
136 143
  {
137 144
    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
138 145
    SparseMap<A,B> map1;
139 146
    SparseMap<A,B> map2(B());
140 147
    SparseMap<A,B> map3 = sparseMap<A,B>();
141 148
    SparseMap<A,B> map4 = sparseMap<A>(B());
142 149

	
143 150
    checkConcept< ReferenceMap<double, int, int&, const int&>,
144 151
                  SparseMap<double, int> >();
145 152
    std::map<double, int> m;
146 153
    SparseMap<double, int> map5(m);
147 154
    SparseMap<double, int> map6(m,10);
0 comments (0 inline)