1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/concepts/maps.h Sat Dec 22 12:35:00 2007 +0000
1.3 @@ -0,0 +1,194 @@
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-2007
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 + /// Returns the value associated with a key.
1.50 +
1.51 + /// \bug Value should n't need to be default constructible.
1.52 + ///
1.53 + Value operator[](const Key &) const {return Value();}
1.54 +
1.55 + template<typename _ReadMap>
1.56 + struct Constraints {
1.57 +
1.58 + void constraints() {
1.59 + Value val = m[key];
1.60 + val = m[key];
1.61 + typename _ReadMap::Value own_val = m[own_key];
1.62 + own_val = m[own_key];
1.63 +
1.64 + ignore_unused_variable_warning(val);
1.65 + ignore_unused_variable_warning(own_val);
1.66 + ignore_unused_variable_warning(key);
1.67 + }
1.68 + Key& key;
1.69 + typename _ReadMap::Key& own_key;
1.70 + _ReadMap& m;
1.71 + };
1.72 +
1.73 + };
1.74 +
1.75 +
1.76 + /// Writable map concept
1.77 + template<typename K, typename T>
1.78 + class WriteMap
1.79 + {
1.80 + public:
1.81 + /// Map's key type.
1.82 + typedef K Key;
1.83 + /// Map's value type. (The type of objects associated with the keys).
1.84 + typedef T Value;
1.85 +
1.86 + /// Sets the value associated with a key.
1.87 + void set(const Key &,const Value &) {}
1.88 +
1.89 + ///Default constructor
1.90 + WriteMap() {}
1.91 +
1.92 + template <typename _WriteMap>
1.93 + struct Constraints {
1.94 + void constraints() {
1.95 + // No constraints for constructor.
1.96 + m.set(key, val);
1.97 + m.set(own_key, own_val);
1.98 + ignore_unused_variable_warning(key);
1.99 + ignore_unused_variable_warning(val);
1.100 + ignore_unused_variable_warning(own_key);
1.101 + ignore_unused_variable_warning(own_val);
1.102 + }
1.103 +
1.104 + Value& val;
1.105 + typename _WriteMap::Value own_val;
1.106 + Key& key;
1.107 + typename _WriteMap::Key& own_key;
1.108 + _WriteMap& m;
1.109 +
1.110 + };
1.111 + };
1.112 +
1.113 + ///Read/Writable map concept
1.114 + template<typename K, typename T>
1.115 + class ReadWriteMap : public ReadMap<K,T>,
1.116 + public WriteMap<K,T>
1.117 + {
1.118 + public:
1.119 + /// Map's key type.
1.120 + typedef K Key;
1.121 + /// Map's value type. (The type of objects associated with the keys).
1.122 + typedef T Value;
1.123 +
1.124 + /// Returns the value associated with a key.
1.125 + Value operator[](const Key &) const {return Value();}
1.126 + /// Sets the value associated with a key.
1.127 + void set(const Key & ,const Value &) {}
1.128 +
1.129 + template<typename _ReadWriteMap>
1.130 + struct Constraints {
1.131 + void constraints() {
1.132 + checkConcept<ReadMap<K, T>, _ReadWriteMap >();
1.133 + checkConcept<WriteMap<K, T>, _ReadWriteMap >();
1.134 + }
1.135 + };
1.136 + };
1.137 +
1.138 +
1.139 + ///Dereferable map concept
1.140 + template<typename K, typename T, typename R, typename CR>
1.141 + class ReferenceMap : public ReadWriteMap<K,T>
1.142 + {
1.143 + public:
1.144 + /// Tag for reference maps.
1.145 + typedef True ReferenceMapTag;
1.146 + /// Map's key type.
1.147 + typedef K Key;
1.148 + /// Map's value type. (The type of objects associated with the keys).
1.149 + typedef T Value;
1.150 + /// Map's reference type.
1.151 + typedef R Reference;
1.152 + /// Map's const reference type.
1.153 + typedef CR ConstReference;
1.154 +
1.155 + protected:
1.156 + Value tmp;
1.157 + public:
1.158 +
1.159 + ///Returns a reference to the value associated to a key.
1.160 + Reference operator[](const Key &) { return tmp; }
1.161 + ///Returns a const reference to the value associated to a key.
1.162 + ConstReference operator[](const Key &) const
1.163 + { return tmp; }
1.164 + /// Sets the value associated with a key.
1.165 + void set(const Key &k,const Value &t) { operator[](k)=t; }
1.166 +
1.167 + // \todo rethink this concept
1.168 + template<typename _ReferenceMap>
1.169 + struct ReferenceMapConcept {
1.170 +
1.171 + void constraints() {
1.172 + checkConcept<ReadWriteMap, _ReferenceMap >();
1.173 + m[key] = val;
1.174 + val = m[key];
1.175 + m[key] = ref;
1.176 + ref = m[key];
1.177 + m[own_key] = own_val;
1.178 + own_val = m[own_key];
1.179 + m[own_key] = own_ref;
1.180 + own_ref = m[own_key];
1.181 + }
1.182 +
1.183 + typename _ReferenceMap::Key& own_key;
1.184 + typename _ReferenceMap::Value& own_val;
1.185 + typename _ReferenceMap::Reference& own_ref;
1.186 + Key& key;
1.187 + Value& val;
1.188 + Reference& ref;
1.189 + _ReferenceMap& m;
1.190 + };
1.191 + };
1.192 +
1.193 + // @}
1.194 +
1.195 + } //namespace concepts
1.196 +} //namespace lemon
1.197 +#endif // LEMON_CONCEPT_MAPS_H