Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

map_iterator.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * lemon/map_iterator.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Research Group on Combinatorial Optimization, EGRES).
00006  *
00007  * Permission to use, modify and distribute this software is granted
00008  * provided that this copyright notice appears in all copies. For
00009  * precise terms see the accompanying LICENSE file.
00010  *
00011  * This software is provided "AS IS" with no warranty of any kind,
00012  * express or implied, and with no claim as to its suitability for any
00013  * purpose.
00014  *
00015  */
00016 
00017 #ifndef LEMON_MAP_ITERATOR_H
00018 #define LEMON_MAP_ITERATOR_H
00019 
00020 #include <iterator>
00021 
00022 #include <lemon/bits/extended_pair.h>
00023 #include <lemon/graph_utils.h>
00024 
00028 
00029 namespace lemon {
00030 
00033 
00039   template <
00040     typename _Graph,
00041     typename _Item>
00042   class MapIteratorBase {
00043 
00044   protected:
00045 
00047     typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt;
00048 
00049     ItemIt it;
00050 
00052     MapIteratorBase() {}
00053 
00055     MapIteratorBase(const ItemIt _it) : it(_it) {}
00056 
00057   public:
00058 
00060     void increment() { 
00061       ++it; 
00062     }
00063 
00065     bool operator==(const MapIteratorBase& _it) const {
00066       return _it.it == it;
00067     }
00068         
00070     bool operator!=(const MapIteratorBase& _it) const {
00071       return !(*this == _it);
00072     }
00073   };
00074 
00075 
00076   template <
00077     typename _Graph,
00078     typename _Item,
00079     typename _Map>
00080   class MapConstIterator;
00081 
00085   template <
00086     typename _Graph,
00087     typename _Item,
00088     typename _Map>
00089   class MapIterator : public MapIteratorBase<_Graph, _Item> {
00090 
00091     friend class MapConstIterator<_Graph, _Item, _Map>;
00092 
00093 
00094   public:
00095 
00097     typedef MapIteratorBase<_Graph, _Item> Parent;
00098 
00099     typedef _Item Item;
00100     typedef _Map Map;
00101     typedef _Graph Graph;
00102 
00103   protected:
00104 
00105     typedef typename Parent::ItemIt ItemIt;
00106 
00107     typedef typename ReferenceMapTraits<_Map>::Value MapValue;
00108     typedef typename ReferenceMapTraits<_Map>::Reference MapReference;
00109     
00110   public:
00111 
00113     typedef extended_pair<Item, const Item&,
00114       MapValue, const MapValue&> Value;
00115 
00117     typedef extended_pair<const Item&, const Item&, 
00118       MapReference, MapReference> Reference;
00119 
00121     MapIterator() {}
00122 
00125     MapIterator(Map& _map, const ItemIt& _it) 
00126       : Parent(_it), map(&_map) {}
00127 
00129     Reference operator*() {
00130       return Reference(Parent::it, (*map)[Parent::it]);
00131     }
00132 
00134     class Pointer {
00135       friend class MapIterator;
00136     protected:
00137       Reference data;
00138       Pointer(const Item& item, MapReference val) 
00139         : data(item, val) {}
00140     public:
00141       Reference* operator->() {return &data;}
00142     };
00143 
00145     Pointer operator->() {
00146       return Pointer(Parent::it, (*map)[Parent::it]); 
00147     }
00148         
00150     MapIterator& operator++() { 
00151       Parent::increment(); 
00152       return *this; 
00153     }
00154 
00156     MapIterator operator++(int) { 
00157       MapIterator tmp(*this); 
00158       Parent::increment(); 
00159       return tmp; 
00160     }
00161 
00162   protected:
00163 
00164     Map* map;
00165 
00166   public:
00167     // STL  compatibility typedefs.
00168     typedef std::forward_iterator_tag iterator_category;
00169     typedef int difference_type;
00170     typedef Value value_type;
00171     typedef Reference reference;
00172     typedef Pointer pointer;
00173   };
00174 
00178   template <
00179     typename _Graph,
00180     typename _Item,
00181     typename _Map>
00182   class MapConstIterator : public MapIteratorBase<_Graph, _Item> {
00183 
00184   public:
00185 
00187     typedef MapIteratorBase<_Graph, _Item> Parent;
00188 
00189     typedef _Graph Graph;
00190     typedef _Item Item;
00191     typedef _Map Map;
00192 
00193   protected:
00194 
00195     typedef typename Parent::ItemIt ItemIt;
00196 
00197     typedef typename ReferenceMapTraits<_Map>::Value MapValue;
00198     typedef typename ReferenceMapTraits<_Map>::ConstReference
00199     MapReference;
00200     
00201   public:
00202 
00204     typedef extended_pair<Item, const Item&,
00205       MapValue, const MapValue&> Value;
00206 
00208     typedef extended_pair<const Item&, const Item&, 
00209       MapReference, MapReference> Reference;
00210 
00212     MapConstIterator() {}
00213 
00216     MapConstIterator(const Map& _map, const ItemIt& _it) 
00217       : Parent(_it), map(&_map) {}
00218 
00220     Reference operator*() {
00221       return Reference(Parent::it, (*map)[Parent::it]);
00222     }
00223 
00225     class Pointer {
00226       friend class MapConstIterator;
00227     protected:
00228       Reference data;
00229       Pointer(const Item& item, MapReference val) 
00230         : data(item, val) {}
00231     public:
00232       Reference* operator->() {return &data;}
00233     };
00234 
00236     Pointer operator->() {
00237       return Pointer(Parent::it, ((*map)[Parent::it])); 
00238     }
00239         
00241     MapConstIterator& operator++() { 
00242       Parent::increment(); 
00243       return *this; 
00244     }
00245 
00247     MapConstIterator operator++(int) { 
00248       MapConstIterator tmp(*this); 
00249       Parent::increment(); 
00250       return tmp; 
00251     }
00252 
00253   protected:
00254     const Map* map;
00255 
00256   public:
00257     // STL  compatibility typedefs.
00258     typedef std::forward_iterator_tag iterator_category;
00259     typedef int difference_type;
00260     typedef Value value_type;
00261     typedef Reference reference;
00262     typedef Pointer pointer;
00263   };
00264  
00268   template <
00269     typename _Graph,
00270     typename _Item>
00271   class MapConstKeyIterator : public MapIteratorBase<_Graph, _Item> {
00272 
00273   public:
00274 
00276     typedef MapIteratorBase<_Graph, _Item> Parent;
00277  
00278     typedef _Graph Graph;
00279     typedef _Item Item;
00280 
00281   protected:
00283     typedef typename Parent::ItemIt ItemIt;
00284 
00285   public:
00286 
00287     typedef Item Value;
00288     typedef const Item& Reference;
00289     typedef const Item* Pointer;
00290 
00292     MapConstKeyIterator() {}
00293 
00295     MapConstKeyIterator(const ItemIt& pit) : Parent(pit) {}
00296 
00298     MapConstKeyIterator& operator++() {
00299       Parent::increment(); 
00300       return *this; 
00301     }
00302 
00304     MapConstKeyIterator operator++(int) {
00305       MapConstKeyIterator tmp(*this);
00306       Parent::increment();
00307       return tmp;
00308     }
00309 
00311     Item operator*() const {
00312       return static_cast<Item>(Parent::it);
00313     }
00314 
00315   public:
00316     // STL  compatibility typedefs.
00317     typedef std::input_iterator_tag iterator_category;
00318     typedef int difference_type;
00319     typedef Value value_type;
00320     typedef Reference reference;
00321     typedef Pointer pointer;
00322   };
00323 
00324   template <
00325     typename _Graph, 
00326     typename _Item,
00327     typename _Map>
00328   class MapConstValueIterator;
00329 
00333   template <
00334     typename _Graph,
00335     typename _Item,
00336     typename _Map>
00337   class MapValueIterator : public MapIteratorBase<_Graph, _Item> {
00338 
00339     friend class MapConstValueIterator<_Graph, _Item, _Map>;
00340 
00341   public:
00342 
00344     typedef MapIteratorBase<_Graph, _Item> Parent;
00345 
00346     typedef _Graph Graph;
00347     typedef _Item Item;
00348     typedef _Map Map;
00349 
00350   protected:
00351 
00353     typedef typename Parent::ItemIt ItemIt;
00354 
00356     typedef typename ReferenceMapTraits<Map>::Value MapValue;
00358     typedef typename ReferenceMapTraits<Map>::Reference MapReference;
00360     typedef typename ReferenceMapTraits<Map>::Pointer MapPointer;
00361 
00362   public:
00363 
00364     typedef MapValue Value;
00365     typedef MapReference Reference;
00366     typedef MapPointer Pointer;
00367 
00369     MapValueIterator() {}
00370 
00372     MapValueIterator(Map& _map, const ItemIt& _it) 
00373       : Parent(_it), map(&_map) {}
00374     
00375 
00377     MapValueIterator& operator++() {
00378       Parent::increment(); 
00379       return *this; 
00380     }
00381 
00383     MapValueIterator operator++(int) {
00384       MapValueIterator tmp(*this);
00385       Parent::increment();
00386       return tmp;
00387     }
00388 
00390     Reference operator*() const {
00391       return (*map)[Parent::it];
00392     }
00393 
00395     Pointer operator->() const {
00396       return &(operator*());
00397     }
00398 
00399   protected:
00400 
00401     Map* map;
00402 
00403   public:
00404     // STL  compatibility typedefs.
00405     typedef std::forward_iterator_tag iterator_category;
00406     typedef int difference_type;
00407     typedef Value value_type;
00408     typedef Reference reference;
00409     typedef Pointer pointer;
00410   };
00411 
00415   template <
00416     typename _Graph,
00417     typename _Item,
00418     typename _Map>
00419   class MapConstValueIterator : public MapIteratorBase<_Graph, _Item> {
00420 
00421   public:
00422 
00424     typedef MapIteratorBase<_Graph, _Item> Parent;
00425 
00426     typedef _Graph Graph;
00427     typedef _Item Item;
00428     typedef _Map Map;
00429 
00430   protected:
00431 
00433     typedef typename Parent::ItemIt ItemIt;
00434 
00436     typedef typename ReferenceMapTraits<Map>::Value MapValue;
00438     typedef typename ReferenceMapTraits<Map>::ConstReference MapReference;
00440     typedef typename ReferenceMapTraits<Map>::ConstPointer MapPointer;
00441 
00442   public:
00443 
00444     typedef MapValue Value;
00445     typedef MapReference Reference;
00446     typedef MapPointer Pointer;
00447 
00449     MapConstValueIterator() {}
00450 
00452     MapConstValueIterator(const Map& _map, const ItemIt& _it) 
00453       : Parent(_it), map(&_map) {}
00454     
00455 
00457     MapConstValueIterator& operator++() {
00458       Parent::increment(); 
00459       return *this; 
00460     }
00461 
00463     MapConstValueIterator operator++(int) {
00464       MapConstValueIterator tmp(*this);
00465       Parent::increment();
00466       return tmp;
00467     }
00468 
00470     Reference operator*() const {
00471       return (*map)[Parent::it];
00472     }
00473 
00475     Pointer operator->() const {
00476       return &(operator*());
00477     }
00478 
00479   protected:
00480 
00481     const Map* map;
00482 
00483   public:
00484     // STL  compatibility typedefs.
00485     typedef std::forward_iterator_tag iterator_category;
00486     typedef int difference_type;
00487     typedef Value value_type;
00488     typedef Reference reference;
00489     typedef Pointer pointer;
00490   };
00491 
00492 
00496   template <typename _Graph, typename _Item>
00497   class MapConstKeySet {
00498 
00499   public:
00500 
00501     typedef _Graph Graph;
00503     typedef _Item Item;
00505 
00506   protected:
00507 
00508     typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt;
00509 
00510   public:
00511 
00513     MapConstKeySet(const Graph& _graph) : graph(&_graph) {}
00514 
00516     typedef MapConstKeyIterator<_Graph, _Item> ConstIterator;
00517 
00518     typedef typename ConstIterator::Value Value;
00520     typedef typename ConstIterator::Reference ConstReference;
00522     typedef typename ConstIterator::Pointer ConstPointer;
00523 
00525     ConstIterator begin() const {
00526       return ConstIterator(ItemIt(*graph));
00527     }
00528             
00530     ConstIterator end() const {
00531       return ConstIterator(ItemIt(INVALID));
00532     }
00533 
00534   protected:
00535 
00536     const Graph* graph;
00537  
00538   public:
00539     // STL  compatibility typedefs.
00540     typedef Value value_type;
00541     typedef ConstIterator const_iterator;
00542     typedef ConstReference const_reference;
00543     typedef ConstPointer const_pointer;
00544     typedef int difference_type;
00545   };
00546 
00551   template <typename _Graph, typename _Item, typename _Map>
00552   class MapConstValueSet {
00553 
00554   public:
00555     
00556     typedef _Graph Graph;
00557     typedef _Item Item;
00558     typedef _Map Map;
00559 
00560   protected:
00561 
00563     typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
00564 
00565   public:
00566 
00568     MapConstValueSet(const Graph& _graph, const Map& _map) 
00569       : graph(&_graph), map(&_map) {}
00570 
00572     typedef MapConstValueIterator<_Graph, _Item, _Map> ConstIterator;
00573 
00574     typedef typename ConstIterator::Value Value;
00575     typedef typename ConstIterator::Reference ConstReference;
00576     typedef typename ConstIterator::Pointer ConstPointer;
00577 
00579     ConstIterator begin() const {
00580       return ConstIterator(*map, ItemIt(*graph));
00581     }
00582 
00584     ConstIterator end() const {
00585       return ConstIterator(*map, ItemIt(INVALID));
00586     }
00587 
00588   protected:
00589     
00590     const Map* map;
00591     const Graph * graph;
00592 
00593   public:
00594     // STL  compatibility typedefs.
00595     typedef Value value_type;
00596     typedef ConstIterator const_iterator;
00597     typedef ConstReference const_reference;
00598     typedef ConstPointer const_pointer;
00599     typedef int difference_type;
00600   };
00601 
00602 
00607   template <typename _Graph, typename _Item, typename _Map>
00608   class MapValueSet {
00609 
00610   public:
00611     
00612     typedef _Graph Graph;
00613     typedef _Item Item;
00614     typedef _Map Map;
00615 
00616   protected:
00617 
00619     typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
00620 
00621   public:
00622 
00624     MapValueSet(const Graph& _graph, Map& _map) 
00625       : map(&_map), graph(&_graph) {}
00626 
00628     typedef MapValueIterator<_Graph, _Item, _Map> Iterator;
00630     typedef MapConstValueIterator<_Graph, _Item, _Map> ConstIterator;
00631 
00632     typedef typename ConstIterator::Value Value;
00633     typedef typename Iterator::Reference Reference;
00634     typedef typename Iterator::Pointer Pointer;
00635     typedef typename ConstIterator::Reference ConstReference;
00636     typedef typename ConstIterator::Pointer ConstPointer;
00637 
00639     ConstIterator begin() const {
00640       return ConstIterator(*map, ItemIt(*graph));
00641     }
00642 
00644     ConstIterator end() const {
00645       return ConstIterator(*map, ItemIt(INVALID));
00646     }
00647 
00649     Iterator begin() {
00650       return Iterator(*map, ItemIt(*graph));
00651     }
00652 
00654     Iterator end() {
00655       return Iterator(*map, ItemIt(INVALID));
00656     }
00657 
00658   protected:
00659     
00660     Map* map;
00661     const Graph * graph;
00662 
00663   public:
00664     // STL  compatibility typedefs.
00665     typedef Value value_type;
00666     typedef Iterator iterator;
00667     typedef ConstIterator const_iterator;
00668     typedef Reference reference;
00669     typedef ConstReference const_reference;
00670     typedef Pointer pointer;
00671     typedef ConstPointer const_pointer;
00672     typedef int difference_type;
00673 
00674   };
00675 
00680   template <
00681     typename _Graph, 
00682     typename _Item,
00683     typename _Map
00684     >
00685   class MapSet {
00686   public:    
00687 
00688     typedef _Graph Graph;
00689     typedef _Item Item;
00690     typedef _Map Map;
00691 
00692   protected:
00693 
00694     typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt;
00695 
00696   public:
00697 
00699     MapSet(const Graph& _graph, Map& _map) : graph(&_graph), map(&_map) {}
00700 
00702     typedef MapIterator<_Graph, _Item, _Map> Iterator;
00703     typedef MapConstIterator<_Graph, _Item, _Map> ConstIterator;
00704 
00705     typedef typename ConstIterator::Value Value;
00706     typedef typename Iterator::Reference Reference;
00707     typedef typename Iterator::Pointer Pointer;
00708     typedef typename ConstIterator::Reference ConstReference;
00709     typedef typename ConstIterator::Pointer ConstPointer;
00710 
00711 
00713     ConstIterator begin() const {
00714       return ConstIterator(*map, ItemIt(*graph));
00715     }
00716 
00718     ConstIterator end() const {
00719       return ConstIterator(*map, ItemIt(INVALID));
00720     }
00721 
00723 
00725     Iterator begin() {
00726       return Iterator(*map, ItemIt(*graph));
00727     }
00728 
00730     Iterator end() {
00731       return Iterator(*map, ItemIt(INVALID));
00732     }
00733 
00734   protected:
00735     
00736     const Graph* graph;
00737     Map* map;
00738             
00739   public:
00740     // STL  compatibility typedefs.
00741     typedef Value value_type;
00742     typedef Iterator iterator;
00743     typedef ConstIterator const_iterator;
00744     typedef Reference reference;
00745     typedef ConstReference const_reference;
00746     typedef Pointer pointer;
00747     typedef ConstPointer const_pointer;
00748     typedef int difference_type;
00749 
00750   };
00751 
00752   template <
00753     typename _Graph, 
00754     typename _Item,
00755     typename _Map
00756     >
00757   class ConstMapSet {
00758     
00759     typedef _Graph Graph;
00760     typedef _Map Map;
00761 
00762     const Graph* graph;
00763     const Map* map;
00764 
00765   public:
00766 
00767     typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt;
00768 
00769 
00771     ConstMapSet(const Graph& _graph, const Map& _map) 
00772       : graph(&_graph), map(&_map) {}
00773 
00775     typedef MapConstIterator<_Graph, _Item, _Map> ConstIterator;
00776 
00777     typedef typename ConstIterator::Value Value;
00778     typedef typename ConstIterator::Reference ConstReference;
00779     typedef typename ConstIterator::Pointer ConstPointer;
00780 
00781 
00783     ConstIterator begin() const {
00784       return ConstIterator(*map, ItemIt(*graph));
00785     }
00786 
00788     ConstIterator end() const {
00789       return ConstIterator(*map, ItemIt(INVALID));
00790     }
00791             
00792   public:
00793     // STL  compatibility typedefs.
00794     typedef Value value_type;
00795     typedef ConstIterator const_iterator;
00796     typedef ConstReference const_reference;
00797     typedef ConstPointer const_pointer;
00798     typedef int difference_type;
00799 
00800   };
00801 
00802   template <typename _Map>
00803   class IterableMapExtender : public _Map {
00804   public:
00805 
00806     typedef _Map Parent;
00807     typedef Parent Map;
00808     typedef typename Map::Graph Graph;
00809     typedef typename Map::Key Item;
00810     typedef typename Map::Value Value;
00811 
00812     typedef MapSet<Graph, Item, Map> MapSet;
00813 
00814     IterableMapExtender() : Parent() {}
00815 
00816     IterableMapExtender(const Graph& graph) : Parent(graph) {}
00817 
00818     IterableMapExtender(const Graph& graph, const Value& value) 
00819       : Parent(graph, value) {}
00820 
00821     MapSet mapSet() { 
00822       return MapSet(*Parent::getGraph(), *this); 
00823     }
00824 
00825     typedef ConstMapSet<Graph, Item, Map> ConstMapSet;
00826 
00827     ConstMapSet mapSet() const { 
00828       return ConstMapSet(*Parent::getGraph(), *this); 
00829     }
00830 
00831     typedef MapConstKeySet<Graph, Item> ConstKeySet;
00832  
00833     ConstKeySet keySet() const {
00834       return ConstKeySet(*Parent::getGraph());
00835     }
00836 
00837     typedef MapValueSet<Graph, Item, Map> ValueSet;
00838  
00839     ValueSet valueSet() {
00840       return ValueSet(*Parent::getGraph(), *this);
00841     }
00842 
00843     typedef MapConstValueSet<Graph, Item, Map> ConstValueSet;
00844  
00845     ConstValueSet valueSet() const {
00846       return ConstValueSet(*Parent::getGraph(), *this);
00847     }
00848 
00849   };
00850 
00852 
00853 }
00854 
00855 #endif

Generated on Sat Aug 27 14:14:50 2005 for LEMON by  doxygen 1.4.4