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) { |
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); |