lemon/bits/map_extender.h
changeset 243 32155c88f59b
parent 107 31a2e6d28f61
child 263 be8a861d3bb7
equal deleted inserted replaced
1:daf4fd41c318 2:7736d124e3df
     1 /* -*- C++ -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2008
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
    30 ///\brief Extenders for iterable maps.
    30 ///\brief Extenders for iterable maps.
    31 
    31 
    32 namespace lemon {
    32 namespace lemon {
    33 
    33 
    34   /// \ingroup graphbits
    34   /// \ingroup graphbits
    35   /// 
    35   ///
    36   /// \brief Extender for maps
    36   /// \brief Extender for maps
    37   template <typename _Map>
    37   template <typename _Map>
    38   class MapExtender : public _Map {
    38   class MapExtender : public _Map {
    39   public:
    39   public:
    40 
    40 
    54     friend class MapIt;
    54     friend class MapIt;
    55     friend class ConstMapIt;
    55     friend class ConstMapIt;
    56 
    56 
    57   public:
    57   public:
    58 
    58 
    59     MapExtender(const Graph& graph) 
    59     MapExtender(const Graph& graph)
    60       : Parent(graph) {}
    60       : Parent(graph) {}
    61 
    61 
    62     MapExtender(const Graph& graph, const Value& value) 
    62     MapExtender(const Graph& graph, const Value& value)
    63       : Parent(graph, value) {}
    63       : Parent(graph, value) {}
    64 
    64 
    65     MapExtender& operator=(const MapExtender& cmap) {
    65     MapExtender& operator=(const MapExtender& cmap) {
    66       return operator=<MapExtender>(cmap);
    66       return operator=<MapExtender>(cmap);
    67     }
    67     }
    68 
    68 
    69     template <typename CMap>
    69     template <typename CMap>
    70     MapExtender& operator=(const CMap& cmap) {
    70     MapExtender& operator=(const CMap& cmap) {
    71       Parent::operator=(cmap);
    71       Parent::operator=(cmap);
    72       return *this;
    72       return *this;
    73     } 
    73     }
    74 
    74 
    75     class MapIt : public Item {
    75     class MapIt : public Item {
    76     public:
    76     public:
    77       
    77 
    78       typedef Item Parent;
    78       typedef Item Parent;
    79       typedef typename Map::Value Value;
    79       typedef typename Map::Value Value;
    80       
    80 
    81       MapIt() {}
    81       MapIt() {}
    82 
    82 
    83       MapIt(Invalid i) : Parent(i) { }
    83       MapIt(Invalid i) : Parent(i) { }
    84 
    84 
    85       explicit MapIt(Map& _map) : map(_map) {
    85       explicit MapIt(Map& _map) : map(_map) {
    86         map.notifier()->first(*this);
    86         map.notifier()->first(*this);
    87       }
    87       }
    88 
    88 
    89       MapIt(const Map& _map, const Item& item) 
    89       MapIt(const Map& _map, const Item& item)
    90 	: Parent(item), map(_map) {}
    90         : Parent(item), map(_map) {}
    91 
    91 
    92       MapIt& operator++() { 
    92       MapIt& operator++() {
    93 	map.notifier()->next(*this);
    93         map.notifier()->next(*this);
    94 	return *this; 
    94         return *this;
    95       }
    95       }
    96       
    96 
    97       typename MapTraits<Map>::ConstReturnValue operator*() const {
    97       typename MapTraits<Map>::ConstReturnValue operator*() const {
    98 	return map[*this];
    98         return map[*this];
    99       }
    99       }
   100 
   100 
   101       typename MapTraits<Map>::ReturnValue operator*() {
   101       typename MapTraits<Map>::ReturnValue operator*() {
   102 	return map[*this];
   102         return map[*this];
   103       }
   103       }
   104       
   104 
   105       void set(const Value& value) {
   105       void set(const Value& value) {
   106 	map.set(*this, value);
   106         map.set(*this, value);
   107       }
   107       }
   108       
   108 
   109     protected:
   109     protected:
   110       Map& map;
   110       Map& map;
   111       
   111 
   112     };
   112     };
   113 
   113 
   114     class ConstMapIt : public Item {
   114     class ConstMapIt : public Item {
   115     public:
   115     public:
   116 
   116 
   117       typedef Item Parent;
   117       typedef Item Parent;
   118 
   118 
   119       typedef typename Map::Value Value;
   119       typedef typename Map::Value Value;
   120       
   120 
   121       ConstMapIt() {}
   121       ConstMapIt() {}
   122 
   122 
   123       ConstMapIt(Invalid i) : Parent(i) { }
   123       ConstMapIt(Invalid i) : Parent(i) { }
   124 
   124 
   125       explicit ConstMapIt(Map& _map) : map(_map) {
   125       explicit ConstMapIt(Map& _map) : map(_map) {
   126         map.notifier()->first(*this);
   126         map.notifier()->first(*this);
   127       }
   127       }
   128 
   128 
   129       ConstMapIt(const Map& _map, const Item& item) 
   129       ConstMapIt(const Map& _map, const Item& item)
   130 	: Parent(item), map(_map) {}
   130         : Parent(item), map(_map) {}
   131 
   131 
   132       ConstMapIt& operator++() { 
   132       ConstMapIt& operator++() {
   133 	map.notifier()->next(*this);
   133         map.notifier()->next(*this);
   134 	return *this; 
   134         return *this;
   135       }
   135       }
   136 
   136 
   137       typename MapTraits<Map>::ConstReturnValue operator*() const {
   137       typename MapTraits<Map>::ConstReturnValue operator*() const {
   138 	return map[*this];
   138         return map[*this];
   139       }
   139       }
   140 
   140 
   141     protected:
   141     protected:
   142       const Map& map;
   142       const Map& map;
   143     };
   143     };
   144 
   144 
   145     class ItemIt : public Item {
   145     class ItemIt : public Item {
   146     public:
   146     public:
   147       
   147 
   148       typedef Item Parent;
   148       typedef Item Parent;
   149       
   149 
   150       ItemIt() {}
   150       ItemIt() {}
   151 
   151 
   152       ItemIt(Invalid i) : Parent(i) { }
   152       ItemIt(Invalid i) : Parent(i) { }
   153 
   153 
   154       explicit ItemIt(Map& _map) : map(_map) {
   154       explicit ItemIt(Map& _map) : map(_map) {
   155         map.notifier()->first(*this);
   155         map.notifier()->first(*this);
   156       }
   156       }
   157 
   157 
   158       ItemIt(const Map& _map, const Item& item) 
   158       ItemIt(const Map& _map, const Item& item)
   159 	: Parent(item), map(_map) {}
   159         : Parent(item), map(_map) {}
   160 
   160 
   161       ItemIt& operator++() { 
   161       ItemIt& operator++() {
   162 	map.notifier()->next(*this);
   162         map.notifier()->next(*this);
   163 	return *this; 
   163         return *this;
   164       }
   164       }
   165 
   165 
   166     protected:
   166     protected:
   167       const Map& map;
   167       const Map& map;
   168       
   168 
   169     };
   169     };
   170   };
   170   };
   171 
   171 
   172   /// \ingroup graphbits
   172   /// \ingroup graphbits
   173   /// 
   173   ///
   174   /// \brief Extender for maps which use a subset of the items.
   174   /// \brief Extender for maps which use a subset of the items.
   175   template <typename _Graph, typename _Map>
   175   template <typename _Graph, typename _Map>
   176   class SubMapExtender : public _Map {
   176   class SubMapExtender : public _Map {
   177   public:
   177   public:
   178 
   178 
   192     friend class MapIt;
   192     friend class MapIt;
   193     friend class ConstMapIt;
   193     friend class ConstMapIt;
   194 
   194 
   195   public:
   195   public:
   196 
   196 
   197     SubMapExtender(const Graph& _graph) 
   197     SubMapExtender(const Graph& _graph)
   198       : Parent(_graph), graph(_graph) {}
   198       : Parent(_graph), graph(_graph) {}
   199 
   199 
   200     SubMapExtender(const Graph& _graph, const Value& _value) 
   200     SubMapExtender(const Graph& _graph, const Value& _value)
   201       : Parent(_graph, _value), graph(_graph) {}
   201       : Parent(_graph, _value), graph(_graph) {}
   202 
   202 
   203     SubMapExtender& operator=(const SubMapExtender& cmap) {
   203     SubMapExtender& operator=(const SubMapExtender& cmap) {
   204       return operator=<MapExtender>(cmap);
   204       return operator=<MapExtender>(cmap);
   205     }
   205     }
   210       Item it;
   210       Item it;
   211       for (graph.first(it); it != INVALID; graph.next(it)) {
   211       for (graph.first(it); it != INVALID; graph.next(it)) {
   212         Parent::set(it, cmap[it]);
   212         Parent::set(it, cmap[it]);
   213       }
   213       }
   214       return *this;
   214       return *this;
   215     } 
   215     }
   216 
   216 
   217     class MapIt : public Item {
   217     class MapIt : public Item {
   218     public:
   218     public:
   219       
   219 
   220       typedef Item Parent;
   220       typedef Item Parent;
   221       typedef typename Map::Value Value;
   221       typedef typename Map::Value Value;
   222       
   222 
   223       MapIt() {}
   223       MapIt() {}
   224 
   224 
   225       MapIt(Invalid i) : Parent(i) { }
   225       MapIt(Invalid i) : Parent(i) { }
   226 
   226 
   227       explicit MapIt(Map& _map) : map(_map) {
   227       explicit MapIt(Map& _map) : map(_map) {
   228         map.graph.first(*this);
   228         map.graph.first(*this);
   229       }
   229       }
   230 
   230 
   231       MapIt(const Map& _map, const Item& item) 
   231       MapIt(const Map& _map, const Item& item)
   232 	: Parent(item), map(_map) {}
   232         : Parent(item), map(_map) {}
   233 
   233 
   234       MapIt& operator++() { 
   234       MapIt& operator++() {
   235 	map.graph.next(*this);
   235         map.graph.next(*this);
   236 	return *this; 
   236         return *this;
   237       }
   237       }
   238       
   238 
   239       typename MapTraits<Map>::ConstReturnValue operator*() const {
   239       typename MapTraits<Map>::ConstReturnValue operator*() const {
   240 	return map[*this];
   240         return map[*this];
   241       }
   241       }
   242 
   242 
   243       typename MapTraits<Map>::ReturnValue operator*() {
   243       typename MapTraits<Map>::ReturnValue operator*() {
   244 	return map[*this];
   244         return map[*this];
   245       }
   245       }
   246       
   246 
   247       void set(const Value& value) {
   247       void set(const Value& value) {
   248 	map.set(*this, value);
   248         map.set(*this, value);
   249       }
   249       }
   250       
   250 
   251     protected:
   251     protected:
   252       Map& map;
   252       Map& map;
   253       
   253 
   254     };
   254     };
   255 
   255 
   256     class ConstMapIt : public Item {
   256     class ConstMapIt : public Item {
   257     public:
   257     public:
   258 
   258 
   259       typedef Item Parent;
   259       typedef Item Parent;
   260 
   260 
   261       typedef typename Map::Value Value;
   261       typedef typename Map::Value Value;
   262       
   262 
   263       ConstMapIt() {}
   263       ConstMapIt() {}
   264 
   264 
   265       ConstMapIt(Invalid i) : Parent(i) { }
   265       ConstMapIt(Invalid i) : Parent(i) { }
   266 
   266 
   267       explicit ConstMapIt(Map& _map) : map(_map) {
   267       explicit ConstMapIt(Map& _map) : map(_map) {
   268         map.graph.first(*this);
   268         map.graph.first(*this);
   269       }
   269       }
   270 
   270 
   271       ConstMapIt(const Map& _map, const Item& item) 
   271       ConstMapIt(const Map& _map, const Item& item)
   272 	: Parent(item), map(_map) {}
   272         : Parent(item), map(_map) {}
   273 
   273 
   274       ConstMapIt& operator++() { 
   274       ConstMapIt& operator++() {
   275 	map.graph.next(*this);
   275         map.graph.next(*this);
   276 	return *this; 
   276         return *this;
   277       }
   277       }
   278 
   278 
   279       typename MapTraits<Map>::ConstReturnValue operator*() const {
   279       typename MapTraits<Map>::ConstReturnValue operator*() const {
   280 	return map[*this];
   280         return map[*this];
   281       }
   281       }
   282 
   282 
   283     protected:
   283     protected:
   284       const Map& map;
   284       const Map& map;
   285     };
   285     };
   286 
   286 
   287     class ItemIt : public Item {
   287     class ItemIt : public Item {
   288     public:
   288     public:
   289       
   289 
   290       typedef Item Parent;
   290       typedef Item Parent;
   291       
   291 
   292       ItemIt() {}
   292       ItemIt() {}
   293 
   293 
   294       ItemIt(Invalid i) : Parent(i) { }
   294       ItemIt(Invalid i) : Parent(i) { }
   295 
   295 
   296       explicit ItemIt(Map& _map) : map(_map) {
   296       explicit ItemIt(Map& _map) : map(_map) {
   297         map.graph.first(*this);
   297         map.graph.first(*this);
   298       }
   298       }
   299 
   299 
   300       ItemIt(const Map& _map, const Item& item) 
   300       ItemIt(const Map& _map, const Item& item)
   301 	: Parent(item), map(_map) {}
   301         : Parent(item), map(_map) {}
   302 
   302 
   303       ItemIt& operator++() { 
   303       ItemIt& operator++() {
   304 	map.graph.next(*this);
   304         map.graph.next(*this);
   305 	return *this; 
   305         return *this;
   306       }
   306       }
   307 
   307 
   308     protected:
   308     protected:
   309       const Map& map;
   309       const Map& map;
   310       
   310 
   311     };
   311     };
   312     
   312 
   313   private:
   313   private:
   314 
   314 
   315     const Graph& graph;
   315     const Graph& graph;
   316     
   316 
   317   };
   317   };
   318 
   318 
   319 }
   319 }
   320 
   320 
   321 #endif
   321 #endif