src/lemon/default_map.h
changeset 946 c94ef40a22ce
parent 937 d4e911acef3d
child 979 b5fb023cdb7b
     1.1 --- a/src/lemon/default_map.h	Mon Oct 25 13:29:46 2004 +0000
     1.2 +++ b/src/lemon/default_map.h	Wed Oct 27 22:38:50 2004 +0000
     1.3 @@ -23,7 +23,7 @@
     1.4  
     1.5  ///\ingroup graphmaps
     1.6  ///\file
     1.7 -///\brief Graph maps that construates and destruates
     1.8 +///\brief Graph maps that construct and destruct
     1.9  ///their elements dynamically.
    1.10  
    1.11  namespace lemon {
    1.12 @@ -41,100 +41,185 @@
    1.13     */
    1.14  
    1.15  
    1.16 -  /** Macro to implement the DefaultMap.
    1.17 -   */
    1.18 -#define DEFAULT_MAP_BODY(DynMap, Value) \
    1.19 -{ \
    1.20 -\
    1.21 -public: \
    1.22 -\
    1.23 -typedef DynMap<MapRegistry, Value> Parent; \
    1.24 -\
    1.25 -typedef typename MapRegistry::Graph Graph; \
    1.26 -\
    1.27 -DefaultMap(const Graph& g, MapRegistry& r) : Parent(g, r) {} \
    1.28 -DefaultMap(const Graph& g, MapRegistry& r, const Value& v) \
    1.29 -  : Parent(g, r, v) {} \
    1.30 -DefaultMap(const DefaultMap& copy) \
    1.31 -  : Parent(static_cast<const Parent&>(copy)) {} \
    1.32 -template <typename TT> \
    1.33 -DefaultMap(const DefaultMap<MapRegistry, TT>& copy) \
    1.34 -  : Parent(*copy.getGraph()) { \
    1.35 -  if (Parent::getGraph()) { \
    1.36 -    for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\
    1.37 -      Parent::operator[](it) = copy[it]; \
    1.38 -    } \
    1.39 -  } \
    1.40 -} \
    1.41 -DefaultMap& operator=(const DefaultMap& copy) { \
    1.42 -  Parent::operator=(static_cast<const Parent&>(copy)); \
    1.43 -  return *this; \
    1.44 -} \
    1.45 -template <typename TT> \
    1.46 -DefaultMap& operator=(const DefaultMap<MapRegistry, TT>& copy) { \
    1.47 -  if (Parent::getGraph() != copy.getGraph()) { \
    1.48 -    Parent::clear(); \
    1.49 -    Parent::MapBase::operator=(copy); \
    1.50 -    Parent::construct(); \
    1.51 -  } \
    1.52 -  if (Parent::getGraph()) { \
    1.53 -    for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\
    1.54 -      Parent::operator[](it) = copy[it]; \
    1.55 -    } \
    1.56 -  } \
    1.57 -  return *this; \
    1.58 -} \
    1.59 -};
    1.60  
    1.61 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap, typename _Value>
    1.62 +  struct DefaultMapSelector {
    1.63 +    typedef ArrayMap<_Graph, _Item, _ItemIt, _IdMap, _Value> Map;
    1.64 +  };
    1.65  
    1.66 -  template <typename MapRegistry, typename Type>
    1.67 -  class DefaultMap : public ArrayMap<MapRegistry, Type> 
    1.68 -  DEFAULT_MAP_BODY(ArrayMap, Type);
    1.69 +  // bool
    1.70 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    1.71 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, bool> {
    1.72 +    typedef VectorMap<_Graph, _Item, _IdMap, bool> Map;
    1.73 +  };
    1.74  
    1.75 -  template <typename MapRegistry>
    1.76 -  class DefaultMap<MapRegistry, bool> 
    1.77 -    : public VectorMap<MapRegistry, bool> 
    1.78 -  DEFAULT_MAP_BODY(VectorMap, bool);
    1.79 +  // char
    1.80 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    1.81 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, char> {
    1.82 +    typedef VectorMap<_Graph, _Item, _IdMap, char> Map;
    1.83 +  };
    1.84  
    1.85 -  template <typename MapRegistry>
    1.86 -  class DefaultMap<MapRegistry, char> 
    1.87 -    : public VectorMap<MapRegistry, char> 
    1.88 -  DEFAULT_MAP_BODY(VectorMap, char);
    1.89 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    1.90 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, signed char> {
    1.91 +    typedef VectorMap<_Graph, _Item, _IdMap, signed char> Map;
    1.92 +  };
    1.93  
    1.94 -  template <typename MapRegistry>
    1.95 -  class DefaultMap<MapRegistry, int> 
    1.96 -    : public VectorMap<MapRegistry, int> 
    1.97 -  DEFAULT_MAP_BODY(VectorMap, int);
    1.98 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    1.99 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, unsigned char> {
   1.100 +    typedef VectorMap<_Graph, _Item, _IdMap, unsigned char> Map;
   1.101 +  };
   1.102  
   1.103 -  template <typename MapRegistry>
   1.104 -  class DefaultMap<MapRegistry, short> 
   1.105 -    : public VectorMap<MapRegistry, short> 
   1.106 -  DEFAULT_MAP_BODY(VectorMap, short);
   1.107  
   1.108 -  template <typename MapRegistry>
   1.109 -  class DefaultMap<MapRegistry, long> 
   1.110 -    : public VectorMap<MapRegistry, long> 
   1.111 -  DEFAULT_MAP_BODY(VectorMap, long);
   1.112 +  // int
   1.113 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.114 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, signed int> {
   1.115 +    typedef VectorMap<_Graph, _Item, _IdMap, signed int> Map;
   1.116 +  };
   1.117  
   1.118 -  template <typename MapRegistry>
   1.119 -  class DefaultMap<MapRegistry, float> 
   1.120 -    : public VectorMap<MapRegistry, float> 
   1.121 -  DEFAULT_MAP_BODY(VectorMap, float);
   1.122 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.123 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, unsigned int> {
   1.124 +    typedef VectorMap<_Graph, _Item, _IdMap, unsigned int> Map;
   1.125 +  };
   1.126  
   1.127 -  template <typename MapRegistry>
   1.128 -  class DefaultMap<MapRegistry, double> 
   1.129 -    : public VectorMap<MapRegistry, double> 
   1.130 -  DEFAULT_MAP_BODY(VectorMap, double);
   1.131  
   1.132 -  template <typename MapRegistry>
   1.133 -  class DefaultMap<MapRegistry, long double> 
   1.134 -    : public VectorMap<MapRegistry, long double> 
   1.135 -  DEFAULT_MAP_BODY(VectorMap, long double);
   1.136 +  // short
   1.137 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.138 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, signed short> {
   1.139 +    typedef VectorMap<_Graph, _Item, _IdMap, signed short> Map;
   1.140 +  };
   1.141  
   1.142 -  template <typename MapRegistry, typename Type>
   1.143 -  class DefaultMap<MapRegistry, Type*>
   1.144 -    : public VectorMap<MapRegistry, Type*> 
   1.145 -  DEFAULT_MAP_BODY(VectorMap, Type*);
   1.146 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.147 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, unsigned short> {
   1.148 +    typedef VectorMap<_Graph, _Item, _IdMap, unsigned short> Map;
   1.149 +  };
   1.150 +
   1.151 +
   1.152 +  // long
   1.153 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.154 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, signed long> {
   1.155 +    typedef VectorMap<_Graph, _Item, _IdMap, signed long> Map;
   1.156 +  };
   1.157 +
   1.158 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.159 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, unsigned long> {
   1.160 +    typedef VectorMap<_Graph, _Item, _IdMap, unsigned long> Map;
   1.161 +  };
   1.162 +
   1.163 +  // \todo handling long long type
   1.164 +
   1.165 +
   1.166 +  // float
   1.167 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.168 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, float> {
   1.169 +    typedef VectorMap<_Graph, _Item, _IdMap, float> Map;
   1.170 +  };
   1.171 +
   1.172 +
   1.173 +  // double
   1.174 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.175 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, double> {
   1.176 +    typedef VectorMap<_Graph, _Item, _IdMap,  double> Map;
   1.177 +  };
   1.178 +
   1.179 +
   1.180 +  // long double
   1.181 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   1.182 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, long double> {
   1.183 +    typedef VectorMap<_Graph, _Item, _IdMap, long double> Map;
   1.184 +  };
   1.185 +
   1.186 +
   1.187 +  // pointer
   1.188 +  template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap, typename _Ptr>
   1.189 +  struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, _Ptr*> {
   1.190 +    typedef VectorMap<_Graph, _Item, _IdMap, _Ptr*> Map;
   1.191 +  };
   1.192 +
   1.193 +
   1.194 +
   1.195 +  template <typename _Graph, 
   1.196 +	    typename _Item,
   1.197 +	    typename _ItemIt,
   1.198 +	    typename _IdMap,
   1.199 +	    typename _Value>
   1.200 +  class DefaultMap : public DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, _Value>::Map {
   1.201 +  public:
   1.202 +    typedef typename DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, _Value>::Map Parent;
   1.203 +    typedef DefaultMap<_Graph, _Item, _ItemIt, _IdMap, bool> Map;
   1.204 +    
   1.205 +    typedef typename Parent::Graph Graph;
   1.206 +    typedef typename Parent::Registry Registry;
   1.207 +    typedef typename Parent::ValueType ValueType;
   1.208 +
   1.209 +    DefaultMap(const Graph& _g, Registry& _r) : Parent(_g, _r) {}
   1.210 +    DefaultMap(const Graph& _g, Registry& _r, const ValueType& _v) : Parent(_g, _r, _v) {}
   1.211 +  };
   1.212 +
   1.213 +
   1.214 +
   1.215 +  template <typename _Base> 
   1.216 +  class DefaultMappableGraphExtender : public _Base {
   1.217 +  public:
   1.218 +
   1.219 +    typedef DefaultMappableGraphExtender<_Base> Graph;
   1.220 +    typedef _Base Parent;
   1.221 +
   1.222 +    typedef typename Parent::Node Node;
   1.223 +    typedef typename Parent::NodeIt NodeIt;
   1.224 +    typedef typename Parent::NodeIdMap NodeIdMap;
   1.225 +    typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
   1.226 +
   1.227 +    typedef typename Parent::Edge Edge;
   1.228 +    typedef typename Parent::EdgeIt EdgeIt;
   1.229 +    typedef typename Parent::EdgeIdMap EdgeIdMap;
   1.230 +    typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
   1.231 +
   1.232 +    
   1.233 +
   1.234 +    template <typename _Value>
   1.235 +    class NodeMap : public DefaultMap<Graph, Node, NodeIt, NodeIdMap, _Value> {
   1.236 +    public:
   1.237 +      typedef DefaultMappableGraphExtender<_Base> Graph;
   1.238 +
   1.239 +      typedef typename Graph::Node Node;
   1.240 +      typedef typename Graph::NodeIt NodeIt;
   1.241 +      typedef typename Graph::NodeIdMap NodeIdMap;
   1.242 +
   1.243 +      typedef DefaultMap<Graph, Node, NodeIt, NodeIdMap, _Value> Parent;
   1.244 +
   1.245 +      typedef typename Parent::Graph Graph;
   1.246 +      typedef typename Parent::ValueType ValueType;
   1.247 +
   1.248 +      NodeMap(const Graph& g) 
   1.249 +	: Parent(g, g.getNodeObserverRegistry()) {}
   1.250 +      NodeMap(const Graph& g, const ValueType& v) 
   1.251 +	: Parent(g, g.getNodeObserverRegistry(), v) {}
   1.252 +
   1.253 +    };
   1.254 +
   1.255 +    template <typename _Value>
   1.256 +    class EdgeMap : public DefaultMap<Graph, Edge, EdgeIt, EdgeIdMap, _Value> {
   1.257 +    public:
   1.258 +      typedef DefaultMappableGraphExtender<_Base> Graph;
   1.259 +
   1.260 +      typedef typename Graph::Edge Edge;
   1.261 +      typedef typename Graph::EdgeIt EdgeIt;
   1.262 +      typedef typename Graph::EdgeIdMap EdgeIdMap;
   1.263 +
   1.264 +      typedef DefaultMap<Graph, Edge, EdgeIt, EdgeIdMap, _Value> Parent;
   1.265 +
   1.266 +      typedef typename Parent::Graph Graph;
   1.267 +      typedef typename Parent::ValueType ValueType;
   1.268 +
   1.269 +      EdgeMap(const Graph& g) 
   1.270 +	: Parent(g, g.getEdgeObserverRegistry()) {}
   1.271 +      EdgeMap(const Graph& g, const ValueType& v) 
   1.272 +	: Parent(g, g.getEdgeObserverRegistry(), v) {}
   1.273 +
   1.274 +    };
   1.275 +    
   1.276 +  };
   1.277 +
   1.278  
   1.279  }
   1.280