| ... |
... |
@@ -1857,280 +1857,299 @@
|
| 1857 |
1857 |
int operator[](const Item& item) const { return _graph->id(item);}
|
| 1858 |
1858 |
|
| 1859 |
1859 |
/// \brief Gives back the \e item by its id.
|
| 1860 |
1860 |
///
|
| 1861 |
1861 |
/// Gives back the \e item by its id.
|
| 1862 |
1862 |
Item operator()(int id) { return _graph->fromId(id, Item()); }
|
| 1863 |
1863 |
|
| 1864 |
1864 |
private:
|
| 1865 |
1865 |
const Graph* _graph;
|
| 1866 |
1866 |
|
| 1867 |
1867 |
public:
|
| 1868 |
1868 |
|
| 1869 |
1869 |
/// \brief This class represents the inverse of its owner (IdMap).
|
| 1870 |
1870 |
///
|
| 1871 |
1871 |
/// This class represents the inverse of its owner (IdMap).
|
| 1872 |
1872 |
/// \see inverse()
|
| 1873 |
1873 |
class InverseMap {
|
| 1874 |
1874 |
public:
|
| 1875 |
1875 |
|
| 1876 |
1876 |
/// \brief Constructor.
|
| 1877 |
1877 |
///
|
| 1878 |
1878 |
/// Constructor for creating an id-to-item map.
|
| 1879 |
1879 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {}
|
| 1880 |
1880 |
|
| 1881 |
1881 |
/// \brief Constructor.
|
| 1882 |
1882 |
///
|
| 1883 |
1883 |
/// Constructor for creating an id-to-item map.
|
| 1884 |
1884 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
|
| 1885 |
1885 |
|
| 1886 |
1886 |
/// \brief Gives back the given item from its id.
|
| 1887 |
1887 |
///
|
| 1888 |
1888 |
/// Gives back the given item from its id.
|
| 1889 |
1889 |
Item operator[](int id) const { return _graph->fromId(id, Item());}
|
| 1890 |
1890 |
|
| 1891 |
1891 |
private:
|
| 1892 |
1892 |
const Graph* _graph;
|
| 1893 |
1893 |
};
|
| 1894 |
1894 |
|
| 1895 |
1895 |
/// \brief Gives back the inverse of the map.
|
| 1896 |
1896 |
///
|
| 1897 |
1897 |
/// Gives back the inverse of the IdMap.
|
| 1898 |
1898 |
InverseMap inverse() const { return InverseMap(*_graph);}
|
| 1899 |
1899 |
};
|
| 1900 |
1900 |
|
| 1901 |
1901 |
|
| 1902 |
1902 |
/// \brief General cross reference graph map type.
|
| 1903 |
1903 |
|
| 1904 |
1904 |
/// This class provides simple invertable graph maps.
|
| 1905 |
|
/// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
|
| 1906 |
|
/// and if a key is set to a new value then store it
|
| 1907 |
|
/// in the inverse map.
|
| 1908 |
|
///
|
|
1905 |
/// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
|
|
1906 |
/// and if a key is set to a new value, then stores it in the inverse map.
|
| 1909 |
1907 |
/// The values of the map can be accessed
|
| 1910 |
1908 |
/// with stl compatible forward iterator.
|
| 1911 |
1909 |
///
|
|
1910 |
/// This type is not reference map, so it cannot be modified with
|
|
1911 |
/// the subscript operator.
|
|
1912 |
///
|
| 1912 |
1913 |
/// \tparam GR The graph type.
|
| 1913 |
1914 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 1914 |
1915 |
/// \c GR::Edge).
|
| 1915 |
1916 |
/// \tparam V The value type of the map.
|
| 1916 |
1917 |
///
|
| 1917 |
1918 |
/// \see IterableValueMap
|
| 1918 |
1919 |
template <typename GR, typename K, typename V>
|
| 1919 |
1920 |
class CrossRefMap
|
| 1920 |
1921 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type {
|
| 1921 |
1922 |
private:
|
| 1922 |
1923 |
|
| 1923 |
1924 |
typedef typename ItemSetTraits<GR, K>::
|
| 1924 |
1925 |
template Map<V>::Type Map;
|
| 1925 |
1926 |
|
| 1926 |
|
typedef std::map<V, K> Container;
|
|
1927 |
typedef std::multimap<V, K> Container;
|
| 1927 |
1928 |
Container _inv_map;
|
| 1928 |
1929 |
|
| 1929 |
1930 |
public:
|
| 1930 |
1931 |
|
| 1931 |
1932 |
/// The graph type of CrossRefMap.
|
| 1932 |
1933 |
typedef GR Graph;
|
| 1933 |
1934 |
typedef GR Digraph;
|
| 1934 |
1935 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
|
| 1935 |
1936 |
typedef K Item;
|
| 1936 |
1937 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
|
| 1937 |
1938 |
typedef K Key;
|
| 1938 |
1939 |
/// The value type of CrossRefMap.
|
| 1939 |
1940 |
typedef V Value;
|
| 1940 |
1941 |
|
| 1941 |
1942 |
/// \brief Constructor.
|
| 1942 |
1943 |
///
|
| 1943 |
1944 |
/// Construct a new CrossRefMap for the given graph.
|
| 1944 |
1945 |
explicit CrossRefMap(const Graph& graph) : Map(graph) {}
|
| 1945 |
1946 |
|
| 1946 |
1947 |
/// \brief Forward iterator for values.
|
| 1947 |
1948 |
///
|
| 1948 |
1949 |
/// This iterator is an stl compatible forward
|
| 1949 |
1950 |
/// iterator on the values of the map. The values can
|
| 1950 |
1951 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range.
|
|
1952 |
/// They are considered with multiplicity, so each value is
|
|
1953 |
/// traversed for each item it is assigned to.
|
| 1951 |
1954 |
class ValueIterator
|
| 1952 |
1955 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 1953 |
1956 |
friend class CrossRefMap;
|
| 1954 |
1957 |
private:
|
| 1955 |
1958 |
ValueIterator(typename Container::const_iterator _it)
|
| 1956 |
1959 |
: it(_it) {}
|
| 1957 |
1960 |
public:
|
| 1958 |
1961 |
|
| 1959 |
1962 |
ValueIterator() {}
|
| 1960 |
1963 |
|
| 1961 |
1964 |
ValueIterator& operator++() { ++it; return *this; }
|
| 1962 |
1965 |
ValueIterator operator++(int) {
|
| 1963 |
1966 |
ValueIterator tmp(*this);
|
| 1964 |
1967 |
operator++();
|
| 1965 |
1968 |
return tmp;
|
| 1966 |
1969 |
}
|
| 1967 |
1970 |
|
| 1968 |
1971 |
const Value& operator*() const { return it->first; }
|
| 1969 |
1972 |
const Value* operator->() const { return &(it->first); }
|
| 1970 |
1973 |
|
| 1971 |
1974 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
| 1972 |
1975 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
| 1973 |
1976 |
|
| 1974 |
1977 |
private:
|
| 1975 |
1978 |
typename Container::const_iterator it;
|
| 1976 |
1979 |
};
|
| 1977 |
1980 |
|
| 1978 |
1981 |
/// \brief Returns an iterator to the first value.
|
| 1979 |
1982 |
///
|
| 1980 |
1983 |
/// Returns an stl compatible iterator to the
|
| 1981 |
1984 |
/// first value of the map. The values of the
|
| 1982 |
1985 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
| 1983 |
1986 |
/// range.
|
| 1984 |
1987 |
ValueIterator beginValue() const {
|
| 1985 |
1988 |
return ValueIterator(_inv_map.begin());
|
| 1986 |
1989 |
}
|
| 1987 |
1990 |
|
| 1988 |
1991 |
/// \brief Returns an iterator after the last value.
|
| 1989 |
1992 |
///
|
| 1990 |
1993 |
/// Returns an stl compatible iterator after the
|
| 1991 |
1994 |
/// last value of the map. The values of the
|
| 1992 |
1995 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
|
| 1993 |
1996 |
/// range.
|
| 1994 |
1997 |
ValueIterator endValue() const {
|
| 1995 |
1998 |
return ValueIterator(_inv_map.end());
|
| 1996 |
1999 |
}
|
| 1997 |
2000 |
|
| 1998 |
2001 |
/// \brief Sets the value associated with the given key.
|
| 1999 |
2002 |
///
|
| 2000 |
2003 |
/// Sets the value associated with the given key.
|
| 2001 |
2004 |
void set(const Key& key, const Value& val) {
|
| 2002 |
2005 |
Value oldval = Map::operator[](key);
|
| 2003 |
|
typename Container::iterator it = _inv_map.find(oldval);
|
| 2004 |
|
if (it != _inv_map.end() && it->second == key) {
|
| 2005 |
|
_inv_map.erase(it);
|
|
2006 |
typename Container::iterator it;
|
|
2007 |
for (it = _inv_map.equal_range(oldval).first;
|
|
2008 |
it != _inv_map.equal_range(oldval).second; ++it) {
|
|
2009 |
if (it->second == key) {
|
|
2010 |
_inv_map.erase(it);
|
|
2011 |
break;
|
|
2012 |
}
|
| 2006 |
2013 |
}
|
| 2007 |
|
_inv_map.insert(make_pair(val, key));
|
|
2014 |
_inv_map.insert(std::make_pair(val, key));
|
| 2008 |
2015 |
Map::set(key, val);
|
| 2009 |
2016 |
}
|
| 2010 |
2017 |
|
| 2011 |
2018 |
/// \brief Returns the value associated with the given key.
|
| 2012 |
2019 |
///
|
| 2013 |
2020 |
/// Returns the value associated with the given key.
|
| 2014 |
2021 |
typename MapTraits<Map>::ConstReturnValue
|
| 2015 |
2022 |
operator[](const Key& key) const {
|
| 2016 |
2023 |
return Map::operator[](key);
|
| 2017 |
2024 |
}
|
| 2018 |
2025 |
|
| 2019 |
|
/// \brief Gives back the item by its value.
|
|
2026 |
/// \brief Gives back an item by its value.
|
| 2020 |
2027 |
///
|
| 2021 |
|
/// Gives back the item by its value.
|
| 2022 |
|
Key operator()(const Value& key) const {
|
| 2023 |
|
typename Container::const_iterator it = _inv_map.find(key);
|
|
2028 |
/// This function gives back an item that is assigned to
|
|
2029 |
/// the given value or \c INVALID if no such item exists.
|
|
2030 |
/// If there are more items with the same associated value,
|
|
2031 |
/// only one of them is returned.
|
|
2032 |
Key operator()(const Value& val) const {
|
|
2033 |
typename Container::const_iterator it = _inv_map.find(val);
|
| 2024 |
2034 |
return it != _inv_map.end() ? it->second : INVALID;
|
| 2025 |
2035 |
}
|
| 2026 |
2036 |
|
| 2027 |
2037 |
protected:
|
| 2028 |
2038 |
|
| 2029 |
2039 |
/// \brief Erase the key from the map and the inverse map.
|
| 2030 |
2040 |
///
|
| 2031 |
2041 |
/// Erase the key from the map and the inverse map. It is called by the
|
| 2032 |
2042 |
/// \c AlterationNotifier.
|
| 2033 |
2043 |
virtual void erase(const Key& key) {
|
| 2034 |
2044 |
Value val = Map::operator[](key);
|
| 2035 |
|
typename Container::iterator it = _inv_map.find(val);
|
| 2036 |
|
if (it != _inv_map.end() && it->second == key) {
|
| 2037 |
|
_inv_map.erase(it);
|
|
2045 |
typename Container::iterator it;
|
|
2046 |
for (it = _inv_map.equal_range(val).first;
|
|
2047 |
it != _inv_map.equal_range(val).second; ++it) {
|
|
2048 |
if (it->second == key) {
|
|
2049 |
_inv_map.erase(it);
|
|
2050 |
break;
|
|
2051 |
}
|
| 2038 |
2052 |
}
|
| 2039 |
2053 |
Map::erase(key);
|
| 2040 |
2054 |
}
|
| 2041 |
2055 |
|
| 2042 |
2056 |
/// \brief Erase more keys from the map and the inverse map.
|
| 2043 |
2057 |
///
|
| 2044 |
2058 |
/// Erase more keys from the map and the inverse map. It is called by the
|
| 2045 |
2059 |
/// \c AlterationNotifier.
|
| 2046 |
2060 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2047 |
2061 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2048 |
2062 |
Value val = Map::operator[](keys[i]);
|
| 2049 |
|
typename Container::iterator it = _inv_map.find(val);
|
| 2050 |
|
if (it != _inv_map.end() && it->second == keys[i]) {
|
| 2051 |
|
_inv_map.erase(it);
|
|
2063 |
typename Container::iterator it;
|
|
2064 |
for (it = _inv_map.equal_range(val).first;
|
|
2065 |
it != _inv_map.equal_range(val).second; ++it) {
|
|
2066 |
if (it->second == keys[i]) {
|
|
2067 |
_inv_map.erase(it);
|
|
2068 |
break;
|
|
2069 |
}
|
| 2052 |
2070 |
}
|
| 2053 |
2071 |
}
|
| 2054 |
2072 |
Map::erase(keys);
|
| 2055 |
2073 |
}
|
| 2056 |
2074 |
|
| 2057 |
2075 |
/// \brief Clear the keys from the map and the inverse map.
|
| 2058 |
2076 |
///
|
| 2059 |
2077 |
/// Clear the keys from the map and the inverse map. It is called by the
|
| 2060 |
2078 |
/// \c AlterationNotifier.
|
| 2061 |
2079 |
virtual void clear() {
|
| 2062 |
2080 |
_inv_map.clear();
|
| 2063 |
2081 |
Map::clear();
|
| 2064 |
2082 |
}
|
| 2065 |
2083 |
|
| 2066 |
2084 |
public:
|
| 2067 |
2085 |
|
| 2068 |
2086 |
/// \brief The inverse map type.
|
| 2069 |
2087 |
///
|
| 2070 |
2088 |
/// The inverse of this map. The subscript operator of the map
|
| 2071 |
2089 |
/// gives back the item that was last assigned to the value.
|
| 2072 |
2090 |
class InverseMap {
|
| 2073 |
2091 |
public:
|
| 2074 |
2092 |
/// \brief Constructor
|
| 2075 |
2093 |
///
|
| 2076 |
2094 |
/// Constructor of the InverseMap.
|
| 2077 |
2095 |
explicit InverseMap(const CrossRefMap& inverted)
|
| 2078 |
2096 |
: _inverted(inverted) {}
|
| 2079 |
2097 |
|
| 2080 |
2098 |
/// The value type of the InverseMap.
|
| 2081 |
2099 |
typedef typename CrossRefMap::Key Value;
|
| 2082 |
2100 |
/// The key type of the InverseMap.
|
| 2083 |
2101 |
typedef typename CrossRefMap::Value Key;
|
| 2084 |
2102 |
|
| 2085 |
2103 |
/// \brief Subscript operator.
|
| 2086 |
2104 |
///
|
| 2087 |
|
/// Subscript operator. It gives back the item
|
| 2088 |
|
/// that was last assigned to the given value.
|
|
2105 |
/// Subscript operator. It gives back an item
|
|
2106 |
/// that is assigned to the given value or \c INVALID
|
|
2107 |
/// if no such item exists.
|
| 2089 |
2108 |
Value operator[](const Key& key) const {
|
| 2090 |
2109 |
return _inverted(key);
|
| 2091 |
2110 |
}
|
| 2092 |
2111 |
|
| 2093 |
2112 |
private:
|
| 2094 |
2113 |
const CrossRefMap& _inverted;
|
| 2095 |
2114 |
};
|
| 2096 |
2115 |
|
| 2097 |
2116 |
/// \brief It gives back the read-only inverse map.
|
| 2098 |
2117 |
///
|
| 2099 |
2118 |
/// It gives back the read-only inverse map.
|
| 2100 |
2119 |
InverseMap inverse() const {
|
| 2101 |
2120 |
return InverseMap(*this);
|
| 2102 |
2121 |
}
|
| 2103 |
2122 |
|
| 2104 |
2123 |
};
|
| 2105 |
2124 |
|
| 2106 |
2125 |
/// \brief Provides continuous and unique ID for the
|
| 2107 |
2126 |
/// items of a graph.
|
| 2108 |
2127 |
///
|
| 2109 |
2128 |
/// RangeIdMap provides a unique and continuous
|
| 2110 |
2129 |
/// ID for each item of a given type (\c Node, \c Arc or
|
| 2111 |
2130 |
/// \c Edge) in a graph. This id is
|
| 2112 |
2131 |
/// - \b unique: different items get different ids,
|
| 2113 |
2132 |
/// - \b continuous: the range of the ids is the set of integers
|
| 2114 |
2133 |
/// between 0 and \c n-1, where \c n is the number of the items of
|
| 2115 |
2134 |
/// this type (\c Node, \c Arc or \c Edge).
|
| 2116 |
2135 |
/// - So, the ids can change when deleting an item of the same type.
|
| 2117 |
2136 |
///
|
| 2118 |
2137 |
/// Thus this id is not (necessarily) the same as what can get using
|
| 2119 |
2138 |
/// the \c id() function of the graph or \ref IdMap.
|
| 2120 |
2139 |
/// This map can be inverted with its member class \c InverseMap,
|
| 2121 |
2140 |
/// or with the \c operator() member.
|
| 2122 |
2141 |
///
|
| 2123 |
2142 |
/// \tparam GR The graph type.
|
| 2124 |
2143 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
|
| 2125 |
2144 |
/// \c GR::Edge).
|
| 2126 |
2145 |
///
|
| 2127 |
2146 |
/// \see IdMap
|
| 2128 |
2147 |
template <typename GR, typename K>
|
| 2129 |
2148 |
class RangeIdMap
|
| 2130 |
2149 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
| 2131 |
2150 |
|
| 2132 |
2151 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
|
| 2133 |
2152 |
|
| 2134 |
2153 |
public:
|
| 2135 |
2154 |
/// The graph type of RangeIdMap.
|
| 2136 |
2155 |
typedef GR Graph;
|