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