lemon/bits/map_extender.h
changeset 2212 0ad3835449f8
parent 1999 2ff283124dfc
child 2260 4274224f8a7d
equal deleted inserted replaced
7:dfae5242794c 8:ca59b5ebf0a5
    20 #define LEMON_BITS_MAP_EXTENDER_H
    20 #define LEMON_BITS_MAP_EXTENDER_H
    21 
    21 
    22 #include <iterator>
    22 #include <iterator>
    23 
    23 
    24 #include <lemon/bits/traits.h>
    24 #include <lemon/bits/traits.h>
       
    25 
       
    26 #include <lemon/concept_check.h>
       
    27 #include <lemon/concept/maps.h>
    25 
    28 
    26 ///\file
    29 ///\file
    27 ///\brief Extenders for iterable maps.
    30 ///\brief Extenders for iterable maps.
    28 
    31 
    29 namespace lemon {
    32 namespace lemon {
    57       : Parent(graph) {}
    60       : Parent(graph) {}
    58 
    61 
    59     MapExtender(const Graph& graph, const Value& value) 
    62     MapExtender(const Graph& graph, const Value& value) 
    60       : Parent(graph, value) {}
    63       : Parent(graph, value) {}
    61 
    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;
       
    73     } 
    62 
    74 
    63     class MapIt : public Item {
    75     class MapIt : public Item {
    64     public:
    76     public:
    65       
    77       
    66       typedef Item Parent;
    78       typedef Item Parent;
   128 
   140 
   129     protected:
   141     protected:
   130       const Map& map;
   142       const Map& map;
   131     };
   143     };
   132 
   144 
   133     class ItemIt : Item {
   145     class ItemIt : public Item {
   134     public:
   146     public:
   135       
   147       
   136       typedef Item Parent;
   148       typedef Item Parent;
   137       
   149       
   138       ItemIt() {}
   150       ItemIt() {}
   139 
   151 
   140       ItemIt(Invalid i) : Parent(i) { }
   152       ItemIt(Invalid i) : Parent(i) { }
   141 
   153 
   142       explicit ItemIt(Map& _map) : map(_map) {
   154       explicit ItemIt(Map& _map) : map(_map) {
   143         map->getNotifier()->first(*this);
   155         map.getNotifier()->first(*this);
   144       }
   156       }
   145 
   157 
   146       ItemIt(const Map& _map, const Item& item) 
   158       ItemIt(const Map& _map, const Item& item) 
   147 	: Parent(item), map(_map) {}
   159 	: Parent(item), map(_map) {}
   148 
   160 
   155       const Map& map;
   167       const Map& map;
   156       
   168       
   157     };
   169     };
   158   };
   170   };
   159 
   171 
       
   172   /// \ingroup graphbits
       
   173   /// 
       
   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 
       
   197     SubMapExtender(const Graph& _graph) 
       
   198       : Parent(_graph), graph(_graph) {}
       
   199 
       
   200     SubMapExtender(const Graph& _graph, const Value& _value) 
       
   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<concept::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;
       
   215     } 
       
   216 
       
   217     class MapIt : public Item {
       
   218     public:
       
   219       
       
   220       typedef Item Parent;
       
   221       typedef typename Map::Value Value;
       
   222       
       
   223       MapIt() {}
       
   224 
       
   225       MapIt(Invalid i) : Parent(i) { }
       
   226 
       
   227       explicit MapIt(Map& _map) : map(_map) {
       
   228         map.graph.first(*this);
       
   229       }
       
   230 
       
   231       MapIt(const Map& _map, const Item& item) 
       
   232 	: Parent(item), map(_map) {}
       
   233 
       
   234       MapIt& operator++() { 
       
   235 	map.graph.next(*this);
       
   236 	return *this; 
       
   237       }
       
   238       
       
   239       typename MapTraits<Map>::ConstReturnValue operator*() const {
       
   240 	return map[*this];
       
   241       }
       
   242 
       
   243       typename MapTraits<Map>::ReturnValue operator*() {
       
   244 	return map[*this];
       
   245       }
       
   246       
       
   247       void set(const Value& value) {
       
   248 	map.set(*this, value);
       
   249       }
       
   250       
       
   251     protected:
       
   252       Map& map;
       
   253       
       
   254     };
       
   255 
       
   256     class ConstMapIt : public Item {
       
   257     public:
       
   258 
       
   259       typedef Item Parent;
       
   260 
       
   261       typedef typename Map::Value Value;
       
   262       
       
   263       ConstMapIt() {}
       
   264 
       
   265       ConstMapIt(Invalid i) : Parent(i) { }
       
   266 
       
   267       explicit ConstMapIt(Map& _map) : map(_map) {
       
   268         map.graph.first(*this);
       
   269       }
       
   270 
       
   271       ConstMapIt(const Map& _map, const Item& item) 
       
   272 	: Parent(item), map(_map) {}
       
   273 
       
   274       ConstMapIt& operator++() { 
       
   275 	map.graph.next(*this);
       
   276 	return *this; 
       
   277       }
       
   278 
       
   279       typename MapTraits<Map>::ConstReturnValue operator*() const {
       
   280 	return map[*this];
       
   281       }
       
   282 
       
   283     protected:
       
   284       const Map& map;
       
   285     };
       
   286 
       
   287     class ItemIt : public Item {
       
   288     public:
       
   289       
       
   290       typedef Item Parent;
       
   291       
       
   292       ItemIt() {}
       
   293 
       
   294       ItemIt(Invalid i) : Parent(i) { }
       
   295 
       
   296       explicit ItemIt(Map& _map) : map(_map) {
       
   297         map.graph.first(*this);
       
   298       }
       
   299 
       
   300       ItemIt(const Map& _map, const Item& item) 
       
   301 	: Parent(item), map(_map) {}
       
   302 
       
   303       ItemIt& operator++() { 
       
   304 	map.graph.next(*this);
       
   305 	return *this; 
       
   306       }
       
   307 
       
   308     protected:
       
   309       const Map& map;
       
   310       
       
   311     };
       
   312     
       
   313   private:
       
   314 
       
   315     const Graph& graph;
       
   316     
       
   317   };
       
   318 
   160 }
   319 }
   161 
   320 
   162 #endif
   321 #endif