1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2013 |
---|
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_CONCEPTS_MAPS_H |
---|
20 | #define LEMON_CONCEPTS_MAPS_H |
---|
21 | |
---|
22 | #include <lemon/core.h> |
---|
23 | #include <lemon/concept_check.h> |
---|
24 | |
---|
25 | ///\ingroup map_concepts |
---|
26 | ///\file |
---|
27 | ///\brief The concept of maps. |
---|
28 | |
---|
29 | namespace lemon { |
---|
30 | |
---|
31 | namespace concepts { |
---|
32 | |
---|
33 | /// \addtogroup map_concepts |
---|
34 | /// @{ |
---|
35 | |
---|
36 | /// Readable map concept |
---|
37 | |
---|
38 | /// Readable map concept. |
---|
39 | /// |
---|
40 | template<typename K, typename T> |
---|
41 | class ReadMap |
---|
42 | { |
---|
43 | public: |
---|
44 | /// The key type of the map. |
---|
45 | typedef K Key; |
---|
46 | /// \brief The value type of the map. |
---|
47 | /// (The type of objects associated with the keys). |
---|
48 | typedef T Value; |
---|
49 | |
---|
50 | /// Returns the value associated with the given key. |
---|
51 | Value operator[](const Key &) const { |
---|
52 | // return *(static_cast<Value *>(0)); |
---|
53 | // return *(static_cast<Value *>(0)+1); |
---|
54 | // return *(static_cast<Value *>(sizeof(Value))); |
---|
55 | // return *(reinterpret_cast<Value *>(0)); |
---|
56 | // return *(reinterpret_cast<Value *>(0)+1); |
---|
57 | return *(reinterpret_cast<Value *>(sizeof(Value))); |
---|
58 | } |
---|
59 | |
---|
60 | template<typename _ReadMap> |
---|
61 | struct Constraints { |
---|
62 | void constraints() { |
---|
63 | Value val = m[key]; |
---|
64 | val = m[key]; |
---|
65 | typename _ReadMap::Value own_val = m[own_key]; |
---|
66 | own_val = m[own_key]; |
---|
67 | |
---|
68 | ::lemon::ignore_unused_variable_warning(key); |
---|
69 | ::lemon::ignore_unused_variable_warning(val); |
---|
70 | ::lemon::ignore_unused_variable_warning(own_key); |
---|
71 | ::lemon::ignore_unused_variable_warning(own_val); |
---|
72 | } |
---|
73 | const Key& key; |
---|
74 | const typename _ReadMap::Key& own_key; |
---|
75 | const _ReadMap& m; |
---|
76 | Constraints() {} |
---|
77 | }; |
---|
78 | |
---|
79 | }; |
---|
80 | |
---|
81 | |
---|
82 | /// Writable map concept |
---|
83 | |
---|
84 | /// Writable map concept. |
---|
85 | /// |
---|
86 | template<typename K, typename T> |
---|
87 | class WriteMap |
---|
88 | { |
---|
89 | public: |
---|
90 | /// The key type of the map. |
---|
91 | typedef K Key; |
---|
92 | /// \brief The value type of the map. |
---|
93 | /// (The type of objects associated with the keys). |
---|
94 | typedef T Value; |
---|
95 | |
---|
96 | /// Sets the value associated with the given key. |
---|
97 | void set(const Key &, const Value &) {} |
---|
98 | |
---|
99 | /// Default constructor. |
---|
100 | WriteMap() {} |
---|
101 | |
---|
102 | template <typename _WriteMap> |
---|
103 | struct Constraints { |
---|
104 | void constraints() { |
---|
105 | m.set(key, val); |
---|
106 | m.set(own_key, own_val); |
---|
107 | |
---|
108 | ::lemon::ignore_unused_variable_warning(key); |
---|
109 | ::lemon::ignore_unused_variable_warning(val); |
---|
110 | ::lemon::ignore_unused_variable_warning(own_key); |
---|
111 | ::lemon::ignore_unused_variable_warning(own_val); |
---|
112 | } |
---|
113 | const Key& key; |
---|
114 | const Value& val; |
---|
115 | const typename _WriteMap::Key& own_key; |
---|
116 | const typename _WriteMap::Value& own_val; |
---|
117 | _WriteMap& m; |
---|
118 | Constraints() {} |
---|
119 | }; |
---|
120 | }; |
---|
121 | |
---|
122 | /// Read/writable map concept |
---|
123 | |
---|
124 | /// Read/writable map concept. |
---|
125 | /// |
---|
126 | template<typename K, typename T> |
---|
127 | class ReadWriteMap : public ReadMap<K,T>, |
---|
128 | public WriteMap<K,T> |
---|
129 | { |
---|
130 | public: |
---|
131 | /// The key type of the map. |
---|
132 | typedef K Key; |
---|
133 | /// \brief The value type of the map. |
---|
134 | /// (The type of objects associated with the keys). |
---|
135 | typedef T Value; |
---|
136 | |
---|
137 | /// Returns the value associated with the given key. |
---|
138 | Value operator[](const Key &) const { |
---|
139 | return *(reinterpret_cast<Value *>(sizeof(Value))); |
---|
140 | } |
---|
141 | |
---|
142 | /// Sets the value associated with the given key. |
---|
143 | void set(const Key &, const Value &) {} |
---|
144 | |
---|
145 | template<typename _ReadWriteMap> |
---|
146 | struct Constraints { |
---|
147 | void constraints() { |
---|
148 | checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
---|
149 | checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
---|
150 | } |
---|
151 | }; |
---|
152 | }; |
---|
153 | |
---|
154 | |
---|
155 | /// Dereferable map concept |
---|
156 | |
---|
157 | /// Dereferable map concept. |
---|
158 | /// |
---|
159 | template<typename K, typename T, typename R, typename CR> |
---|
160 | class ReferenceMap : public ReadWriteMap<K,T> |
---|
161 | { |
---|
162 | public: |
---|
163 | /// Tag for reference maps. |
---|
164 | typedef True ReferenceMapTag; |
---|
165 | /// The key type of the map. |
---|
166 | typedef K Key; |
---|
167 | /// \brief The value type of the map. |
---|
168 | /// (The type of objects associated with the keys). |
---|
169 | typedef T Value; |
---|
170 | /// The reference type of the map. |
---|
171 | typedef R Reference; |
---|
172 | /// The const reference type of the map. |
---|
173 | typedef CR ConstReference; |
---|
174 | |
---|
175 | public: |
---|
176 | |
---|
177 | /// Returns a reference to the value associated with the given key. |
---|
178 | Reference operator[](const Key &) { |
---|
179 | return *(reinterpret_cast<Value *>(sizeof(Value))); |
---|
180 | } |
---|
181 | |
---|
182 | /// Returns a const reference to the value associated with the given key. |
---|
183 | ConstReference operator[](const Key &) const { |
---|
184 | return *(reinterpret_cast<Value *>(sizeof(Value))); |
---|
185 | } |
---|
186 | |
---|
187 | /// Sets the value associated with the given key. |
---|
188 | void set(const Key &k,const Value &t) { operator[](k)=t; } |
---|
189 | |
---|
190 | template<typename _ReferenceMap> |
---|
191 | struct Constraints { |
---|
192 | typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type |
---|
193 | constraints() { |
---|
194 | checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
---|
195 | ref = m[key]; |
---|
196 | m[key] = val; |
---|
197 | m[key] = ref; |
---|
198 | m[key] = cref; |
---|
199 | own_ref = m[own_key]; |
---|
200 | m[own_key] = own_val; |
---|
201 | m[own_key] = own_ref; |
---|
202 | m[own_key] = own_cref; |
---|
203 | m[key] = m[own_key]; |
---|
204 | m[own_key] = m[key]; |
---|
205 | } |
---|
206 | const Key& key; |
---|
207 | Value& val; |
---|
208 | Reference ref; |
---|
209 | ConstReference cref; |
---|
210 | const typename _ReferenceMap::Key& own_key; |
---|
211 | typename _ReferenceMap::Value& own_val; |
---|
212 | typename _ReferenceMap::Reference own_ref; |
---|
213 | typename _ReferenceMap::ConstReference own_cref; |
---|
214 | _ReferenceMap& m; |
---|
215 | Constraints() {} |
---|
216 | }; |
---|
217 | }; |
---|
218 | |
---|
219 | // @} |
---|
220 | |
---|
221 | } //namespace concepts |
---|
222 | |
---|
223 | } //namespace lemon |
---|
224 | |
---|
225 | #endif |
---|