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