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