gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Port general map related stuff from svn -r3424 + minor changes - Do automatic name changes in lemon/maps.h (only affects doc) - Do not use MapTraits in ComposeMap
0 2 4
default
6 files changed with 1924 insertions and 1 deletions:
↑ Collapse diff ↑
Show white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
// Modified for use in LEMON.
20
// We should really consider using Boost...
21

	
22
//
23
// (C) Copyright Jeremy Siek 2000.
24
// Distributed under the Boost Software License, Version 1.0. (See
25
// accompanying file LICENSE_1_0.txt or copy at
26
// http://www.boost.org/LICENSE_1_0.txt)
27
//
28
// Revision History:
29
//   05 May   2001: Workarounds for HP aCC from Thomas Matelich. (Jeremy Siek)
30
//   02 April 2001: Removed limits header altogether. (Jeremy Siek)
31
//   01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock)
32
//
33

	
34
// See http://www.boost.org/libs/concept_check for documentation.
35

	
36
#ifndef LEMON_BOOST_CONCEPT_CHECKS_HPP
37
#define LEMON_BOOST_CONCEPT_CHECKS_HPP
38

	
39
namespace lemon {
40

	
41
  /*
42
    "inline" is used for ignore_unused_variable_warning()
43
    and function_requires() to make sure there is no
44
    overtarget with g++.
45
  */
46

	
47
  template <class T> inline void ignore_unused_variable_warning(const T&) { }
48

	
49
  template <class Concept>
50
  inline void function_requires()
51
  {
52
#if !defined(NDEBUG)
53
    void (Concept::*x)() = & Concept::constraints;
54
    ignore_unused_variable_warning(x);
55
#endif
56
  }
57

	
58
  template <typename Concept, typename Type>
59
  inline void checkConcept() {
60
#if !defined(NDEBUG)
61
    typedef typename Concept::template Constraints<Type> ConceptCheck;
62
    void (ConceptCheck::*x)() = & ConceptCheck::constraints;
63
    ignore_unused_variable_warning(x);
64
#endif
65
  }
66

	
67
#define BOOST_CLASS_REQUIRE(type_var, ns, concept) \
68
  typedef void (ns::concept <type_var>::* func##type_var##concept)(); \
69
  template <func##type_var##concept Tp1_> \
70
  struct concept_checking_##type_var##concept { }; \
71
  typedef concept_checking_##type_var##concept< \
72
    BOOST_FPTR ns::concept<type_var>::constraints> \
73
    concept_checking_typedef_##type_var##concept
74

	
75
#define BOOST_CLASS_REQUIRE2(type_var1, type_var2, ns, concept) \
76
  typedef void (ns::concept <type_var1,type_var2>::* \
77
     func##type_var1##type_var2##concept)(); \
78
  template <func##type_var1##type_var2##concept Tp1_> \
79
  struct concept_checking_##type_var1##type_var2##concept { }; \
80
  typedef concept_checking_##type_var1##type_var2##concept< \
81
    BOOST_FPTR ns::concept<type_var1,type_var2>::constraints> \
82
    concept_checking_typedef_##type_var1##type_var2##concept
83

	
84
#define BOOST_CLASS_REQUIRE3(tv1, tv2, tv3, ns, concept) \
85
  typedef void (ns::concept <tv1,tv2,tv3>::* \
86
     func##tv1##tv2##tv3##concept)(); \
87
  template <func##tv1##tv2##tv3##concept Tp1_> \
88
  struct concept_checking_##tv1##tv2##tv3##concept { }; \
89
  typedef concept_checking_##tv1##tv2##tv3##concept< \
90
    BOOST_FPTR ns::concept<tv1,tv2,tv3>::constraints> \
91
    concept_checking_typedef_##tv1##tv2##tv3##concept
92

	
93
#define BOOST_CLASS_REQUIRE4(tv1, tv2, tv3, tv4, ns, concept) \
94
  typedef void (ns::concept <tv1,tv2,tv3,tv4>::* \
95
     func##tv1##tv2##tv3##tv4##concept)(); \
96
  template <func##tv1##tv2##tv3##tv4##concept Tp1_> \
97
  struct concept_checking_##tv1##tv2##tv3##tv4##concept { }; \
98
  typedef concept_checking_##tv1##tv2##tv3##tv4##concept< \
99
    BOOST_FPTR ns::concept<tv1,tv2,tv3,tv4>::constraints> \
100
    concept_checking_typedef_##tv1##tv2##tv3##tv4##concept
101

	
102

	
103
} // namespace lemon
104

	
105
#endif // LEMON_BOOST_CONCEPT_CHECKS_HPP
Show white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_CONCEPT_MAPS_H
20
#define LEMON_CONCEPT_MAPS_H
21

	
22
#include <lemon/bits/utility.h>
23
#include <lemon/concept_check.h>
24

	
25
///\ingroup concept
26
///\file
27
///\brief Map concepts checking classes for testing and documenting.
28

	
29
namespace lemon {
30

	
31
  namespace concepts {
32
  
33
    /// \addtogroup concept
34
    /// @{
35

	
36
    /// Readable map concept
37
    template<typename K, typename T>
38
    class ReadMap
39
    {
40
    public:
41
      /// Map's key type.
42
      typedef K Key;    
43
      /// Map's value type. (The type of objects associated with the keys).
44
      typedef T Value;
45

	
46
      /// Returns the value associated with a key.
47

	
48
      /// \bug Value should n't need to be default constructible.
49
      ///
50
      Value operator[](const Key &) const {return Value();}
51

	
52
      template<typename _ReadMap>
53
      struct Constraints {
54

	
55
	void constraints() {
56
	  Value val = m[key];
57
	  val = m[key];
58
	  typename _ReadMap::Value own_val = m[own_key]; 
59
	  own_val = m[own_key]; 
60

	
61
	  ignore_unused_variable_warning(val);
62
	  ignore_unused_variable_warning(own_val);
63
	  ignore_unused_variable_warning(key);
64
	}
65
	Key& key;
66
	typename _ReadMap::Key& own_key;
67
	_ReadMap& m;
68
      };
69
      
70
    };
71

	
72

	
73
    /// Writable map concept
74
    template<typename K, typename T>
75
    class WriteMap
76
    {
77
    public:
78
      /// Map's key type.
79
      typedef K Key;    
80
      /// Map's value type. (The type of objects associated with the keys).
81
      typedef T Value;
82

	
83
      /// Sets the value associated with a key.
84
      void set(const Key &,const Value &) {}
85

	
86
      ///Default constructor
87
      WriteMap() {}
88

	
89
      template <typename _WriteMap>
90
      struct Constraints {
91
	void constraints() {
92
	  // No constraints for constructor.
93
	  m.set(key, val);
94
	  m.set(own_key, own_val);
95
	  ignore_unused_variable_warning(key);
96
	  ignore_unused_variable_warning(val);
97
	  ignore_unused_variable_warning(own_key);
98
	  ignore_unused_variable_warning(own_val);
99
	}
100

	
101
	Value& val;
102
	typename _WriteMap::Value own_val;
103
	Key& key;
104
	typename _WriteMap::Key& own_key;
105
	_WriteMap& m;
106

	
107
      };
108
    };
109

	
110
    ///Read/Writable map concept
111
    template<typename K, typename T>
112
    class ReadWriteMap : public ReadMap<K,T>,
113
			    public WriteMap<K,T>
114
    {
115
    public:
116
      /// Map's key type.
117
      typedef K Key;    
118
      /// Map's value type. (The type of objects associated with the keys).
119
      typedef T Value;
120

	
121
      /// Returns the value associated with a key.
122
      Value operator[](const Key &) const {return Value();}
123
      /// Sets the value associated with a key.
124
      void set(const Key & ,const Value &) {}
125

	
126
      template<typename _ReadWriteMap>
127
      struct Constraints {
128
	void constraints() {
129
	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
130
	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
131
	}
132
      };
133
    };
134
  
135
  
136
    ///Dereferable map concept
137
    template<typename K, typename T, typename R, typename CR>
138
    class ReferenceMap : public ReadWriteMap<K,T>
139
    {
140
    public:
141
      /// Tag for reference maps.
142
      typedef True ReferenceMapTag;
143
      /// Map's key type.
144
      typedef K Key;    
145
      /// Map's value type. (The type of objects associated with the keys).
146
      typedef T Value;
147
      /// Map's reference type.
148
      typedef R Reference;
149
      /// Map's const reference type.
150
      typedef CR ConstReference;
151

	
152
    protected:
153
      Value tmp;
154
    public:
155

	
156
      ///Returns a reference to the value associated to a key.
157
      Reference operator[](const Key &) { return tmp; }
158
      ///Returns a const reference to the value associated to a key.
159
      ConstReference operator[](const Key &) const
160
      { return tmp; }
161
      /// Sets the value associated with a key.
162
      void set(const Key &k,const Value &t) { operator[](k)=t; }
163

	
164
      // \todo rethink this concept
165
      template<typename _ReferenceMap>
166
      struct ReferenceMapConcept {
167

	
168
	void constraints() {
169
	  checkConcept<ReadWriteMap, _ReferenceMap >();
170
	  m[key] = val;
171
	  val  = m[key];
172
	  m[key] = ref;
173
	  ref = m[key];
174
	  m[own_key] = own_val;
175
	  own_val  = m[own_key];
176
	  m[own_key] = own_ref;
177
	  own_ref = m[own_key];	  	  
178
	}
179

	
180
	typename _ReferenceMap::Key& own_key;
181
	typename _ReferenceMap::Value& own_val;
182
	typename _ReferenceMap::Reference& own_ref;
183
	Key& key;
184
	Value& val;
185
	Reference& ref;
186
	_ReferenceMap& m;
187
      };
188
    };
189

	
190
    // @}
191

	
192
  } //namespace concepts
193
} //namespace lemon
194
#endif // LEMON_CONCEPT_MAPS_H
Show white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_MAPS_H
20
#define LEMON_MAPS_H
21

	
22
#include <iterator>
23
#include <functional>
24
#include <vector>
25

	
26
#include <lemon/bits/utility.h>
27
// #include <lemon/bits/traits.h>
28

	
29
///\file
30
///\ingroup maps
31
///\brief Miscellaneous property maps
32
///
33
#include <map>
34

	
35
namespace lemon {
36

	
37
  /// \addtogroup maps
38
  /// @{
39

	
40
  /// Base class of maps.
41

	
42
  /// Base class of maps.
43
  /// It provides the necessary <tt>typedef</tt>s required by the map concept.
44
  template<typename K, typename T>
45
  class MapBase {
46
  public:
47
    ///\e
48
    typedef K Key;
49
    ///\e
50
    typedef T Value;
51
  };
52

	
53
  /// Null map. (a.k.a. DoNothingMap)
54

	
55
  /// If you have to provide a map only for its type definitions,
56
  /// or if you have to provide a writable map, but
57
  /// data written to it will sent to <tt>/dev/null</tt>...
58
  template<typename K, typename T>
59
  class NullMap : public MapBase<K, T> {
60
  public:
61
    typedef MapBase<K, T> Parent;
62
    typedef typename Parent::Key Key;
63
    typedef typename Parent::Value Value;
64
    
65
    /// Gives back a default constructed element.
66
    T operator[](const K&) const { return T(); }
67
    /// Absorbs the value.
68
    void set(const K&, const T&) {}
69
  };
70

	
71
  template <typename K, typename V> 
72
  NullMap<K, V> nullMap() {
73
    return NullMap<K, V>();
74
  }
75

	
76

	
77
  /// Constant map.
78

	
79
  /// This is a readable map which assigns a specified value to each key.
80
  /// In other aspects it is equivalent to the \c NullMap.
81
  template<typename K, typename T>
82
  class ConstMap : public MapBase<K, T> {
83
  private:
84
    T v;
85
  public:
86

	
87
    typedef MapBase<K, T> Parent;
88
    typedef typename Parent::Key Key;
89
    typedef typename Parent::Value Value;
90

	
91
    /// Default constructor
92

	
93
    /// The value of the map will be uninitialized. 
94
    /// (More exactly it will be default constructed.)
95
    ConstMap() {}
96
    ///\e
97

	
98
    /// \param _v The initial value of the map.
99
    ///
100
    ConstMap(const T &_v) : v(_v) {}
101
    
102
    ///\e
103
    T operator[](const K&) const { return v; }
104

	
105
    ///\e
106
    void setAll(const T &t) {
107
      v = t;
108
    }    
109

	
110
    template<typename T1>
111
    struct rebind {
112
      typedef ConstMap<K, T1> other;
113
    };
114

	
115
    template<typename T1>
116
    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
117
  };
118

	
119
  ///Returns a \c ConstMap class
120

	
121
  ///This function just returns a \c ConstMap class.
122
  ///\relates ConstMap
123
  template<typename K, typename V> 
124
  inline ConstMap<K, V> constMap(const V &v) {
125
    return ConstMap<K, V>(v);
126
  }
127

	
128

	
129
  template<typename T, T v>
130
  struct Const { };
131

	
132
  /// Constant map with inlined constant value.
133

	
134
  /// This is a readable map which assigns a specified value to each key.
135
  /// In other aspects it is equivalent to the \c NullMap.
136
  template<typename K, typename V, V v>
137
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
138
  public:
139
    typedef MapBase<K, V> Parent;
140
    typedef typename Parent::Key Key;
141
    typedef typename Parent::Value Value;
142

	
143
    ConstMap() { }
144
    ///\e
145
    V operator[](const K&) const { return v; }
146
    ///\e
147
    void set(const K&, const V&) { }
148
  };
149

	
150
  ///Returns a \c ConstMap class
151

	
152
  ///This function just returns a \c ConstMap class with inlined value.
153
  ///\relates ConstMap
154
  template<typename K, typename V, V v> 
155
  inline ConstMap<K, Const<V, v> > constMap() {
156
    return ConstMap<K, Const<V, v> >();
157
  }
158

	
159
  ///Map based on std::map
160

	
161
  ///This is essentially a wrapper for \c std::map. With addition that
162
  ///you can specify a default value different from \c Value() .
163
  template <typename K, typename T, typename Compare = std::less<K> >
164
  class StdMap {
165
    template <typename K1, typename T1, typename C1>
166
    friend class StdMap;
167
  public:
168

	
169
    typedef True ReferenceMapTag;
170
    ///\e
171
    typedef K Key;
172
    ///\e
173
    typedef T Value;
174
    ///\e
175
    typedef T& Reference;
176
    ///\e
177
    typedef const T& ConstReference;
178

	
179
  private:
180
    
181
    typedef std::map<K, T, Compare> Map;
182
    Value _value;
183
    Map _map;
184

	
185
  public:
186

	
187
    /// Constructor with specified default value
188
    StdMap(const T& value = T()) : _value(value) {}
189
    /// \brief Constructs the map from an appropriate std::map, and explicitly
190
    /// specifies a default value.
191
    template <typename T1, typename Comp1>
192
    StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) 
193
      : _map(map.begin(), map.end()), _value(value) {}
194
    
195
    /// \brief Constructs a map from an other StdMap.
196
    template<typename T1, typename Comp1>
197
    StdMap(const StdMap<Key, T1, Comp1> &c) 
198
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
199

	
200
  private:
201

	
202
    StdMap& operator=(const StdMap&);
203

	
204
  public:
205

	
206
    ///\e
207
    Reference operator[](const Key &k) {
208
      typename Map::iterator it = _map.lower_bound(k);
209
      if (it != _map.end() && !_map.key_comp()(k, it->first))
210
	return it->second;
211
      else
212
	return _map.insert(it, std::make_pair(k, _value))->second;
213
    }
214

	
215
    /// \e 
216
    ConstReference operator[](const Key &k) const {
217
      typename Map::const_iterator it = _map.find(k);
218
      if (it != _map.end())
219
	return it->second;
220
      else
221
	return _value;
222
    }
223

	
224
    /// \e 
225
    void set(const Key &k, const T &t) {
226
      typename Map::iterator it = _map.lower_bound(k);
227
      if (it != _map.end() && !_map.key_comp()(k, it->first))
228
	it->second = t;
229
      else
230
	_map.insert(it, std::make_pair(k, t));
231
    }
232

	
233
    /// \e
234
    void setAll(const T &t) {
235
      _value = t;
236
      _map.clear();
237
    }    
238

	
239
    template <typename T1, typename C1 = std::less<T1> >
240
    struct rebind {
241
      typedef StdMap<Key, T1, C1> other;
242
    };
243
  };
244

	
245
  /// \brief Map for storing values for the range \c [0..size-1] range keys
246
  ///
247
  /// The current map has the \c [0..size-1] keyset and the values
248
  /// are stored in a \c std::vector<T>  container. It can be used with
249
  /// some data structures, for example \c UnionFind, \c BinHeap, when 
250
  /// the used items are small integer numbers.
251
  template <typename T>
252
  class IntegerMap {
253

	
254
    template <typename T1>
255
    friend class IntegerMap;
256

	
257
  public:
258

	
259
    typedef True ReferenceMapTag;
260
    ///\e
261
    typedef int Key;
262
    ///\e
263
    typedef T Value;
264
    ///\e
265
    typedef T& Reference;
266
    ///\e
267
    typedef const T& ConstReference;
268

	
269
  private:
270
    
271
    typedef std::vector<T> Vector;
272
    Vector _vector;
273

	
274
  public:
275

	
276
    /// Constructor with specified default value
277
    IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
278

	
279
    /// \brief Constructs the map from an appropriate std::vector.
280
    template <typename T1>
281
    IntegerMap(const std::vector<T1>& vector) 
282
      : _vector(vector.begin(), vector.end()) {}
283
    
284
    /// \brief Constructs a map from an other IntegerMap.
285
    template <typename T1>
286
    IntegerMap(const IntegerMap<T1> &c) 
287
      : _vector(c._vector.begin(), c._vector.end()) {}
288

	
289
    /// \brief Resize the container
290
    void resize(int size, const T& value = T()) {
291
      _vector.resize(size, value);
292
    }
293

	
294
  private:
295

	
296
    IntegerMap& operator=(const IntegerMap&);
297

	
298
  public:
299

	
300
    ///\e
301
    Reference operator[](Key k) {
302
      return _vector[k];
303
    }
304

	
305
    /// \e 
306
    ConstReference operator[](Key k) const {
307
      return _vector[k];
308
    }
309

	
310
    /// \e 
311
    void set(const Key &k, const T& t) {
312
      _vector[k] = t;
313
    }
314

	
315
  };
316

	
317
  /// @}
318

	
319
  /// \addtogroup map_adaptors
320
  /// @{
321

	
322
  /// \brief Identity mapping.
323
  ///
324
  /// This mapping gives back the given key as value without any
325
  /// modification. 
326
  template <typename T>
327
  class IdentityMap : public MapBase<T, T> {
328
  public:
329
    typedef MapBase<T, T> Parent;
330
    typedef typename Parent::Key Key;
331
    typedef typename Parent::Value Value;
332

	
333
    /// \e
334
    const T& operator[](const T& t) const {
335
      return t;
336
    }
337
  };
338

	
339
  ///Returns an \c IdentityMap class
340

	
341
  ///This function just returns an \c IdentityMap class.
342
  ///\relates IdentityMap
343
  template<typename T>
344
  inline IdentityMap<T> identityMap() {
345
    return IdentityMap<T>();
346
  }
347
  
348

	
349
  ///Convert the \c Value of a map to another type.
350

	
351
  ///This \c concepts::ReadMap "read only map"
352
  ///converts the \c Value of a maps to type \c T.
353
  ///Its \c Key is inherited from \c M.
354
  template <typename M, typename T> 
355
  class ConvertMap : public MapBase<typename M::Key, T> {
356
    const M& m;
357
  public:
358
    typedef MapBase<typename M::Key, T> Parent;
359
    typedef typename Parent::Key Key;
360
    typedef typename Parent::Value Value;
361

	
362
    ///Constructor
363

	
364
    ///Constructor
365
    ///\param _m is the underlying map
366
    ConvertMap(const M &_m) : m(_m) {};
367

	
368
    /// \brief The subscript operator.
369
    ///
370
    /// The subscript operator.
371
    /// \param k The key
372
    /// \return The target of the arc 
373
    Value operator[](const Key& k) const {return m[k];}
374
  };
375
  
376
  ///Returns an \c ConvertMap class
377

	
378
  ///This function just returns an \c ConvertMap class.
379
  ///\relates ConvertMap
380
  template<typename T, typename M>
381
  inline ConvertMap<M, T> convertMap(const M &m) {
382
    return ConvertMap<M, T>(m);
383
  }
384

	
385
  ///Simple wrapping of the map
386

	
387
  ///This \c concepts::ReadMap "read only map" returns the simple
388
  ///wrapping of the given map. Sometimes the reference maps cannot be
389
  ///combined with simple read maps. This map adaptor wraps the given
390
  ///map to simple read map.
391
  template<typename M> 
392
  class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
393
    const M& m;
394

	
395
  public:
396
    typedef MapBase<typename M::Key, typename M::Value> Parent;
397
    typedef typename Parent::Key Key;
398
    typedef typename Parent::Value Value;
399

	
400
    ///Constructor
401
    SimpleMap(const M &_m) : m(_m) {};
402
    ///\e
403
    Value operator[](Key k) const {return m[k];}
404
  };
405

	
406
  ///Simple writeable wrapping of the map
407

	
408
  ///This \c concepts::ReadMap "read only map" returns the simple
409
  ///wrapping of the given map. Sometimes the reference maps cannot be
410
  ///combined with simple read-write maps. This map adaptor wraps the
411
  ///given map to simple read-write map.
412
  template<typename M> 
413
  class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
414
    M& m;
415

	
416
  public:
417
    typedef MapBase<typename M::Key, typename M::Value> Parent;
418
    typedef typename Parent::Key Key;
419
    typedef typename Parent::Value Value;
420

	
421
    ///Constructor
422
    SimpleWriteMap(M &_m) : m(_m) {};
423
    ///\e
424
    Value operator[](Key k) const {return m[k];}
425
    ///\e
426
    void set(Key k, const Value& c) { m.set(k, c); }
427
  };
428

	
429
  ///Sum of two maps
430

	
431
  ///This \c concepts::ReadMap "read only map" returns the sum of the two
432
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
433
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
434

	
435
  template<typename M1, typename M2> 
436
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
437
    const M1& m1;
438
    const M2& m2;
439

	
440
  public:
441
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
442
    typedef typename Parent::Key Key;
443
    typedef typename Parent::Value Value;
444

	
445
    ///Constructor
446
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
447
    ///\e
448
    Value operator[](Key k) const {return m1[k]+m2[k];}
449
  };
