src/lemon/concept/maps.h
changeset 959 c80ef5912903
child 987 87f7c54892df
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lemon/concept/maps.h	Thu Nov 04 20:24:59 2004 +0000
     1.3 @@ -0,0 +1,246 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/concept/maps.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Combinatorial Optimization Research Group, 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 KeyType;    
    1.43 +      /// Map's value type. (The type of objects associated with the keys).
    1.44 +      typedef T ValueType;
    1.45 +
    1.46 +      /// Returns the value associated with a key.
    1.47 +      ValueType operator[](const KeyType &k) const {return ValueType();}
    1.48 +
    1.49 +      ///Default constructor
    1.50 +      ReadMap() {}
    1.51 +    };
    1.52 +
    1.53 +
    1.54 +    /// Writable map concept
    1.55 +    template<typename K, typename T>
    1.56 +    class WriteMap
    1.57 +    {
    1.58 +    public:
    1.59 +      /// Map's key type.
    1.60 +      typedef K KeyType;    
    1.61 +      /// Map's value type. (The type of objects associated with the keys).
    1.62 +      typedef T ValueType;
    1.63 +
    1.64 +      /// Sets the value associated with a key.
    1.65 +      void set(const KeyType &k,const ValueType &t) {}
    1.66 +
    1.67 +      ///Default constructor
    1.68 +      WriteMap() {}
    1.69 +    };
    1.70 +
    1.71 +    ///Read/Writable map concept
    1.72 +    template<typename K, typename T>
    1.73 +    class ReadWriteMap : public ReadMap<K,T>,
    1.74 +			    public WriteMap<K,T>
    1.75 +    {
    1.76 +    public:
    1.77 +      /// Map's key type.
    1.78 +      typedef K KeyType;    
    1.79 +      /// Map's value type. (The type of objects associated with the keys).
    1.80 +      typedef T ValueType;
    1.81 +
    1.82 +      /// Returns the value associated with a key.
    1.83 +      ValueType operator[](const KeyType &k) const {return ValueType();}
    1.84 +      /// Sets the value associated with a key.
    1.85 +      void set(const KeyType &k,const ValueType &t) {}
    1.86 +
    1.87 +      ///Default constructor
    1.88 +      ReadWriteMap() {}
    1.89 +    };
    1.90 +  
    1.91 +  
    1.92 +    ///Dereferable map concept
    1.93 +    template<typename K, typename T>
    1.94 +    class ReferenceMap : public ReadWriteMap<K,T>
    1.95 +    {
    1.96 +    public:
    1.97 +      /// Map's key type.
    1.98 +      typedef K KeyType;    
    1.99 +      /// Map's value type. (The type of objects associated with the keys).
   1.100 +      typedef T ValueType;
   1.101 +
   1.102 +    protected:
   1.103 +      ValueType tmp;
   1.104 +    public:
   1.105 +      typedef ValueType& ReferenceType;
   1.106 +      /// Map's const reference type.
   1.107 +      typedef const ValueType& ConstReferenceType;
   1.108 +
   1.109 +      ///Returns a reference to the value associated to a key.
   1.110 +      ReferenceType operator[](const KeyType &i) { return tmp; }
   1.111 +      ///Returns a const reference to the value associated to a key.
   1.112 +      ConstReferenceType operator[](const KeyType &i) const
   1.113 +      { return tmp; }
   1.114 +      /// Sets the value associated with a key.
   1.115 +      void set(const KeyType &k,const ValueType &t) { operator[](k)=t; }
   1.116 +
   1.117 +      ///Default constructor
   1.118 +      ReferenceMap() {}
   1.119 +    };
   1.120 +
   1.121 +
   1.122 +    template<typename Item, typename T, typename Graph>
   1.123 +    class GraphMap : public ReadWriteMap<Item, T> {
   1.124 +      // I really, really don't like the idea that every graph should have
   1.125 +      // reference maps! --klao
   1.126 +
   1.127 +    private:
   1.128 +      // We state explicitly that graph maps have no default constructor?
   1.129 +      GraphMap();
   1.130 +
   1.131 +    public:
   1.132 +      explicit GraphMap(Graph const&) {}
   1.133 +      // value for initializing
   1.134 +      GraphMap(Graph const&, T) {}
   1.135 +
   1.136 +      // this probably should be required:
   1.137 +      GraphMap(GraphMap const&) {}
   1.138 +      GraphMap& operator=(GraphMap const&) { return *this; }
   1.139 +
   1.140 +      // but this is a absolute no-op! We should provide a more generic
   1.141 +      // graph-map-copy operation.
   1.142 +      //
   1.143 +      // template<typename TT>
   1.144 +      // GraphMap(GraphMap<TT> const&);
   1.145 +      //
   1.146 +      // template<typename TT>
   1.147 +      // GraphMap& operator=(const GraphMap<TT>&);
   1.148 +    };
   1.149 +
   1.150 +
   1.151 +    /****************  Concept-checking classes  ****************/
   1.152 +
   1.153 +    template<typename ReadMap>
   1.154 +    struct ReadMapConcept {
   1.155 +      typedef typename ReadMap::KeyType KeyType;
   1.156 +      typedef typename ReadMap::ValueType ValueType;
   1.157 +
   1.158 +      void constraints() {
   1.159 +	// No constraints for constructor.
   1.160 +
   1.161 +	// What are the requirement for the ValueType?
   1.162 +	// CopyConstructible? Assignable? None of these?
   1.163 +	ValueType v = m[k];
   1.164 +	v = m[k];
   1.165 +
   1.166 +	// FIXME:
   1.167 +	ignore_unused_variable_warning(v);
   1.168 +      }
   1.169 +
   1.170 +      ReadMap m;
   1.171 +      KeyType k;
   1.172 +    };
   1.173 +
   1.174 +    template<typename WriteMap>
   1.175 +    struct WriteMapConcept {
   1.176 +      typedef typename WriteMap::KeyType KeyType;
   1.177 +      typedef typename WriteMap::ValueType ValueType;
   1.178 +
   1.179 +      void constraints() {
   1.180 +	// No constraints for constructor.
   1.181 +
   1.182 +	m.set(k, v);
   1.183 +      }
   1.184 +
   1.185 +      WriteMap m;
   1.186 +      KeyType k;
   1.187 +      ValueType v;
   1.188 +    };
   1.189 +
   1.190 +    template<typename ReadWriteMap>
   1.191 +    struct ReadWriteMapConcept {
   1.192 +      void constraints() {
   1.193 +	function_requires< ReadMapConcept<ReadWriteMap> >();
   1.194 +	function_requires< WriteMapConcept<ReadWriteMap> >();
   1.195 +      }
   1.196 +    };
   1.197 +
   1.198 +    template<typename ReferenceMap>
   1.199 +    struct ReferenceMapConcept {
   1.200 +      typedef typename ReferenceMap::KeyType KeyType;
   1.201 +      typedef typename ReferenceMap::ValueType ValueType;
   1.202 +      typedef typename ReferenceMap::ReferenceType ReferenceType;
   1.203 +
   1.204 +      // What for is this?
   1.205 +      typedef typename ReferenceMap::ConstReferenceType ConstReferenceType;
   1.206 +
   1.207 +      void constraints() {
   1.208 +	function_requires< ReadWriteMapConcept<ReferenceMap> >();
   1.209 +
   1.210 +	m[k] = v;
   1.211 +	// Or should we require real reference?
   1.212 +	// Like this:
   1.213 +	// ValueType &vv = m[k];
   1.214 +	// ignore_unused_variable_warning(vv);
   1.215 +      }
   1.216 +
   1.217 +      ReferenceMap m;
   1.218 +      KeyType k;
   1.219 +      ValueType v;
   1.220 +    };
   1.221 +
   1.222 +    /// \todo GraphMapConceptCheck
   1.223 +
   1.224 +    template<typename GraphMap, typename Graph>
   1.225 +    struct GraphMapConcept {
   1.226 +      void constraints() {
   1.227 +	function_requires< ReadWriteMapConcept<GraphMap> >();
   1.228 +	// Construction with a graph parameter
   1.229 +	GraphMap a(g);
   1.230 +	// Ctor with a graph and a default value parameter
   1.231 +	GraphMap a2(g,t);
   1.232 +	// Copy ctor. Do we need it?
   1.233 +	GraphMap b=c;
   1.234 +	// Copy operator. Do we need it?
   1.235 +	a=b;
   1.236 +
   1.237 +	ignore_unused_variable_warning(a2);
   1.238 +      }
   1.239 +      const GraphMap &c;
   1.240 +      const Graph &g;
   1.241 +      const typename GraphMap::ValueType &t;
   1.242 +    };
   1.243 +    
   1.244 +
   1.245 +    // @}
   1.246 +
   1.247 +  } //namespace concept
   1.248 +} //namespace lemon
   1.249 +#endif // LEMON_CONCEPT_MAPS_H