lemon/map_iterator.h
author hegyi
Mon, 09 Jan 2006 12:41:06 +0000
changeset 1887 22fdc00894aa
parent 1810 474d093466a5
child 1956 a055123339d5
permissions -rw-r--r--
The tree that is created for evaluation of expression string at new map creation is deleted after usage.
     1 /* -*- C++ -*-
     2  * lemon/map_iterator.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_MAP_ITERATOR_H
    18 #define LEMON_MAP_ITERATOR_H
    19 
    20 #include <lemon/traits.h>
    21 #include <lemon/utility.h>
    22 
    23 /// \ingroup gutils
    24 /// \file
    25 /// \brief Iterators on the maps.
    26 
    27 namespace lemon {
    28 
    29   /// \ingroup gutils
    30   ///
    31   /// \brief Iterator for maps with possibility of changing values.
    32   ///
    33   /// Iterator for maps with possibility of changing values.
    34   template <typename Graph, typename Item, typename Map>
    35   class MapIt : public ItemSetTraits<Graph, Item>::ItemIt {
    36   public:
    37       
    38     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
    39     
    40     typedef typename Map::Value Value;
    41     
    42     /// \brief Creates an iterator
    43     ///
    44     /// Creates an iterator for the map, which iterates on the
    45     /// given graph item set.
    46     MapIt(const Graph& _graph, Map& _map) : Parent(_graph), map(_map) {}
    47 
    48     /// \brief Gives back the map's value on the current position.
    49     ///
    50     /// Gives back the map's value on the current position.
    51     typename MapTraits<Map>::ConstReturnValue operator*() const {
    52       return map[*this];
    53     }
    54 
    55     /// \brief Gives back a reference to the map's value.
    56     ///
    57     /// Gives back a reference to the map's value on the current position.
    58     typename MapTraits<Map>::ReturnValue operator*() {
    59       return map[*this];
    60     }
    61     
    62     /// \brief Sets the value on the current position
    63     ///
    64     /// Sets the value on the current position.
    65     void set(const Value& value) {
    66       map.set(*this, value);
    67     }
    68     
    69   protected:
    70     Map& map;
    71       
    72   };
    73 
    74   /// \ingroup gutils
    75   ///
    76   /// \brief Iterator for maps with possibility of getting values.
    77   ///
    78   /// Iterator for maps with possibility of getting values.
    79   template <typename Graph, typename Item, typename Map>
    80   class ConstMapIt : public ItemSetTraits<Graph, Item>::ItemIt {
    81   public:
    82     
    83     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
    84 
    85     typedef typename Map::Value Value;
    86     
    87     /// \brief Creates an iterator
    88     ///
    89     /// Creates an iterator for the map, which iterates on the
    90     /// given graph item set.
    91     ConstMapIt(const Graph& _graph, const Map& _map) 
    92       : Parent(_graph), map(_map) {}
    93     
    94     /// \brief Gives back the map's value on the current position.
    95     ///
    96     /// Gives back the map's value on the current position.
    97     typename MapTraits<Map>::ConstReturnValue operator*() const {
    98       return map[*this];
    99     }
   100     
   101   protected:
   102     const Map& map;
   103   };
   104 
   105 
   106   /// \ingroup gutils
   107   ///
   108   /// \brief Iterator for maps which filters items by the values.
   109   ///
   110   /// Iterator for maps which gives back only that items which mapped
   111   /// to an given value.
   112   template <typename Graph, typename Item, typename Map>
   113   class FilterMapIt 
   114     : public ItemSetTraits<Graph, Item>::ItemIt {
   115   public:
   116     
   117     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
   118 
   119     typedef typename Map::Value Value;
   120     
   121     /// \brief Creates an iterator
   122     ///
   123     /// Creates an iterator for the map, which iterates on the
   124     /// given graph item set and filters all items which mapped value
   125     /// differ from \c _value.
   126     FilterMapIt(const Graph& _graph, const Map& _map, const Value& _value) 
   127       : Parent(_graph), map(_map), value(_value) {}
   128     
   129     /// \brief Increment operator
   130     ///
   131     /// Skips items which has not the given value.
   132     FilterMapIt& operator++() {
   133       Parent::operator++();
   134       while (*this != INVALID && map[*this] != value) Parent::operator++();
   135     }
   136     
   137   protected:
   138     const Map& map;
   139     Value value;
   140   };
   141 
   142   
   143 }
   144 
   145 #endif