450
  
451
  ///Returns an \c AddMap class
452

	
453
  ///This function just returns an \c AddMap class.
454
  ///\todo How to call these type of functions?
455
  ///
456
  ///\relates AddMap
457
  template<typename M1, typename M2> 
458
  inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
459
    return AddMap<M1, M2>(m1,m2);
460
  }
461

	
462
  ///Shift a map with a constant.
463

	
464
  ///This \c concepts::ReadMap "read only map" returns the sum of the
465
  ///given map and a constant value.
466
  ///Its \c Key and \c Value is inherited from \c M.
467
  ///
468
  ///Actually,
469
  ///\code
470
  ///  ShiftMap<X> sh(x,v);
471
  ///\endcode
472
  ///is equivalent with
473
  ///\code
474
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
475
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
476
  ///\endcode
477
  template<typename M, typename C = typename M::Value> 
478
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
479
    const M& m;
480
    C v;
481
  public:
482
    typedef MapBase<typename M::Key, typename M::Value> Parent;
483
    typedef typename Parent::Key Key;
484
    typedef typename Parent::Value Value;
485

	
486
    ///Constructor
487

	
488
    ///Constructor
489
    ///\param _m is the undelying map
490
    ///\param _v is the shift value
491
    ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
492
    ///\e
493
    Value operator[](Key k) const {return m[k] + v;}
