lemon/map_iterator.h
changeset 1810 474d093466a5
child 1875 98698b69a902
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/map_iterator.h	Wed Nov 16 18:58:10 2005 +0000
     1.3 @@ -0,0 +1,145 @@
     1.4 +/* -*- C++ -*-
     1.5 + * lemon/map_iterator.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef LEMON_MAP_ITERATOR_H
    1.21 +#define LEMON_MAP_ITERATOR_H
    1.22 +
    1.23 +#include <lemon/traits.h>
    1.24 +#include <lemon/utility.h>
    1.25 +
    1.26 +/// \ingroup gutils
    1.27 +/// \file
    1.28 +/// \brief Iterators on the maps.
    1.29 +
    1.30 +namespace lemon {
    1.31 +
    1.32 +  /// \ingroup gutils
    1.33 +  ///
    1.34 +  /// \brief Iterator for maps with possibility of changing values.
    1.35 +  ///
    1.36 +  /// Iterator for maps with possibility of changing values.
    1.37 +  template <typename Graph, typename Item, typename Map>
    1.38 +  class MapIt : public ItemSetTraits<Graph, Item>::ItemIt {
    1.39 +  public:
    1.40 +      
    1.41 +    typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
    1.42 +    
    1.43 +    typedef typename Map::Value Value;
    1.44 +    
    1.45 +    /// \brief Creates an iterator
    1.46 +    ///
    1.47 +    /// Creates an iterator for the map, which iterates on the
    1.48 +    /// given graph item set.
    1.49 +    MapIt(const Graph& _graph, Map& _map) : Parent(_graph), map(_map) {}
    1.50 +
    1.51 +    /// \brief Gives back the map's value on the current position.
    1.52 +    ///
    1.53 +    /// Gives back the map's value on the current position.
    1.54 +    typename MapTraits<Map>::ConstReturnValue operator*() const {
    1.55 +      return map[*this];
    1.56 +    }
    1.57 +
    1.58 +    /// \brief Gives back a reference to the map's value.
    1.59 +    ///
    1.60 +    /// Gives back a reference to the map's value on the current position.
    1.61 +    typename MapTraits<Map>::ReturnValue operator*() {
    1.62 +      return map[*this];
    1.63 +    }
    1.64 +    
    1.65 +    /// \brief Sets the value on the current position
    1.66 +    ///
    1.67 +    /// Sets the value on the current position.
    1.68 +    void set(const Value& value) {
    1.69 +      map.set(*this, value);
    1.70 +    }
    1.71 +    
    1.72 +  protected:
    1.73 +    Map& map;
    1.74 +      
    1.75 +  };
    1.76 +
    1.77 +  /// \ingroup gutils
    1.78 +  ///
    1.79 +  /// \brief Iterator for maps with possibility of getting values.
    1.80 +  ///
    1.81 +  /// Iterator for maps with possibility of getting values.
    1.82 +  template <typename Graph, typename Item, typename Map>
    1.83 +  class ConstMapIt : public ItemSetTraits<Graph, Item>::ItemIt {
    1.84 +  public:
    1.85 +    
    1.86 +    typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
    1.87 +
    1.88 +    typedef typename Map::Value Value;
    1.89 +    
    1.90 +    /// \brief Creates an iterator
    1.91 +    ///
    1.92 +    /// Creates an iterator for the map, which iterates on the
    1.93 +    /// given graph item set.
    1.94 +    ConstMapIt(const Graph& _graph, const Map& _map) 
    1.95 +      : Parent(_graph), map(_map) {}
    1.96 +    
    1.97 +    /// \brief Gives back the map's value on the current position.
    1.98 +    ///
    1.99 +    /// Gives back the map's value on the current position.
   1.100 +    typename MapTraits<Map>::ConstReturnValue operator*() const {
   1.101 +      return map[*this];
   1.102 +    }
   1.103 +    
   1.104 +  protected:
   1.105 +    const Map& map;
   1.106 +  };
   1.107 +
   1.108 +
   1.109 +  /// \ingroup gutils
   1.110 +  ///
   1.111 +  /// \brief Iterator for maps which filters items by the values.
   1.112 +  ///
   1.113 +  /// Iterator for maps which gives back only that items which mapped
   1.114 +  /// to an given value.
   1.115 +  template <typename Graph, typename Item, typename Map>
   1.116 +  class FilterMapIt 
   1.117 +    : public ItemSetTraits<Graph, Item>::ItemIt {
   1.118 +  public:
   1.119 +    
   1.120 +    typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
   1.121 +
   1.122 +    typedef typename Map::Value Value;
   1.123 +    
   1.124 +    /// \brief Creates an iterator
   1.125 +    ///
   1.126 +    /// Creates an iterator for the map, which iterates on the
   1.127 +    /// given graph item set and filters all items which mapped value
   1.128 +    /// differ from \c _value.
   1.129 +    FilterMapIt(const Graph& _graph, const Map& _map, const Value& _value) 
   1.130 +      : Parent(_graph), map(_map), value(_value) {}
   1.131 +    
   1.132 +    /// \brief Increment operator
   1.133 +    ///
   1.134 +    /// Skips items which has not the given value.
   1.135 +    FilterMapIt& operator++() {
   1.136 +      Parent::operator++();
   1.137 +      while (*this != INVALID && map[*this] != value) Parent::operator++();
   1.138 +    }
   1.139 +    
   1.140 +  protected:
   1.141 +    const Map& map;
   1.142 +    Value value;
   1.143 +  };
   1.144 +
   1.145 +  
   1.146 +}
   1.147 +
   1.148 +#endif