src/lemon/default_map.h
changeset 953 d9c115e2eeaf
parent 937 d4e911acef3d
child 979 b5fb023cdb7b
equal deleted inserted replaced
1:62b608736e36 2:898ebe99e0fd
    21 #include <lemon/array_map.h>
    21 #include <lemon/array_map.h>
    22 #include <lemon/vector_map.h>
    22 #include <lemon/vector_map.h>
    23 
    23 
    24 ///\ingroup graphmaps
    24 ///\ingroup graphmaps
    25 ///\file
    25 ///\file
    26 ///\brief Graph maps that construates and destruates
    26 ///\brief Graph maps that construct and destruct
    27 ///their elements dynamically.
    27 ///their elements dynamically.
    28 
    28 
    29 namespace lemon {
    29 namespace lemon {
    30 
    30 
    31 /// \addtogroup graphmaps
    31 /// \addtogroup graphmaps
    39    *  The template parameter is the MapRegistry that the maps
    39    *  The template parameter is the MapRegistry that the maps
    40    *  will belong to and the ValueType.
    40    *  will belong to and the ValueType.
    41    */
    41    */
    42 
    42 
    43 
    43 
    44   /** Macro to implement the DefaultMap.
    44 
    45    */
    45   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap, typename _Value>
    46 #define DEFAULT_MAP_BODY(DynMap, Value) \
    46   struct DefaultMapSelector {
    47 { \
    47     typedef ArrayMap<_Graph, _Item, _ItemIt, _IdMap, _Value> Map;
    48 \
    48   };
    49 public: \
    49 
    50 \
    50   // bool
    51 typedef DynMap<MapRegistry, Value> Parent; \
    51   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    52 \
    52   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, bool> {
    53 typedef typename MapRegistry::Graph Graph; \
    53     typedef VectorMap<_Graph, _Item, _IdMap, bool> Map;
    54 \
    54   };
    55 DefaultMap(const Graph& g, MapRegistry& r) : Parent(g, r) {} \
    55 
    56 DefaultMap(const Graph& g, MapRegistry& r, const Value& v) \
    56   // char
    57   : Parent(g, r, v) {} \
    57   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    58 DefaultMap(const DefaultMap& copy) \
    58   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, char> {
    59   : Parent(static_cast<const Parent&>(copy)) {} \
    59     typedef VectorMap<_Graph, _Item, _IdMap, char> Map;
    60 template <typename TT> \
    60   };
    61 DefaultMap(const DefaultMap<MapRegistry, TT>& copy) \
    61 
    62   : Parent(*copy.getGraph()) { \
    62   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    63   if (Parent::getGraph()) { \
    63   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, signed char> {
    64     for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\
    64     typedef VectorMap<_Graph, _Item, _IdMap, signed char> Map;
    65       Parent::operator[](it) = copy[it]; \
    65   };
    66     } \
    66 
    67   } \
    67   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    68 } \
    68   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, unsigned char> {
    69 DefaultMap& operator=(const DefaultMap& copy) { \
    69     typedef VectorMap<_Graph, _Item, _IdMap, unsigned char> Map;
    70   Parent::operator=(static_cast<const Parent&>(copy)); \
    70   };
    71   return *this; \
    71 
    72 } \
    72 
    73 template <typename TT> \
    73   // int
    74 DefaultMap& operator=(const DefaultMap<MapRegistry, TT>& copy) { \
    74   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    75   if (Parent::getGraph() != copy.getGraph()) { \
    75   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, signed int> {
    76     Parent::clear(); \
    76     typedef VectorMap<_Graph, _Item, _IdMap, signed int> Map;
    77     Parent::MapBase::operator=(copy); \
    77   };
    78     Parent::construct(); \
    78 
    79   } \
    79   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    80   if (Parent::getGraph()) { \
    80   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, unsigned int> {
    81     for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\
    81     typedef VectorMap<_Graph, _Item, _IdMap, unsigned int> Map;
    82       Parent::operator[](it) = copy[it]; \
    82   };
    83     } \
    83 
    84   } \
    84 
    85   return *this; \
    85   // short
    86 } \
    86   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    87 };
    87   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, signed short> {
    88 
    88     typedef VectorMap<_Graph, _Item, _IdMap, signed short> Map;
    89 
    89   };
    90   template <typename MapRegistry, typename Type>
    90 
    91   class DefaultMap : public ArrayMap<MapRegistry, Type> 
    91   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    92   DEFAULT_MAP_BODY(ArrayMap, Type);
    92   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, unsigned short> {
    93 
    93     typedef VectorMap<_Graph, _Item, _IdMap, unsigned short> Map;
    94   template <typename MapRegistry>
    94   };
    95   class DefaultMap<MapRegistry, bool> 
    95 
    96     : public VectorMap<MapRegistry, bool> 
    96 
    97   DEFAULT_MAP_BODY(VectorMap, bool);
    97   // long
    98 
    98   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
    99   template <typename MapRegistry>
    99   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, signed long> {
   100   class DefaultMap<MapRegistry, char> 
   100     typedef VectorMap<_Graph, _Item, _IdMap, signed long> Map;
   101     : public VectorMap<MapRegistry, char> 
   101   };
   102   DEFAULT_MAP_BODY(VectorMap, char);
   102 
   103 
   103   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   104   template <typename MapRegistry>
   104   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, unsigned long> {
   105   class DefaultMap<MapRegistry, int> 
   105     typedef VectorMap<_Graph, _Item, _IdMap, unsigned long> Map;
   106     : public VectorMap<MapRegistry, int> 
   106   };
   107   DEFAULT_MAP_BODY(VectorMap, int);
   107 
   108 
   108   // \todo handling long long type
   109   template <typename MapRegistry>
   109 
   110   class DefaultMap<MapRegistry, short> 
   110 
   111     : public VectorMap<MapRegistry, short> 
   111   // float
   112   DEFAULT_MAP_BODY(VectorMap, short);
   112   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   113 
   113   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, float> {
   114   template <typename MapRegistry>
   114     typedef VectorMap<_Graph, _Item, _IdMap, float> Map;
   115   class DefaultMap<MapRegistry, long> 
   115   };
   116     : public VectorMap<MapRegistry, long> 
   116 
   117   DEFAULT_MAP_BODY(VectorMap, long);
   117 
   118 
   118   // double
   119   template <typename MapRegistry>
   119   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   120   class DefaultMap<MapRegistry, float> 
   120   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, double> {
   121     : public VectorMap<MapRegistry, float> 
   121     typedef VectorMap<_Graph, _Item, _IdMap,  double> Map;
   122   DEFAULT_MAP_BODY(VectorMap, float);
   122   };
   123 
   123 
   124   template <typename MapRegistry>
   124 
   125   class DefaultMap<MapRegistry, double> 
   125   // long double
   126     : public VectorMap<MapRegistry, double> 
   126   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap>
   127   DEFAULT_MAP_BODY(VectorMap, double);
   127   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, long double> {
   128 
   128     typedef VectorMap<_Graph, _Item, _IdMap, long double> Map;
   129   template <typename MapRegistry>
   129   };
   130   class DefaultMap<MapRegistry, long double> 
   130 
   131     : public VectorMap<MapRegistry, long double> 
   131 
   132   DEFAULT_MAP_BODY(VectorMap, long double);
   132   // pointer
   133 
   133   template <typename _Graph, typename _Item, typename _ItemIt, typename _IdMap, typename _Ptr>
   134   template <typename MapRegistry, typename Type>
   134   struct DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, _Ptr*> {
   135   class DefaultMap<MapRegistry, Type*>
   135     typedef VectorMap<_Graph, _Item, _IdMap, _Ptr*> Map;
   136     : public VectorMap<MapRegistry, Type*> 
   136   };
   137   DEFAULT_MAP_BODY(VectorMap, Type*);
   137 
       
   138 
       
   139 
       
   140   template <typename _Graph, 
       
   141 	    typename _Item,
       
   142 	    typename _ItemIt,
       
   143 	    typename _IdMap,
       
   144 	    typename _Value>
       
   145   class DefaultMap : public DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, _Value>::Map {
       
   146   public:
       
   147     typedef typename DefaultMapSelector<_Graph, _Item, _ItemIt, _IdMap, _Value>::Map Parent;
       
   148     typedef DefaultMap<_Graph, _Item, _ItemIt, _IdMap, bool> Map;
       
   149     
       
   150     typedef typename Parent::Graph Graph;
       
   151     typedef typename Parent::Registry Registry;
       
   152     typedef typename Parent::ValueType ValueType;
       
   153 
       
   154     DefaultMap(const Graph& _g, Registry& _r) : Parent(_g, _r) {}
       
   155     DefaultMap(const Graph& _g, Registry& _r, const ValueType& _v) : Parent(_g, _r, _v) {}
       
   156   };
       
   157 
       
   158 
       
   159 
       
   160   template <typename _Base> 
       
   161   class DefaultMappableGraphExtender : public _Base {
       
   162   public:
       
   163 
       
   164     typedef DefaultMappableGraphExtender<_Base> Graph;
       
   165     typedef _Base Parent;
       
   166 
       
   167     typedef typename Parent::Node Node;
       
   168     typedef typename Parent::NodeIt NodeIt;
       
   169     typedef typename Parent::NodeIdMap NodeIdMap;
       
   170     typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
       
   171 
       
   172     typedef typename Parent::Edge Edge;
       
   173     typedef typename Parent::EdgeIt EdgeIt;
       
   174     typedef typename Parent::EdgeIdMap EdgeIdMap;
       
   175     typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
       
   176 
       
   177     
       
   178 
       
   179     template <typename _Value>
       
   180     class NodeMap : public DefaultMap<Graph, Node, NodeIt, NodeIdMap, _Value> {
       
   181     public:
       
   182       typedef DefaultMappableGraphExtender<_Base> Graph;
       
   183 
       
   184       typedef typename Graph::Node Node;
       
   185       typedef typename Graph::NodeIt NodeIt;
       
   186       typedef typename Graph::NodeIdMap NodeIdMap;
       
   187 
       
   188       typedef DefaultMap<Graph, Node, NodeIt, NodeIdMap, _Value> Parent;
       
   189 
       
   190       typedef typename Parent::Graph Graph;
       
   191       typedef typename Parent::ValueType ValueType;
       
   192 
       
   193       NodeMap(const Graph& g) 
       
   194 	: Parent(g, g.getNodeObserverRegistry()) {}
       
   195       NodeMap(const Graph& g, const ValueType& v) 
       
   196 	: Parent(g, g.getNodeObserverRegistry(), v) {}
       
   197 
       
   198     };
       
   199 
       
   200     template <typename _Value>
       
   201     class EdgeMap : public DefaultMap<Graph, Edge, EdgeIt, EdgeIdMap, _Value> {
       
   202     public:
       
   203       typedef DefaultMappableGraphExtender<_Base> Graph;
       
   204 
       
   205       typedef typename Graph::Edge Edge;
       
   206       typedef typename Graph::EdgeIt EdgeIt;
       
   207       typedef typename Graph::EdgeIdMap EdgeIdMap;
       
   208 
       
   209       typedef DefaultMap<Graph, Edge, EdgeIt, EdgeIdMap, _Value> Parent;
       
   210 
       
   211       typedef typename Parent::Graph Graph;
       
   212       typedef typename Parent::ValueType ValueType;
       
   213 
       
   214       EdgeMap(const Graph& g) 
       
   215 	: Parent(g, g.getEdgeObserverRegistry()) {}
       
   216       EdgeMap(const Graph& g, const ValueType& v) 
       
   217 	: Parent(g, g.getEdgeObserverRegistry(), v) {}
       
   218 
       
   219     };
       
   220     
       
   221   };
       
   222 
   138 
   223 
   139 }
   224 }
   140 
   225 
   141 #endif
   226 #endif