494
  };
495

	
496
  ///Shift a map with a constant.
497

	
498
  ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the
499
  ///given map and a constant value. It makes also possible to write the map.
500
  ///Its \c Key and \c Value is inherited from \c M.
501
  ///
502
  ///Actually,
503
  ///\code
504
  ///  ShiftMap<X> sh(x,v);
505
  ///\endcode
506
  ///is equivalent with
507
  ///\code
508
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
509
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
510
  ///\endcode
511
  template<typename M, typename C = typename M::Value> 
512
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
513
    M& m;
514
    C v;
515
  public:
516
    typedef MapBase<typename M::Key, typename M::Value> Parent;
517
    typedef typename Parent::Key Key;
518
    typedef typename Parent::Value Value;
519

	
520
    ///Constructor
521

	
522
    ///Constructor
523
    ///\param _m is the undelying map
524
    ///\param _v is the shift value
525
    ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
526
    /// \e
527
    Value operator[](Key k) const {return m[k] + v;}
528
    /// \e
529
    void set(Key k, const Value& c) { m.set(k, c - v); }
530
  };
531
  
532
  ///Returns an \c ShiftMap class
533

	
534
  ///This function just returns an \c ShiftMap class.
535
  ///\relates ShiftMap
536
  template<typename M, typename C> 
