lemon/maps.h
branch1.2
changeset 905 d149eaf24638
parent 792 a2d5fd4c309a
child 915 633956ca9421
equal deleted inserted replaced
47:b99d7f8c037d 48:5b9dbfa87621
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2009
     5  * Copyright (C) 2003-2010
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
     9  * Permission to use, modify and distribute this software is granted
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    10  * provided that this copyright notice appears in all copies. For
   231   /// This map is essentially a wrapper for \c std::vector. It assigns
   231   /// This map is essentially a wrapper for \c std::vector. It assigns
   232   /// values to integer keys from the range <tt>[0..size-1]</tt>.
   232   /// values to integer keys from the range <tt>[0..size-1]</tt>.
   233   /// It can be used together with some data structures, e.g.
   233   /// It can be used together with some data structures, e.g.
   234   /// heap types and \c UnionFind, when the used items are small
   234   /// heap types and \c UnionFind, when the used items are small
   235   /// integers. This map conforms to the \ref concepts::ReferenceMap
   235   /// integers. This map conforms to the \ref concepts::ReferenceMap
   236   /// "ReferenceMap" concept. 
   236   /// "ReferenceMap" concept.
   237   ///
   237   ///
   238   /// The simplest way of using this map is through the rangeMap()
   238   /// The simplest way of using this map is through the rangeMap()
   239   /// function.
   239   /// function.
   240   template <typename V>
   240   template <typename V>
   241   class RangeMap : public MapBase<int, V> {
   241   class RangeMap : public MapBase<int, V> {
  1914   /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
  1914   /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
  1915   /// and if a key is set to a new value, then stores it in the inverse map.
  1915   /// and if a key is set to a new value, then stores it in the inverse map.
  1916   /// The graph items can be accessed by their values either using
  1916   /// The graph items can be accessed by their values either using
  1917   /// \c InverseMap or \c operator()(), and the values of the map can be
  1917   /// \c InverseMap or \c operator()(), and the values of the map can be
  1918   /// accessed with an STL compatible forward iterator (\c ValueIt).
  1918   /// accessed with an STL compatible forward iterator (\c ValueIt).
  1919   /// 
  1919   ///
  1920   /// This map is intended to be used when all associated values are
  1920   /// This map is intended to be used when all associated values are
  1921   /// different (the map is actually invertable) or there are only a few
  1921   /// different (the map is actually invertable) or there are only a few
  1922   /// items with the same value.
  1922   /// items with the same value.
  1923   /// Otherwise consider to use \c IterableValueMap, which is more 
  1923   /// Otherwise consider to use \c IterableValueMap, which is more
  1924   /// suitable and more efficient for such cases. It provides iterators
  1924   /// suitable and more efficient for such cases. It provides iterators
  1925   /// to traverse the items with the same associated value, but
  1925   /// to traverse the items with the same associated value, but
  1926   /// it does not have \c InverseMap.
  1926   /// it does not have \c InverseMap.
  1927   ///
  1927   ///
  1928   /// This type is not reference map, so it cannot be modified with
  1928   /// This type is not reference map, so it cannot be modified with
  2000       bool operator!=(ValueIt jt) const { return it != jt.it; }
  2000       bool operator!=(ValueIt jt) const { return it != jt.it; }
  2001 
  2001 
  2002     private:
  2002     private:
  2003       typename Container::const_iterator it;
  2003       typename Container::const_iterator it;
  2004     };
  2004     };
  2005     
  2005 
  2006     /// Alias for \c ValueIt
  2006     /// Alias for \c ValueIt
  2007     typedef ValueIt ValueIterator;
  2007     typedef ValueIt ValueIterator;
  2008 
  2008 
  2009     /// \brief Returns an iterator to the first value.
  2009     /// \brief Returns an iterator to the first value.
  2010     ///
  2010     ///
  2059     /// only one of them is returned.
  2059     /// only one of them is returned.
  2060     Key operator()(const Value& val) const {
  2060     Key operator()(const Value& val) const {
  2061       typename Container::const_iterator it = _inv_map.find(val);
  2061       typename Container::const_iterator it = _inv_map.find(val);
  2062       return it != _inv_map.end() ? it->second : INVALID;
  2062       return it != _inv_map.end() ? it->second : INVALID;
  2063     }
  2063     }
  2064     
  2064 
  2065     /// \brief Returns the number of items with the given value.
  2065     /// \brief Returns the number of items with the given value.
  2066     ///
  2066     ///
  2067     /// This function returns the number of items with the given value
  2067     /// This function returns the number of items with the given value
  2068     /// associated with it.
  2068     /// associated with it.
  2069     int count(const Value &val) const {
  2069     int count(const Value &val) const {
  2376   /// \relates RangeIdMap
  2376   /// \relates RangeIdMap
  2377   template <typename K, typename GR>
  2377   template <typename K, typename GR>
  2378   inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
  2378   inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
  2379     return RangeIdMap<GR, K>(graph);
  2379     return RangeIdMap<GR, K>(graph);
  2380   }
  2380   }
  2381   
  2381 
  2382   /// \brief Dynamic iterable \c bool map.
  2382   /// \brief Dynamic iterable \c bool map.
  2383   ///
  2383   ///
  2384   /// This class provides a special graph map type which can store a
  2384   /// This class provides a special graph map type which can store a
  2385   /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
  2385   /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
  2386   /// For both \c true and \c false values it is possible to iterate on
  2386   /// For both \c true and \c false values it is possible to iterate on
  2636       /// Creates an iterator with a value. It iterates on the
  2636       /// Creates an iterator with a value. It iterates on the
  2637       /// keys mapped to the given value.
  2637       /// keys mapped to the given value.
  2638       /// \param map The IterableBoolMap.
  2638       /// \param map The IterableBoolMap.
  2639       /// \param value The value.
  2639       /// \param value The value.
  2640       ItemIt(const IterableBoolMap& map, bool value)
  2640       ItemIt(const IterableBoolMap& map, bool value)
  2641         : Parent(value ? 
  2641         : Parent(value ?
  2642                  (map._sep > 0 ?
  2642                  (map._sep > 0 ?
  2643                   map._array[map._sep - 1] : INVALID) :
  2643                   map._array[map._sep - 1] : INVALID) :
  2644                  (map._sep < int(map._array.size()) ?
  2644                  (map._sep < int(map._array.size()) ?
  2645                   map._array.back() : INVALID)), _map(&map) {}
  2645                   map._array.back() : INVALID)), _map(&map) {}
  2646 
  2646 
  3784   /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
  3784   /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
  3785   template <typename GR, typename From, typename To>
  3785   template <typename GR, typename From, typename To>
  3786   void mapCopy(const GR& gr, const From& from, To& to) {
  3786   void mapCopy(const GR& gr, const From& from, To& to) {
  3787     typedef typename To::Key Item;
  3787     typedef typename To::Key Item;
  3788     typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
  3788     typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
  3789     
  3789 
  3790     for (ItemIt it(gr); it != INVALID; ++it) {
  3790     for (ItemIt it(gr); it != INVALID; ++it) {
  3791       to.set(it, from[it]);
  3791       to.set(it, from[it]);
  3792     }
  3792     }
  3793   }
  3793   }
  3794 
  3794 
  3795   /// \brief Compare two graph maps.
  3795   /// \brief Compare two graph maps.
  3796   ///
  3796   ///
  3797   /// This function compares the values of two graph maps. It returns 
  3797   /// This function compares the values of two graph maps. It returns
  3798   /// \c true if the maps assign the same value for all items in the graph.
  3798   /// \c true if the maps assign the same value for all items in the graph.
  3799   /// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal
  3799   /// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal
  3800   /// and their \c Value types must be comparable using \c %operator==().
  3800   /// and their \c Value types must be comparable using \c %operator==().
  3801   ///
  3801   ///
  3802   /// \param gr The graph for which the maps are defined.
  3802   /// \param gr The graph for which the maps are defined.
  3804   /// \param map2 The second map.
  3804   /// \param map2 The second map.
  3805   template <typename GR, typename Map1, typename Map2>
  3805   template <typename GR, typename Map1, typename Map2>
  3806   bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
  3806   bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
  3807     typedef typename Map2::Key Item;
  3807     typedef typename Map2::Key Item;
  3808     typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
  3808     typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
  3809     
  3809 
  3810     for (ItemIt it(gr); it != INVALID; ++it) {
  3810     for (ItemIt it(gr); it != INVALID; ++it) {
  3811       if (!(map1[it] == map2[it])) return false;
  3811       if (!(map1[it] == map2[it])) return false;
  3812     }
  3812     }
  3813     return true;
  3813     return true;
  3814   }
  3814   }