src/lemon/graph_utils.h
changeset 1413 3f45d58969d4
parent 1402 655d8e78454d
child 1419 c3244a26adb1
equal deleted inserted replaced
12:41a924a30722 13:8477bfeb497c
    20 #include <iterator>
    20 #include <iterator>
    21 #include <map>
    21 #include <map>
    22 
    22 
    23 #include <lemon/invalid.h>
    23 #include <lemon/invalid.h>
    24 #include <lemon/utility.h>
    24 #include <lemon/utility.h>
       
    25 #include <lemon/maps.h>
    25 
    26 
    26 ///\ingroup gutils
    27 ///\ingroup gutils
    27 ///\file
    28 ///\file
    28 ///\brief Graph utilities.
    29 ///\brief Graph utilities.
    29 ///
    30 ///
   337   /// @}
   338   /// @}
   338 
   339 
   339   /// \addtogroup graph_maps
   340   /// \addtogroup graph_maps
   340   /// @{
   341   /// @{
   341 
   342 
   342   /// Provides an immutable and unique id for each item in the graph.
       
   343 
       
   344   /// The IdMap class provides an unique and immutable mapping for each item
       
   345   /// in the graph.
       
   346   ///
       
   347   template <typename _Graph, typename _Item>
       
   348   class IdMap {
       
   349   public:
       
   350     typedef _Graph Graph;
       
   351     typedef int Value;
       
   352     typedef _Item Item;
       
   353     typedef _Item Key;
       
   354 
       
   355     /// \brief The class represents the inverse of the map.
       
   356     ///
       
   357     /// The class represents the inverse of the map.
       
   358     /// \see inverse()
       
   359     class InverseMap {
       
   360     public:
       
   361       /// \brief Constructor.
       
   362       ///
       
   363       /// Constructor for creating an id-to-item map.
       
   364       InverseMap(const Graph& _graph) : graph(&_graph) {}
       
   365       /// \brief Gives back the given item from its id.
       
   366       ///
       
   367       /// Gives back the given item from its id.
       
   368       /// 
       
   369       Item operator[](int id) const { return graph->fromId(id, Item());}
       
   370     private:
       
   371       const Graph* graph;
       
   372     };
       
   373 
       
   374     /// \brief Constructor.
       
   375     ///
       
   376     /// Constructor for creating id map.
       
   377     IdMap(const Graph& _graph) : graph(&_graph) {}
       
   378 
       
   379     /// \brief Gives back the \e id of the item.
       
   380     ///
       
   381     /// Gives back the immutable and unique \e id of the map.
       
   382     int operator[](const Item& item) const { return graph->id(item);}
       
   383 
       
   384     /// \brief Gives back the inverse of the map.
       
   385     ///
       
   386     /// Gives back the inverse of the map.
       
   387     InverseMap inverse() const { return InverseMap(*graph);} 
       
   388 
       
   389   private:
       
   390     const Graph* graph;
       
   391 
       
   392   };
       
   393 
       
   394   
       
   395   template <typename Map, typename Enable = void>
   343   template <typename Map, typename Enable = void>
   396   struct ReferenceMapTraits {
   344   struct ReferenceMapTraits {
   397     typedef typename Map::Value Value;
   345     typedef typename Map::Value Value;
   398     typedef typename Map::Value& Reference;
   346     typedef typename Map::Value& Reference;
   399     typedef const typename Map::Value& ConstReference;
   347     typedef const typename Map::Value& ConstReference;
   411     typedef typename Map::ConstReference ConstReference;
   359     typedef typename Map::ConstReference ConstReference;
   412     typedef typename Map::Pointer Pointer;
   360     typedef typename Map::Pointer Pointer;
   413     typedef typename Map::ConstPointer ConstPointer;
   361     typedef typename Map::ConstPointer ConstPointer;
   414   };
   362   };
   415 
   363 
       
   364 
       
   365   /// Provides an immutable and unique id for each item in the graph.
       
   366 
       
   367   /// The IdMap class provides an unique and immutable mapping for each item
       
   368   /// in the graph.
       
   369   ///
       
   370   template <typename _Graph, typename _Item>
       
   371   class IdMap {
       
   372   public:
       
   373     typedef _Graph Graph;
       
   374     typedef int Value;
       
   375     typedef _Item Item;
       
   376     typedef _Item Key;
       
   377 
       
   378     /// \brief Constructor.
       
   379     ///
       
   380     /// Constructor for creating id map.
       
   381     IdMap(const Graph& _graph) : graph(&_graph) {}
       
   382 
       
   383     /// \brief Gives back the \e id of the item.
       
   384     ///
       
   385     /// Gives back the immutable and unique \e id of the map.
       
   386     int operator[](const Item& item) const { return graph->id(item);}
       
   387 
       
   388 
       
   389   private:
       
   390     const Graph* graph;
       
   391 
       
   392   public:
       
   393 
       
   394     /// \brief The class represents the inverse of the map.
       
   395     ///
       
   396     /// The class represents the inverse of the map.
       
   397     /// \see inverse()
       
   398     class InverseMap {
       
   399     public:
       
   400       /// \brief Constructor.
       
   401       ///
       
   402       /// Constructor for creating an id-to-item map.
       
   403       InverseMap(const Graph& _graph) : graph(&_graph) {}
       
   404 
       
   405       /// \brief Constructor.
       
   406       ///
       
   407       /// Constructor for creating an id-to-item map.
       
   408       InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
       
   409 
       
   410       /// \brief Gives back the given item from its id.
       
   411       ///
       
   412       /// Gives back the given item from its id.
       
   413       /// 
       
   414       Item operator[](int id) const { return graph->fromId(id, Item());}
       
   415     private:
       
   416       const Graph* graph;
       
   417     };
       
   418 
       
   419     /// \brief Gives back the inverse of the map.
       
   420     ///
       
   421     /// Gives back the inverse of the map.
       
   422     InverseMap inverse() const { return InverseMap(*graph);} 
       
   423 
       
   424   };
       
   425 
       
   426   
   416   /// \brief General inversable graph-map type.
   427   /// \brief General inversable graph-map type.
   417 
   428 
   418   /// This type provides simple inversable map functions. 
   429   /// This type provides simple inversable map functions. 
   419   /// The InversableMap wraps an arbitrary ReadWriteMap 
   430   /// The InversableMap wraps an arbitrary ReadWriteMap 
   420   /// and if a key is setted to a new value then store it
   431   /// and if a key is setted to a new value then store it
   424   template <
   435   template <
   425     typename _Graph,
   436     typename _Graph,
   426     typename _Item, 
   437     typename _Item, 
   427     typename _Value,
   438     typename _Value,
   428     typename _Map 
   439     typename _Map 
   429     = typename ItemSetTraits<_Graph, _Item>::template Map<_Value> 
   440     = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
   430   >
   441   >
   431   class InversableMap : protected _Map {
   442   class InvertableMap : protected _Map {
   432 
   443 
   433   public:
   444   public:
   434  
   445  
   435     typedef _Map Map;
   446     typedef _Map Map;
   436     typedef _Graph Graph;
   447     typedef _Graph Graph;
   437    /// The key type of InversableMap (Node, Edge, UndirEdge).
   448 
       
   449     /// The key type of InvertableMap (Node, Edge, UndirEdge).
   438     typedef typename _Map::Key Key;
   450     typedef typename _Map::Key Key;
   439     /// The value type of the InversableMap.
   451     /// The value type of the InvertableMap.
   440     typedef typename _Map::Value Value;
   452     typedef typename _Map::Value Value;
   441 
   453 
   442     typedef std::map<Value, Key> InverseMap;
       
   443     
       
   444     typedef typename _Map::ConstReference ConstReference;
       
   445 
       
   446     /// \brief Constructor.
   454     /// \brief Constructor.
   447     ///
   455     ///
   448     /// Construct a new InversableMap for the graph.
   456     /// Construct a new InvertableMap for the graph.
   449     ///
   457     ///
   450     InversableMap(const Graph& graph) : Map(graph) {} 
   458     InvertableMap(const Graph& graph) : Map(graph) {} 
   451     
   459     
   452     /// \brief The setter function of the map.
   460     /// \brief The setter function of the map.
   453     ///
   461     ///
   454 
   462     /// Sets the mapped value.
   455     void set(const Key& key, const Value& val) {
   463     void set(const Key& key, const Value& val) {
   456       Value oldval = Map::operator[](key);
   464       Value oldval = Map::operator[](key);
   457       typename InverseMap::iterator it = invMap.find(oldval);
   465       typename Container::iterator it = invMap.find(oldval);
   458       if (it != invMap.end() && it->second == key) {
   466       if (it != invMap.end() && it->second == key) {
   459 	invMap.erase(it);
   467 	invMap.erase(it);
   460       }      
   468       }      
   461       invMap.insert(make_pair(val, key));
   469       invMap.insert(make_pair(val, key));
   462       Map::set(key, val);
   470       Map::set(key, val);
   463     }
   471     }
   464 
   472 
   465     /// \brief The getter function of the map.
   473     /// \brief The getter function of the map.
   466     ///
   474     ///
   467     /// It gives back the value associated with the key.
   475     /// It gives back the value associated with the key.
   468     ConstReference operator[](const Key& key) const {
   476     const Value operator[](const Key& key) const {
   469       return Map::operator[](key);
   477       return Map::operator[](key);
   470     }
   478     }
   471 
   479 
   472     /// \brief Add a new key to the map.
   480     /// \brief Add a new key to the map.
   473     ///
   481     ///
   481     ///
   489     ///
   482     /// Erase the key to the map. It is called by the
   490     /// Erase the key to the map. It is called by the
   483     /// \c AlterationNotifier.
   491     /// \c AlterationNotifier.
   484     virtual void erase(const Key& key) {
   492     virtual void erase(const Key& key) {
   485       Value val = Map::operator[](key);
   493       Value val = Map::operator[](key);
   486       typename InverseMap::iterator it = invMap.find(val);
   494       typename Container::iterator it = invMap.find(val);
   487       if (it != invMap.end() && it->second == key) {
   495       if (it != invMap.end() && it->second == key) {
   488 	invMap.erase(it);
   496 	invMap.erase(it);
   489       }
   497       }
   490       Map::erase(key);
   498       Map::erase(key);
   491     }
   499     }
   497     virtual void clear() {
   505     virtual void clear() {
   498       invMap.clear();
   506       invMap.clear();
   499       Map::clear();
   507       Map::clear();
   500     }
   508     }
   501 
   509 
       
   510   private:
       
   511     
       
   512     typedef std::map<Value, Key> Container;
       
   513     Container invMap;    
       
   514 
       
   515   public:
       
   516 
       
   517     /// \brief The inverse map type.
       
   518     ///
       
   519     /// The inverse of this map. The subscript operator of the map
       
   520     /// gives back always the item what was last assigned to the value. 
       
   521     class InverseMap {
       
   522     public:
       
   523       /// \brief Constructor of the InverseMap.
       
   524       ///
       
   525       /// Constructor of the InverseMap.
       
   526       InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
       
   527 
       
   528       /// The value type of the InverseMap.
       
   529       typedef typename InvertableMap::Key Value;
       
   530       /// The key type of the InverseMap.
       
   531       typedef typename InvertableMap::Value Key; 
       
   532 
       
   533       /// \brief Subscript operator. 
       
   534       ///
       
   535       /// Subscript operator. It gives back always the item 
       
   536       /// what was last assigned to the value.
       
   537       Value operator[](const Key& key) const {
       
   538 	typename Container::const_iterator it = inverted.invMap.find(key);
       
   539 	return it->second;
       
   540       }
       
   541       
       
   542     private:
       
   543       const InvertableMap& inverted;
       
   544     };
       
   545 
   502     /// \brief It gives back the just readeable inverse map.
   546     /// \brief It gives back the just readeable inverse map.
   503     ///
   547     ///
   504     /// It gives back the just readeable inverse map.
   548     /// It gives back the just readeable inverse map.
   505     const InverseMap& inverse() const {
   549     InverseMap inverse() const {
   506       return invMap;
   550       return InverseMap(*this);
   507     } 
   551     } 
   508 
   552 
   509 
   553 
   510   private:
   554     
   511     InverseMap invMap;    
       
   512   };
   555   };
   513 
   556 
   514   /// \brief Provides a mutable, continuous and unique descriptor for each 
   557   /// \brief Provides a mutable, continuous and unique descriptor for each 
   515   /// item in the graph.
   558   /// item in the graph.
   516   ///
   559   ///
   517   /// The DescriptorMap class provides a mutable, continuous and immutable
   560   /// The DescriptorMap class provides a mutable, continuous and immutable
   518   /// mapping for each item in the graph.
   561   /// mapping for each item in the graph. The value for an item may mutated
       
   562   /// on each operation when the an item erased or added to graph.
   519   ///
   563   ///
   520   /// \param _Graph The graph class the \c DescriptorMap belongs to.
   564   /// \param _Graph The graph class the \c DescriptorMap belongs to.
   521   /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
   565   /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
   522   /// UndirEdge.
   566   /// UndirEdge.
   523   /// \param _Map A ReadWriteMap mapping from the item type to integer.
   567   /// \param _Map A ReadWriteMap mapping from the item type to integer.
   524 
       
   525   template <
   568   template <
   526     typename _Graph,   
   569     typename _Graph,   
   527     typename _Item,
   570     typename _Item,
   528     typename _Map = typename ItemSetTraits<_Graph, _Item>::template Map<int>
   571     typename _Map 
       
   572     = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
   529   >
   573   >
   530   class DescriptorMap : protected _Map {
   574   class DescriptorMap : protected _Map {
   531 
   575 
   532     typedef _Item Item;
   576     typedef _Item Item;
   533     typedef _Map Map;
   577     typedef _Map Map;
   539     /// The key type of DescriptorMap (Node, Edge, UndirEdge).
   583     /// The key type of DescriptorMap (Node, Edge, UndirEdge).
   540     typedef typename _Map::Key Key;
   584     typedef typename _Map::Key Key;
   541     /// The value type of DescriptorMap.
   585     /// The value type of DescriptorMap.
   542     typedef typename _Map::Value Value;
   586     typedef typename _Map::Value Value;
   543 
   587 
   544     typedef std::vector<Item> InverseMap;
       
   545 
       
   546     /// \brief Constructor.
   588     /// \brief Constructor.
   547     ///
   589     ///
   548     /// Constructor for creating descriptor map.
   590     /// Constructor for descriptor map.
   549     DescriptorMap(const Graph& _graph) : Map(_graph) {
   591     DescriptorMap(const Graph& _graph) : Map(_graph) {
   550       build();
   592       build();
   551     }
   593     }
   552 
   594 
   553     /// \brief Add a new key to the map.
   595     /// \brief Add a new key to the map.
   565     /// Erase the key to the map. It is called by the
   607     /// Erase the key to the map. It is called by the
   566     /// \c AlterationNotifier.
   608     /// \c AlterationNotifier.
   567     virtual void erase(const Item& item) {
   609     virtual void erase(const Item& item) {
   568       Map::set(invMap.back(), Map::operator[](item));
   610       Map::set(invMap.back(), Map::operator[](item));
   569       invMap[Map::operator[](item)] = invMap.back();
   611       invMap[Map::operator[](item)] = invMap.back();
       
   612       invMap.pop_back();
   570       Map::erase(item);
   613       Map::erase(item);
   571     }
   614     }
   572 
   615 
   573     /// \brief Build the unique map.
   616     /// \brief Build the unique map.
   574     ///
   617     ///
   598     /// Gives back the mutable and unique \e descriptor of the map.
   641     /// Gives back the mutable and unique \e descriptor of the map.
   599     int operator[](const Item& item) const {
   642     int operator[](const Item& item) const {
   600       return Map::operator[](item);
   643       return Map::operator[](item);
   601     }
   644     }
   602     
   645     
       
   646   private:
       
   647 
       
   648     typedef std::vector<Item> Container;
       
   649     Container invMap;
       
   650 
       
   651   public:
       
   652     /// \brief The inverse map type.
       
   653     ///
       
   654     /// The inverse map type.
       
   655     class InverseMap {
       
   656     public:
       
   657       /// \brief Constructor of the InverseMap.
       
   658       ///
       
   659       /// Constructor of the InverseMap.
       
   660       InverseMap(const DescriptorMap& _inverted) 
       
   661 	: inverted(_inverted) {}
       
   662 
       
   663 
       
   664       /// The value type of the InverseMap.
       
   665       typedef typename DescriptorMap::Key Value;
       
   666       /// The key type of the InverseMap.
       
   667       typedef typename DescriptorMap::Value Key; 
       
   668 
       
   669       /// \brief Subscript operator. 
       
   670       ///
       
   671       /// Subscript operator. It gives back the item 
       
   672       /// that the descriptor belongs to currently.
       
   673       Value operator[](const Key& key) const {
       
   674 	return inverted.invMap[key];
       
   675       }
       
   676       
       
   677     private:
       
   678       const DescriptorMap& inverted;
       
   679     };
       
   680 
   603     /// \brief Gives back the inverse of the map.
   681     /// \brief Gives back the inverse of the map.
   604     ///
   682     ///
   605     /// Gives back the inverse of the map.
   683     /// Gives back the inverse of the map.
   606     const InverseMap inverse() const {
   684     const InverseMap inverse() const {
   607       return invMap;
   685       return InverseMap(*this);
   608     }
   686     }
   609 
       
   610   private:
       
   611     std::vector<Item> invMap;
       
   612   };
   687   };
   613 
   688 
   614   /// \brief Returns the source of the given edge.
   689   /// \brief Returns the source of the given edge.
   615   ///
   690   ///
   616   /// The SourceMap gives back the source Node of the given edge. 
   691   /// The SourceMap gives back the source Node of the given edge.