537
  inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
538
    return ShiftMap<M, C>(m,v);
539
  }
540

	
541
  template<typename M, typename C> 
542
  inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
543
    return ShiftWriteMap<M, C>(m,v);
544
  }
545

	
546
  ///Difference of two maps
547

	
548
  ///This \c concepts::ReadMap "read only map" returns the difference
549
  ///of the values of the two
550
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
551
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
552

	
553
  template<typename M1, typename M2> 
554
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
555
    const M1& m1;
556
    const M2& m2;
557
  public:
558
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
559
    typedef typename Parent::Key Key;
560
    typedef typename Parent::Value Value;
561

	
562
    ///Constructor
563
    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
564
    /// \e
565
    Value operator[](Key k) const {return m1[k]-m2[k];}
566
  };
567
  
568
  ///Returns a \c SubMap class
569

	
570
  ///This function just returns a \c SubMap class.
571
  ///
572
  ///\relates SubMap
573
  template<typename M1, typename M2> 
574
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
575
    return SubMap<M1, M2>(m1, m2);
576
  }
577

	
578
  ///Product of two maps
579

	
580
  ///This \c concepts::ReadMap "read only map" returns the product of the
581
  ///values of the two
582
  ///given
583
  ///maps. Its \c Key and \c Value will be inherited from \c M1.
584
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
585

	
586
  template<typename M1, typename M2> 
587
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
588
    const M1& m1;
589
    const M2& m2;
590
  public:
591
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
592
    typedef typename Parent::Key Key;
593
    typedef typename Parent::Value Value;
594

	
595
    ///Constructor
596
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
597
    /// \e
598
    Value operator[](Key k) const {return m1[k]*m2[k];}
599
  };
600
  
601
  ///Returns a \c MulMap class
602

	
603
  ///This function just returns a \c MulMap class.
604
  ///\relates MulMap
605
  template<typename M1, typename M2> 
606
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
607
    return MulMap<M1, M2>(m1,m2);
608
  }
609
 
610
  ///Scales a maps with a constant.
611

	
612
  ///This \c concepts::ReadMap "read only map" returns the value of the
613
  ///given map multiplied from the left side with a constant value.
614
  ///Its \c Key and \c Value is inherited from \c M.
615
  ///
616
  ///Actually,
617
  ///\code
618
  ///  ScaleMap<X> sc(x,v);
619
  ///\endcode
620
  ///is equivalent with
621
  ///\code
622
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
623
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
624
  ///\endcode
625
  template<typename M, typename C = typename M::Value> 
626
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
627
    const M& m;
628
    C v;
629
  public:
630
    typedef MapBase<typename M::Key, typename M::Value> Parent;
631
    typedef typename Parent::Key Key;
632
    typedef typename Parent::Value Value;
633

	
634
    ///Constructor
635

	
636
    ///Constructor
637
    ///\param _m is the undelying map
638
    ///\param _v is the scaling value
639
    ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
640
    /// \e
641
    Value operator[](Key k) const {return v * m[k];}
642
  };
643

	
644
  ///Scales a maps with a constant.
645

	
646
  ///This \c concepts::ReadWriteMap "read-write map" returns the value of the
647
  ///given map multiplied from the left side with a constant value. It can
648
  ///be used as write map also if the given multiplier is not zero.
649
  ///Its \c Key and \c Value is inherited from \c M.
650
  template<typename M, typename C = typename M::Value> 
651
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
652
    M& m;
653
    C v;
654
  public:
655
    typedef MapBase<typename M::Key, typename M::Value> Parent;
656
    typedef typename Parent::Key Key;
657
    typedef typename Parent::Value Value;
658

	
659
    ///Constructor
660

	
661
    ///Constructor
662
    ///\param _m is the undelying map
663
    ///\param _v is the scaling value
664
    ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
665
    /// \e
666
    Value operator[](Key k) const {return v * m[k];}
667
    /// \e
668
    void set(Key k, const Value& c) { m.set(k, c / v);}
669
  };
670
  
671
  ///Returns an \c ScaleMap class
672

	
673
  ///This function just returns an \c ScaleMap class.
674
  ///\relates ScaleMap
675
  template<typename M, typename C> 
676
  inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
677
    return ScaleMap<M, C>(m,v);
678
  }
679

	
680
  template<typename M, typename C> 
681
  inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
682
    return ScaleWriteMap<M, C>(m,v);
683
  }
684

	
685
  ///Quotient of two maps
686

	
687
  ///This \c concepts::ReadMap "read only map" returns the quotient of the
688
  ///values of the two
689
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
690
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
691

	
692
  template<typename M1, typename M2> 
693
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
694
    const M1& m1;
695
    const M2& m2;
696
  public:
697
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
698
    typedef typename Parent::Key Key;
699
    typedef typename Parent::Value Value;
700

	
701
    ///Constructor
702
    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
703
    /// \e
704
    Value operator[](Key k) const {return m1[k]/m2[k];}
705
  };
706
  
707
  ///Returns a \c DivMap class
708

	
709
  ///This function just returns a \c DivMap class.
710
  ///\relates DivMap
711
  template<typename M1, typename M2> 
712
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
713
    return DivMap<M1, M2>(m1,m2);
