0
2
0
... | ... |
@@ -1754,597 +1754,192 @@ |
1754 | 1754 |
/// For example it makes easier to store the nodes in the processing |
1755 | 1755 |
/// order of Dfs algorithm, as the following examples show. |
1756 | 1756 |
/// \code |
1757 | 1757 |
/// std::vector<Node> v; |
1758 | 1758 |
/// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
1759 | 1759 |
/// \endcode |
1760 | 1760 |
/// \code |
1761 | 1761 |
/// std::vector<Node> v(countNodes(g)); |
1762 | 1762 |
/// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run(); |
1763 | 1763 |
/// \endcode |
1764 | 1764 |
/// |
1765 | 1765 |
/// \note The container of the iterator must contain enough space |
1766 | 1766 |
/// for the elements or the iterator should be an inserter iterator. |
1767 | 1767 |
/// |
1768 | 1768 |
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so |
1769 | 1769 |
/// it cannot be used when a readable map is needed, for example as |
1770 | 1770 |
/// \c ReachedMap for \ref Bfs, \ref Dfs and \ref Dijkstra algorithms. |
1771 | 1771 |
/// |
1772 | 1772 |
/// \relates LoggerBoolMap |
1773 | 1773 |
template<typename Iterator> |
1774 | 1774 |
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) { |
1775 | 1775 |
return LoggerBoolMap<Iterator>(it); |
1776 | 1776 |
} |
1777 | 1777 |
|
1778 | 1778 |
/// Provides an immutable and unique id for each item in the graph. |
1779 | 1779 |
|
1780 | 1780 |
/// The IdMap class provides a unique and immutable id for each item of the |
1781 | 1781 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
1782 | 1782 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
1783 | 1783 |
/// item (node) does not change (even if you delete other nodes). </ul> |
1784 | 1784 |
/// Through this map you get access (i.e. can read) the inner id values of |
1785 | 1785 |
/// the items stored in the graph. This map can be inverted with its member |
1786 | 1786 |
/// class \c InverseMap or with the \c operator() member. |
1787 | 1787 |
/// |
1788 | 1788 |
template <typename _Graph, typename _Item> |
1789 | 1789 |
class IdMap { |
1790 | 1790 |
public: |
1791 | 1791 |
typedef _Graph Graph; |
1792 | 1792 |
typedef int Value; |
1793 | 1793 |
typedef _Item Item; |
1794 | 1794 |
typedef _Item Key; |
1795 | 1795 |
|
1796 | 1796 |
/// \brief Constructor. |
1797 | 1797 |
/// |
1798 | 1798 |
/// Constructor of the map. |
1799 | 1799 |
explicit IdMap(const Graph& graph) : _graph(&graph) {} |
1800 | 1800 |
|
1801 | 1801 |
/// \brief Gives back the \e id of the item. |
1802 | 1802 |
/// |
1803 | 1803 |
/// Gives back the immutable and unique \e id of the item. |
1804 | 1804 |
int operator[](const Item& item) const { return _graph->id(item);} |
1805 | 1805 |
|
1806 | 1806 |
/// \brief Gives back the item by its id. |
1807 | 1807 |
/// |
1808 | 1808 |
/// Gives back the item by its id. |
1809 | 1809 |
Item operator()(int id) { return _graph->fromId(id, Item()); } |
1810 | 1810 |
|
1811 | 1811 |
private: |
1812 | 1812 |
const Graph* _graph; |
1813 | 1813 |
|
1814 | 1814 |
public: |
1815 | 1815 |
|
1816 | 1816 |
/// \brief The class represents the inverse of its owner (IdMap). |
1817 | 1817 |
/// |
1818 | 1818 |
/// The class represents the inverse of its owner (IdMap). |
1819 | 1819 |
/// \see inverse() |
1820 | 1820 |
class InverseMap { |
1821 | 1821 |
public: |
1822 | 1822 |
|
1823 | 1823 |
/// \brief Constructor. |
1824 | 1824 |
/// |
1825 | 1825 |
/// Constructor for creating an id-to-item map. |
1826 | 1826 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
1827 | 1827 |
|
1828 | 1828 |
/// \brief Constructor. |
1829 | 1829 |
/// |
1830 | 1830 |
/// Constructor for creating an id-to-item map. |
1831 | 1831 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
1832 | 1832 |
|
1833 | 1833 |
/// \brief Gives back the given item from its id. |
1834 | 1834 |
/// |
1835 | 1835 |
/// Gives back the given item from its id. |
1836 | 1836 |
/// |
1837 | 1837 |
Item operator[](int id) const { return _graph->fromId(id, Item());} |
1838 | 1838 |
|
1839 | 1839 |
private: |
1840 | 1840 |
const Graph* _graph; |
1841 | 1841 |
}; |
1842 | 1842 |
|
1843 | 1843 |
/// \brief Gives back the inverse of the map. |
1844 | 1844 |
/// |
1845 | 1845 |
/// Gives back the inverse of the IdMap. |
1846 | 1846 |
InverseMap inverse() const { return InverseMap(*_graph);} |
1847 | 1847 |
|
1848 | 1848 |
}; |
1849 | 1849 |
|
1850 |
|
|
1851 |
/// \brief General invertable graph-map type. |
|
1852 |
|
|
1853 |
/// This type provides simple invertable graph-maps. |
|
1854 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
|
1855 |
/// and if a key is set to a new value then store it |
|
1856 |
/// in the inverse map. |
|
1857 |
/// |
|
1858 |
/// The values of the map can be accessed |
|
1859 |
/// with stl compatible forward iterator. |
|
1860 |
/// |
|
1861 |
/// \tparam _Graph The graph type. |
|
1862 |
/// \tparam _Item The item type of the graph. |
|
1863 |
/// \tparam _Value The value type of the map. |
|
1864 |
/// |
|
1865 |
/// \see IterableValueMap |
|
1866 |
template <typename _Graph, typename _Item, typename _Value> |
|
1867 |
class InvertableMap |
|
1868 |
: protected ItemSetTraits<_Graph, _Item>::template Map<_Value>::Type { |
|
1869 |
private: |
|
1870 |
|
|
1871 |
typedef typename ItemSetTraits<_Graph, _Item>:: |
|
1872 |
template Map<_Value>::Type Map; |
|
1873 |
typedef _Graph Graph; |
|
1874 |
|
|
1875 |
typedef std::map<_Value, _Item> Container; |
|
1876 |
Container _inv_map; |
|
1877 |
|
|
1878 |
public: |
|
1879 |
|
|
1880 |
/// The key type of InvertableMap (Node, Arc, Edge). |
|
1881 |
typedef typename Map::Key Key; |
|
1882 |
/// The value type of the InvertableMap. |
|
1883 |
typedef typename Map::Value Value; |
|
1884 |
|
|
1885 |
|
|
1886 |
|
|
1887 |
/// \brief Constructor. |
|
1888 |
/// |
|
1889 |
/// Construct a new InvertableMap for the graph. |
|
1890 |
/// |
|
1891 |
explicit InvertableMap(const Graph& graph) : Map(graph) {} |
|
1892 |
|
|
1893 |
/// \brief Forward iterator for values. |
|
1894 |
/// |
|
1895 |
/// This iterator is an stl compatible forward |
|
1896 |
/// iterator on the values of the map. The values can |
|
1897 |
/// be accessed in the [beginValue, endValue) range. |
|
1898 |
/// |
|
1899 |
class ValueIterator |
|
1900 |
: public std::iterator<std::forward_iterator_tag, Value> { |
|
1901 |
friend class InvertableMap; |
|
1902 |
private: |
|
1903 |
ValueIterator(typename Container::const_iterator _it) |
|
1904 |
: it(_it) {} |
|
1905 |
public: |
|
1906 |
|
|
1907 |
ValueIterator() {} |
|
1908 |
|
|
1909 |
ValueIterator& operator++() { ++it; return *this; } |
|
1910 |
ValueIterator operator++(int) { |
|
1911 |
ValueIterator tmp(*this); |
|
1912 |
operator++(); |
|
1913 |
return tmp; |
|
1914 |
} |
|
1915 |
|
|
1916 |
const Value& operator*() const { return it->first; } |
|
1917 |
const Value* operator->() const { return &(it->first); } |
|
1918 |
|
|
1919 |
bool operator==(ValueIterator jt) const { return it == jt.it; } |
|
1920 |
bool operator!=(ValueIterator jt) const { return it != jt.it; } |
|
1921 |
|
|
1922 |
private: |
|
1923 |
typename Container::const_iterator it; |
|
1924 |
}; |
|
1925 |
|
|
1926 |
/// \brief Returns an iterator to the first value. |
|
1927 |
/// |
|
1928 |
/// Returns an stl compatible iterator to the |
|
1929 |
/// first value of the map. The values of the |
|
1930 |
/// map can be accessed in the [beginValue, endValue) |
|
1931 |
/// range. |
|
1932 |
ValueIterator beginValue() const { |
|
1933 |
return ValueIterator(_inv_map.begin()); |
|
1934 |
} |
|
1935 |
|
|
1936 |
/// \brief Returns an iterator after the last value. |
|
1937 |
/// |
|
1938 |
/// Returns an stl compatible iterator after the |
|
1939 |
/// last value of the map. The values of the |
|
1940 |
/// map can be accessed in the [beginValue, endValue) |
|
1941 |
/// range. |
|
1942 |
ValueIterator endValue() const { |
|
1943 |
return ValueIterator(_inv_map.end()); |
|
1944 |
} |
|
1945 |
|
|
1946 |
/// \brief The setter function of the map. |
|
1947 |
/// |
|
1948 |
/// Sets the mapped value. |
|
1949 |
void set(const Key& key, const Value& val) { |
|
1950 |
Value oldval = Map::operator[](key); |
|
1951 |
typename Container::iterator it = _inv_map.find(oldval); |
|
1952 |
if (it != _inv_map.end() && it->second == key) { |
|
1953 |
_inv_map.erase(it); |
|
1954 |
} |
|
1955 |
_inv_map.insert(make_pair(val, key)); |
|
1956 |
Map::set(key, val); |
|
1957 |
} |
|
1958 |
|
|
1959 |
/// \brief The getter function of the map. |
|
1960 |
/// |
|
1961 |
/// It gives back the value associated with the key. |
|
1962 |
typename MapTraits<Map>::ConstReturnValue |
|
1963 |
operator[](const Key& key) const { |
|
1964 |
return Map::operator[](key); |
|
1965 |
} |
|
1966 |
|
|
1967 |
/// \brief Gives back the item by its value. |
|
1968 |
/// |
|
1969 |
/// Gives back the item by its value. |
|
1970 |
Key operator()(const Value& key) const { |
|
1971 |
typename Container::const_iterator it = _inv_map.find(key); |
|
1972 |
return it != _inv_map.end() ? it->second : INVALID; |
|
1973 |
} |
|
1974 |
|
|
1975 |
protected: |
|
1976 |
|
|
1977 |
/// \brief Erase the key from the map. |
|
1978 |
/// |
|
1979 |
/// Erase the key to the map. It is called by the |
|
1980 |
/// \c AlterationNotifier. |
|
1981 |
virtual void erase(const Key& key) { |
|
1982 |
Value val = Map::operator[](key); |
|
1983 |
typename Container::iterator it = _inv_map.find(val); |
|
1984 |
if (it != _inv_map.end() && it->second == key) { |
|
1985 |
_inv_map.erase(it); |
|
1986 |
} |
|
1987 |
Map::erase(key); |
|
1988 |
} |
|
1989 |
|
|
1990 |
/// \brief Erase more keys from the map. |
|
1991 |
/// |
|
1992 |
/// Erase more keys from the map. It is called by the |
|
1993 |
/// \c AlterationNotifier. |
|
1994 |
virtual void erase(const std::vector<Key>& keys) { |
|
1995 |
for (int i = 0; i < int(keys.size()); ++i) { |
|
1996 |
Value val = Map::operator[](keys[i]); |
|
1997 |
typename Container::iterator it = _inv_map.find(val); |
|
1998 |
if (it != _inv_map.end() && it->second == keys[i]) { |
|
1999 |
_inv_map.erase(it); |
|
2000 |
} |
|
2001 |
} |
|
2002 |
Map::erase(keys); |
|
2003 |
} |
|
2004 |
|
|
2005 |
/// \brief Clear the keys from the map and inverse map. |
|
2006 |
/// |
|
2007 |
/// Clear the keys from the map and inverse map. It is called by the |
|
2008 |
/// \c AlterationNotifier. |
|
2009 |
virtual void clear() { |
|
2010 |
_inv_map.clear(); |
|
2011 |
Map::clear(); |
|
2012 |
} |
|
2013 |
|
|
2014 |
public: |
|
2015 |
|
|
2016 |
/// \brief The inverse map type. |
|
2017 |
/// |
|
2018 |
/// The inverse of this map. The subscript operator of the map |
|
2019 |
/// gives back always the item what was last assigned to the value. |
|
2020 |
class InverseMap { |
|
2021 |
public: |
|
2022 |
/// \brief Constructor of the InverseMap. |
|
2023 |
/// |
|
2024 |
/// Constructor of the InverseMap. |
|
2025 |
explicit InverseMap(const InvertableMap& inverted) |
|
2026 |
: _inverted(inverted) {} |
|
2027 |
|
|
2028 |
/// The value type of the InverseMap. |
|
2029 |
typedef typename InvertableMap::Key Value; |
|
2030 |
/// The key type of the InverseMap. |
|
2031 |
typedef typename InvertableMap::Value Key; |
|
2032 |
|
|
2033 |
/// \brief Subscript operator. |
|
2034 |
/// |
|
2035 |
/// Subscript operator. It gives back always the item |
|
2036 |
/// what was last assigned to the value. |
|
2037 |
Value operator[](const Key& key) const { |
|
2038 |
return _inverted(key); |
|
2039 |
} |
|
2040 |
|
|
2041 |
private: |
|
2042 |
const InvertableMap& _inverted; |
|
2043 |
}; |
|
2044 |
|
|
2045 |
/// \brief It gives back the just readable inverse map. |
|
2046 |
/// |
|
2047 |
/// It gives back the just readable inverse map. |
|
2048 |
InverseMap inverse() const { |
|
2049 |
return InverseMap(*this); |
|
2050 |
} |
|
2051 |
|
|
2052 |
|
|
2053 |
|
|
2054 |
}; |
|
2055 |
|
|
2056 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
|
2057 |
/// item in the graph. |
|
2058 |
/// |
|
2059 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
|
2060 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
|
2061 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
|
2062 |
/// different ids <li>\b continuous: the range of the ids is the set of |
|
2063 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
|
2064 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
|
2065 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
|
2066 |
/// with its member class \c InverseMap, or with the \c operator() member. |
|
2067 |
/// |
|
2068 |
/// \tparam _Graph The graph class the \c DescriptorMap belongs to. |
|
2069 |
/// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or |
|
2070 |
/// Edge. |
|
2071 |
template <typename _Graph, typename _Item> |
|
2072 |
class DescriptorMap |
|
2073 |
: protected ItemSetTraits<_Graph, _Item>::template Map<int>::Type { |
|
2074 |
|
|
2075 |
typedef _Item Item; |
|
2076 |
typedef typename ItemSetTraits<_Graph, _Item>::template Map<int>::Type Map; |
|
2077 |
|
|
2078 |
public: |
|
2079 |
/// The graph class of DescriptorMap. |
|
2080 |
typedef _Graph Graph; |
|
2081 |
|
|
2082 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
|
2083 |
typedef typename Map::Key Key; |
|
2084 |
/// The value type of DescriptorMap. |
|
2085 |
typedef typename Map::Value Value; |
|
2086 |
|
|
2087 |
/// \brief Constructor. |
|
2088 |
/// |
|
2089 |
/// Constructor for descriptor map. |
|
2090 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) { |
|
2091 |
Item it; |
|
2092 |
const typename Map::Notifier* nf = Map::notifier(); |
|
2093 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
2094 |
Map::set(it, _inv_map.size()); |
|
2095 |
_inv_map.push_back(it); |
|
2096 |
} |
|
2097 |
} |
|
2098 |
|
|
2099 |
protected: |
|
2100 |
|
|
2101 |
/// \brief Add a new key to the map. |
|
2102 |
/// |
|
2103 |
/// Add a new key to the map. It is called by the |
|
2104 |
/// \c AlterationNotifier. |
|
2105 |
virtual void add(const Item& item) { |
|
2106 |
Map::add(item); |
|
2107 |
Map::set(item, _inv_map.size()); |
|
2108 |
_inv_map.push_back(item); |
|
2109 |
} |
|
2110 |
|
|
2111 |
/// \brief Add more new keys to the map. |
|
2112 |
/// |
|
2113 |
/// Add more new keys to the map. It is called by the |
|
2114 |
/// \c AlterationNotifier. |
|
2115 |
virtual void add(const std::vector<Item>& items) { |
|
2116 |
Map::add(items); |
|
2117 |
for (int i = 0; i < int(items.size()); ++i) { |
|
2118 |
Map::set(items[i], _inv_map.size()); |
|
2119 |
_inv_map.push_back(items[i]); |
|
2120 |
} |
|
2121 |
} |
|
2122 |
|
|
2123 |
/// \brief Erase the key from the map. |
|
2124 |
/// |
|
2125 |
/// Erase the key from the map. It is called by the |
|
2126 |
/// \c AlterationNotifier. |
|
2127 |
virtual void erase(const Item& item) { |
|
2128 |
Map::set(_inv_map.back(), Map::operator[](item)); |
|
2129 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
|
2130 |
_inv_map.pop_back(); |
|
2131 |
Map::erase(item); |
|
2132 |
} |
|
2133 |
|
|
2134 |
/// \brief Erase more keys from the map. |
|
2135 |
/// |
|
2136 |
/// Erase more keys from the map. It is called by the |
|
2137 |
/// \c AlterationNotifier. |
|
2138 |
virtual void erase(const std::vector<Item>& items) { |
|
2139 |
for (int i = 0; i < int(items.size()); ++i) { |
|
2140 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
|
2141 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
|
2142 |
_inv_map.pop_back(); |
|
2143 |
} |
|
2144 |
Map::erase(items); |
|
2145 |
} |
|
2146 |
|
|
2147 |
/// \brief Build the unique map. |
|
2148 |
/// |
|
2149 |
/// Build the unique map. It is called by the |
|
2150 |
/// \c AlterationNotifier. |
|
2151 |
virtual void build() { |
|
2152 |
Map::build(); |
|
2153 |
Item it; |
|
2154 |
const typename Map::Notifier* nf = Map::notifier(); |
|
2155 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
|
2156 |
Map::set(it, _inv_map.size()); |
|
2157 |
_inv_map.push_back(it); |
|
2158 |
} |
|
2159 |
} |
|
2160 |
|
|
2161 |
/// \brief Clear the keys from the map. |
|
2162 |
/// |
|
2163 |
/// Clear the keys from the map. It is called by the |
|
2164 |
/// \c AlterationNotifier. |
|
2165 |
virtual void clear() { |
|
2166 |
_inv_map.clear(); |
|
2167 |
Map::clear(); |
|
2168 |
} |
|
2169 |
|
|
2170 |
public: |
|
2171 |
|
|
2172 |
/// \brief Returns the maximal value plus one. |
|
2173 |
/// |
|
2174 |
/// Returns the maximal value plus one in the map. |
|
2175 |
unsigned int size() const { |
|
2176 |
return _inv_map.size(); |
|
2177 |
} |
|
2178 |
|
|
2179 |
/// \brief Swaps the position of the two items in the map. |
|
2180 |
/// |
|
2181 |
/// Swaps the position of the two items in the map. |
|
2182 |
void swap(const Item& p, const Item& q) { |
|
2183 |
int pi = Map::operator[](p); |
|
2184 |
int qi = Map::operator[](q); |
|
2185 |
Map::set(p, qi); |
|
2186 |
_inv_map[qi] = p; |
|
2187 |
Map::set(q, pi); |
|
2188 |
_inv_map[pi] = q; |
|
2189 |
} |
|
2190 |
|
|
2191 |
/// \brief Gives back the \e descriptor of the item. |
|
2192 |
/// |
|
2193 |
/// Gives back the mutable and unique \e descriptor of the map. |
|
2194 |
int operator[](const Item& item) const { |
|
2195 |
return Map::operator[](item); |
|
2196 |
} |
|
2197 |
|
|
2198 |
/// \brief Gives back the item by its descriptor. |
|
2199 |
/// |
|
2200 |
/// Gives back th item by its descriptor. |
|
2201 |
Item operator()(int id) const { |
|
2202 |
return _inv_map[id]; |
|
2203 |
} |
|
2204 |
|
|
2205 |
private: |
|
2206 |
|
|
2207 |
typedef std::vector<Item> Container; |
|
2208 |
Container _inv_map; |
|
2209 |
|
|
2210 |
public: |
|
2211 |
/// \brief The inverse map type of DescriptorMap. |
|
2212 |
/// |
|
2213 |
/// The inverse map type of DescriptorMap. |
|
2214 |
class InverseMap { |
|
2215 |
public: |
|
2216 |
/// \brief Constructor of the InverseMap. |
|
2217 |
/// |
|
2218 |
/// Constructor of the InverseMap. |
|
2219 |
explicit InverseMap(const DescriptorMap& inverted) |
|
2220 |
: _inverted(inverted) {} |
|
2221 |
|
|
2222 |
|
|
2223 |
/// The value type of the InverseMap. |
|
2224 |
typedef typename DescriptorMap::Key Value; |
|
2225 |
/// The key type of the InverseMap. |
|
2226 |
typedef typename DescriptorMap::Value Key; |
|
2227 |
|
|
2228 |
/// \brief Subscript operator. |
|
2229 |
/// |
|
2230 |
/// Subscript operator. It gives back the item |
|
2231 |
/// that the descriptor belongs to currently. |
|
2232 |
Value operator[](const Key& key) const { |
|
2233 |
return _inverted(key); |
|
2234 |
} |
|
2235 |
|
|
2236 |
/// \brief Size of the map. |
|
2237 |
/// |
|
2238 |
/// Returns the size of the map. |
|
2239 |
unsigned int size() const { |
|
2240 |
return _inverted.size(); |
|
2241 |
} |
|
2242 |
|
|
2243 |
private: |
|
2244 |
const DescriptorMap& _inverted; |
|
2245 |
}; |
|
2246 |
|
|
2247 |
/// \brief Gives back the inverse of the map. |
|
2248 |
/// |
|
2249 |
/// Gives back the inverse of the map. |
|
2250 |
const InverseMap inverse() const { |
|
2251 |
return InverseMap(*this); |
|
2252 |
} |
|
2253 |
}; |
|
2254 |
|
|
2255 | 1850 |
/// \brief Returns the source of the given arc. |
2256 | 1851 |
/// |
2257 | 1852 |
/// The SourceMap gives back the source Node of the given arc. |
2258 | 1853 |
/// \see TargetMap |
2259 | 1854 |
template <typename Digraph> |
2260 | 1855 |
class SourceMap { |
2261 | 1856 |
public: |
2262 | 1857 |
|
2263 | 1858 |
typedef typename Digraph::Node Value; |
2264 | 1859 |
typedef typename Digraph::Arc Key; |
2265 | 1860 |
|
2266 | 1861 |
/// \brief Constructor |
2267 | 1862 |
/// |
2268 | 1863 |
/// Constructor |
2269 | 1864 |
/// \param _digraph The digraph that the map belongs to. |
2270 | 1865 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} |
2271 | 1866 |
|
2272 | 1867 |
/// \brief The subscript operator. |
2273 | 1868 |
/// |
2274 | 1869 |
/// The subscript operator. |
2275 | 1870 |
/// \param arc The arc |
2276 | 1871 |
/// \return The source of the arc |
2277 | 1872 |
Value operator[](const Key& arc) const { |
2278 | 1873 |
return _digraph.source(arc); |
2279 | 1874 |
} |
2280 | 1875 |
|
2281 | 1876 |
private: |
2282 | 1877 |
const Digraph& _digraph; |
2283 | 1878 |
}; |
2284 | 1879 |
|
2285 | 1880 |
/// \brief Returns a \ref SourceMap class. |
2286 | 1881 |
/// |
2287 | 1882 |
/// This function just returns an \ref SourceMap class. |
2288 | 1883 |
/// \relates SourceMap |
2289 | 1884 |
template <typename Digraph> |
2290 | 1885 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) { |
2291 | 1886 |
return SourceMap<Digraph>(digraph); |
2292 | 1887 |
} |
2293 | 1888 |
|
2294 | 1889 |
/// \brief Returns the target of the given arc. |
2295 | 1890 |
/// |
2296 | 1891 |
/// The TargetMap gives back the target Node of the given arc. |
2297 | 1892 |
/// \see SourceMap |
2298 | 1893 |
template <typename Digraph> |
2299 | 1894 |
class TargetMap { |
2300 | 1895 |
public: |
2301 | 1896 |
|
2302 | 1897 |
typedef typename Digraph::Node Value; |
2303 | 1898 |
typedef typename Digraph::Arc Key; |
2304 | 1899 |
|
2305 | 1900 |
/// \brief Constructor |
2306 | 1901 |
/// |
2307 | 1902 |
/// Constructor |
2308 | 1903 |
/// \param _digraph The digraph that the map belongs to. |
2309 | 1904 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} |
2310 | 1905 |
|
2311 | 1906 |
/// \brief The subscript operator. |
2312 | 1907 |
/// |
2313 | 1908 |
/// The subscript operator. |
2314 | 1909 |
/// \param e The arc |
2315 | 1910 |
/// \return The target of the arc |
2316 | 1911 |
Value operator[](const Key& e) const { |
2317 | 1912 |
return _digraph.target(e); |
2318 | 1913 |
} |
2319 | 1914 |
|
2320 | 1915 |
private: |
2321 | 1916 |
const Digraph& _digraph; |
2322 | 1917 |
}; |
2323 | 1918 |
|
2324 | 1919 |
/// \brief Returns a \ref TargetMap class. |
2325 | 1920 |
/// |
2326 | 1921 |
/// This function just returns a \ref TargetMap class. |
2327 | 1922 |
/// \relates TargetMap |
2328 | 1923 |
template <typename Digraph> |
2329 | 1924 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) { |
2330 | 1925 |
return TargetMap<Digraph>(digraph); |
2331 | 1926 |
} |
2332 | 1927 |
|
2333 | 1928 |
/// \brief Returns the "forward" directed arc view of an edge. |
2334 | 1929 |
/// |
2335 | 1930 |
/// Returns the "forward" directed arc view of an edge. |
2336 | 1931 |
/// \see BackwardMap |
2337 | 1932 |
template <typename Graph> |
2338 | 1933 |
class ForwardMap { |
2339 | 1934 |
public: |
2340 | 1935 |
|
2341 | 1936 |
typedef typename Graph::Arc Value; |
2342 | 1937 |
typedef typename Graph::Edge Key; |
2343 | 1938 |
|
2344 | 1939 |
/// \brief Constructor |
2345 | 1940 |
/// |
2346 | 1941 |
/// Constructor |
2347 | 1942 |
/// \param _graph The graph that the map belongs to. |
2348 | 1943 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {} |
2349 | 1944 |
|
2350 | 1945 |
/// \brief The subscript operator. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <cstdlib> |
20 | 20 |
#include <ctime> |
21 | 21 |
|
22 | 22 |
#include <lemon/random.h> |
23 | 23 |
#include <lemon/list_graph.h> |
24 | 24 |
#include <lemon/smart_graph.h> |
25 | 25 |
#include <lemon/maps.h> |
26 | 26 |
|
27 | 27 |
#include "graph_test.h" |
28 | 28 |
#include "test_tools.h" |
29 | 29 |
|
30 | 30 |
using namespace lemon; |
31 | 31 |
|
32 | 32 |
template <typename Digraph> |
33 | 33 |
void checkFindArcs() { |
34 | 34 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
35 | 35 |
|
36 | 36 |
{ |
37 | 37 |
Digraph digraph; |
38 |
typename Digraph::template NodeMap<int> nodes(digraph); |
|
39 |
std::vector<Node> invNodes; |
|
38 | 40 |
for (int i = 0; i < 10; ++i) { |
39 |
digraph.addNode(); |
|
41 |
invNodes.push_back(digraph.addNode()); |
|
42 |
nodes[invNodes.back()]=invNodes.size()-1; |
|
40 | 43 |
} |
41 |
DescriptorMap<Digraph, Node> nodes(digraph); |
|
42 |
typename DescriptorMap<Digraph, Node>::InverseMap invNodes(nodes); |
|
43 | 44 |
for (int i = 0; i < 100; ++i) { |
44 | 45 |
int src = rnd[invNodes.size()]; |
45 | 46 |
int trg = rnd[invNodes.size()]; |
46 | 47 |
digraph.addArc(invNodes[src], invNodes[trg]); |
47 | 48 |
} |
48 | 49 |
typename Digraph::template ArcMap<bool> found(digraph, false); |
49 |
DescriptorMap<Digraph, Arc> arcs(digraph); |
|
50 | 50 |
for (NodeIt src(digraph); src != INVALID; ++src) { |
51 | 51 |
for (NodeIt trg(digraph); trg != INVALID; ++trg) { |
52 | 52 |
for (ConArcIt<Digraph> con(digraph, src, trg); con != INVALID; ++con) { |
53 | 53 |
check(digraph.source(con) == src, "Wrong source."); |
54 | 54 |
check(digraph.target(con) == trg, "Wrong target."); |
55 | 55 |
check(found[con] == false, "The arc found already."); |
56 | 56 |
found[con] = true; |
57 | 57 |
} |
58 | 58 |
} |
59 | 59 |
} |
60 | 60 |
for (ArcIt it(digraph); it != INVALID; ++it) { |
61 | 61 |
check(found[it] == true, "The arc is not found."); |
62 | 62 |
} |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
{ |
66 | 66 |
int num = 5; |
67 | 67 |
Digraph fg; |
68 | 68 |
std::vector<Node> nodes; |
69 | 69 |
for (int i = 0; i < num; ++i) { |
70 | 70 |
nodes.push_back(fg.addNode()); |
71 | 71 |
} |
72 | 72 |
for (int i = 0; i < num * num; ++i) { |
73 | 73 |
fg.addArc(nodes[i / num], nodes[i % num]); |
74 | 74 |
} |
75 | 75 |
check(countNodes(fg) == num, "Wrong node number."); |
76 | 76 |
check(countArcs(fg) == num*num, "Wrong arc number."); |
77 | 77 |
for (NodeIt src(fg); src != INVALID; ++src) { |
78 | 78 |
for (NodeIt trg(fg); trg != INVALID; ++trg) { |
79 | 79 |
ConArcIt<Digraph> con(fg, src, trg); |
80 | 80 |
check(con != INVALID, "There is no connecting arc."); |
81 | 81 |
check(fg.source(con) == src, "Wrong source."); |
82 | 82 |
check(fg.target(con) == trg, "Wrong target."); |
83 | 83 |
check(++con == INVALID, "There is more connecting arc."); |
84 | 84 |
} |
85 | 85 |
} |
86 | 86 |
ArcLookUp<Digraph> al1(fg); |
87 | 87 |
DynArcLookUp<Digraph> al2(fg); |
88 | 88 |
AllArcLookUp<Digraph> al3(fg); |
89 | 89 |
for (NodeIt src(fg); src != INVALID; ++src) { |
90 | 90 |
for (NodeIt trg(fg); trg != INVALID; ++trg) { |
91 | 91 |
Arc con1 = al1(src, trg); |
92 | 92 |
Arc con2 = al2(src, trg); |
93 | 93 |
Arc con3 = al3(src, trg); |
94 | 94 |
Arc con4 = findArc(fg, src, trg); |
95 | 95 |
check(con1 == con2 && con2 == con3 && con3 == con4, |
96 | 96 |
"Different results.") |
97 | 97 |
check(con1 != INVALID, "There is no connecting arc."); |
98 | 98 |
check(fg.source(con1) == src, "Wrong source."); |
99 | 99 |
check(fg.target(con1) == trg, "Wrong target."); |
100 | 100 |
check(al3(src, trg, con3) == INVALID, |
101 | 101 |
"There is more connecting arc."); |
102 | 102 |
check(findArc(fg, src, trg, con4) == INVALID, |
103 | 103 |
"There is more connecting arc."); |
104 | 104 |
} |
105 | 105 |
} |
106 | 106 |
} |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
template <typename Graph> |
110 | 110 |
void checkFindEdges() { |
111 | 111 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
112 | 112 |
Graph graph; |
113 |
typename Graph::template NodeMap<int> nodes(graph); |
|
114 |
std::vector<Node> invNodes; |
|
113 | 115 |
for (int i = 0; i < 10; ++i) { |
114 |
graph.addNode(); |
|
116 |
invNodes.push_back(graph.addNode()); |
|
117 |
nodes[invNodes.back()]=invNodes.size()-1; |
|
115 | 118 |
} |
116 |
DescriptorMap<Graph, Node> nodes(graph); |
|
117 |
typename DescriptorMap<Graph, Node>::InverseMap invNodes(nodes); |
|
118 | 119 |
for (int i = 0; i < 100; ++i) { |
119 | 120 |
int src = rnd[invNodes.size()]; |
120 | 121 |
int trg = rnd[invNodes.size()]; |
121 | 122 |
graph.addEdge(invNodes[src], invNodes[trg]); |
122 | 123 |
} |
123 | 124 |
typename Graph::template EdgeMap<int> found(graph, 0); |
124 |
DescriptorMap<Graph, Edge> edges(graph); |
|
125 | 125 |
for (NodeIt src(graph); src != INVALID; ++src) { |
126 | 126 |
for (NodeIt trg(graph); trg != INVALID; ++trg) { |
127 | 127 |
for (ConEdgeIt<Graph> con(graph, src, trg); con != INVALID; ++con) { |
128 | 128 |
check( (graph.u(con) == src && graph.v(con) == trg) || |
129 | 129 |
(graph.v(con) == src && graph.u(con) == trg), |
130 | 130 |
"Wrong end nodes."); |
131 | 131 |
++found[con]; |
132 | 132 |
check(found[con] <= 2, "The edge found more than twice."); |
133 | 133 |
} |
134 | 134 |
} |
135 | 135 |
} |
136 | 136 |
for (EdgeIt it(graph); it != INVALID; ++it) { |
137 | 137 |
check( (graph.u(it) != graph.v(it) && found[it] == 2) || |
138 | 138 |
(graph.u(it) == graph.v(it) && found[it] == 1), |
139 | 139 |
"The edge is not found correctly."); |
140 | 140 |
} |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
template <class Digraph> |
144 | 144 |
void checkDeg() |
145 | 145 |
{ |
146 | 146 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
147 | 147 |
|
148 | 148 |
const int nodeNum = 10; |
149 | 149 |
const int arcNum = 100; |
150 | 150 |
Digraph digraph; |
151 | 151 |
InDegMap<Digraph> inDeg(digraph); |
152 | 152 |
OutDegMap<Digraph> outDeg(digraph); |
153 | 153 |
std::vector<Node> nodes(nodeNum); |
154 | 154 |
for (int i = 0; i < nodeNum; ++i) { |
155 | 155 |
nodes[i] = digraph.addNode(); |
156 | 156 |
} |
157 | 157 |
std::vector<Arc> arcs(arcNum); |
158 | 158 |
for (int i = 0; i < arcNum; ++i) { |
159 | 159 |
arcs[i] = digraph.addArc(nodes[rnd[nodeNum]], nodes[rnd[nodeNum]]); |
160 | 160 |
} |
161 | 161 |
for (int i = 0; i < nodeNum; ++i) { |
162 | 162 |
check(inDeg[nodes[i]] == countInArcs(digraph, nodes[i]), |
163 | 163 |
"Wrong in degree map"); |
164 | 164 |
} |
165 | 165 |
for (int i = 0; i < nodeNum; ++i) { |
166 | 166 |
check(outDeg[nodes[i]] == countOutArcs(digraph, nodes[i]), |
167 | 167 |
"Wrong out degree map"); |
168 | 168 |
} |
169 | 169 |
} |
170 | 170 |
|
171 | 171 |
template <class Digraph> |
172 | 172 |
void checkSnapDeg() |
173 | 173 |
{ |
174 | 174 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
175 | 175 |
|
176 | 176 |
Digraph g; |
177 | 177 |
Node n1=g.addNode(); |
178 | 178 |
Node n2=g.addNode(); |
179 | 179 |
|
180 | 180 |
InDegMap<Digraph> ind(g); |
181 | 181 |
|
182 | 182 |
g.addArc(n1,n2); |
183 | 183 |
|
184 | 184 |
typename Digraph::Snapshot snap(g); |
185 | 185 |
|
186 | 186 |
OutDegMap<Digraph> outd(g); |
187 | 187 |
|
188 | 188 |
check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value."); |
189 | 189 |
check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value."); |
190 | 190 |
|
191 | 191 |
g.addArc(n1,n2); |
192 | 192 |
g.addArc(n2,n1); |
193 | 193 |
|
194 | 194 |
check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value."); |
195 | 195 |
check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value."); |
196 | 196 |
|
197 | 197 |
snap.restore(); |
198 | 198 |
|
199 | 199 |
check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value."); |
200 | 200 |
check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value."); |
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
int main() { |
204 | 204 |
// Checking ConArcIt, ConEdgeIt, ArcLookUp, AllArcLookUp, and DynArcLookUp |
205 | 205 |
checkFindArcs<ListDigraph>(); |
206 | 206 |
checkFindArcs<SmartDigraph>(); |
207 | 207 |
checkFindEdges<ListGraph>(); |
208 | 208 |
checkFindEdges<SmartGraph>(); |
209 | 209 |
|
210 | 210 |
// Checking In/OutDegMap (and Snapshot feature) |
211 | 211 |
checkDeg<ListDigraph>(); |
212 | 212 |
checkDeg<SmartDigraph>(); |
213 | 213 |
checkSnapDeg<ListDigraph>(); |
214 | 214 |
checkSnapDeg<SmartDigraph>(); |
215 | 215 |
|
216 | 216 |
return 0; |
217 | 217 |
} |
0 comments (0 inline)