[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[57] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[57] | 4 | * |
---|
[107] | 5 | * Copyright (C) 2003-2008 |
---|
[57] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_BITS_MAP_EXTENDER_H |
---|
| 20 | #define LEMON_BITS_MAP_EXTENDER_H |
---|
| 21 | |
---|
| 22 | #include <iterator> |
---|
| 23 | |
---|
| 24 | #include <lemon/bits/traits.h> |
---|
| 25 | |
---|
| 26 | #include <lemon/concept_check.h> |
---|
| 27 | #include <lemon/concepts/maps.h> |
---|
| 28 | |
---|
| 29 | ///\file |
---|
| 30 | ///\brief Extenders for iterable maps. |
---|
| 31 | |
---|
| 32 | namespace lemon { |
---|
| 33 | |
---|
| 34 | /// \ingroup graphbits |
---|
[209] | 35 | /// |
---|
[57] | 36 | /// \brief Extender for maps |
---|
| 37 | template <typename _Map> |
---|
| 38 | class MapExtender : public _Map { |
---|
| 39 | public: |
---|
| 40 | |
---|
| 41 | typedef _Map Parent; |
---|
| 42 | typedef MapExtender Map; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | typedef typename Parent::Graph Graph; |
---|
| 46 | typedef typename Parent::Key Item; |
---|
| 47 | |
---|
| 48 | typedef typename Parent::Key Key; |
---|
| 49 | typedef typename Parent::Value Value; |
---|
| 50 | |
---|
| 51 | class MapIt; |
---|
| 52 | class ConstMapIt; |
---|
| 53 | |
---|
| 54 | friend class MapIt; |
---|
| 55 | friend class ConstMapIt; |
---|
| 56 | |
---|
| 57 | public: |
---|
| 58 | |
---|
[209] | 59 | MapExtender(const Graph& graph) |
---|
[57] | 60 | : Parent(graph) {} |
---|
| 61 | |
---|
[209] | 62 | MapExtender(const Graph& graph, const Value& value) |
---|
[57] | 63 | : Parent(graph, value) {} |
---|
| 64 | |
---|
| 65 | MapExtender& operator=(const MapExtender& cmap) { |
---|
| 66 | return operator=<MapExtender>(cmap); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | template <typename CMap> |
---|
| 70 | MapExtender& operator=(const CMap& cmap) { |
---|
| 71 | Parent::operator=(cmap); |
---|
| 72 | return *this; |
---|
[209] | 73 | } |
---|
[57] | 74 | |
---|
| 75 | class MapIt : public Item { |
---|
| 76 | public: |
---|
[209] | 77 | |
---|
[57] | 78 | typedef Item Parent; |
---|
| 79 | typedef typename Map::Value Value; |
---|
[209] | 80 | |
---|
[57] | 81 | MapIt() {} |
---|
| 82 | |
---|
| 83 | MapIt(Invalid i) : Parent(i) { } |
---|
| 84 | |
---|
| 85 | explicit MapIt(Map& _map) : map(_map) { |
---|
| 86 | map.notifier()->first(*this); |
---|
| 87 | } |
---|
| 88 | |
---|
[209] | 89 | MapIt(const Map& _map, const Item& item) |
---|
| 90 | : Parent(item), map(_map) {} |
---|
[57] | 91 | |
---|
[209] | 92 | MapIt& operator++() { |
---|
| 93 | map.notifier()->next(*this); |
---|
| 94 | return *this; |
---|
[57] | 95 | } |
---|
[209] | 96 | |
---|
[57] | 97 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 98 | return map[*this]; |
---|
[57] | 99 | } |
---|
| 100 | |
---|
| 101 | typename MapTraits<Map>::ReturnValue operator*() { |
---|
[209] | 102 | return map[*this]; |
---|
[57] | 103 | } |
---|
[209] | 104 | |
---|
[57] | 105 | void set(const Value& value) { |
---|
[209] | 106 | map.set(*this, value); |
---|
[57] | 107 | } |
---|
[209] | 108 | |
---|
[57] | 109 | protected: |
---|
| 110 | Map& map; |
---|
[209] | 111 | |
---|
[57] | 112 | }; |
---|
| 113 | |
---|
| 114 | class ConstMapIt : public Item { |
---|
| 115 | public: |
---|
| 116 | |
---|
| 117 | typedef Item Parent; |
---|
| 118 | |
---|
| 119 | typedef typename Map::Value Value; |
---|
[209] | 120 | |
---|
[57] | 121 | ConstMapIt() {} |
---|
| 122 | |
---|
| 123 | ConstMapIt(Invalid i) : Parent(i) { } |
---|
| 124 | |
---|
| 125 | explicit ConstMapIt(Map& _map) : map(_map) { |
---|
| 126 | map.notifier()->first(*this); |
---|
| 127 | } |
---|
| 128 | |
---|
[209] | 129 | ConstMapIt(const Map& _map, const Item& item) |
---|
| 130 | : Parent(item), map(_map) {} |
---|
[57] | 131 | |
---|
[209] | 132 | ConstMapIt& operator++() { |
---|
| 133 | map.notifier()->next(*this); |
---|
| 134 | return *this; |
---|
[57] | 135 | } |
---|
| 136 | |
---|
| 137 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 138 | return map[*this]; |
---|
[57] | 139 | } |
---|
| 140 | |
---|
| 141 | protected: |
---|
| 142 | const Map& map; |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | class ItemIt : public Item { |
---|
| 146 | public: |
---|
[209] | 147 | |
---|
[57] | 148 | typedef Item Parent; |
---|
[209] | 149 | |
---|
[57] | 150 | ItemIt() {} |
---|
| 151 | |
---|
| 152 | ItemIt(Invalid i) : Parent(i) { } |
---|
| 153 | |
---|
| 154 | explicit ItemIt(Map& _map) : map(_map) { |
---|
| 155 | map.notifier()->first(*this); |
---|
| 156 | } |
---|
| 157 | |
---|
[209] | 158 | ItemIt(const Map& _map, const Item& item) |
---|
| 159 | : Parent(item), map(_map) {} |
---|
[57] | 160 | |
---|
[209] | 161 | ItemIt& operator++() { |
---|
| 162 | map.notifier()->next(*this); |
---|
| 163 | return *this; |
---|
[57] | 164 | } |
---|
| 165 | |
---|
| 166 | protected: |
---|
| 167 | const Map& map; |
---|
[209] | 168 | |
---|
[57] | 169 | }; |
---|
| 170 | }; |
---|
| 171 | |
---|
| 172 | /// \ingroup graphbits |
---|
[209] | 173 | /// |
---|
[57] | 174 | /// \brief Extender for maps which use a subset of the items. |
---|
| 175 | template <typename _Graph, typename _Map> |
---|
| 176 | class SubMapExtender : public _Map { |
---|
| 177 | public: |
---|
| 178 | |
---|
| 179 | typedef _Map Parent; |
---|
| 180 | typedef SubMapExtender Map; |
---|
| 181 | |
---|
| 182 | typedef _Graph Graph; |
---|
| 183 | |
---|
| 184 | typedef typename Parent::Key Item; |
---|
| 185 | |
---|
| 186 | typedef typename Parent::Key Key; |
---|
| 187 | typedef typename Parent::Value Value; |
---|
| 188 | |
---|
| 189 | class MapIt; |
---|
| 190 | class ConstMapIt; |
---|
| 191 | |
---|
| 192 | friend class MapIt; |
---|
| 193 | friend class ConstMapIt; |
---|
| 194 | |
---|
| 195 | public: |
---|
| 196 | |
---|
[209] | 197 | SubMapExtender(const Graph& _graph) |
---|
[57] | 198 | : Parent(_graph), graph(_graph) {} |
---|
| 199 | |
---|
[209] | 200 | SubMapExtender(const Graph& _graph, const Value& _value) |
---|
[57] | 201 | : Parent(_graph, _value), graph(_graph) {} |
---|
| 202 | |
---|
| 203 | SubMapExtender& operator=(const SubMapExtender& cmap) { |
---|
| 204 | return operator=<MapExtender>(cmap); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | template <typename CMap> |
---|
| 208 | SubMapExtender& operator=(const CMap& cmap) { |
---|
| 209 | checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
---|
| 210 | Item it; |
---|
| 211 | for (graph.first(it); it != INVALID; graph.next(it)) { |
---|
| 212 | Parent::set(it, cmap[it]); |
---|
| 213 | } |
---|
| 214 | return *this; |
---|
[209] | 215 | } |
---|
[57] | 216 | |
---|
| 217 | class MapIt : public Item { |
---|
| 218 | public: |
---|
[209] | 219 | |
---|
[57] | 220 | typedef Item Parent; |
---|
| 221 | typedef typename Map::Value Value; |
---|
[209] | 222 | |
---|
[57] | 223 | MapIt() {} |
---|
| 224 | |
---|
| 225 | MapIt(Invalid i) : Parent(i) { } |
---|
| 226 | |
---|
| 227 | explicit MapIt(Map& _map) : map(_map) { |
---|
| 228 | map.graph.first(*this); |
---|
| 229 | } |
---|
| 230 | |
---|
[209] | 231 | MapIt(const Map& _map, const Item& item) |
---|
| 232 | : Parent(item), map(_map) {} |
---|
[57] | 233 | |
---|
[209] | 234 | MapIt& operator++() { |
---|
| 235 | map.graph.next(*this); |
---|
| 236 | return *this; |
---|
[57] | 237 | } |
---|
[209] | 238 | |
---|
[57] | 239 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 240 | return map[*this]; |
---|
[57] | 241 | } |
---|
| 242 | |
---|
| 243 | typename MapTraits<Map>::ReturnValue operator*() { |
---|
[209] | 244 | return map[*this]; |
---|
[57] | 245 | } |
---|
[209] | 246 | |
---|
[57] | 247 | void set(const Value& value) { |
---|
[209] | 248 | map.set(*this, value); |
---|
[57] | 249 | } |
---|
[209] | 250 | |
---|
[57] | 251 | protected: |
---|
| 252 | Map& map; |
---|
[209] | 253 | |
---|
[57] | 254 | }; |
---|
| 255 | |
---|
| 256 | class ConstMapIt : public Item { |
---|
| 257 | public: |
---|
| 258 | |
---|
| 259 | typedef Item Parent; |
---|
| 260 | |
---|
| 261 | typedef typename Map::Value Value; |
---|
[209] | 262 | |
---|
[57] | 263 | ConstMapIt() {} |
---|
| 264 | |
---|
| 265 | ConstMapIt(Invalid i) : Parent(i) { } |
---|
| 266 | |
---|
| 267 | explicit ConstMapIt(Map& _map) : map(_map) { |
---|
| 268 | map.graph.first(*this); |
---|
| 269 | } |
---|
| 270 | |
---|
[209] | 271 | ConstMapIt(const Map& _map, const Item& item) |
---|
| 272 | : Parent(item), map(_map) {} |
---|
[57] | 273 | |
---|
[209] | 274 | ConstMapIt& operator++() { |
---|
| 275 | map.graph.next(*this); |
---|
| 276 | return *this; |
---|
[57] | 277 | } |
---|
| 278 | |
---|
| 279 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 280 | return map[*this]; |
---|
[57] | 281 | } |
---|
| 282 | |
---|
| 283 | protected: |
---|
| 284 | const Map& map; |
---|
| 285 | }; |
---|
| 286 | |
---|
| 287 | class ItemIt : public Item { |
---|
| 288 | public: |
---|
[209] | 289 | |
---|
[57] | 290 | typedef Item Parent; |
---|
[209] | 291 | |
---|
[57] | 292 | ItemIt() {} |
---|
| 293 | |
---|
| 294 | ItemIt(Invalid i) : Parent(i) { } |
---|
| 295 | |
---|
| 296 | explicit ItemIt(Map& _map) : map(_map) { |
---|
| 297 | map.graph.first(*this); |
---|
| 298 | } |
---|
| 299 | |
---|
[209] | 300 | ItemIt(const Map& _map, const Item& item) |
---|
| 301 | : Parent(item), map(_map) {} |
---|
[57] | 302 | |
---|
[209] | 303 | ItemIt& operator++() { |
---|
| 304 | map.graph.next(*this); |
---|
| 305 | return *this; |
---|
[57] | 306 | } |
---|
| 307 | |
---|
| 308 | protected: |
---|
| 309 | const Map& map; |
---|
[209] | 310 | |
---|
[57] | 311 | }; |
---|
[209] | 312 | |
---|
[57] | 313 | private: |
---|
| 314 | |
---|
| 315 | const Graph& graph; |
---|
[209] | 316 | |
---|
[57] | 317 | }; |
---|
| 318 | |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | #endif |
---|