714
  }
715
  
716
  ///Composition of two maps
717

	
718
  ///This \c concepts::ReadMap "read only map" returns the composition of
719
  ///two
720
  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
721
  ///of \c M2,
722
  ///then for
723
  ///\code
724
  ///  ComposeMap<M1, M2> cm(m1,m2);
725
  ///\endcode
726
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
727
  ///
728
  ///Its \c Key is inherited from \c M2 and its \c Value is from
729
  ///\c M1.
730
  ///The \c M2::Value must be convertible to \c M1::Key.
731
  ///\todo Check the requirements.
732
  template <typename M1, typename M2> 
733
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
734
    const M1& m1;
735
    const M2& m2;
736
  public:
737
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
738
    typedef typename Parent::Key Key;
739
    typedef typename Parent::Value Value;
740

	
741
    ///Constructor
742
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
743
    
744
    /// \e
745

	
746

	
747
    /// \todo Use the  MapTraits once it is ported.
748
    ///
749

	
750
    //typename MapTraits<M1>::ConstReturnValue
751
    typename M1::Value
752
    operator[](Key k) const {return m1[m2[k]];}
753
  };
754
  ///Returns a \c ComposeMap class
755

	
756
  ///This function just returns a \c ComposeMap class.
757
  ///
758
  ///\relates ComposeMap
759
  template <typename M1, typename M2> 
760
  inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
761
    return ComposeMap<M1, M2>(m1,m2);
762
  }
763
  
764
  ///Combines of two maps using an STL (binary) functor.
765

	
766
  ///Combines of two maps using an STL (binary) functor.
767
  ///
768
  ///
769
  ///This \c concepts::ReadMap "read only map" takes two maps and a
770
  ///binary functor and returns the composition of
771
  ///the two
772
  ///given maps unsing the functor. 
773
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
774
  ///and \c f is of \c F,
775
  ///then for
776
  ///\code
777
  ///  CombineMap<M1, M2,F,V> cm(m1,m2,f);
778
  ///\endcode
779
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
780
  ///
781
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
782
  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
783
  ///input parameter of \c F and the return type of \c F must be convertible
784
  ///to \c V.
785
  ///\todo Check the requirements.
786
  template<typename M1, typename M2, typename F,
787
	   typename V = typename F::result_type> 
788
  class CombineMap : public MapBase<typename M1::Key, V> {
789
    const M1& m1;
790
    const M2& m2;
791
    F f;
792
  public:
793
    typedef MapBase<typename M1::Key, V> Parent;
794
    typedef typename Parent::Key Key;
795
    typedef typename Parent::Value Value;
796

	
797
    ///Constructor
798
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F())
799
      : m1(_m1), m2(_m2), f(_f) {};
800
    /// \e
801
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
802
  };
803
  
804
  ///Returns a \c CombineMap class
805

	
806
  ///This function just returns a \c CombineMap class.
807
  ///
808
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
809
  ///\code
810
  ///combineMap<double>(m1,m2,std::plus<double>())
811
  ///\endcode
812
  ///is equivalent with
813
  ///\code
814
  ///addMap(m1,m2)
815
  ///\endcode
816
  ///
817
  ///This function is specialized for adaptable binary function
818
  ///classes and c++ functions.
819
  ///
820
  ///\relates CombineMap
821
  template<typename M1, typename M2, typename F, typename V> 
822
  inline CombineMap<M1, M2, F, V> 
823
  combineMap(const M1& m1,const M2& m2, const F& f) {
824
    return CombineMap<M1, M2, F, V>(m1,m2,f);
825
  }
826

	
827
  template<typename M1, typename M2, typename F> 
828
  inline CombineMap<M1, M2, F, typename F::result_type> 
829
  combineMap(const M1& m1, const M2& m2, const F& f) {
830
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
831
  }
832

	
833
  template<typename M1, typename M2, typename K1, typename K2, typename V> 
834
  inline CombineMap<M1, M2, V (*)(K1, K2), V> 
835
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
836
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
837
  }
838

	
839
  ///Negative value of a map
840

	
841
  ///This \c concepts::ReadMap "read only map" returns the negative
842
  ///value of the
843
  ///value returned by the
844
  ///given map. Its \c Key and \c Value will be inherited from \c M.
845
  ///The unary \c - operator must be defined for \c Value, of course.
846

	
847
  template<typename M> 
848
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
849
    const M& m;
850
  public:
851
    typedef MapBase<typename M::Key, typename M::Value> Parent;
852
    typedef typename Parent::Key Key;
853
    typedef typename Parent::Value Value;
854

	
855
    ///Constructor
856
    NegMap(const M &_m) : m(_m) {};
857
    /// \e
858
    Value operator[](Key k) const {return -m[k];}
859
  };
860
  
861
  ///Negative value of a map
862

	
863
  ///This \c concepts::ReadWriteMap "read-write map" returns the negative
864
  ///value of the value returned by the
865
  ///given map. Its \c Key and \c Value will be inherited from \c M.
866
  ///The unary \c - operator must be defined for \c Value, of course.
867

	
868
  template<typename M> 
869
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
870
    M& m;
871
  public:
872
    typedef MapBase<typename M::Key, typename M::Value> Parent;
873
    typedef typename Parent::Key Key;
874
    typedef typename Parent::Value Value;
875

	
876
    ///Constructor
877
    NegWriteMap(M &_m) : m(_m) {};
878
    /// \e
879
    Value operator[](Key k) const {return -m[k];}
880
    /// \e
881
    void set(Key k, const Value& v) { m.set(k, -v); }
882
  };
883

	
884
  ///Returns a \c NegMap class
885

	
886
  ///This function just returns a \c NegMap class.
887
  ///\relates NegMap
888
  template <typename M> 
889
  inline NegMap<M> negMap(const M &m) {
890
    return NegMap<M>(m);
891
  }
892

	
893
  template <typename M> 
894
  inline NegWriteMap<M> negMap(M &m) {
895
    return NegWriteMap<M>(m);
896
  }
897

	
898
  ///Absolute value of a map
899

	
900
  ///This \c concepts::ReadMap "read only map" returns the absolute value
901
  ///of the
902
  ///value returned by the
903
  ///given map. Its \c Key and \c Value will be inherited
904
  ///from <tt>M</tt>. <tt>Value</tt>
905
  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
906
  ///operator must be defined for it, of course.
907
  ///
908
  ///\bug We need a unified way to handle the situation below:
909
  ///\code
910
  ///  struct _UnConvertible {};
911
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
912
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
913
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
914
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
915
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
916
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
917
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
918
  ///\endcode
919
  
920

	
921
  template<typename M> 
922
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
923
    const M& m;
924
  public:
925
    typedef MapBase<typename M::Key, typename M::Value> Parent;
926
    typedef typename Parent::Key Key;
927
    typedef typename Parent::Value Value;
928

	
929
    ///Constructor
930
    AbsMap(const M &_m) : m(_m) {};
931
    /// \e
932
    Value operator[](Key k) const {
933
      Value tmp = m[k]; 
934
      return tmp >= 0 ? tmp : -tmp;
935
    }
936

	
937
  };
938
  
939
  ///Returns a \c AbsMap class
940

	
941
  ///This function just returns a \c AbsMap class.
942
  ///\relates AbsMap
943
  template<typename M> 
944
  inline AbsMap<M> absMap(const M &m) {
945
    return AbsMap<M>(m);
946
  }
947

	
948
  ///Converts an STL style functor to a map
949

	
950
  ///This \c concepts::ReadMap "read only map" returns the value
951
  ///of a
952
  ///given map.
953
  ///
954
  ///Template parameters \c K and \c V will become its
955
  ///\c Key and \c Value. They must be given explicitely
