1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/bits/map_extender.h Sun Jan 20 20:43:48 2008 +0100
1.3 @@ -0,0 +1,321 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2007
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_BITS_MAP_EXTENDER_H
1.23 +#define LEMON_BITS_MAP_EXTENDER_H
1.24 +
1.25 +#include <iterator>
1.26 +
1.27 +#include <lemon/bits/traits.h>
1.28 +
1.29 +#include <lemon/concept_check.h>
1.30 +#include <lemon/concepts/maps.h>
1.31 +
1.32 +///\file
1.33 +///\brief Extenders for iterable maps.
1.34 +
1.35 +namespace lemon {
1.36 +
1.37 + /// \ingroup graphbits
1.38 + ///
1.39 + /// \brief Extender for maps
1.40 + template <typename _Map>
1.41 + class MapExtender : public _Map {
1.42 + public:
1.43 +
1.44 + typedef _Map Parent;
1.45 + typedef MapExtender Map;
1.46 +
1.47 +
1.48 + typedef typename Parent::Graph Graph;
1.49 + typedef typename Parent::Key Item;
1.50 +
1.51 + typedef typename Parent::Key Key;
1.52 + typedef typename Parent::Value Value;
1.53 +
1.54 + class MapIt;
1.55 + class ConstMapIt;
1.56 +
1.57 + friend class MapIt;
1.58 + friend class ConstMapIt;
1.59 +
1.60 + public:
1.61 +
1.62 + MapExtender(const Graph& graph)
1.63 + : Parent(graph) {}
1.64 +
1.65 + MapExtender(const Graph& graph, const Value& value)
1.66 + : Parent(graph, value) {}
1.67 +
1.68 + MapExtender& operator=(const MapExtender& cmap) {
1.69 + return operator=<MapExtender>(cmap);
1.70 + }
1.71 +
1.72 + template <typename CMap>
1.73 + MapExtender& operator=(const CMap& cmap) {
1.74 + Parent::operator=(cmap);
1.75 + return *this;
1.76 + }
1.77 +
1.78 + class MapIt : public Item {
1.79 + public:
1.80 +
1.81 + typedef Item Parent;
1.82 + typedef typename Map::Value Value;
1.83 +
1.84 + MapIt() {}
1.85 +
1.86 + MapIt(Invalid i) : Parent(i) { }
1.87 +
1.88 + explicit MapIt(Map& _map) : map(_map) {
1.89 + map.notifier()->first(*this);
1.90 + }
1.91 +
1.92 + MapIt(const Map& _map, const Item& item)
1.93 + : Parent(item), map(_map) {}
1.94 +
1.95 + MapIt& operator++() {
1.96 + map.notifier()->next(*this);
1.97 + return *this;
1.98 + }
1.99 +
1.100 + typename MapTraits<Map>::ConstReturnValue operator*() const {
1.101 + return map[*this];
1.102 + }
1.103 +
1.104 + typename MapTraits<Map>::ReturnValue operator*() {
1.105 + return map[*this];
1.106 + }
1.107 +
1.108 + void set(const Value& value) {
1.109 + map.set(*this, value);
1.110 + }
1.111 +
1.112 + protected:
1.113 + Map& map;
1.114 +
1.115 + };
1.116 +
1.117 + class ConstMapIt : public Item {
1.118 + public:
1.119 +
1.120 + typedef Item Parent;
1.121 +
1.122 + typedef typename Map::Value Value;
1.123 +
1.124 + ConstMapIt() {}
1.125 +
1.126 + ConstMapIt(Invalid i) : Parent(i) { }
1.127 +
1.128 + explicit ConstMapIt(Map& _map) : map(_map) {
1.129 + map.notifier()->first(*this);
1.130 + }
1.131 +
1.132 + ConstMapIt(const Map& _map, const Item& item)
1.133 + : Parent(item), map(_map) {}
1.134 +
1.135 + ConstMapIt& operator++() {
1.136 + map.notifier()->next(*this);
1.137 + return *this;
1.138 + }
1.139 +
1.140 + typename MapTraits<Map>::ConstReturnValue operator*() const {
1.141 + return map[*this];
1.142 + }
1.143 +
1.144 + protected:
1.145 + const Map& map;
1.146 + };
1.147 +
1.148 + class ItemIt : public Item {
1.149 + public:
1.150 +
1.151 + typedef Item Parent;
1.152 +
1.153 + ItemIt() {}
1.154 +
1.155 + ItemIt(Invalid i) : Parent(i) { }
1.156 +
1.157 + explicit ItemIt(Map& _map) : map(_map) {
1.158 + map.notifier()->first(*this);
1.159 + }
1.160 +
1.161 + ItemIt(const Map& _map, const Item& item)
1.162 + : Parent(item), map(_map) {}
1.163 +
1.164 + ItemIt& operator++() {
1.165 + map.notifier()->next(*this);
1.166 + return *this;
1.167 + }
1.168 +
1.169 + protected:
1.170 + const Map& map;
1.171 +
1.172 + };
1.173 + };
1.174 +
1.175 + /// \ingroup graphbits
1.176 + ///
1.177 + /// \brief Extender for maps which use a subset of the items.
1.178 + template <typename _Graph, typename _Map>
1.179 + class SubMapExtender : public _Map {
1.180 + public:
1.181 +
1.182 + typedef _Map Parent;
1.183 + typedef SubMapExtender Map;
1.184 +
1.185 + typedef _Graph Graph;
1.186 +
1.187 + typedef typename Parent::Key Item;
1.188 +
1.189 + typedef typename Parent::Key Key;
1.190 + typedef typename Parent::Value Value;
1.191 +
1.192 + class MapIt;
1.193 + class ConstMapIt;
1.194 +
1.195 + friend class MapIt;
1.196 + friend class ConstMapIt;
1.197 +
1.198 + public:
1.199 +
1.200 + SubMapExtender(const Graph& _graph)
1.201 + : Parent(_graph), graph(_graph) {}
1.202 +
1.203 + SubMapExtender(const Graph& _graph, const Value& _value)
1.204 + : Parent(_graph, _value), graph(_graph) {}
1.205 +
1.206 + SubMapExtender& operator=(const SubMapExtender& cmap) {
1.207 + return operator=<MapExtender>(cmap);
1.208 + }
1.209 +
1.210 + template <typename CMap>
1.211 + SubMapExtender& operator=(const CMap& cmap) {
1.212 + checkConcept<concepts::ReadMap<Key, Value>, CMap>();
1.213 + Item it;
1.214 + for (graph.first(it); it != INVALID; graph.next(it)) {
1.215 + Parent::set(it, cmap[it]);
1.216 + }
1.217 + return *this;
1.218 + }
1.219 +
1.220 + class MapIt : public Item {
1.221 + public:
1.222 +
1.223 + typedef Item Parent;
1.224 + typedef typename Map::Value Value;
1.225 +
1.226 + MapIt() {}
1.227 +
1.228 + MapIt(Invalid i) : Parent(i) { }
1.229 +
1.230 + explicit MapIt(Map& _map) : map(_map) {
1.231 + map.graph.first(*this);
1.232 + }
1.233 +
1.234 + MapIt(const Map& _map, const Item& item)
1.235 + : Parent(item), map(_map) {}
1.236 +
1.237 + MapIt& operator++() {
1.238 + map.graph.next(*this);
1.239 + return *this;
1.240 + }
1.241 +
1.242 + typename MapTraits<Map>::ConstReturnValue operator*() const {
1.243 + return map[*this];
1.244 + }
1.245 +
1.246 + typename MapTraits<Map>::ReturnValue operator*() {
1.247 + return map[*this];
1.248 + }
1.249 +
1.250 + void set(const Value& value) {
1.251 + map.set(*this, value);
1.252 + }
1.253 +
1.254 + protected:
1.255 + Map& map;
1.256 +
1.257 + };
1.258 +
1.259 + class ConstMapIt : public Item {
1.260 + public:
1.261 +
1.262 + typedef Item Parent;
1.263 +
1.264 + typedef typename Map::Value Value;
1.265 +
1.266 + ConstMapIt() {}
1.267 +
1.268 + ConstMapIt(Invalid i) : Parent(i) { }
1.269 +
1.270 + explicit ConstMapIt(Map& _map) : map(_map) {
1.271 + map.graph.first(*this);
1.272 + }
1.273 +
1.274 + ConstMapIt(const Map& _map, const Item& item)
1.275 + : Parent(item), map(_map) {}
1.276 +
1.277 + ConstMapIt& operator++() {
1.278 + map.graph.next(*this);
1.279 + return *this;
1.280 + }
1.281 +
1.282 + typename MapTraits<Map>::ConstReturnValue operator*() const {
1.283 + return map[*this];
1.284 + }
1.285 +
1.286 + protected:
1.287 + const Map& map;
1.288 + };
1.289 +
1.290 + class ItemIt : public Item {
1.291 + public:
1.292 +
1.293 + typedef Item Parent;
1.294 +
1.295 + ItemIt() {}
1.296 +
1.297 + ItemIt(Invalid i) : Parent(i) { }
1.298 +
1.299 + explicit ItemIt(Map& _map) : map(_map) {
1.300 + map.graph.first(*this);
1.301 + }
1.302 +
1.303 + ItemIt(const Map& _map, const Item& item)
1.304 + : Parent(item), map(_map) {}
1.305 +
1.306 + ItemIt& operator++() {
1.307 + map.graph.next(*this);
1.308 + return *this;
1.309 + }
1.310 +
1.311 + protected:
1.312 + const Map& map;
1.313 +
1.314 + };
1.315 +
1.316 + private:
1.317 +
1.318 + const Graph& graph;
1.319 +
1.320 + };
1.321 +
1.322 +}
1.323 +
1.324 +#endif