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