956
  ///because a functor does not provide such typedefs.
957
  ///
958
  ///Parameter \c F is the type of the used functor.
959
  template<typename F, 
960
	   typename K = typename F::argument_type, 
961
	   typename V = typename F::result_type> 
962
  class FunctorMap : public MapBase<K, V> {
963
    F f;
964
  public:
965
    typedef MapBase<K, V> Parent;
966
    typedef typename Parent::Key Key;
967
    typedef typename Parent::Value Value;
968

	
969
    ///Constructor
970
    FunctorMap(const F &_f = F()) : f(_f) {}
971
    /// \e
972
    Value operator[](Key k) const { return f(k);}
973
  };
974
  
975
  ///Returns a \c FunctorMap class
976

	
977
  ///This function just returns a \c FunctorMap class.
978
  ///
979
  ///It is specialized for adaptable function classes and
980
  ///c++ functions.
981
  ///\relates FunctorMap
982
  template<typename K, typename V, typename F> inline 
983
  FunctorMap<F, K, V> functorMap(const F &f) {
984
    return FunctorMap<F, K, V>(f);
985
  }
986

	
987
  template <typename F> inline 
988
  FunctorMap<F, typename F::argument_type, typename F::result_type> 
989
  functorMap(const F &f) {
990
    return FunctorMap<F, typename F::argument_type, 
991
      typename F::result_type>(f);
992
  }
993

	
994
  template <typename K, typename V> inline 
995
  FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
996
    return FunctorMap<V (*)(K), K, V>(f);
997
  }
998

	
999

	
1000
  ///Converts a map to an STL style (unary) functor
1001

	
1002
  ///This class Converts a map to an STL style (unary) functor.
1003
  ///that is it provides an <tt>operator()</tt> to read its values.
1004
  ///
1005
  ///For the sake of convenience it also works as
1006
  ///a ususal \c concepts::ReadMap "readable map",
1007
  ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
1008
  template <typename M> 
1009
  class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
1010
    const M& m;
1011
  public:
1012
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1013
    typedef typename Parent::Key Key;
1014
    typedef typename Parent::Value Value;
1015

	
1016
    typedef typename M::Key argument_type;
1017
    typedef typename M::Value result_type;
1018

	
1019
    ///Constructor
1020
    MapFunctor(const M &_m) : m(_m) {};
1021
    ///\e
1022
    Value operator()(Key k) const {return m[k];}
1023
    ///\e
1024
    Value operator[](Key k) const {return m[k];}
1025
  };
1026
  
1027
  ///Returns a \c MapFunctor class
1028

	
1029
  ///This function just returns a \c MapFunctor class.
1030
  ///\relates MapFunctor
1031
  template<typename M> 
1032
  inline MapFunctor<M> mapFunctor(const M &m) {
1033
    return MapFunctor<M>(m);
1034
  }
1035

	
1036
  ///Applies all map setting operations to two maps
1037

	
1038
  ///This map has two \c concepts::ReadMap "readable map"
1039
  ///parameters and each read request will be passed just to the
1040
  ///first map. This class is the just readable map type of the ForkWriteMap.
1041
  ///
1042
  ///The \c Key and \c Value will be inherited from \c M1.
1043
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
1044
  template<typename  M1, typename M2> 
1045
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
1046
    const M1& m1;
1047
    const M2& m2;
1048
  public:
1049
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1050
    typedef typename Parent::Key Key;
1051
    typedef typename Parent::Value Value;
1052

	
1053
    ///Constructor
1054
    ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
1055
    /// \e
1056
    Value operator[](Key k) const {return m1[k];}
1057
  };
1058

	
1059

	
1060
  ///Applies all map setting operations to two maps
1061

	
1062
  ///This map has two \c concepts::WriteMap "writable map"
1063
  ///parameters and each write request will be passed to both of them.
1064
  ///If \c M1 is also \c concepts::ReadMap "readable",
1065
  ///then the read operations will return the
1066
  ///corresponding values of \c M1.
1067
  ///
1068
  ///The \c Key and \c Value will be inherited from \c M1.
1069
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
1070
  template<typename  M1, typename M2> 
1071
  class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
1072
    M1& m1;
1073
    M2& m2;
1074
  public:
1075
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
1076
    typedef typename Parent::Key Key;
1077
    typedef typename Parent::Value Value;
1078

	
1079
    ///Constructor
1080
    ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
1081
    ///\e
1082
    Value operator[](Key k) const {return m1[k];}
1083
    ///\e
1084
    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
1085
  };
1086
  
1087
  ///Returns an \c ForkMap class
1088

	
1089
  ///This function just returns an \c ForkMap class.
1090
  ///
1091
  ///\relates ForkMap
1092
  template <typename M1, typename M2> 
1093
  inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
1094
    return ForkMap<M1, M2>(m1,m2);
1095
  }
1096

	
1097
  template <typename M1, typename M2> 
1098
  inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
1099
    return ForkWriteMap<M1, M2>(m1,m2);
1100
  }
1101

	
1102

	
1103
  
1104
  /* ************* BOOL MAPS ******************* */
1105
  
1106
  ///Logical 'not' of a map
1107
  
1108
  ///This bool \c concepts::ReadMap "read only map" returns the 
1109
  ///logical negation of
1110
  ///value returned by the
1111
  ///given map. Its \c Key and will be inherited from \c M,
1112
  ///its Value is <tt>bool</tt>.
1113
  template <typename M> 
1114
  class NotMap : public MapBase<typename M::Key, bool> {
1115
    const M& m;
1116
  public:
1117
    typedef MapBase<typename M::Key, bool> Parent;
1118
    typedef typename Parent::Key Key;
1119
    typedef typename Parent::Value Value;
1120

	
1121
    /// Constructor
1122
    NotMap(const M &_m) : m(_m) {};
1123
    ///\e
1124
    Value operator[](Key k) const {return !m[k];}
1125
  };
1126

	
1127
  ///Logical 'not' of a map with writing possibility
1128
  
1129
  ///This bool \c concepts::ReadWriteMap "read-write map" returns the 
1130
  ///logical negation of value returned by the given map. When it is set,
1131
  ///the opposite value is set to the original map.
1132
  ///Its \c Key and will be inherited from \c M,
1133
  ///its Value is <tt>bool</tt>.
1134
  template <typename M> 
1135
  class NotWriteMap : public MapBase<typename M::Key, bool> {
1136
    M& m;
1137
  public:
1138
    typedef MapBase<typename M::Key, bool> Parent;
1139
    typedef typename Parent::Key Key;
1140
    typedef typename Parent::Value Value;
1141

	
1142
    /// Constructor
1143
    NotWriteMap(M &_m) : m(_m) {};
1144
    ///\e
1145
    Value operator[](Key k) const {return !m[k];}
1146
    ///\e
1147
    void set(Key k, bool v) { m.set(k, !v); }
1148
  };
1149
  
1150
  ///Returns a \c NotMap class
1151
  
1152
  ///This function just returns a \c NotMap class.
1153
  ///\relates NotMap
1154
  template <typename M> 
1155
  inline NotMap<M> notMap(const M &m) {
1156
    return NotMap<M>(m);
1157
  }
1158
  
1159
  template <typename M> 
1160
  inline NotWriteMap<M> notMap(M &m) {
1161
    return NotWriteMap<M>(m);
1162
  }
1163

	
1164
  namespace _maps_bits {
1165

	
1166
    template <typename Value>
1167
    struct Identity {
1168
      typedef Value argument_type;
1169
      typedef Value result_type;
1170
      Value operator()(const Value& val) const {
1171
	return val;
1172
      }
1173
    };
1174

	
1175
    template <typename _Iterator, typename Enable = void>
1176
    struct IteratorTraits {
1177
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
1178
    };
1179

	
1180
    template <typename _Iterator>
1181
    struct IteratorTraits<_Iterator,
1182
      typename exists<typename _Iterator::container_type>::type> 
1183
    {
1184
      typedef typename _Iterator::container_type::value_type Value;
1185
    };
1186

	
1187
  }
