klao@959
|
1 |
/* -*- C++ -*-
|
klao@959
|
2 |
* src/lemon/concept/maps.h - Part of LEMON, a generic C++ optimization library
|
klao@959
|
3 |
*
|
klao@959
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
klao@959
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
klao@959
|
6 |
*
|
klao@959
|
7 |
* Permission to use, modify and distribute this software is granted
|
klao@959
|
8 |
* provided that this copyright notice appears in all copies. For
|
klao@959
|
9 |
* precise terms see the accompanying LICENSE file.
|
klao@959
|
10 |
*
|
klao@959
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
klao@959
|
12 |
* express or implied, and with no claim as to its suitability for any
|
klao@959
|
13 |
* purpose.
|
klao@959
|
14 |
*
|
klao@959
|
15 |
*/
|
klao@959
|
16 |
|
klao@959
|
17 |
#ifndef LEMON_CONCEPT_MAPS_H
|
klao@959
|
18 |
#define LEMON_CONCEPT_MAPS_H
|
klao@959
|
19 |
|
klao@959
|
20 |
#include <lemon/concept_check.h>
|
klao@959
|
21 |
|
klao@959
|
22 |
///\ingroup concept
|
klao@959
|
23 |
///\file
|
klao@959
|
24 |
///\brief Map concepts checking classes for testing and documenting.
|
klao@959
|
25 |
|
klao@959
|
26 |
namespace lemon {
|
klao@959
|
27 |
|
klao@959
|
28 |
namespace concept {
|
klao@959
|
29 |
|
klao@959
|
30 |
/// \addtogroup concept
|
klao@959
|
31 |
/// @{
|
klao@959
|
32 |
|
klao@959
|
33 |
/// Readable map concept
|
klao@959
|
34 |
template<typename K, typename T>
|
klao@959
|
35 |
class ReadMap
|
klao@959
|
36 |
{
|
klao@959
|
37 |
public:
|
klao@959
|
38 |
/// Map's key type.
|
alpar@987
|
39 |
typedef K Key;
|
klao@959
|
40 |
/// Map's value type. (The type of objects associated with the keys).
|
alpar@987
|
41 |
typedef T Value;
|
klao@959
|
42 |
|
klao@959
|
43 |
/// Returns the value associated with a key.
|
alpar@987
|
44 |
Value operator[](const Key &k) const {return Value();}
|
klao@959
|
45 |
|
klao@959
|
46 |
///Default constructor
|
klao@959
|
47 |
ReadMap() {}
|
klao@959
|
48 |
};
|
klao@959
|
49 |
|
klao@959
|
50 |
|
klao@959
|
51 |
/// Writable map concept
|
klao@959
|
52 |
template<typename K, typename T>
|
klao@959
|
53 |
class WriteMap
|
klao@959
|
54 |
{
|
klao@959
|
55 |
public:
|
klao@959
|
56 |
/// Map's key type.
|
alpar@987
|
57 |
typedef K Key;
|
klao@959
|
58 |
/// Map's value type. (The type of objects associated with the keys).
|
alpar@987
|
59 |
typedef T Value;
|
klao@959
|
60 |
|
klao@959
|
61 |
/// Sets the value associated with a key.
|
alpar@987
|
62 |
void set(const Key &k,const Value &t) {}
|
klao@959
|
63 |
|
klao@959
|
64 |
///Default constructor
|
klao@959
|
65 |
WriteMap() {}
|
klao@959
|
66 |
};
|
klao@959
|
67 |
|
klao@959
|
68 |
///Read/Writable map concept
|
klao@959
|
69 |
template<typename K, typename T>
|
klao@959
|
70 |
class ReadWriteMap : public ReadMap<K,T>,
|
klao@959
|
71 |
public WriteMap<K,T>
|
klao@959
|
72 |
{
|
klao@959
|
73 |
public:
|
klao@959
|
74 |
/// Map's key type.
|
alpar@987
|
75 |
typedef K Key;
|
klao@959
|
76 |
/// Map's value type. (The type of objects associated with the keys).
|
alpar@987
|
77 |
typedef T Value;
|
klao@959
|
78 |
|
klao@959
|
79 |
/// Returns the value associated with a key.
|
alpar@987
|
80 |
Value operator[](const Key &k) const {return Value();}
|
klao@959
|
81 |
/// Sets the value associated with a key.
|
alpar@987
|
82 |
void set(const Key &k,const Value &t) {}
|
klao@959
|
83 |
|
klao@959
|
84 |
///Default constructor
|
klao@959
|
85 |
ReadWriteMap() {}
|
klao@959
|
86 |
};
|
klao@959
|
87 |
|
klao@959
|
88 |
|
klao@959
|
89 |
///Dereferable map concept
|
klao@959
|
90 |
template<typename K, typename T>
|
klao@959
|
91 |
class ReferenceMap : public ReadWriteMap<K,T>
|
klao@959
|
92 |
{
|
klao@959
|
93 |
public:
|
klao@959
|
94 |
/// Map's key type.
|
alpar@987
|
95 |
typedef K Key;
|
klao@959
|
96 |
/// Map's value type. (The type of objects associated with the keys).
|
alpar@987
|
97 |
typedef T Value;
|
klao@959
|
98 |
|
klao@959
|
99 |
protected:
|
alpar@987
|
100 |
Value tmp;
|
klao@959
|
101 |
public:
|
alpar@987
|
102 |
typedef Value& Reference;
|
klao@959
|
103 |
/// Map's const reference type.
|
alpar@987
|
104 |
typedef const Value& ConstReference;
|
klao@959
|
105 |
|
klao@959
|
106 |
///Returns a reference to the value associated to a key.
|
alpar@987
|
107 |
Reference operator[](const Key &i) { return tmp; }
|
klao@959
|
108 |
///Returns a const reference to the value associated to a key.
|
alpar@987
|
109 |
ConstReference operator[](const Key &i) const
|
klao@959
|
110 |
{ return tmp; }
|
klao@959
|
111 |
/// Sets the value associated with a key.
|
alpar@987
|
112 |
void set(const Key &k,const Value &t) { operator[](k)=t; }
|
klao@959
|
113 |
|
klao@959
|
114 |
///Default constructor
|
klao@959
|
115 |
ReferenceMap() {}
|
klao@959
|
116 |
};
|
klao@959
|
117 |
|
klao@959
|
118 |
|
klao@959
|
119 |
template<typename Item, typename T, typename Graph>
|
klao@959
|
120 |
class GraphMap : public ReadWriteMap<Item, T> {
|
klao@959
|
121 |
// I really, really don't like the idea that every graph should have
|
klao@959
|
122 |
// reference maps! --klao
|
klao@959
|
123 |
|
klao@959
|
124 |
private:
|
klao@959
|
125 |
// We state explicitly that graph maps have no default constructor?
|
klao@959
|
126 |
GraphMap();
|
klao@959
|
127 |
|
klao@959
|
128 |
public:
|
klao@959
|
129 |
explicit GraphMap(Graph const&) {}
|
klao@959
|
130 |
// value for initializing
|
klao@959
|
131 |
GraphMap(Graph const&, T) {}
|
klao@959
|
132 |
|
klao@959
|
133 |
// this probably should be required:
|
klao@959
|
134 |
GraphMap(GraphMap const&) {}
|
klao@959
|
135 |
GraphMap& operator=(GraphMap const&) { return *this; }
|
klao@959
|
136 |
|
klao@959
|
137 |
// but this is a absolute no-op! We should provide a more generic
|
klao@959
|
138 |
// graph-map-copy operation.
|
klao@959
|
139 |
//
|
klao@959
|
140 |
// template<typename TT>
|
klao@959
|
141 |
// GraphMap(GraphMap<TT> const&);
|
klao@959
|
142 |
//
|
klao@959
|
143 |
// template<typename TT>
|
klao@959
|
144 |
// GraphMap& operator=(const GraphMap<TT>&);
|
klao@959
|
145 |
};
|
klao@959
|
146 |
|
klao@959
|
147 |
|
klao@959
|
148 |
/**************** Concept-checking classes ****************/
|
klao@959
|
149 |
|
klao@959
|
150 |
template<typename ReadMap>
|
klao@959
|
151 |
struct ReadMapConcept {
|
alpar@987
|
152 |
typedef typename ReadMap::Key Key;
|
alpar@987
|
153 |
typedef typename ReadMap::Value Value;
|
klao@959
|
154 |
|
klao@959
|
155 |
void constraints() {
|
klao@959
|
156 |
// No constraints for constructor.
|
klao@959
|
157 |
|
alpar@987
|
158 |
// What are the requirement for the Value?
|
klao@959
|
159 |
// CopyConstructible? Assignable? None of these?
|
alpar@987
|
160 |
Value v = m[k];
|
klao@959
|
161 |
v = m[k];
|
klao@959
|
162 |
|
klao@959
|
163 |
// FIXME:
|
klao@959
|
164 |
ignore_unused_variable_warning(v);
|
klao@959
|
165 |
}
|
klao@959
|
166 |
|
klao@959
|
167 |
ReadMap m;
|
alpar@987
|
168 |
Key k;
|
klao@959
|
169 |
};
|
klao@959
|
170 |
|
klao@959
|
171 |
template<typename WriteMap>
|
klao@959
|
172 |
struct WriteMapConcept {
|
alpar@987
|
173 |
typedef typename WriteMap::Key Key;
|
alpar@987
|
174 |
typedef typename WriteMap::Value Value;
|
klao@959
|
175 |
|
klao@959
|
176 |
void constraints() {
|
klao@959
|
177 |
// No constraints for constructor.
|
klao@959
|
178 |
|
klao@959
|
179 |
m.set(k, v);
|
klao@959
|
180 |
}
|
klao@959
|
181 |
|
klao@959
|
182 |
WriteMap m;
|
alpar@987
|
183 |
Key k;
|
alpar@987
|
184 |
Value v;
|
klao@959
|
185 |
};
|
klao@959
|
186 |
|
klao@959
|
187 |
template<typename ReadWriteMap>
|
klao@959
|
188 |
struct ReadWriteMapConcept {
|
klao@959
|
189 |
void constraints() {
|
klao@959
|
190 |
function_requires< ReadMapConcept<ReadWriteMap> >();
|
klao@959
|
191 |
function_requires< WriteMapConcept<ReadWriteMap> >();
|
klao@959
|
192 |
}
|
klao@959
|
193 |
};
|
klao@959
|
194 |
|
klao@959
|
195 |
template<typename ReferenceMap>
|
klao@959
|
196 |
struct ReferenceMapConcept {
|
alpar@987
|
197 |
typedef typename ReferenceMap::Key Key;
|
alpar@987
|
198 |
typedef typename ReferenceMap::Value Value;
|
alpar@987
|
199 |
typedef typename ReferenceMap::Reference Reference;
|
klao@959
|
200 |
|
klao@959
|
201 |
// What for is this?
|
alpar@987
|
202 |
typedef typename ReferenceMap::ConstReference ConstReference;
|
klao@959
|
203 |
|
klao@959
|
204 |
void constraints() {
|
klao@959
|
205 |
function_requires< ReadWriteMapConcept<ReferenceMap> >();
|
klao@959
|
206 |
|
klao@959
|
207 |
m[k] = v;
|
klao@959
|
208 |
// Or should we require real reference?
|
klao@959
|
209 |
// Like this:
|
alpar@987
|
210 |
// Value &vv = m[k];
|
klao@959
|
211 |
// ignore_unused_variable_warning(vv);
|
klao@959
|
212 |
}
|
klao@959
|
213 |
|
klao@959
|
214 |
ReferenceMap m;
|
alpar@987
|
215 |
Key k;
|
alpar@987
|
216 |
Value v;
|
klao@959
|
217 |
};
|
klao@959
|
218 |
|
klao@959
|
219 |
/// \todo GraphMapConceptCheck
|
klao@959
|
220 |
|
klao@959
|
221 |
template<typename GraphMap, typename Graph>
|
klao@959
|
222 |
struct GraphMapConcept {
|
klao@959
|
223 |
void constraints() {
|
klao@959
|
224 |
function_requires< ReadWriteMapConcept<GraphMap> >();
|
klao@959
|
225 |
// Construction with a graph parameter
|
klao@959
|
226 |
GraphMap a(g);
|
klao@959
|
227 |
// Ctor with a graph and a default value parameter
|
klao@959
|
228 |
GraphMap a2(g,t);
|
klao@959
|
229 |
// Copy ctor. Do we need it?
|
klao@959
|
230 |
GraphMap b=c;
|
klao@959
|
231 |
// Copy operator. Do we need it?
|
klao@959
|
232 |
a=b;
|
klao@959
|
233 |
|
klao@959
|
234 |
ignore_unused_variable_warning(a2);
|
klao@959
|
235 |
}
|
klao@959
|
236 |
const GraphMap &c;
|
klao@959
|
237 |
const Graph &g;
|
alpar@987
|
238 |
const typename GraphMap::Value &t;
|
klao@959
|
239 |
};
|
klao@959
|
240 |
|
klao@959
|
241 |
|
klao@959
|
242 |
// @}
|
klao@959
|
243 |
|
klao@959
|
244 |
} //namespace concept
|
klao@959
|
245 |
} //namespace lemon
|
klao@959
|
246 |
#endif // LEMON_CONCEPT_MAPS_H
|