deba@1810: /* -*- C++ -*-
deba@1810:  *
alpar@1956:  * This file is a part of LEMON, a generic C++ optimization library
alpar@1956:  *
alpar@1956:  * Copyright (C) 2003-2006
alpar@1956:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1810:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1810:  *
deba@1810:  * Permission to use, modify and distribute this software is granted
deba@1810:  * provided that this copyright notice appears in all copies. For
deba@1810:  * precise terms see the accompanying LICENSE file.
deba@1810:  *
deba@1810:  * This software is provided "AS IS" with no warranty of any kind,
deba@1810:  * express or implied, and with no claim as to its suitability for any
deba@1810:  * purpose.
deba@1810:  *
deba@1810:  */
deba@1810: 
deba@1810: #ifndef LEMON_MAP_ITERATOR_H
deba@1810: #define LEMON_MAP_ITERATOR_H
deba@1810: 
deba@1993: #include <lemon/bits/traits.h>
deba@1993: #include <lemon/bits/utility.h>
deba@1810: 
deba@1810: /// \ingroup gutils
deba@1810: /// \file
deba@1810: /// \brief Iterators on the maps.
deba@1810: 
deba@1810: namespace lemon {
deba@1810: 
deba@1810:   /// \ingroup gutils
deba@1810:   ///
deba@1810:   /// \brief Iterator for maps with possibility of changing values.
deba@1810:   ///
deba@1810:   /// Iterator for maps with possibility of changing values.
deba@1810:   template <typename Graph, typename Item, typename Map>
deba@1810:   class MapIt : public ItemSetTraits<Graph, Item>::ItemIt {
deba@1810:   public:
deba@1810:       
deba@1810:     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
deba@1810:     
deba@1810:     typedef typename Map::Value Value;
deba@1810:     
deba@1810:     /// \brief Creates an iterator
deba@1810:     ///
deba@1810:     /// Creates an iterator for the map, which iterates on the
deba@1810:     /// given graph item set.
deba@1810:     MapIt(const Graph& _graph, Map& _map) : Parent(_graph), map(_map) {}
deba@1810: 
deba@1810:     /// \brief Gives back the map's value on the current position.
deba@1810:     ///
deba@1810:     /// Gives back the map's value on the current position.
deba@1810:     typename MapTraits<Map>::ConstReturnValue operator*() const {
deba@1810:       return map[*this];
deba@1810:     }
deba@1810: 
deba@1810:     /// \brief Gives back a reference to the map's value.
deba@1810:     ///
deba@1810:     /// Gives back a reference to the map's value on the current position.
deba@1810:     typename MapTraits<Map>::ReturnValue operator*() {
deba@1810:       return map[*this];
deba@1810:     }
deba@1810:     
deba@1810:     /// \brief Sets the value on the current position
deba@1810:     ///
deba@1810:     /// Sets the value on the current position.
deba@1810:     void set(const Value& value) {
deba@1810:       map.set(*this, value);
deba@1810:     }
deba@1810:     
deba@1810:   protected:
deba@1810:     Map& map;
deba@1810:       
deba@1810:   };
deba@1810: 
deba@1810:   /// \ingroup gutils
deba@1810:   ///
deba@1810:   /// \brief Iterator for maps with possibility of getting values.
deba@1810:   ///
deba@1810:   /// Iterator for maps with possibility of getting values.
deba@1810:   template <typename Graph, typename Item, typename Map>
deba@1810:   class ConstMapIt : public ItemSetTraits<Graph, Item>::ItemIt {
deba@1810:   public:
deba@1810:     
deba@1810:     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
deba@1810: 
deba@1810:     typedef typename Map::Value Value;
deba@1810:     
deba@1810:     /// \brief Creates an iterator
deba@1810:     ///
deba@1810:     /// Creates an iterator for the map, which iterates on the
deba@1810:     /// given graph item set.
deba@1810:     ConstMapIt(const Graph& _graph, const Map& _map) 
deba@1810:       : Parent(_graph), map(_map) {}
deba@1810:     
deba@1810:     /// \brief Gives back the map's value on the current position.
deba@1810:     ///
deba@1810:     /// Gives back the map's value on the current position.
deba@1810:     typename MapTraits<Map>::ConstReturnValue operator*() const {
deba@1810:       return map[*this];
deba@1810:     }
deba@1810:     
deba@1810:   protected:
deba@1810:     const Map& map;
deba@1810:   };
deba@1810: 
deba@1810: 
deba@1810:   /// \ingroup gutils
deba@1810:   ///
deba@1810:   /// \brief Iterator for maps which filters items by the values.
deba@1810:   ///
deba@1810:   /// Iterator for maps which gives back only that items which mapped
deba@1810:   /// to an given value.
deba@1810:   template <typename Graph, typename Item, typename Map>
deba@1810:   class FilterMapIt 
deba@1810:     : public ItemSetTraits<Graph, Item>::ItemIt {
deba@1810:   public:
deba@1810:     
deba@1810:     typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
deba@1810: 
deba@1810:     typedef typename Map::Value Value;
deba@1810:     
deba@1810:     /// \brief Creates an iterator
deba@1810:     ///
deba@1810:     /// Creates an iterator for the map, which iterates on the
deba@1810:     /// given graph item set and filters all items which mapped value
deba@1810:     /// differ from \c _value.
deba@1810:     FilterMapIt(const Graph& _graph, const Map& _map, const Value& _value) 
deba@1810:       : Parent(_graph), map(_map), value(_value) {}
deba@1810:     
deba@1810:     /// \brief Increment operator
deba@1810:     ///
deba@1810:     /// Skips items which has not the given value.
deba@1810:     FilterMapIt& operator++() {
deba@1810:       Parent::operator++();
deba@1810:       while (*this != INVALID && map[*this] != value) Parent::operator++();
deba@1810:     }
deba@1810:     
deba@1810:   protected:
deba@1810:     const Map& map;
deba@1810:     Value value;
deba@1810:   };
deba@1810: 
deba@1810:   
deba@1810: }
deba@1810: 
deba@1810: #endif