COIN-OR::LEMON - Graph Library

source: lemon/lemon/bits/map_extender.h @ 913:2f9d9bcc1867

1.1
Last change on this file since 913:2f9d9bcc1867 was 913:2f9d9bcc1867, checked in by Alpar Juttner <alpar@…>, 14 years ago

Merge 4 backouts (#50, #312)

File size: 6.8 KB
RevLine 
[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 *
[463]5 * Copyright (C) 2003-2009
[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
[314]29//\file
30//\brief Extenders for iterable maps.
[57]31
32namespace lemon {
33
[314]34  // \ingroup graphbits
35  //
36  // \brief Extender for maps
[57]37  template <typename _Map>
38  class MapExtender : public _Map {
[664]39    typedef _Map Parent;
40    typedef typename Parent::GraphType GraphType;
41
[57]42  public:
43
44    typedef MapExtender Map;
45    typedef typename Parent::Key Item;
46
47    typedef typename Parent::Key Key;
48    typedef typename Parent::Value Value;
[627]49    typedef typename Parent::Reference Reference;
50    typedef typename Parent::ConstReference ConstReference;
[57]51
52    class MapIt;
53    class ConstMapIt;
54
55    friend class MapIt;
56    friend class ConstMapIt;
57
58  public:
59
[664]60    MapExtender(const GraphType& graph)
[57]61      : Parent(graph) {}
62
[664]63    MapExtender(const GraphType& graph, const Value& value)
[57]64      : Parent(graph, value) {}
65
[263]66  private:
[57]67    MapExtender& operator=(const MapExtender& cmap) {
68      return operator=<MapExtender>(cmap);
69    }
70
71    template <typename CMap>
72    MapExtender& operator=(const CMap& cmap) {
73      Parent::operator=(cmap);
74      return *this;
[209]75    }
[57]76
[263]77  public:
[57]78    class MapIt : public Item {
[664]79      typedef Item Parent;
80
[57]81    public:
[209]82
[57]83      typedef typename Map::Value Value;
[209]84
[865]85      MapIt() : map(NULL) {}
[57]86
[865]87      MapIt(Invalid i) : Parent(i), map(NULL) {}
[57]88
[865]89      explicit MapIt(Map& _map) : map(&_map) {
90        map->notifier()->first(*this);
[57]91      }
92
[209]93      MapIt(const Map& _map, const Item& item)
[865]94        : Parent(item), map(&_map) {}
[57]95
[209]96      MapIt& operator++() {
[865]97        map->notifier()->next(*this);
[209]98        return *this;
[57]99      }
[209]100
[57]101      typename MapTraits<Map>::ConstReturnValue operator*() const {
[865]102        return (*map)[*this];
[57]103      }
104
105      typename MapTraits<Map>::ReturnValue operator*() {
[865]106        return (*map)[*this];
[57]107      }
[209]108
[57]109      void set(const Value& value) {
[865]110        map->set(*this, value);
[57]111      }
[209]112
[57]113    protected:
[865]114      Map* map;
[209]115
[57]116    };
117
118    class ConstMapIt : public Item {
[664]119      typedef Item Parent;
120
[57]121    public:
122
123      typedef typename Map::Value Value;
[209]124
[865]125      ConstMapIt() : map(NULL) {}
[57]126
[865]127      ConstMapIt(Invalid i) : Parent(i), map(NULL) {}
[57]128
[865]129      explicit ConstMapIt(Map& _map) : map(&_map) {
130        map->notifier()->first(*this);
[57]131      }
132
[209]133      ConstMapIt(const Map& _map, const Item& item)
134        : Parent(item), map(_map) {}
[57]135
[209]136      ConstMapIt& operator++() {
[865]137        map->notifier()->next(*this);
[209]138        return *this;
[57]139      }
140
141      typename MapTraits<Map>::ConstReturnValue operator*() const {
[209]142        return map[*this];
[57]143      }
144
145    protected:
[865]146      const Map* map;
[57]147    };
148
149    class ItemIt : public Item {
[664]150      typedef Item Parent;
151
[57]152    public:
[865]153      ItemIt() : map(NULL) {}
[209]154
[57]155
[865]156      ItemIt(Invalid i) : Parent(i), map(NULL) {}
[57]157
[865]158      explicit ItemIt(Map& _map) : map(&_map) {
159        map->notifier()->first(*this);
[57]160      }
161
[209]162      ItemIt(const Map& _map, const Item& item)
[865]163        : Parent(item), map(&_map) {}
[57]164
[209]165      ItemIt& operator++() {
[865]166        map->notifier()->next(*this);
[209]167        return *this;
[57]168      }
169
170    protected:
[865]171      const Map* map;
[209]172
[57]173    };
174  };
175
[314]176  // \ingroup graphbits
177  //
178  // \brief Extender for maps which use a subset of the items.
[57]179  template <typename _Graph, typename _Map>
180  class SubMapExtender : public _Map {
[664]181    typedef _Map Parent;
182    typedef _Graph GraphType;
183
[57]184  public:
185
186    typedef SubMapExtender Map;
187    typedef typename Parent::Key Item;
188
189    typedef typename Parent::Key Key;
190    typedef typename Parent::Value Value;
[627]191    typedef typename Parent::Reference Reference;
192    typedef typename Parent::ConstReference ConstReference;
[57]193
194    class MapIt;
195    class ConstMapIt;
196
197    friend class MapIt;
198    friend class ConstMapIt;
199
200  public:
201
[664]202    SubMapExtender(const GraphType& _graph)
[57]203      : Parent(_graph), graph(_graph) {}
204
[664]205    SubMapExtender(const GraphType& _graph, const Value& _value)
[57]206      : Parent(_graph, _value), graph(_graph) {}
207
[263]208  private:
[57]209    SubMapExtender& operator=(const SubMapExtender& cmap) {
210      return operator=<MapExtender>(cmap);
211    }
212
213    template <typename CMap>
214    SubMapExtender& operator=(const CMap& cmap) {
215      checkConcept<concepts::ReadMap<Key, Value>, CMap>();
216      Item it;
217      for (graph.first(it); it != INVALID; graph.next(it)) {
218        Parent::set(it, cmap[it]);
219      }
220      return *this;
[209]221    }
[57]222
[263]223  public:
[57]224    class MapIt : public Item {
[664]225      typedef Item Parent;
226
[57]227    public:
228      typedef typename Map::Value Value;
[209]229
[865]230      MapIt() : map(NULL) {}
[57]231
[865]232      MapIt(Invalid i) : Parent(i), map(NULL) { }
[57]233
[865]234      explicit MapIt(Map& _map) : map(&_map) {
235        map->graph.first(*this);
[57]236      }
237
[209]238      MapIt(const Map& _map, const Item& item)
[865]239        : Parent(item), map(&_map) {}
[57]240
[209]241      MapIt& operator++() {
[865]242        map->graph.next(*this);
[209]243        return *this;
[57]244      }
[209]245
[57]246      typename MapTraits<Map>::ConstReturnValue operator*() const {
[865]247        return (*map)[*this];
[57]248      }
249
250      typename MapTraits<Map>::ReturnValue operator*() {
[865]251        return (*map)[*this];
[57]252      }
[209]253
[57]254      void set(const Value& value) {
[865]255        map->set(*this, value);
[57]256      }
[209]257
[57]258    protected:
[865]259      Map* map;
[209]260
[57]261    };
262
263    class ConstMapIt : public Item {
[664]264      typedef Item Parent;
265
[57]266    public:
267
268      typedef typename Map::Value Value;
[209]269
[865]270      ConstMapIt() : map(NULL) {}
[57]271
[865]272      ConstMapIt(Invalid i) : Parent(i), map(NULL) { }
[57]273
[865]274      explicit ConstMapIt(Map& _map) : map(&_map) {
275        map->graph.first(*this);
[57]276      }
277
[209]278      ConstMapIt(const Map& _map, const Item& item)
[865]279        : Parent(item), map(&_map) {}
[57]280
[209]281      ConstMapIt& operator++() {
[865]282        map->graph.next(*this);
[209]283        return *this;
[57]284      }
285
286      typename MapTraits<Map>::ConstReturnValue operator*() const {
[865]287        return (*map)[*this];
[57]288      }
289
290    protected:
[865]291      const Map* map;
[57]292    };
293
294    class ItemIt : public Item {
[664]295      typedef Item Parent;
296
[57]297    public:
[865]298      ItemIt() : map(NULL) {}
[209]299
[57]300
[865]301      ItemIt(Invalid i) : Parent(i), map(NULL) { }
[57]302
[865]303      explicit ItemIt(Map& _map) : map(&_map) {
304        map->graph.first(*this);
[57]305      }
306
[209]307      ItemIt(const Map& _map, const Item& item)
[865]308        : Parent(item), map(&_map) {}
[57]309
[209]310      ItemIt& operator++() {
[865]311        map->graph.next(*this);
[209]312        return *this;
[57]313      }
314
315    protected:
[865]316      const Map* map;
[209]317
[57]318    };
[209]319
[57]320  private:
321
[664]322    const GraphType& graph;
[209]323
[57]324  };
325
326}
327
328#endif
Note: See TracBrowser for help on using the repository browser.