# HG changeset patch # User deba # Date 1132167490 0 # Node ID 474d093466a578befdeb76fd7918ed7cfa760194 # Parent 029cc4f638d1f8f5bb106659bb22d6eddae3c799 Modified iterators on graph maps Other iterators for not graph maps diff -r 029cc4f638d1 -r 474d093466a5 lemon/bits/array_map.h --- a/lemon/bits/array_map.h Wed Nov 16 14:46:22 2005 +0000 +++ b/lemon/bits/array_map.h Wed Nov 16 18:58:10 2005 +0000 @@ -18,7 +18,7 @@ #define LEMON_ARRAY_MAP_H #include -#include +#include #include #include diff -r 029cc4f638d1 -r 474d093466a5 lemon/bits/map_extender.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/bits/map_extender.h Wed Nov 16 18:58:10 2005 +0000 @@ -0,0 +1,115 @@ +/* -*- C++ -*- + * lemon/map_extender.h - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_BITS_MAP_EXTENDER_H +#define LEMON_BITS_MAP_EXTENDER_H + +#include + +#include + +///\file +///\brief Extenders for iterable maps. + +namespace lemon { + + template + class IterableMapExtender : public _Map { + public: + + typedef _Map Parent; + typedef IterableMapExtender Map; + + + typedef typename Map::Graph Graph; + typedef typename Map::Key Item; + + typedef typename Map::Key Key; + typedef typename Map::Value Value; + + class MapIt; + class ConstMapIt; + + friend class MapIt; + friend class ConstMapIt; + + protected: + + using Parent::getGraph; + + public: + + IterableMapExtender(const Graph& graph) : Parent(graph) {} + + IterableMapExtender(const Graph& graph, const Value& value) + : Parent(graph, value) {} + + + class MapIt : public ItemSetTraits::ItemIt { + public: + + typedef typename ItemSetTraits::ItemIt Parent; + + typedef typename Map::Value Value; + + MapIt(Map& _map) : Parent(*_map.getGraph()), map(_map) {} + + typename MapTraits::ConstReturnValue operator*() const { + return map[*this]; + } + + typename MapTraits::ReturnValue operator*() { + return map[*this]; + } + + void set(const Value& value) { + map.set(*this, value); + } + + protected: + Map& map; + + }; + + class ConstMapIt : public ItemSetTraits::ItemIt { + public: + + typedef typename ItemSetTraits::ItemIt Parent; + + typedef typename Map::Value Value; + + ConstMapIt(const Map& _map) : Parent(*_map.getGraph()), map(_map) {} + + typename MapTraits::ConstReturnValue operator*() const { + return map[*this]; + } + protected: + const Map& map; + }; + + class ItemIt : public ItemSetTraits::ItemIt { + public: + + typedef typename ItemSetTraits::ItemIt Parent; + + ItemIt(Map& _map) : Parent(*_map.getGraph()) {} + + }; + }; + +} + +#endif diff -r 029cc4f638d1 -r 474d093466a5 lemon/bits/map_iterator.h --- a/lemon/bits/map_iterator.h Wed Nov 16 14:46:22 2005 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,854 +0,0 @@ -/* -*- C++ -*- - * lemon/map_iterator.h - Part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Research Group on Combinatorial Optimization, EGRES). - * - * Permission to use, modify and distribute this software is granted - * provided that this copyright notice appears in all copies. For - * precise terms see the accompanying LICENSE file. - * - * This software is provided "AS IS" with no warranty of any kind, - * express or implied, and with no claim as to its suitability for any - * purpose. - * - */ - -#ifndef LEMON_MAP_ITERATOR_H -#define LEMON_MAP_ITERATOR_H - -#include - -#include -#include - -///\ingroup graphmapfactory -///\file -///\brief Iterators on the maps. - -namespace lemon { - - /// \addtogroup graphmapfactory - /// @{ - - /** The base class all of the map iterators. - * The class defines the typedefs of the iterators, - * simple step functions and equality operators. - */ - - template < - typename _Graph, - typename _Item> - class MapIteratorBase { - - protected: - - /// The key type of the iterator. - typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt; - - ItemIt it; - - /// Default constructor. - MapIteratorBase() {} - - /// ItemIt initialized MapIteratorBase constructor. - MapIteratorBase(const ItemIt _it) : it(_it) {} - - public: - - /// Stepping forward in the map. - void increment() { - ++it; - } - - /// The equality operator of the map. - bool operator==(const MapIteratorBase& _it) const { - return _it.it == it; - } - - /// The not-equality operator of the map. - bool operator!=(const MapIteratorBase& _it) const { - return !(*this == _it); - } - }; - - - template < - typename _Graph, - typename _Item, - typename _Map> - class MapConstIterator; - - /** Compatible iterator with the stl maps' iterators. - * It iterates on pairs of a key and a value. - */ - template < - typename _Graph, - typename _Item, - typename _Map> - class MapIterator : public MapIteratorBase<_Graph, _Item> { - - friend class MapConstIterator<_Graph, _Item, _Map>; - - - public: - - /// The iterator base class. - typedef MapIteratorBase<_Graph, _Item> Parent; - - typedef _Item Item; - typedef _Map Map; - typedef _Graph Graph; - - protected: - - typedef typename Parent::ItemIt ItemIt; - - typedef typename _Map::Value MapValue; - typedef typename _Map::Reference MapReference; - - public: - - /// The value type of the iterator. - typedef extended_pair Value; - - /// The reference type of the iterator. - typedef extended_pair Reference; - - /// Default constructor. - MapIterator() {} - - /// Constructor to initalize the iterators returned - /// by the begin() and end(). - MapIterator(Map& _map, const ItemIt& _it) - : Parent(_it), map(&_map) {} - - /// Dereference operator for the iterator. - Reference operator*() { - return Reference(Parent::it, (*map)[Parent::it]); - } - - /// The pointer type of the iterator. - class Pointer { - friend class MapIterator; - protected: - Reference data; - Pointer(const Item& item, MapReference val) - : data(item, val) {} - public: - Reference* operator->() {return &data;} - }; - - /// Arrow operator for the iterator. - Pointer operator->() { - return Pointer(Parent::it, (*map)[Parent::it]); - } - - /// The pre increment operator of the iterator. - MapIterator& operator++() { - Parent::increment(); - return *this; - } - - /// The post increment operator of the iterator. - MapIterator operator++(int) { - MapIterator tmp(*this); - Parent::increment(); - return tmp; - } - - protected: - - Map* map; - - public: - // STL compatibility typedefs. - typedef std::forward_iterator_tag iterator_category; - typedef int difference_type; - typedef Value value_type; - typedef Reference reference; - typedef Pointer pointer; - }; - - /** Compatible iterator with the stl maps' iterators. - * It iterates on pairs of a key and a value. - */ - template < - typename _Graph, - typename _Item, - typename _Map> - class MapConstIterator : public MapIteratorBase<_Graph, _Item> { - - public: - - /// The iterator base class. - typedef MapIteratorBase<_Graph, _Item> Parent; - - typedef _Graph Graph; - typedef _Item Item; - typedef _Map Map; - - protected: - - typedef typename Parent::ItemIt ItemIt; - - typedef typename _Map::Value MapValue; - typedef typename _Map::ConstReference MapReference; - - public: - - /// The value type of the iterator. - typedef extended_pair Value; - - /// The reference type of the iterator. - typedef extended_pair Reference; - - /// Default constructor. - MapConstIterator() {} - - /// Constructor to initalize the iterators returned - /// by the begin() and end(). - MapConstIterator(const Map& _map, const ItemIt& _it) - : Parent(_it), map(&_map) {} - - /// Dereference operator for the iterator. - Reference operator*() { - return Reference(Parent::it, (*map)[Parent::it]); - } - - /// The pointer type of the iterator. - class Pointer { - friend class MapConstIterator; - protected: - Reference data; - Pointer(const Item& item, MapReference val) - : data(item, val) {} - public: - Reference* operator->() {return &data;} - }; - - /// Arrow operator for the iterator. - Pointer operator->() { - return Pointer(Parent::it, ((*map)[Parent::it])); - } - - /// The pre increment operator of the iterator. - MapConstIterator& operator++() { - Parent::increment(); - return *this; - } - - /// The post increment operator of the iterator. - MapConstIterator operator++(int) { - MapConstIterator tmp(*this); - Parent::increment(); - return tmp; - } - - protected: - const Map* map; - - public: - // STL compatibility typedefs. - typedef std::forward_iterator_tag iterator_category; - typedef int difference_type; - typedef Value value_type; - typedef Reference reference; - typedef Pointer pointer; - }; - - /** The class makes the ItemIt to an stl compatible iterator - * with dereferencing operator. - */ - template < - typename _Graph, - typename _Item> - class MapConstKeyIterator : public MapIteratorBase<_Graph, _Item> { - - public: - - /// The iterator base class. - typedef MapIteratorBase<_Graph, _Item> Parent; - - typedef _Graph Graph; - typedef _Item Item; - - protected: - /// The iterator to iterate on the keys. - typedef typename Parent::ItemIt ItemIt; - - public: - - typedef Item Value; - typedef const Item& Reference; - typedef const Item* Pointer; - - /// Default constructor. - MapConstKeyIterator() {} - - /// ItemIt initialized iterator. - MapConstKeyIterator(const ItemIt& pit) : Parent(pit) {} - - /// The pre increment operator of the iterator. - MapConstKeyIterator& operator++() { - Parent::increment(); - return *this; - } - - /// The post increment operator of the iterator. - MapConstKeyIterator operator++(int) { - MapConstKeyIterator tmp(*this); - Parent::increment(); - return tmp; - } - - /// The dereferencing operator of the iterator. - Item operator*() const { - return static_cast(Parent::it); - } - - public: - // STL compatibility typedefs. - typedef std::input_iterator_tag iterator_category; - typedef int difference_type; - typedef Value value_type; - typedef Reference reference; - typedef Pointer pointer; - }; - - template < - typename _Graph, - typename _Item, - typename _Map> - class MapConstValueIterator; - - /** MapValueIterator creates an stl compatible iterator - * for the values. - */ - template < - typename _Graph, - typename _Item, - typename _Map> - class MapValueIterator : public MapIteratorBase<_Graph, _Item> { - - friend class MapConstValueIterator<_Graph, _Item, _Map>; - - public: - - /// The iterator base class. - typedef MapIteratorBase<_Graph, _Item> Parent; - - typedef _Graph Graph; - typedef _Item Item; - typedef _Map Map; - - protected: - - /// The iterator to iterate on the keys. - typedef typename Parent::ItemIt ItemIt; - - /// The value type of the iterator. - typedef typename Map::Value MapValue; - /// The reference type of the iterator. - typedef typename Map::Reference MapReference; - /// The pointer type of the iterator. - typedef typename Map::Pointer MapPointer; - - public: - - typedef MapValue Value; - typedef MapReference Reference; - typedef MapPointer Pointer; - - /// Default constructor. - MapValueIterator() {} - - /// Map and ItemIt initialized iterator. - MapValueIterator(Map& _map, const ItemIt& _it) - : Parent(_it), map(&_map) {} - - - /// The pre increment operator of the iterator. - MapValueIterator& operator++() { - Parent::increment(); - return *this; - } - - /// The post increment operator of the iterator. - MapValueIterator operator++(int) { - MapValueIterator tmp(*this); - Parent::increment(); - return tmp; - } - - /// The dereferencing operator of the iterator. - Reference operator*() const { - return (*map)[Parent::it]; - } - - /// The arrow operator of the iterator. - Pointer operator->() const { - return &(operator*()); - } - - protected: - - Map* map; - - public: - // STL compatibility typedefs. - typedef std::forward_iterator_tag iterator_category; - typedef int difference_type; - typedef Value value_type; - typedef Reference reference; - typedef Pointer pointer; - }; - - /** MapValueIterator creates an stl compatible iterator - * for the values. - */ - template < - typename _Graph, - typename _Item, - typename _Map> - class MapConstValueIterator : public MapIteratorBase<_Graph, _Item> { - - public: - - /// The iterator base class. - typedef MapIteratorBase<_Graph, _Item> Parent; - - typedef _Graph Graph; - typedef _Item Item; - typedef _Map Map; - - protected: - - /// The iterator to iterate on the keys. - typedef typename Parent::ItemIt ItemIt; - - /// The value type of the iterator. - typedef typename Map::Value MapValue; - /// The reference type of the iterator. - typedef typename Map::ConstReference MapReference; - /// The pointer type of the iterator. - typedef typename Map::ConstPointer MapPointer; - - public: - - typedef MapValue Value; - typedef MapReference Reference; - typedef MapPointer Pointer; - - /// Default constructor. - MapConstValueIterator() {} - - /// Map and ItemIt initialized iterator. - MapConstValueIterator(const Map& _map, const ItemIt& _it) - : Parent(_it), map(&_map) {} - - - /// The pre increment operator of the iterator. - MapConstValueIterator& operator++() { - Parent::increment(); - return *this; - } - - /// The post increment operator of the iterator. - MapConstValueIterator operator++(int) { - MapConstValueIterator tmp(*this); - Parent::increment(); - return tmp; - } - - /// The dereferencing operator of the iterator. - Reference operator*() const { - return (*map)[Parent::it]; - } - - /// The arrow operator of the iterator. - Pointer operator->() const { - return &(operator*()); - } - - protected: - - const Map* map; - - public: - // STL compatibility typedefs. - typedef std::forward_iterator_tag iterator_category; - typedef int difference_type; - typedef Value value_type; - typedef Reference reference; - typedef Pointer pointer; - }; - - - /** This class makes from a map an iteratable set - * which contains all the keys of the map. - */ - template - class MapConstKeySet { - - public: - - typedef _Graph Graph; - /// The key type of the iterator. - typedef _Item Item; - /// The iterator to iterate on the keys. - - protected: - - typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt; - - public: - - /// The map initialized const key set. - MapConstKeySet(const Graph& _graph) : graph(&_graph) {} - - /// The const iterator of the set. - typedef MapConstKeyIterator<_Graph, _Item> ConstIterator; - - typedef typename ConstIterator::Value Value; - /// The reference type of the iterator. - typedef typename ConstIterator::Reference ConstReference; - /// The pointer type of the iterator. - typedef typename ConstIterator::Pointer ConstPointer; - - /// It gives back the const iterator pointed to the first element. - ConstIterator begin() const { - return ConstIterator(ItemIt(*graph)); - } - - /// It gives back the const iterator pointed to the first ivalid element. - ConstIterator end() const { - return ConstIterator(ItemIt(INVALID)); - } - - protected: - - const Graph* graph; - - public: - // STL compatibility typedefs. - typedef Value value_type; - typedef ConstIterator const_iterator; - typedef ConstReference const_reference; - typedef ConstPointer const_pointer; - typedef int difference_type; - }; - - /** This class makes from a map an iteratable set - * which contains all the values of the map. - * The values cannot be modified. - */ - template - class MapConstValueSet { - - public: - - typedef _Graph Graph; - typedef _Item Item; - typedef _Map Map; - - protected: - - /// The iterator to iterate on the keys. - typedef typename ItemSetTraits::ItemIt ItemIt; - - public: - - /// The map initialized const value set. - MapConstValueSet(const Graph& _graph, const Map& _map) - : graph(&_graph), map(&_map) {} - - /// The const iterator of the set. - typedef MapConstValueIterator<_Graph, _Item, _Map> ConstIterator; - - typedef typename ConstIterator::Value Value; - typedef typename ConstIterator::Reference ConstReference; - typedef typename ConstIterator::Pointer ConstPointer; - - /// It gives back the const iterator pointed to the first element. - ConstIterator begin() const { - return ConstIterator(*map, ItemIt(*graph)); - } - - /// It gives back the const iterator pointed to the first invalid element. - ConstIterator end() const { - return ConstIterator(*map, ItemIt(INVALID)); - } - - protected: - - const Map* map; - const Graph * graph; - - public: - // STL compatibility typedefs. - typedef Value value_type; - typedef ConstIterator const_iterator; - typedef ConstReference const_reference; - typedef ConstPointer const_pointer; - typedef int difference_type; - }; - - - /** This class makes from a map an iteratable set - * which contains all the values of the map. - * The values can be modified. - */ - template - class MapValueSet { - - public: - - typedef _Graph Graph; - typedef _Item Item; - typedef _Map Map; - - protected: - - /// The iterator to iterate on the keys. - typedef typename ItemSetTraits::ItemIt ItemIt; - - public: - - /// The map initialized const value set. - MapValueSet(const Graph& _graph, Map& _map) - : map(&_map), graph(&_graph) {} - - /// The const iterator of the set. - typedef MapValueIterator<_Graph, _Item, _Map> Iterator; - /// The const iterator of the set. - typedef MapConstValueIterator<_Graph, _Item, _Map> ConstIterator; - - typedef typename ConstIterator::Value Value; - typedef typename Iterator::Reference Reference; - typedef typename Iterator::Pointer Pointer; - typedef typename ConstIterator::Reference ConstReference; - typedef typename ConstIterator::Pointer ConstPointer; - - /// It gives back the const iterator pointed to the first element. - ConstIterator begin() const { - return ConstIterator(*map, ItemIt(*graph)); - } - - /// It gives back the const iterator pointed to the first invalid element. - ConstIterator end() const { - return ConstIterator(*map, ItemIt(INVALID)); - } - - /// It gives back the iterator pointed to the first element. - Iterator begin() { - return Iterator(*map, ItemIt(*graph)); - } - - /// It gives back the iterator pointed to the first invalid element. - Iterator end() { - return Iterator(*map, ItemIt(INVALID)); - } - - protected: - - Map* map; - const Graph * graph; - - public: - // STL compatibility typedefs. - typedef Value value_type; - typedef Iterator iterator; - typedef ConstIterator const_iterator; - typedef Reference reference; - typedef ConstReference const_reference; - typedef Pointer pointer; - typedef ConstPointer const_pointer; - typedef int difference_type; - - }; - - /** This class makes from a map an iteratable set - * which contains all the values of the map. - * The values can be modified. - */ - template < - typename _Graph, - typename _Item, - typename _Map - > - class MapSet { - public: - - typedef _Graph Graph; - typedef _Item Item; - typedef _Map Map; - - protected: - - typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt; - - public: - - /// The map initialized value set. - MapSet(const Graph& _graph, Map& _map) : graph(&_graph), map(&_map) {} - - /// The const iterator of the set. - typedef MapIterator<_Graph, _Item, _Map> Iterator; - typedef MapConstIterator<_Graph, _Item, _Map> ConstIterator; - - typedef typename ConstIterator::Value Value; - typedef typename Iterator::Reference Reference; - typedef typename Iterator::Pointer Pointer; - typedef typename ConstIterator::Reference ConstReference; - typedef typename ConstIterator::Pointer ConstPointer; - - - /// It gives back the const iterator pointed to the first element. - ConstIterator begin() const { - return ConstIterator(*map, ItemIt(*graph)); - } - - /// It gives back the const iterator pointed to the first invalid element. - ConstIterator end() const { - return ConstIterator(*map, ItemIt(INVALID)); - } - - /// The iterator of the set. - - /// It gives back the iterator pointed to the first element. - Iterator begin() { - return Iterator(*map, ItemIt(*graph)); - } - - /// It gives back the iterator pointed to the first invalid element. - Iterator end() { - return Iterator(*map, ItemIt(INVALID)); - } - - protected: - - const Graph* graph; - Map* map; - - public: - // STL compatibility typedefs. - typedef Value value_type; - typedef Iterator iterator; - typedef ConstIterator const_iterator; - typedef Reference reference; - typedef ConstReference const_reference; - typedef Pointer pointer; - typedef ConstPointer const_pointer; - typedef int difference_type; - - }; - - template < - typename _Graph, - typename _Item, - typename _Map - > - class ConstMapSet { - - typedef _Graph Graph; - typedef _Map Map; - - const Graph* graph; - const Map* map; - - public: - - typedef typename ItemSetTraits<_Graph, _Item>::ItemIt ItemIt; - - - /// The map initialized value set. - ConstMapSet(const Graph& _graph, const Map& _map) - : graph(&_graph), map(&_map) {} - - /// The const iterator of the set. - typedef MapConstIterator<_Graph, _Item, _Map> ConstIterator; - - typedef typename ConstIterator::Value Value; - typedef typename ConstIterator::Reference ConstReference; - typedef typename ConstIterator::Pointer ConstPointer; - - - /// It gives back the const iterator pointed to the first element. - ConstIterator begin() const { - return ConstIterator(*map, ItemIt(*graph)); - } - - /// It gives back the const iterator pointed to the first invalid element. - ConstIterator end() const { - return ConstIterator(*map, ItemIt(INVALID)); - } - - public: - // STL compatibility typedefs. - typedef Value value_type; - typedef ConstIterator const_iterator; - typedef ConstReference const_reference; - typedef ConstPointer const_pointer; - typedef int difference_type; - - }; - - template - class IterableMapExtender : public _Map { - public: - - typedef _Map Parent; - typedef Parent Map; - typedef typename Map::Graph Graph; - typedef typename Map::Key Item; - typedef typename Map::Value Value; - - typedef MapSet MapSet; - - IterableMapExtender() : Parent() {} - - IterableMapExtender(const Graph& graph) : Parent(graph) {} - - IterableMapExtender(const Graph& graph, const Value& value) - : Parent(graph, value) {} - - MapSet mapSet() { - return MapSet(*Parent::getGraph(), *this); - } - - typedef ConstMapSet ConstMapSet; - - ConstMapSet mapSet() const { - return ConstMapSet(*Parent::getGraph(), *this); - } - - typedef MapConstKeySet ConstKeySet; - - ConstKeySet keySet() const { - return ConstKeySet(*Parent::getGraph()); - } - - typedef MapValueSet ValueSet; - - ValueSet valueSet() { - return ValueSet(*Parent::getGraph(), *this); - } - - typedef MapConstValueSet ConstValueSet; - - ConstValueSet valueSet() const { - return ConstValueSet(*Parent::getGraph(), *this); - } - - }; - - /// @} - -} - -#endif diff -r 029cc4f638d1 -r 474d093466a5 lemon/bits/static_map.h --- a/lemon/bits/static_map.h Wed Nov 16 14:46:22 2005 +0000 +++ b/lemon/bits/static_map.h Wed Nov 16 18:58:10 2005 +0000 @@ -21,7 +21,7 @@ #include #include -#include +#include #include #include #include diff -r 029cc4f638d1 -r 474d093466a5 lemon/bits/vector_map.h --- a/lemon/bits/vector_map.h Wed Nov 16 14:46:22 2005 +0000 +++ b/lemon/bits/vector_map.h Wed Nov 16 18:58:10 2005 +0000 @@ -21,7 +21,7 @@ #include #include -#include +#include #include #include #include diff -r 029cc4f638d1 -r 474d093466a5 lemon/iterable_maps.h --- a/lemon/iterable_maps.h Wed Nov 16 14:46:22 2005 +0000 +++ b/lemon/iterable_maps.h Wed Nov 16 18:58:10 2005 +0000 @@ -136,11 +136,9 @@ explicit IterableBoolMap(BaseMap &_m,bool init=false) : cref(_m) { sep=0; - for(typename BaseMap::MapSet::iterator i=cref.mapSet().begin(); - i!=cref.mapSet().end(); - ++i) { - i->second=sep; - vals.push_back(i->first); + for(typename BaseMap::MapIt i(cref);i!=INVALID; ++i) { + i.set(sep); + vals.push_back(i); sep++; } if(init) sep=0; @@ -303,7 +301,8 @@ namespace _iterable_maps_bits { template struct IterableIntMapNode { - IterableIntMapNode() : value(-1) {} + IterableIntMapNode() {} + IterableIntMapNode(int _value) : value(_value) {} Item prev, next; int value; }; @@ -313,7 +312,13 @@ /// /// \brief Dynamic iterable integer map. /// - /// \todo Document please + /// This class provides a special graph map type which can store + /// for each graph item(node, edge, etc.) an integer value. For each + /// non negative value it is possible to iterate on the keys which + /// mapped to the given value. + /// + /// \param _Graph The graph type. + /// \param _Item One of the graph's item type, the key of the map. template class IterableIntMap : protected ItemSetTraits<_Graph, _Item> ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent { @@ -322,11 +327,30 @@ ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> > ::Parent Parent; + /// The key type typedef _Item Key; + /// The value type typedef int Value; + /// The graph type typedef _Graph Graph; - explicit IterableIntMap(const Graph& graph) : Parent(graph) {} + /// \brief Constructor of the Map. + /// + /// Constructor of the Map. It set all values -1. + explicit IterableIntMap(const Graph& graph) + : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(-1)) {} + + /// \brief Constructor of the Map with a given value. + /// + /// Constructor of the Map with a given value. + explicit IterableIntMap(const Graph& graph, int value) + : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) { + if (value >= 0) { + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { + lace(it); + } + } + } private: @@ -362,8 +386,13 @@ public: + /// Indicates that the map if reference map. typedef True ReferenceMapTag; + /// \brief Refernce to the value of the map. + /// + /// This class is near to similar to the int type. It can + /// be converted to int and it has the same operators. class Reference { friend class IterableIntMap; private: @@ -447,33 +476,63 @@ Key _key; IterableIntMap& _map; }; - + + /// The const reference type. typedef const Value& ConstReference; + /// \brief Gives back the maximal value plus one. + /// + /// Gives back the maximal value plus one. int size() const { return (int)first.size(); } + /// \brief Set operation of the map. + /// + /// Set operation of the map. void set(const Key& key, const Value& value) { unlace(key); Parent::operator[](key).value = value; lace(key); } + /// \brief Const subscript operator of the map. + /// + /// Const subscript operator of the map. const Value& operator[](const Key& key) const { return Parent::operator[](key).value; } + /// \brief Subscript operator of the map. + /// + /// Subscript operator of the map. Reference operator[](const Key& key) { return Reference(*this, key); } + /// \brief Iterator for the keys with the same value. + /// + /// Iterator for the keys with the same value. It works + /// like a graph item iterator in the map, it can be converted + /// the item type of the map, incremented with \c ++ operator, and + /// if the iterator leave the last valid item it will be equal to + /// \c INVALID. class ItemIt : public _Item { public: typedef _Item Parent; + /// \brief Invalid constructor \& conversion. + /// + /// This constructor initializes the item to be invalid. + /// \sa Invalid for more details. ItemIt(Invalid) : Parent(INVALID), _map(0) {} + /// \brief Creates an iterator with a value. + /// + /// Creates an iterator with a value. It iterates on the + /// keys which have the given value. + /// \param map The IterableIntMap + /// \param value The value ItemIt(const IterableIntMap& map, int value) : _map(&map) { if (value < 0 || value >= (int)_map->first.size()) { Parent::operator=(INVALID); @@ -482,6 +541,9 @@ } } + /// \brief Increment operator. + /// + /// Increment Operator. ItemIt& operator++() { Parent::operator=(_map->IterableIntMap::Parent:: operator[](static_cast(*this)).next); diff -r 029cc4f638d1 -r 474d093466a5 lemon/lp_base.h --- a/lemon/lp_base.h Wed Nov 16 14:46:22 2005 +0000 +++ b/lemon/lp_base.h Wed Nov 16 18:58:10 2005 +0000 @@ -681,16 +681,13 @@ return s; } template - typename enable_if::type addColSet(T &t,dummy<2> = 2) { - ///\bug return addColSet(t.valueSet()); should also work. int s=0; - for(typename T::ValueSet::iterator i=t.valueSet().begin(); - i!=t.valueSet().end(); - ++i) + for(typename T::MapIt i(t); i!=INVALID; ++i) { - *i=addCol(); + i.set(addCol()); s++; } return s; @@ -787,16 +784,13 @@ return s; } template - typename enable_if::type addRowSet(T &t,dummy<2> = 2) { - ///\bug return addRowSet(t.valueSet()); should also work. int s=0; - for(typename T::ValueSet::iterator i=t.valueSet().begin(); - i!=t.valueSet().end(); - ++i) + for(typename T::MapIt i(t); i!=INVALID; ++i) { - *i=addRow(); + i.set(addRow()); s++; } return s; diff -r 029cc4f638d1 -r 474d093466a5 lemon/map_iterator.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/map_iterator.h Wed Nov 16 18:58:10 2005 +0000 @@ -0,0 +1,145 @@ +/* -*- C++ -*- + * lemon/map_iterator.h - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#ifndef LEMON_MAP_ITERATOR_H +#define LEMON_MAP_ITERATOR_H + +#include +#include + +/// \ingroup gutils +/// \file +/// \brief Iterators on the maps. + +namespace lemon { + + /// \ingroup gutils + /// + /// \brief Iterator for maps with possibility of changing values. + /// + /// Iterator for maps with possibility of changing values. + template + class MapIt : public ItemSetTraits::ItemIt { + public: + + typedef typename ItemSetTraits::ItemIt Parent; + + typedef typename Map::Value Value; + + /// \brief Creates an iterator + /// + /// Creates an iterator for the map, which iterates on the + /// given graph item set. + MapIt(const Graph& _graph, Map& _map) : Parent(_graph), map(_map) {} + + /// \brief Gives back the map's value on the current position. + /// + /// Gives back the map's value on the current position. + typename MapTraits::ConstReturnValue operator*() const { + return map[*this]; + } + + /// \brief Gives back a reference to the map's value. + /// + /// Gives back a reference to the map's value on the current position. + typename MapTraits::ReturnValue operator*() { + return map[*this]; + } + + /// \brief Sets the value on the current position + /// + /// Sets the value on the current position. + void set(const Value& value) { + map.set(*this, value); + } + + protected: + Map& map; + + }; + + /// \ingroup gutils + /// + /// \brief Iterator for maps with possibility of getting values. + /// + /// Iterator for maps with possibility of getting values. + template + class ConstMapIt : public ItemSetTraits::ItemIt { + public: + + typedef typename ItemSetTraits::ItemIt Parent; + + typedef typename Map::Value Value; + + /// \brief Creates an iterator + /// + /// Creates an iterator for the map, which iterates on the + /// given graph item set. + ConstMapIt(const Graph& _graph, const Map& _map) + : Parent(_graph), map(_map) {} + + /// \brief Gives back the map's value on the current position. + /// + /// Gives back the map's value on the current position. + typename MapTraits::ConstReturnValue operator*() const { + return map[*this]; + } + + protected: + const Map& map; + }; + + + /// \ingroup gutils + /// + /// \brief Iterator for maps which filters items by the values. + /// + /// Iterator for maps which gives back only that items which mapped + /// to an given value. + template + class FilterMapIt + : public ItemSetTraits::ItemIt { + public: + + typedef typename ItemSetTraits::ItemIt Parent; + + typedef typename Map::Value Value; + + /// \brief Creates an iterator + /// + /// Creates an iterator for the map, which iterates on the + /// given graph item set and filters all items which mapped value + /// differ from \c _value. + FilterMapIt(const Graph& _graph, const Map& _map, const Value& _value) + : Parent(_graph), map(_map), value(_value) {} + + /// \brief Increment operator + /// + /// Skips items which has not the given value. + FilterMapIt& operator++() { + Parent::operator++(); + while (*this != INVALID && map[*this] != value) Parent::operator++(); + } + + protected: + const Map& map; + Value value; + }; + + +} + +#endif