[906] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/default_map.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[921] | 17 | #ifndef LEMON_DEFAULT_MAP_H |
---|
| 18 | #define LEMON_DEFAULT_MAP_H |
---|
[822] | 19 | |
---|
| 20 | |
---|
[1307] | 21 | #include <lemon/bits/array_map.h> |
---|
| 22 | #include <lemon/bits/vector_map.h> |
---|
[822] | 23 | |
---|
[1587] | 24 | ///\ingroup graphmapfactory |
---|
[822] | 25 | ///\file |
---|
[946] | 26 | ///\brief Graph maps that construct and destruct |
---|
[822] | 27 | ///their elements dynamically. |
---|
| 28 | |
---|
[921] | 29 | namespace lemon { |
---|
[822] | 30 | |
---|
[1669] | 31 | /// \addtogroup graphmapfactory |
---|
| 32 | /// @{ |
---|
[822] | 33 | |
---|
[1267] | 34 | template <typename _Graph, typename _Item, typename _Value> |
---|
[946] | 35 | struct DefaultMapSelector { |
---|
[1267] | 36 | typedef ArrayMap<_Graph, _Item, _Value> Map; |
---|
[946] | 37 | }; |
---|
[822] | 38 | |
---|
[946] | 39 | // bool |
---|
[1267] | 40 | template <typename _Graph, typename _Item> |
---|
| 41 | struct DefaultMapSelector<_Graph, _Item, bool> { |
---|
[980] | 42 | typedef VectorMap<_Graph, _Item, bool> Map; |
---|
[946] | 43 | }; |
---|
[822] | 44 | |
---|
[946] | 45 | // char |
---|
[1267] | 46 | template <typename _Graph, typename _Item> |
---|
| 47 | struct DefaultMapSelector<_Graph, _Item, char> { |
---|
[980] | 48 | typedef VectorMap<_Graph, _Item, char> Map; |
---|
[946] | 49 | }; |
---|
[822] | 50 | |
---|
[1267] | 51 | template <typename _Graph, typename _Item> |
---|
| 52 | struct DefaultMapSelector<_Graph, _Item, signed char> { |
---|
[980] | 53 | typedef VectorMap<_Graph, _Item, signed char> Map; |
---|
[946] | 54 | }; |
---|
[822] | 55 | |
---|
[1267] | 56 | template <typename _Graph, typename _Item> |
---|
| 57 | struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
---|
[980] | 58 | typedef VectorMap<_Graph, _Item, unsigned char> Map; |
---|
[946] | 59 | }; |
---|
[822] | 60 | |
---|
| 61 | |
---|
[946] | 62 | // int |
---|
[1267] | 63 | template <typename _Graph, typename _Item> |
---|
| 64 | struct DefaultMapSelector<_Graph, _Item, signed int> { |
---|
[980] | 65 | typedef VectorMap<_Graph, _Item, signed int> Map; |
---|
[946] | 66 | }; |
---|
[822] | 67 | |
---|
[1267] | 68 | template <typename _Graph, typename _Item> |
---|
| 69 | struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
---|
[980] | 70 | typedef VectorMap<_Graph, _Item, unsigned int> Map; |
---|
[946] | 71 | }; |
---|
[822] | 72 | |
---|
| 73 | |
---|
[946] | 74 | // short |
---|
[1267] | 75 | template <typename _Graph, typename _Item> |
---|
| 76 | struct DefaultMapSelector<_Graph, _Item, signed short> { |
---|
[980] | 77 | typedef VectorMap<_Graph, _Item, signed short> Map; |
---|
[946] | 78 | }; |
---|
[822] | 79 | |
---|
[1267] | 80 | template <typename _Graph, typename _Item> |
---|
| 81 | struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
---|
[980] | 82 | typedef VectorMap<_Graph, _Item, unsigned short> Map; |
---|
[946] | 83 | }; |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | // long |
---|
[1267] | 87 | template <typename _Graph, typename _Item> |
---|
| 88 | struct DefaultMapSelector<_Graph, _Item, signed long> { |
---|
[980] | 89 | typedef VectorMap<_Graph, _Item, signed long> Map; |
---|
[946] | 90 | }; |
---|
| 91 | |
---|
[1267] | 92 | template <typename _Graph, typename _Item> |
---|
| 93 | struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
---|
[980] | 94 | typedef VectorMap<_Graph, _Item, unsigned long> Map; |
---|
[946] | 95 | }; |
---|
| 96 | |
---|
| 97 | // \todo handling long long type |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | // float |
---|
[1267] | 101 | template <typename _Graph, typename _Item> |
---|
| 102 | struct DefaultMapSelector<_Graph, _Item, float> { |
---|
[980] | 103 | typedef VectorMap<_Graph, _Item, float> Map; |
---|
[946] | 104 | }; |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | // double |
---|
[1267] | 108 | template <typename _Graph, typename _Item> |
---|
| 109 | struct DefaultMapSelector<_Graph, _Item, double> { |
---|
[980] | 110 | typedef VectorMap<_Graph, _Item, double> Map; |
---|
[946] | 111 | }; |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | // long double |
---|
[1267] | 115 | template <typename _Graph, typename _Item> |
---|
| 116 | struct DefaultMapSelector<_Graph, _Item, long double> { |
---|
[980] | 117 | typedef VectorMap<_Graph, _Item, long double> Map; |
---|
[946] | 118 | }; |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | // pointer |
---|
[1267] | 122 | template <typename _Graph, typename _Item, typename _Ptr> |
---|
| 123 | struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
---|
[980] | 124 | typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
---|
[946] | 125 | }; |
---|
| 126 | |
---|
[1669] | 127 | /// \e |
---|
[1267] | 128 | template < |
---|
| 129 | typename _Graph, |
---|
| 130 | typename _Item, |
---|
| 131 | typename _Value> |
---|
| 132 | class DefaultMap |
---|
| 133 | : public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
---|
[946] | 134 | public: |
---|
[1267] | 135 | typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
---|
| 136 | typedef DefaultMap<_Graph, _Item, _Value> Map; |
---|
[946] | 137 | |
---|
| 138 | typedef typename Parent::Graph Graph; |
---|
[987] | 139 | typedef typename Parent::Value Value; |
---|
[946] | 140 | |
---|
[980] | 141 | DefaultMap(const Graph& _g) : Parent(_g) {} |
---|
[987] | 142 | DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {} |
---|
[1669] | 143 | |
---|
[946] | 144 | }; |
---|
| 145 | |
---|
| 146 | |
---|
[1669] | 147 | /// \e |
---|
[946] | 148 | template <typename _Base> |
---|
[1669] | 149 | class MappableGraphExtender : public _Base { |
---|
[946] | 150 | public: |
---|
| 151 | |
---|
[1669] | 152 | typedef MappableGraphExtender<_Base> Graph; |
---|
[946] | 153 | typedef _Base Parent; |
---|
| 154 | |
---|
| 155 | typedef typename Parent::Node Node; |
---|
| 156 | typedef typename Parent::NodeIt NodeIt; |
---|
| 157 | |
---|
| 158 | typedef typename Parent::Edge Edge; |
---|
| 159 | typedef typename Parent::EdgeIt EdgeIt; |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | template <typename _Value> |
---|
[1267] | 163 | class NodeMap |
---|
| 164 | : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > { |
---|
[946] | 165 | public: |
---|
[1669] | 166 | typedef MappableGraphExtender Graph; |
---|
[1267] | 167 | typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent; |
---|
[946] | 168 | |
---|
[980] | 169 | NodeMap(const Graph& _g) |
---|
| 170 | : Parent(_g) {} |
---|
[1022] | 171 | NodeMap(const Graph& _g, const _Value& _v) |
---|
[980] | 172 | : Parent(_g, _v) {} |
---|
[1669] | 173 | |
---|
[1672] | 174 | NodeMap& operator=(const NodeMap& cmap) { |
---|
| 175 | return operator=<NodeMap>(cmap); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
[1669] | 179 | /// \brief Template assign operator. |
---|
| 180 | /// |
---|
| 181 | /// The given parameter should be conform to the ReadMap |
---|
| 182 | /// concecpt and could be indiced by the current item set of |
---|
| 183 | /// the NodeMap. In this case the value for each item |
---|
| 184 | /// is assigned by the value of the given ReadMap. |
---|
| 185 | template <typename CMap> |
---|
| 186 | NodeMap& operator=(const CMap& cmap) { |
---|
| 187 | checkConcept<concept::ReadMap<Node, _Value>, CMap>(); |
---|
| 188 | const typename Parent::Graph* graph = Parent::getGraph(); |
---|
| 189 | Node it; |
---|
| 190 | for (graph->first(it); it != INVALID; graph->next(it)) { |
---|
| 191 | Parent::set(it, cmap[it]); |
---|
| 192 | } |
---|
| 193 | return *this; |
---|
| 194 | } |
---|
| 195 | |
---|
[946] | 196 | }; |
---|
| 197 | |
---|
| 198 | template <typename _Value> |
---|
[1267] | 199 | class EdgeMap |
---|
| 200 | : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > { |
---|
[946] | 201 | public: |
---|
[1669] | 202 | typedef MappableGraphExtender Graph; |
---|
[1267] | 203 | typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
---|
[946] | 204 | |
---|
[980] | 205 | EdgeMap(const Graph& _g) |
---|
| 206 | : Parent(_g) {} |
---|
[1022] | 207 | EdgeMap(const Graph& _g, const _Value& _v) |
---|
[980] | 208 | : Parent(_g, _v) {} |
---|
[1669] | 209 | |
---|
[1672] | 210 | EdgeMap& operator=(const EdgeMap& cmap) { |
---|
| 211 | return operator=<EdgeMap>(cmap); |
---|
| 212 | } |
---|
| 213 | |
---|
[1669] | 214 | template <typename CMap> |
---|
| 215 | EdgeMap& operator=(const CMap& cmap) { |
---|
| 216 | checkConcept<concept::ReadMap<Edge, _Value>, CMap>(); |
---|
| 217 | const typename Parent::Graph* graph = Parent::getGraph(); |
---|
| 218 | Edge it; |
---|
| 219 | for (graph->first(it); it != INVALID; graph->next(it)) { |
---|
| 220 | Parent::set(it, cmap[it]); |
---|
| 221 | } |
---|
| 222 | return *this; |
---|
| 223 | } |
---|
[946] | 224 | }; |
---|
| 225 | |
---|
| 226 | }; |
---|
| 227 | |
---|
[1669] | 228 | /// \e |
---|
[1022] | 229 | template <typename _Base> |
---|
| 230 | class MappableUndirGraphExtender : |
---|
[1669] | 231 | public MappableGraphExtender<_Base> { |
---|
[1022] | 232 | public: |
---|
| 233 | |
---|
| 234 | typedef MappableUndirGraphExtender Graph; |
---|
[1669] | 235 | typedef MappableGraphExtender<_Base> Parent; |
---|
[1022] | 236 | |
---|
| 237 | typedef typename Parent::UndirEdge UndirEdge; |
---|
| 238 | |
---|
| 239 | template <typename _Value> |
---|
[1267] | 240 | class UndirEdgeMap |
---|
| 241 | : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > { |
---|
[1022] | 242 | public: |
---|
| 243 | typedef MappableUndirGraphExtender Graph; |
---|
[1267] | 244 | typedef IterableMapExtender< |
---|
| 245 | DefaultMap<Graph, UndirEdge, _Value> > Parent; |
---|
[1022] | 246 | |
---|
| 247 | UndirEdgeMap(const Graph& _g) |
---|
| 248 | : Parent(_g) {} |
---|
| 249 | UndirEdgeMap(const Graph& _g, const _Value& _v) |
---|
| 250 | : Parent(_g, _v) {} |
---|
[1669] | 251 | |
---|
[1672] | 252 | UndirEdgeMap& operator=(const UndirEdgeMap& cmap) { |
---|
| 253 | return operator=<UndirEdgeMap>(cmap); |
---|
| 254 | } |
---|
| 255 | |
---|
[1669] | 256 | template <typename CMap> |
---|
| 257 | UndirEdgeMap& operator=(const CMap& cmap) { |
---|
| 258 | checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>(); |
---|
| 259 | const typename Parent::Graph* graph = Parent::getGraph(); |
---|
| 260 | UndirEdge it; |
---|
| 261 | for (graph->first(it); it != INVALID; graph->next(it)) { |
---|
| 262 | Parent::set(it, cmap[it]); |
---|
| 263 | } |
---|
| 264 | return *this; |
---|
| 265 | } |
---|
[1022] | 266 | }; |
---|
| 267 | |
---|
| 268 | |
---|
| 269 | }; |
---|
[822] | 270 | |
---|
[1669] | 271 | /// @} |
---|
[822] | 272 | } |
---|
| 273 | |
---|
| 274 | #endif |
---|