1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_CONCEPTS_MAPS_H
20 #define LEMON_CONCEPTS_MAPS_H
22 #include <lemon/core.h>
23 #include <lemon/concept_check.h>
25 ///\ingroup map_concepts
27 ///\brief The concept of maps.
33 /// \addtogroup map_concepts
36 /// Readable map concept
38 /// Readable map concept.
40 template<typename K, typename T>
44 /// The key type of the map.
46 /// \brief The value type of the map.
47 /// (The type of objects associated with the keys).
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)));
60 template<typename _ReadMap>
65 typename _ReadMap::Value own_val = m[own_key];
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);
74 const typename _ReadMap::Key& own_key;
82 /// Writable map concept
84 /// Writable map concept.
86 template<typename K, typename T>
90 /// The key type of the map.
92 /// \brief The value type of the map.
93 /// (The type of objects associated with the keys).
96 /// Sets the value associated with the given key.
97 void set(const Key &, const Value &) {}
99 /// Default constructor.
102 template <typename _WriteMap>
106 m.set(own_key, own_val);
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);
115 const typename _WriteMap::Key& own_key;
116 const typename _WriteMap::Value& own_val;
122 /// Read/writable map concept
124 /// Read/writable map concept.
126 template<typename K, typename T>
127 class ReadWriteMap : public ReadMap<K,T>,
131 /// The key type of the map.
133 /// \brief The value type of the map.
134 /// (The type of objects associated with the keys).
137 /// Returns the value associated with the given key.
138 Value operator[](const Key &) const {
139 return *(reinterpret_cast<Value *>(sizeof(Value)));
142 /// Sets the value associated with the given key.
143 void set(const Key &, const Value &) {}
145 template<typename _ReadWriteMap>
148 checkConcept<ReadMap<K, T>, _ReadWriteMap >();
149 checkConcept<WriteMap<K, T>, _ReadWriteMap >();
155 /// Dereferable map concept
157 /// Dereferable map concept.
159 template<typename K, typename T, typename R, typename CR>
160 class ReferenceMap : public ReadWriteMap<K,T>
163 /// Tag for reference maps.
164 typedef True ReferenceMapTag;
165 /// The key type of the map.
167 /// \brief The value type of the map.
168 /// (The type of objects associated with the keys).
170 /// The reference type of the map.
172 /// The const reference type of the map.
173 typedef CR ConstReference;
177 /// Returns a reference to the value associated with the given key.
178 Reference operator[](const Key &) {
179 return *(reinterpret_cast<Value *>(sizeof(Value)));
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)));
187 /// Sets the value associated with the given key.
188 void set(const Key &k,const Value &t) { operator[](k)=t; }
190 template<typename _ReferenceMap>
192 typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type
194 checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
199 own_ref = m[own_key];
200 m[own_key] = own_val;
201 m[own_key] = own_ref;
202 m[own_key] = own_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;
221 } //namespace concepts