1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/concept/maps.h Mon May 23 04:48:14 2005 +0000
1.3 @@ -0,0 +1,187 @@
1.4 +/* -*- C++ -*-
1.5 + * lemon/concept/maps.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_CONCEPT_MAPS_H
1.21 +#define LEMON_CONCEPT_MAPS_H
1.22 +
1.23 +#include <lemon/concept_check.h>
1.24 +
1.25 +///\ingroup concept
1.26 +///\file
1.27 +///\brief Map concepts checking classes for testing and documenting.
1.28 +
1.29 +namespace lemon {
1.30 +
1.31 + namespace concept {
1.32 +
1.33 + /// \addtogroup concept
1.34 + /// @{
1.35 +
1.36 + /// Readable map concept
1.37 + template<typename K, typename T>
1.38 + class ReadMap
1.39 + {
1.40 + public:
1.41 + /// Map's key type.
1.42 + typedef K Key;
1.43 + /// Map's value type. (The type of objects associated with the keys).
1.44 + typedef T Value;
1.45 +
1.46 + // \bug Value don't need to be default constructible.
1.47 + /// Returns the value associated with a key.
1.48 + Value operator[](const Key &) const {return Value();}
1.49 +
1.50 + template<typename _ReadMap>
1.51 + struct Constraints {
1.52 +
1.53 + void constraints() {
1.54 + Value val = m[key];
1.55 + val = m[key];
1.56 + typename _ReadMap::Value own_val = m[own_key];
1.57 + own_val = m[own_key];
1.58 +
1.59 + ignore_unused_variable_warning(val);
1.60 + ignore_unused_variable_warning(own_val);
1.61 + ignore_unused_variable_warning(key);
1.62 + }
1.63 + Key& key;
1.64 + typename _ReadMap::Key& own_key;
1.65 + _ReadMap& m;
1.66 + };
1.67 +
1.68 + };
1.69 +
1.70 +
1.71 + /// Writable map concept
1.72 + template<typename K, typename T>
1.73 + class WriteMap
1.74 + {
1.75 + public:
1.76 + /// Map's key type.
1.77 + typedef K Key;
1.78 + /// Map's value type. (The type of objects associated with the keys).
1.79 + typedef T Value;
1.80 +
1.81 + /// Sets the value associated with a key.
1.82 + void set(const Key &,const Value &) {}
1.83 +
1.84 + ///Default constructor
1.85 + WriteMap() {}
1.86 +
1.87 + template <typename _WriteMap>
1.88 + struct Constraints {
1.89 + void constraints() {
1.90 + // No constraints for constructor.
1.91 + m.set(key, val);
1.92 + m.set(own_key, own_val);
1.93 + ignore_unused_variable_warning(key);
1.94 + ignore_unused_variable_warning(val);
1.95 + ignore_unused_variable_warning(own_key);
1.96 + ignore_unused_variable_warning(own_val);
1.97 + }
1.98 +
1.99 + Value& val;
1.100 + typename _WriteMap::Value own_val;
1.101 + Key& key;
1.102 + typename _WriteMap::Key& own_key;
1.103 + WriteMap& m;
1.104 +
1.105 + };
1.106 + };
1.107 +
1.108 + ///Read/Writable map concept
1.109 + template<typename K, typename T>
1.110 + class ReadWriteMap : public ReadMap<K,T>,
1.111 + public WriteMap<K,T>
1.112 + {
1.113 + public:
1.114 + /// Map's key type.
1.115 + typedef K Key;
1.116 + /// Map's value type. (The type of objects associated with the keys).
1.117 + typedef T Value;
1.118 +
1.119 + /// Returns the value associated with a key.
1.120 + Value operator[](const Key &) const {return Value();}
1.121 + /// Sets the value associated with a key.
1.122 + void set(const Key & ,const Value &) {}
1.123 +
1.124 + template<typename _ReadWriteMap>
1.125 + struct Constraints {
1.126 + void constraints() {
1.127 + checkConcept<ReadMap<K, T>, _ReadWriteMap >();
1.128 + checkConcept<ReadMap<K, T>, _ReadWriteMap >();
1.129 + }
1.130 + };
1.131 + };
1.132 +
1.133 +
1.134 + ///Dereferable map concept
1.135 + template<typename K, typename T, typename R, typename CR>
1.136 + class ReferenceMap : public ReadWriteMap<K,T>
1.137 + {
1.138 + public:
1.139 + /// Map's key type.
1.140 + typedef K Key;
1.141 + /// Map's value type. (The type of objects associated with the keys).
1.142 + typedef T Value;
1.143 + /// Map's reference type.
1.144 + typedef R Reference;
1.145 + /// Map's const reference type.
1.146 + typedef CR ConstReference;
1.147 +
1.148 + protected:
1.149 + Value tmp;
1.150 + public:
1.151 +
1.152 + ///Returns a reference to the value associated to a key.
1.153 + Reference operator[](const Key &) { return tmp; }
1.154 + ///Returns a const reference to the value associated to a key.
1.155 + ConstReference operator[](const Key &) const
1.156 + { return tmp; }
1.157 + /// Sets the value associated with a key.
1.158 + void set(const Key &k,const Value &t) { operator[](k)=t; }
1.159 +
1.160 + // \todo rethink this concept
1.161 + template<typename _ReferenceMap>
1.162 + struct ReferenceMapConcept {
1.163 +
1.164 + void constraints() {
1.165 + checkConcept<ReadWriteMap, _ReferenceMap >();
1.166 + m[key] = val;
1.167 + val = m[key];
1.168 + m[key] = ref;
1.169 + ref = m[key];
1.170 + m[own_key] = own_val;
1.171 + own_val = m[own_key];
1.172 + m[own_key] = own_ref;
1.173 + own_ref = m[own_key];
1.174 + }
1.175 +
1.176 + typename _ReferenceMap::Key& own_key;
1.177 + typename _ReferenceMap::Value& own_val;
1.178 + typename _ReferenceMap::Reference& own_ref;
1.179 + Key& key;
1.180 + Value& val;
1.181 + Reference& ref;
1.182 + ReferenceMap& m;
1.183 + };
1.184 + };
1.185 +
1.186 + // @}
1.187 +
1.188 + } //namespace concept
1.189 +} //namespace lemon
1.190 +#endif // LEMON_CONCEPT_MAPS_H