1188
  
1189

	
1190
  /// \brief Writable bool map for store each true assigned elements.
1191
  ///
1192
  /// Writable bool map to store each true assigned elements. It will
1193
  /// copies all the keys set to true to the given iterator.
1194
  ///
1195
  /// \note The container of the iterator should contain space 
1196
  /// for each element.
1197
  ///
1198
  /// The next example shows how can you write the nodes directly
1199
  /// to the standard output.
1200
  ///\code
1201
  /// typedef IdMap<Graph, Edge> EdgeIdMap;
1202
  /// EdgeIdMap edgeId(graph);
1203
  ///
1204
  /// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor;
1205
  /// EdgeIdFunctor edgeIdFunctor(edgeId);
1206
  ///
1207
  /// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor> 
1208
  ///   writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor);
1209
  ///
1210
  /// prim(graph, cost, writerMap);
1211
  ///\endcode
1212
  template <typename _Iterator, 
1213
            typename _Functor =
1214
            _maps_bits::Identity<typename _maps_bits::
1215
                                 IteratorTraits<_Iterator>::Value> >
1216
  class StoreBoolMap {
1217
  public:
1218
    typedef _Iterator Iterator;
1219

	
1220
    typedef typename _Functor::argument_type Key;
1221
    typedef bool Value;
1222

	
1223
    typedef _Functor Functor;
1224

	
1225
    /// Constructor
1226
    StoreBoolMap(Iterator it, const Functor& functor = Functor()) 
1227
      : _begin(it), _end(it), _functor(functor) {}
1228

	
1229
    /// Gives back the given iterator set for the first time.
1230
    Iterator begin() const {
1231
      return _begin;
1232
    }
1233
 
1234
    /// Gives back the iterator after the last set operation.
1235
    Iterator end() const {
1236
      return _end;
1237
    }
1238

	
1239
    /// Setter function of the map
1240
    void set(const Key& key, Value value) const {
1241
      if (value) {
1242
	*_end++ = _functor(key);
1243
      }
1244
    }
1245
    
1246
  private:
1247
    Iterator _begin;
1248
    mutable Iterator _end;
1249
    Functor _functor;
1250
  };
1251

	
1252
  /// \brief Writable bool map for store each true assigned elements in 
1253
  /// a back insertable container.
1254
  ///
1255
  /// Writable bool map for store each true assigned elements in a back 
1256
  /// insertable container. It will push back all the keys set to true into
1257
  /// the container. It can be used to retrieve the items into a standard
1258
  /// container. The next example shows how can you store the undirected
1259
  /// arcs in a vector with prim algorithm.
1260
  ///
1261
  ///\code
1262
  /// vector<Edge> span_tree_edges;
1263
  /// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges);
1264
  /// prim(graph, cost, inserter_map);
1265
  ///\endcode
1266
  template <typename Container,
1267
            typename Functor =
1268
            _maps_bits::Identity<typename Container::value_type> >
1269
  class BackInserterBoolMap {
1270
  public:
1271
    typedef typename Container::value_type Key;
1272
    typedef bool Value;
1273

	
1274
    /// Constructor
1275
    BackInserterBoolMap(Container& _container, 
1276
                        const Functor& _functor = Functor()) 
1277
      : container(_container), functor(_functor) {}
1278

	
1279
    /// Setter function of the map
1280
    void set(const Key& key, Value value) {
1281
      if (value) {
1282
	container.push_back(functor(key));
1283
      }
1284
    }
1285
    
1286
  private:
1287
    Container& container;
1288
    Functor functor;
1289
  };
1290

	
1291
  /// \brief Writable bool map for store each true assigned elements in 
1292
  /// a front insertable container.
1293
  ///
1294
  /// Writable bool map for store each true assigned elements in a front 
1295
  /// insertable container. It will push front all the keys set to \c true into
1296
  /// the container. For example see the BackInserterBoolMap.
1297
  template <typename Container,
1298
            typename Functor =
1299
            _maps_bits::Identity<typename Container::value_type> >
1300
  class FrontInserterBoolMap {
1301
  public:
1302
    typedef typename Container::value_type Key;
1303
    typedef bool Value;
1304

	
1305
    /// Constructor
1306
    FrontInserterBoolMap(Container& _container,
1307
                         const Functor& _functor = Functor()) 
1308
      : container(_container), functor(_functor) {}
1309

	
1310
    /// Setter function of the map
1311
    void set(const Key& key, Value value) {
1312
      if (value) {
1313
	container.push_front(key);
1314
      }
1315
    }
1316
    
1317
  private:
1318
    Container& container;    
1319
    Functor functor;
1320
  };
1321

	
1322
  /// \brief Writable bool map for store each true assigned elements in 
1323
  /// an insertable container.
1324
  ///
1325
  /// Writable bool map for store each true assigned elements in an 
1326
  /// insertable container. It will insert all the keys set to \c true into
1327
  /// the container. If you want to store the cut arcs of the strongly
1328
  /// connected components in a set you can use the next code:
1329
  ///
1330
  ///\code
1331
  /// set<Arc> cut_arcs;
1332
  /// InserterBoolMap<set<Arc> > inserter_map(cut_arcs);
1333
  /// stronglyConnectedCutArcs(digraph, cost, inserter_map);
1334
  ///\endcode
1335
  template <typename Container,
1336
            typename Functor =
1337
            _maps_bits::Identity<typename Container::value_type> >
1338
  class InserterBoolMap {
1339
  public:
1340
    typedef typename Container::value_type Key;
1341
    typedef bool Value;
1342

	
1343
    /// Constructor
1344
    InserterBoolMap(Container& _container, typename Container::iterator _it,
1345
                    const Functor& _functor = Functor()) 
1346
      : container(_container), it(_it), functor(_functor) {}
1347

	
1348
    /// Constructor
1349
    InserterBoolMap(Container& _container, const Functor& _functor = Functor())
1350
      : container(_container), it(_container.end()), functor(_functor) {}
1351

	
1352
    /// Setter function of the map
1353
    void set(const Key& key, Value value) {
1354
      if (value) {
1355
	it = container.insert(it, key);
1356
        ++it;
1357
      }
1358
    }
1359
    
1360
  private:
1361
    Container& container;
1362
    typename Container::iterator it;
1363
    Functor functor;
1364
  };
1365

	
1366
  /// \brief Fill the true set elements with a given value.
1367
  ///
1368
  /// Writable bool map to fill the elements set to \c true with a given value.
1369
  /// The value can set 
1370
  /// the container.
1371
  ///
1372
  /// The next code finds the connected components of the undirected digraph
1373
  /// and stores it in the \c comp map:
1374
  ///\code
1375
  /// typedef Graph::NodeMap<int> ComponentMap;
1376
  /// ComponentMap comp(graph);
1377
  /// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap;
1378
  /// ComponentFillerMap filler(comp, 0);
1379
  ///
1380
  /// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph);
1381
  /// dfs.processedMap(filler);
1382
  /// dfs.init();
1383
  /// for (NodeIt it(graph); it != INVALID; ++it) {
1384
  ///   if (!dfs.reached(it)) {
1385
  ///     dfs.addSource(it);
1386
  ///     dfs.start();
1387
  ///     ++filler.fillValue();
1388
  ///   }
1389
  /// }
1390
  ///\endcode
1391
  template <typename Map>
