lemon/map_iterator.h
author deba
Thu, 20 Mar 2008 16:25:47 +0000
changeset 2596 9c00e972cdfd
parent 2391 14a343be7a5a
permissions -rw-r--r--
Back porting commit 81563e019fa4
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     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_MAP_ITERATOR_H
    20 #define LEMON_MAP_ITERATOR_H
    21 
    22 #include <lemon/bits/traits.h>
    23 #include <lemon/bits/utility.h>
    24 
    25 /// \ingroup gutils
    26 /// \file
    27 /// \brief Iterators on the maps.
    28 
    29 namespace lemon {
    30 
    31   /// \ingroup gutils
    32   ///
    33   /// \brief Iterator for maps with possibility of changing values.
    34   ///
    35   /// Iterator for maps with possibility of changing values.
    36   template <typename Graph, typename Item, typename Map>
    37   class MapIt : public ItemSetTraits<Graph, Item>::ItemIt {
    38   public:
    39       
    40     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
    41     
    42     typedef typename Map::Value Value;
    43     
    44     /// \brief Creates an iterator
    45     ///
    46     /// Creates an iterator for the map, which iterates on the
    47     /// given graph item set.
    48     MapIt(const Graph& _graph, Map& _map) : Parent(_graph), map(_map) {}
    49 
    50     /// \brief Gives back the map's value on the current position.
    51     ///
    52     /// Gives back the map's value on the current position.
    53     typename MapTraits<Map>::ConstReturnValue operator*() const {
    54       return map[*this];
    55     }
    56 
    57     /// \brief Gives back a reference to the map's value.
    58     ///
    59     /// Gives back a reference to the map's value on the current position.
    60     typename MapTraits<Map>::ReturnValue operator*() {
    61       return map[*this];
    62     }
    63     
    64     /// \brief Sets the value on the current position
    65     ///
    66     /// Sets the value on the current position.
    67     void set(const Value& value) {
    68       map.set(*this, value);
    69     }
    70     
    71   protected:
    72     Map& map;
    73       
    74   };
    75 
    76   /// \ingroup gutils
    77   ///
    78   /// \brief Iterator for maps with possibility of getting values.
    79   ///
    80   /// Iterator for maps with possibility of getting values.
    81   template <typename Graph, typename Item, typename Map>
    82   class ConstMapIt : public ItemSetTraits<Graph, Item>::ItemIt {
    83   public:
    84     
    85     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
    86 
    87     typedef typename Map::Value Value;
    88     
    89     /// \brief Creates an iterator
    90     ///
    91     /// Creates an iterator for the map, which iterates on the
    92     /// given graph item set.
    93     ConstMapIt(const Graph& _graph, const Map& _map) 
    94       : Parent(_graph), map(_map) {}
    95     
    96     /// \brief Gives back the map's value on the current position.
    97     ///
    98     /// Gives back the map's value on the current position.
    99     typename MapTraits<Map>::ConstReturnValue operator*() const {
   100       return map[*this];
   101     }
   102     
   103   protected:
   104     const Map& map;
   105   };
   106 
   107 
   108   /// \ingroup gutils
   109   ///
   110   /// \brief Iterator for maps which filters items by the values.
   111   ///
   112   /// Iterator for maps which gives back only that items which mapped
   113   /// to an given value.
   114   template <typename Graph, typename Item, typename Map>
   115   class FilterMapIt 
   116     : public ItemSetTraits<Graph, Item>::ItemIt {
   117   public:
   118     
   119     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
   120 
   121     typedef typename Map::Value Value;
   122     
   123     /// \brief Creates an iterator
   124     ///
   125     /// Creates an iterator for the map, which iterates on the
   126     /// given graph item set and filters all items which mapped value
   127     /// is not equal to the \c _value.
   128     FilterMapIt(const Graph& _graph, const Map& _map, const Value& _value) 
   129       : Parent(_graph), map(_map), value(_value) {}
   130     
   131     /// \brief Increment operator
   132     ///
   133     /// Skips items which has not mapped to the given value.
   134     FilterMapIt& operator++() {
   135       Parent::operator++();
   136       while (*this != INVALID && map[*this] != value) Parent::operator++();
   137     }
   138     
   139   protected:
   140     const Map& map;
   141     Value value;
   142   };
   143 
   144   
   145 }
   146 
   147 #endif