1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_BITS_MAP_EXTENDER_H
20 #define LEMON_BITS_MAP_EXTENDER_H
24 #include <lemon/bits/traits.h>
26 #include <lemon/concept_check.h>
27 #include <lemon/concepts/maps.h>
30 //\brief Extenders for iterable maps.
36 // \brief Extender for maps
37 template <typename _Map>
38 class MapExtender : public _Map {
40 typedef typename Parent::GraphType GraphType;
44 typedef MapExtender Map;
45 typedef typename Parent::Key Item;
47 typedef typename Parent::Key Key;
48 typedef typename Parent::Value Value;
49 typedef typename Parent::Reference Reference;
50 typedef typename Parent::ConstReference ConstReference;
56 friend class ConstMapIt;
60 MapExtender(const GraphType& graph)
63 MapExtender(const GraphType& graph, const Value& value)
64 : Parent(graph, value) {}
67 MapExtender& operator=(const MapExtender& cmap) {
68 return operator=<MapExtender>(cmap);
71 template <typename CMap>
72 MapExtender& operator=(const CMap& cmap) {
73 Parent::operator=(cmap);
78 class MapIt : public Item {
83 typedef typename Map::Value Value;
85 MapIt() : map(NULL) {}
87 MapIt(Invalid i) : Parent(i), map(NULL) {}
89 explicit MapIt(Map& _map) : map(&_map) {
90 map->notifier()->first(*this);
93 MapIt(const Map& _map, const Item& item)
94 : Parent(item), map(&_map) {}
97 map->notifier()->next(*this);
101 typename MapTraits<Map>::ConstReturnValue operator*() const {
102 return (*map)[*this];
105 typename MapTraits<Map>::ReturnValue operator*() {
106 return (*map)[*this];
109 void set(const Value& value) {
110 map->set(*this, value);
118 class ConstMapIt : public Item {
123 typedef typename Map::Value Value;
125 ConstMapIt() : map(NULL) {}
127 ConstMapIt(Invalid i) : Parent(i), map(NULL) {}
129 explicit ConstMapIt(Map& _map) : map(&_map) {
130 map->notifier()->first(*this);
133 ConstMapIt(const Map& _map, const Item& item)
134 : Parent(item), map(_map) {}
136 ConstMapIt& operator++() {
137 map->notifier()->next(*this);
141 typename MapTraits<Map>::ConstReturnValue operator*() const {
149 class ItemIt : public Item {
153 ItemIt() : map(NULL) {}
156 ItemIt(Invalid i) : Parent(i), map(NULL) {}
158 explicit ItemIt(Map& _map) : map(&_map) {
159 map->notifier()->first(*this);
162 ItemIt(const Map& _map, const Item& item)
163 : Parent(item), map(&_map) {}
165 ItemIt& operator++() {
166 map->notifier()->next(*this);
176 // \ingroup graphbits
178 // \brief Extender for maps which use a subset of the items.
179 template <typename _Graph, typename _Map>
180 class SubMapExtender : public _Map {
182 typedef _Graph GraphType;
186 typedef SubMapExtender Map;
187 typedef typename Parent::Key Item;
189 typedef typename Parent::Key Key;
190 typedef typename Parent::Value Value;
191 typedef typename Parent::Reference Reference;
192 typedef typename Parent::ConstReference ConstReference;
198 friend class ConstMapIt;
202 SubMapExtender(const GraphType& _graph)
203 : Parent(_graph), graph(_graph) {}
205 SubMapExtender(const GraphType& _graph, const Value& _value)
206 : Parent(_graph, _value), graph(_graph) {}
209 SubMapExtender& operator=(const SubMapExtender& cmap) {
210 return operator=<MapExtender>(cmap);
213 template <typename CMap>
214 SubMapExtender& operator=(const CMap& cmap) {
215 checkConcept<concepts::ReadMap<Key, Value>, CMap>();
217 for (graph.first(it); it != INVALID; graph.next(it)) {
218 Parent::set(it, cmap[it]);
224 class MapIt : public Item {
228 typedef typename Map::Value Value;
230 MapIt() : map(NULL) {}
232 MapIt(Invalid i) : Parent(i), map(NULL) { }
234 explicit MapIt(Map& _map) : map(&_map) {
235 map->graph.first(*this);
238 MapIt(const Map& _map, const Item& item)
239 : Parent(item), map(&_map) {}
241 MapIt& operator++() {
242 map->graph.next(*this);
246 typename MapTraits<Map>::ConstReturnValue operator*() const {
247 return (*map)[*this];
250 typename MapTraits<Map>::ReturnValue operator*() {
251 return (*map)[*this];
254 void set(const Value& value) {
255 map->set(*this, value);
263 class ConstMapIt : public Item {
268 typedef typename Map::Value Value;
270 ConstMapIt() : map(NULL) {}
272 ConstMapIt(Invalid i) : Parent(i), map(NULL) { }
274 explicit ConstMapIt(Map& _map) : map(&_map) {
275 map->graph.first(*this);
278 ConstMapIt(const Map& _map, const Item& item)
279 : Parent(item), map(&_map) {}
281 ConstMapIt& operator++() {
282 map->graph.next(*this);
286 typename MapTraits<Map>::ConstReturnValue operator*() const {
287 return (*map)[*this];
294 class ItemIt : public Item {
298 ItemIt() : map(NULL) {}
301 ItemIt(Invalid i) : Parent(i), map(NULL) { }
303 explicit ItemIt(Map& _map) : map(&_map) {
304 map->graph.first(*this);
307 ItemIt(const Map& _map, const Item& item)
308 : Parent(item), map(&_map) {}
310 ItemIt& operator++() {
311 map->graph.next(*this);
322 const GraphType& graph;