lemon/graph_utils.h
changeset 1933 a876a3d6a4c7
parent 1909 2d806130e700
child 1946 17eb3eaad9f8
equal deleted inserted replaced
35:5588a06c73c0 36:84b8b9401e50
   884 
   884 
   885   /// This type provides simple invertable graph-maps. 
   885   /// This type provides simple invertable graph-maps. 
   886   /// The InvertableMap wraps an arbitrary ReadWriteMap 
   886   /// The InvertableMap wraps an arbitrary ReadWriteMap 
   887   /// and if a key is set to a new value then store it
   887   /// and if a key is set to a new value then store it
   888   /// in the inverse map.
   888   /// in the inverse map.
       
   889   ///
       
   890   /// The values of the map can be accessed
       
   891   /// with stl compatible forward iterator.
       
   892   ///
   889   /// \param _Graph The graph type.
   893   /// \param _Graph The graph type.
   890   /// \param _Item The item type of the graph.
   894   /// \param _Item The item type of the graph.
   891   /// \param _Value The value type of the map.
   895   /// \param _Value The value type of the map.
       
   896   ///
       
   897   /// \see IterableValueMap
   892 #ifndef DOXYGEN
   898 #ifndef DOXYGEN
   893   /// \param _Map A ReadWriteMap mapping from the item type to integer.
   899   /// \param _Map A ReadWriteMap mapping from the item type to integer.
   894   template <
   900   template <
   895     typename _Graph, typename _Item, typename _Value, typename _Map 
   901     typename _Graph, typename _Item, typename _Value, typename _Map 
   896     = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
   902     = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent 
   897   >
   903   >
   898 #else
   904 #else
   899   template <typename _Graph, typename _Item, typename _Value>
   905   template <typename _Graph, typename _Item, typename _Value>
   900 #endif
   906 #endif
   901   class InvertableMap : protected _Map {
   907   class InvertableMap : protected _Map {
   902 
   908   public:
   903   public:
       
   904  
       
   905     typedef _Map Map;
       
   906     typedef _Graph Graph;
       
   907 
   909 
   908     /// The key type of InvertableMap (Node, Edge, UEdge).
   910     /// The key type of InvertableMap (Node, Edge, UEdge).
   909     typedef typename _Map::Key Key;
   911     typedef typename _Map::Key Key;
   910     /// The value type of the InvertableMap.
   912     /// The value type of the InvertableMap.
   911     typedef typename _Map::Value Value;
   913     typedef typename _Map::Value Value;
   912 
   914 
       
   915   private:
       
   916     
       
   917     typedef _Map Map;
       
   918     typedef _Graph Graph;
       
   919 
       
   920     typedef std::map<Value, Key> Container;
       
   921     Container invMap;    
       
   922 
       
   923   public:
       
   924  
       
   925 
       
   926 
   913     /// \brief Constructor.
   927     /// \brief Constructor.
   914     ///
   928     ///
   915     /// Construct a new InvertableMap for the graph.
   929     /// Construct a new InvertableMap for the graph.
   916     ///
   930     ///
   917     InvertableMap(const Graph& graph) : Map(graph) {} 
   931     InvertableMap(const Graph& graph) : Map(graph) {} 
       
   932 
       
   933     /// \brief Forward iterator for values.
       
   934     ///
       
   935     /// This iterator is an stl compatible forward
       
   936     /// iterator on the values of the map. The values can
       
   937     /// be accessed in the [beginValue, endValue) range.
       
   938     ///
       
   939     class ValueIterator 
       
   940       : public std::iterator<std::forward_iterator_tag, Value> {
       
   941       friend class InvertableMap;
       
   942     private:
       
   943       ValueIterator(typename Container::const_iterator _it) 
       
   944         : it(_it) {}
       
   945     public:
       
   946       
       
   947       ValueIterator() {}
       
   948 
       
   949       ValueIterator& operator++() { ++it; return *this; }
       
   950       ValueIterator operator++(int) { 
       
   951         ValueIterator tmp(*this); 
       
   952         operator++();
       
   953         return tmp; 
       
   954       }
       
   955 
       
   956       const Value& operator*() const { return it->first; }
       
   957       const Value* operator->() const { return &(it->first); }
       
   958 
       
   959       bool operator==(ValueIterator jt) const { return it == jt.it; }
       
   960       bool operator!=(ValueIterator jt) const { return it != jt.it; }
       
   961       
       
   962     private:
       
   963       typename Container::const_iterator it;
       
   964     };
       
   965 
       
   966     /// \brief Returns an iterator to the first value.
       
   967     ///
       
   968     /// Returns an stl compatible iterator to the 
       
   969     /// first value of the map. The values of the
       
   970     /// map can be accessed in the [beginValue, endValue)
       
   971     /// range.
       
   972     ValueIterator beginValue() const {
       
   973       return ValueIterator(invMap.begin());
       
   974     }
       
   975 
       
   976     /// \brief Returns an iterator after the last value.
       
   977     ///
       
   978     /// Returns an stl compatible iterator after the 
       
   979     /// last value of the map. The values of the
       
   980     /// map can be accessed in the [beginValue, endValue)
       
   981     /// range.
       
   982     ValueIterator endValue() const {
       
   983       return ValueIterator(invMap.end());
       
   984     }
   918     
   985     
   919     /// \brief The setter function of the map.
   986     /// \brief The setter function of the map.
   920     ///
   987     ///
   921     /// Sets the mapped value.
   988     /// Sets the mapped value.
   922     void set(const Key& key, const Value& val) {
   989     void set(const Key& key, const Value& val) {
   930     }
   997     }
   931 
   998 
   932     /// \brief The getter function of the map.
   999     /// \brief The getter function of the map.
   933     ///
  1000     ///
   934     /// It gives back the value associated with the key.
  1001     /// It gives back the value associated with the key.
   935     Value operator[](const Key& key) const {
  1002     typename MapTraits<Map>::ConstReturnValue 
       
  1003     operator[](const Key& key) const {
   936       return Map::operator[](key);
  1004       return Map::operator[](key);
   937     }
  1005     }
   938 
  1006 
   939   protected:
  1007   protected:
   940 
  1008 
   972     /// \c AlterationNotifier.
  1040     /// \c AlterationNotifier.
   973     virtual void clear() {
  1041     virtual void clear() {
   974       invMap.clear();
  1042       invMap.clear();
   975       Map::clear();
  1043       Map::clear();
   976     }
  1044     }
   977 
       
   978   private:
       
   979     
       
   980     typedef std::map<Value, Key> Container;
       
   981     Container invMap;    
       
   982 
  1045 
   983   public:
  1046   public:
   984 
  1047 
   985     /// \brief The inverse map type.
  1048     /// \brief The inverse map type.
   986     ///
  1049     ///
  1138       Map::clear();
  1201       Map::clear();
  1139     }
  1202     }
  1140 
  1203 
  1141   public:
  1204   public:
  1142 
  1205 
       
  1206     /// \brief Returns the maximal value plus one.
       
  1207     ///
       
  1208     /// Returns the maximal value plus one in the map.
       
  1209     unsigned int size() const {
       
  1210       return invMap.size();
       
  1211     }
       
  1212 
  1143     /// \brief Swaps the position of the two items in the map.
  1213     /// \brief Swaps the position of the two items in the map.
  1144     ///
  1214     ///
  1145     /// Swaps the position of the two items in the map.
  1215     /// Swaps the position of the two items in the map.
  1146     void swap(const Item& p, const Item& q) {
  1216     void swap(const Item& p, const Item& q) {
  1147       int pi = Map::operator[](p);
  1217       int pi = Map::operator[](p);
  1191       }
  1261       }
  1192 
  1262 
  1193       /// \brief Size of the map.
  1263       /// \brief Size of the map.
  1194       ///
  1264       ///
  1195       /// Returns the size of the map.
  1265       /// Returns the size of the map.
  1196       int size() const {
  1266       unsigned int size() const {
  1197 	return inverted.invMap.size();
  1267 	return inverted.invMap.size();
  1198       }
  1268       }
  1199       
  1269       
  1200     private:
  1270     private:
  1201       const DescriptorMap& inverted;
  1271       const DescriptorMap& inverted;
  1445       
  1515       
  1446       virtual void add(const Key& key) {
  1516       virtual void add(const Key& key) {
  1447 	Parent::add(key);
  1517 	Parent::add(key);
  1448 	Parent::set(key, 0);
  1518 	Parent::set(key, 0);
  1449       }
  1519       }
       
  1520 
  1450       virtual void add(const std::vector<Key>& keys) {
  1521       virtual void add(const std::vector<Key>& keys) {
  1451 	Parent::add(keys);
  1522 	Parent::add(keys);
  1452 	for (int i = 0; i < (int)keys.size(); ++i) {
  1523 	for (int i = 0; i < (int)keys.size(); ++i) {
  1453 	  Parent::set(keys[i], 0);
  1524 	  Parent::set(keys[i], 0);
  1454 	}
  1525 	}
  1485 
  1556 
  1486     virtual void add(const Edge& edge) {
  1557     virtual void add(const Edge& edge) {
  1487       ++deg[graph.target(edge)];
  1558       ++deg[graph.target(edge)];
  1488     }
  1559     }
  1489 
  1560 
       
  1561     virtual void add(const std::vector<Edge>& edges) {
       
  1562       for (int i = 0; i < (int)edges.size(); ++i) {
       
  1563         ++deg[graph.target(edges[i])];
       
  1564       }
       
  1565     }
       
  1566 
  1490     virtual void erase(const Edge& edge) {
  1567     virtual void erase(const Edge& edge) {
  1491       --deg[graph.target(edge)];
  1568       --deg[graph.target(edge)];
       
  1569     }
       
  1570 
       
  1571     virtual void erase(const std::vector<Edge>& edges) {
       
  1572       for (int i = 0; i < (int)edges.size(); ++i) {
       
  1573         --deg[graph.target(edges[i])];
       
  1574       }
  1492     }
  1575     }
  1493 
  1576 
  1494     virtual void build() {
  1577     virtual void build() {
  1495       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1578       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1496 	deg[it] = countInEdges(graph, it);
  1579 	deg[it] = countInEdges(graph, it);
  1589 
  1672 
  1590     virtual void add(const Edge& edge) {
  1673     virtual void add(const Edge& edge) {
  1591       ++deg[graph.source(edge)];
  1674       ++deg[graph.source(edge)];
  1592     }
  1675     }
  1593 
  1676 
       
  1677     virtual void add(const std::vector<Edge>& edges) {
       
  1678       for (int i = 0; i < (int)edges.size(); ++i) {
       
  1679         ++deg[graph.source(edges[i])];
       
  1680       }
       
  1681     }
       
  1682 
  1594     virtual void erase(const Edge& edge) {
  1683     virtual void erase(const Edge& edge) {
  1595       --deg[graph.source(edge)];
  1684       --deg[graph.source(edge)];
       
  1685     }
       
  1686 
       
  1687     virtual void erase(const std::vector<Edge>& edges) {
       
  1688       for (int i = 0; i < (int)edges.size(); ++i) {
       
  1689         --deg[graph.source(edges[i])];
       
  1690       }
  1596     }
  1691     }
  1597 
  1692 
  1598     virtual void build() {
  1693     virtual void build() {
  1599       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1694       for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
  1600 	deg[it] = countOutEdges(graph, it);
  1695 	deg[it] = countOutEdges(graph, it);