1392
  class FillBoolMap {
1393
  public:
1394
    typedef typename Map::Key Key;
1395
    typedef bool Value;
1396

	
1397
    /// Constructor
1398
    FillBoolMap(Map& _map, const typename Map::Value& _fill) 
1399
      : map(_map), fill(_fill) {}
1400

	
1401
    /// Constructor
1402
    FillBoolMap(Map& _map) 
1403
      : map(_map), fill() {}
1404

	
1405
    /// Gives back the current fill value
1406
    const typename Map::Value& fillValue() const {
1407
      return fill;
1408
    } 
1409

	
1410
    /// Gives back the current fill value
1411
    typename Map::Value& fillValue() {
1412
      return fill;
1413
    } 
1414

	
1415
    /// Sets the current fill value
1416
    void fillValue(const typename Map::Value& _fill) {
1417
      fill = _fill;
1418
    } 
1419

	
1420
    /// Setter function of the map
1421
    void set(const Key& key, Value value) {
1422
      if (value) {
1423
	map.set(key, fill);
1424
      }
1425
    }
1426
    
1427
  private:
1428
    Map& map;
1429
    typename Map::Value fill;
1430
  };
1431

	
1432

	
1433
  /// \brief Writable bool map which stores for each true assigned elements  
1434
  /// the setting order number.
1435
  ///
1436
  /// Writable bool map which stores for each true assigned elements  
1437
  /// the setting order number. It make easy to calculate the leaving
1438
  /// order of the nodes in the \c Dfs algorithm.
1439
  ///
1440
  ///\code
1441
  /// typedef Digraph::NodeMap<int> OrderMap;
1442
  /// OrderMap order(digraph);
1443
  /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1444
  /// OrderSetterMap setter(order);
1445
  /// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph);
1446
  /// dfs.processedMap(setter);
1447
  /// dfs.init();
1448
  /// for (NodeIt it(digraph); it != INVALID; ++it) {
1449
  ///   if (!dfs.reached(it)) {
1450
  ///     dfs.addSource(it);
1451
  ///     dfs.start();
1452
  ///   }
1453
  /// }
1454
  ///\endcode
1455
  ///
1456
  /// The discovering order can be stored a little harder because the
1457
  /// ReachedMap should be readable in the dfs algorithm but the setting
1458
  /// order map is not readable. Now we should use the fork map:
1459
  ///
1460
  ///\code
1461
  /// typedef Digraph::NodeMap<int> OrderMap;
1462
  /// OrderMap order(digraph);
1463
  /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1464
  /// OrderSetterMap setter(order);
1465
  /// typedef Digraph::NodeMap<bool> StoreMap;
1466
  /// StoreMap store(digraph);
1467
  ///
1468
  /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
1469
  /// ReachedMap reached(store, setter);
1470
  ///
1471
  /// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph);
1472
  /// dfs.reachedMap(reached);
1473
  /// dfs.init();
1474
  /// for (NodeIt it(digraph); it != INVALID; ++it) {
1475
  ///   if (!dfs.reached(it)) {
1476
  ///     dfs.addSource(it);
1477
  ///     dfs.start();
1478
  ///   }
1479
  /// }
1480
  ///\endcode
1481
  template <typename Map>
1482
  class SettingOrderBoolMap {
1483
  public:
1484
    typedef typename Map::Key Key;
1485
    typedef bool Value;
1486

	
1487
    /// Constructor
1488
    SettingOrderBoolMap(Map& _map) 
1489
      : map(_map), counter(0) {}
1490

	
1491
    /// Number of set operations.
1492
    int num() const {
1493
      return counter;
1494
    }
1495

	
1496
    /// Setter function of the map
1497
    void set(const Key& key, Value value) {
1498
      if (value) {
1499
	map.set(key, counter++);
1500
      }
1501
    }
1502
    
1503
  private:
1504
    Map& map;
1505
    int counter;
1506
  };
1507

	
1508
  /// @}
1509
}
1510

	
1511
#endif // LEMON_MAPS_H
Show white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2007
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#include <deque>
20
#include <set>
21

	
22
#include <lemon/concept_check.h>
23
#include <lemon/concepts/maps.h>
24
#include <lemon/maps.h>
25

	
26
#include "test_tools.h"
27

	
28
using namespace lemon;
29
using namespace lemon::concepts;
30

	
31
struct A {};
32
inline bool operator<(A, A) { return true; }
33
struct B {};
34

	
35
class F {
36
public:
37
  typedef A argument_type;
38
  typedef B result_type;
39

	
40
  B operator()(const A &) const {return B();}
41
};
42

	
43
int func(A) {return 3;}
44

	
45
int binc(int, B) {return 4;}
46

	
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;
52

	
53
int main()
54
{ // checking graph components
55
  
56
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
57
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
58
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
59
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
60

	
61
  checkConcept<ReadMap<A,double>, AddMap<DoubleMap,DoubleMap> >();
62
  checkConcept<ReadMap<A,double>, SubMap<DoubleMap,DoubleMap> >();
63
  checkConcept<ReadMap<A,double>, MulMap<DoubleMap,DoubleMap> >();
64
  checkConcept<ReadMap<A,double>, DivMap<DoubleMap,DoubleMap> >();
65
  checkConcept<ReadMap<A,double>, NegMap<DoubleMap> >();
66
  checkConcept<ReadWriteMap<A,double>, NegWriteMap<WriteDoubleMap> >();
67
  checkConcept<ReadMap<A,double>, AbsMap<DoubleMap> >();
68
  checkConcept<ReadMap<A,double>, ShiftMap<DoubleMap> >();
69
  checkConcept<ReadWriteMap<A,double>, ShiftWriteMap<WriteDoubleMap> >();
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> >();
75
  
76
  checkConcept<ReadMap<B,double>, ComposeMap<DoubleMap,ReadMap<B,A> > >();
77

	
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> > >();
89

	
90
  int a;
91
  
92
  a=mapFunctor(constMap<A,int>(2))(A());
93
  check(a==2,"Something is wrong with mapFunctor");
94

	
95
  B b;
96
  b=functorMap(F())[A()];
97

	
98
  a=functorMap(&func)[A()];
99
  check(a==3,"Something is wrong with functorMap");
100

	
101
  a=combineMap(constMap<B, int, 1>(), identityMap<B>(), &binc)[B()];
102
  check(a==4,"Something is wrong with combineMap");
103
  
104

	
105
  std::cout << __FILE__ ": All tests passed.\n";
106
  
107
  return 0;
108
}
Show white space 6 line context
... ...
@@ -13,11 +13,14 @@
13 13
lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) $(CPLEX_LIBS) $(SOPLEX_LIBS)
14 14

	
15 15
lemon_HEADERS += \
16
        lemon/concept_check.h \
16 17
	lemon/list_graph.h \
18
        lemon/maps.h \
17 19
        lemon/tolerance.h
18 20

	
19 21
bits_HEADERS += \
20 22
        lemon/bits/invalid.h \
21 23
        lemon/bits/utility.h
22 24

	
23
concept_HEADERS +=
25
concept_HEADERS += \
26
        lemon/concepts/maps.h
Show white space 6 line context
... ...
@@ -5,11 +5,13 @@
5 5
        test/test_tools.h
6 6
 
7 7
check_PROGRAMS += \
8
        test/maps_test \
8 9
        test/test_tools_fail \
9 10
        test/test_tools_pass
10 11
 
11 12
TESTS += $(check_PROGRAMS)
12 13
XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
13 14

	
15
test_maps_test_SOURCES = test/maps_test.cc
14 16
test_test_tools_fail_SOURCES = test/test_tools_fail.cc
15 17
test_test_tools_pass_SOURCES = test/test_tools_pass.cc
0 comments (0 inline)