0
2
0
126
117
| 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-2009 |
| 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 |
#ifndef LEMON_MAPS_H |
| 20 | 20 |
#define LEMON_MAPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
#include <functional> |
| 24 | 24 |
#include <vector> |
| 25 |
#include <map> |
|
| 25 | 26 |
|
| 26 | 27 |
#include <lemon/core.h> |
| 27 |
#include <lemon/smart_graph.h> |
|
| 28 | 28 |
|
| 29 | 29 |
///\file |
| 30 | 30 |
///\ingroup maps |
| 31 | 31 |
///\brief Miscellaneous property maps |
| 32 | 32 |
|
| 33 |
#include <map> |
|
| 34 |
|
|
| 35 | 33 |
namespace lemon {
|
| 36 | 34 |
|
| 37 | 35 |
/// \addtogroup maps |
| 38 | 36 |
/// @{
|
| 39 | 37 |
|
| 40 | 38 |
/// Base class of maps. |
| 41 | 39 |
|
| 42 | 40 |
/// Base class of maps. It provides the necessary type definitions |
| 43 | 41 |
/// required by the map %concepts. |
| 44 | 42 |
template<typename K, typename V> |
| 45 | 43 |
class MapBase {
|
| 46 | 44 |
public: |
| 47 | 45 |
/// \brief The key type of the map. |
| 48 | 46 |
typedef K Key; |
| 49 | 47 |
/// \brief The value type of the map. |
| 50 | 48 |
/// (The type of objects associated with the keys). |
| 51 | 49 |
typedef V Value; |
| 52 | 50 |
}; |
| 53 | 51 |
|
| 54 | 52 |
|
| 55 | 53 |
/// Null map. (a.k.a. DoNothingMap) |
| 56 | 54 |
|
| 57 | 55 |
/// This map can be used if you have to provide a map only for |
| 58 | 56 |
/// its type definitions, or if you have to provide a writable map, |
| 59 | 57 |
/// but data written to it is not required (i.e. it will be sent to |
| 60 | 58 |
/// <tt>/dev/null</tt>). |
| 61 | 59 |
/// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 62 | 60 |
/// |
| 63 | 61 |
/// \sa ConstMap |
| 64 | 62 |
template<typename K, typename V> |
| 65 | 63 |
class NullMap : public MapBase<K, V> {
|
| 66 | 64 |
public: |
| 67 | 65 |
///\e |
| 68 | 66 |
typedef K Key; |
| 69 | 67 |
///\e |
| 70 | 68 |
typedef V Value; |
| 71 | 69 |
|
| 72 | 70 |
/// Gives back a default constructed element. |
| 73 | 71 |
Value operator[](const Key&) const { return Value(); }
|
| 74 | 72 |
/// Absorbs the value. |
| 75 | 73 |
void set(const Key&, const Value&) {}
|
| 76 | 74 |
}; |
| 77 | 75 |
|
| 78 | 76 |
/// Returns a \c NullMap class |
| 79 | 77 |
|
| 80 | 78 |
/// This function just returns a \c NullMap class. |
| 81 | 79 |
/// \relates NullMap |
| 82 | 80 |
template <typename K, typename V> |
| 83 | 81 |
NullMap<K, V> nullMap() {
|
| 84 | 82 |
return NullMap<K, V>(); |
| 85 | 83 |
} |
| 86 | 84 |
|
| 87 | 85 |
|
| 88 | 86 |
/// Constant map. |
| 89 | 87 |
|
| 90 | 88 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
| 91 | 89 |
/// value to each key. |
| 92 | 90 |
/// |
| 93 | 91 |
/// In other aspects it is equivalent to \c NullMap. |
| 94 | 92 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
| 95 | 93 |
/// concept, but it absorbs the data written to it. |
| 96 | 94 |
/// |
| 97 | 95 |
/// The simplest way of using this map is through the constMap() |
| 98 | 96 |
/// function. |
| ... | ... |
@@ -1845,132 +1843,134 @@ |
| 1845 | 1843 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge). |
| 1846 | 1844 |
typedef K Key; |
| 1847 | 1845 |
/// The value type of IdMap. |
| 1848 | 1846 |
typedef int Value; |
| 1849 | 1847 |
|
| 1850 | 1848 |
/// \brief Constructor. |
| 1851 | 1849 |
/// |
| 1852 | 1850 |
/// Constructor of the map. |
| 1853 | 1851 |
explicit IdMap(const Graph& graph) : _graph(&graph) {}
|
| 1854 | 1852 |
|
| 1855 | 1853 |
/// \brief Gives back the \e id of the item. |
| 1856 | 1854 |
/// |
| 1857 | 1855 |
/// Gives back the immutable and unique \e id of the item. |
| 1858 | 1856 |
int operator[](const Item& item) const { return _graph->id(item);}
|
| 1859 | 1857 |
|
| 1860 | 1858 |
/// \brief Gives back the \e item by its id. |
| 1861 | 1859 |
/// |
| 1862 | 1860 |
/// Gives back the \e item by its id. |
| 1863 | 1861 |
Item operator()(int id) { return _graph->fromId(id, Item()); }
|
| 1864 | 1862 |
|
| 1865 | 1863 |
private: |
| 1866 | 1864 |
const Graph* _graph; |
| 1867 | 1865 |
|
| 1868 | 1866 |
public: |
| 1869 | 1867 |
|
| 1870 | 1868 |
/// \brief This class represents the inverse of its owner (IdMap). |
| 1871 | 1869 |
/// |
| 1872 | 1870 |
/// This class represents the inverse of its owner (IdMap). |
| 1873 | 1871 |
/// \see inverse() |
| 1874 | 1872 |
class InverseMap {
|
| 1875 | 1873 |
public: |
| 1876 | 1874 |
|
| 1877 | 1875 |
/// \brief Constructor. |
| 1878 | 1876 |
/// |
| 1879 | 1877 |
/// Constructor for creating an id-to-item map. |
| 1880 | 1878 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {}
|
| 1881 | 1879 |
|
| 1882 | 1880 |
/// \brief Constructor. |
| 1883 | 1881 |
/// |
| 1884 | 1882 |
/// Constructor for creating an id-to-item map. |
| 1885 | 1883 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
|
| 1886 | 1884 |
|
| 1887 | 1885 |
/// \brief Gives back the given item from its id. |
| 1888 | 1886 |
/// |
| 1889 | 1887 |
/// Gives back the given item from its id. |
| 1890 | 1888 |
Item operator[](int id) const { return _graph->fromId(id, Item());}
|
| 1891 | 1889 |
|
| 1892 | 1890 |
private: |
| 1893 | 1891 |
const Graph* _graph; |
| 1894 | 1892 |
}; |
| 1895 | 1893 |
|
| 1896 | 1894 |
/// \brief Gives back the inverse of the map. |
| 1897 | 1895 |
/// |
| 1898 | 1896 |
/// Gives back the inverse of the IdMap. |
| 1899 | 1897 |
InverseMap inverse() const { return InverseMap(*_graph);}
|
| 1900 | 1898 |
}; |
| 1901 | 1899 |
|
| 1902 | 1900 |
|
| 1903 | 1901 |
/// \brief General cross reference graph map type. |
| 1904 | 1902 |
|
| 1905 | 1903 |
/// This class provides simple invertable graph maps. |
| 1906 | 1904 |
/// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap" |
| 1907 | 1905 |
/// and if a key is set to a new value then store it |
| 1908 | 1906 |
/// in the inverse map. |
| 1909 |
/// |
|
| 1910 | 1907 |
/// The values of the map can be accessed |
| 1911 | 1908 |
/// with stl compatible forward iterator. |
| 1912 | 1909 |
/// |
| 1910 |
/// This type is not reference map, so it cannot be modified with |
|
| 1911 |
/// the subscription operator. |
|
| 1912 |
/// |
|
| 1913 | 1913 |
/// \tparam GR The graph type. |
| 1914 | 1914 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 1915 | 1915 |
/// \c GR::Edge). |
| 1916 | 1916 |
/// \tparam V The value type of the map. |
| 1917 | 1917 |
/// |
| 1918 | 1918 |
/// \see IterableValueMap |
| 1919 | 1919 |
template <typename GR, typename K, typename V> |
| 1920 | 1920 |
class CrossRefMap |
| 1921 | 1921 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type {
|
| 1922 | 1922 |
private: |
| 1923 | 1923 |
|
| 1924 | 1924 |
typedef typename ItemSetTraits<GR, K>:: |
| 1925 | 1925 |
template Map<V>::Type Map; |
| 1926 | 1926 |
|
| 1927 | 1927 |
typedef std::map<V, K> Container; |
| 1928 | 1928 |
Container _inv_map; |
| 1929 | 1929 |
|
| 1930 | 1930 |
public: |
| 1931 | 1931 |
|
| 1932 | 1932 |
/// The graph type of CrossRefMap. |
| 1933 | 1933 |
typedef GR Graph; |
| 1934 | 1934 |
typedef GR Digraph; |
| 1935 | 1935 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). |
| 1936 | 1936 |
typedef K Item; |
| 1937 | 1937 |
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). |
| 1938 | 1938 |
typedef K Key; |
| 1939 | 1939 |
/// The value type of CrossRefMap. |
| 1940 | 1940 |
typedef V Value; |
| 1941 | 1941 |
|
| 1942 | 1942 |
/// \brief Constructor. |
| 1943 | 1943 |
/// |
| 1944 | 1944 |
/// Construct a new CrossRefMap for the given graph. |
| 1945 | 1945 |
explicit CrossRefMap(const Graph& graph) : Map(graph) {}
|
| 1946 | 1946 |
|
| 1947 | 1947 |
/// \brief Forward iterator for values. |
| 1948 | 1948 |
/// |
| 1949 | 1949 |
/// This iterator is an stl compatible forward |
| 1950 | 1950 |
/// iterator on the values of the map. The values can |
| 1951 | 1951 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range. |
| 1952 | 1952 |
class ValueIterator |
| 1953 | 1953 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 1954 | 1954 |
friend class CrossRefMap; |
| 1955 | 1955 |
private: |
| 1956 | 1956 |
ValueIterator(typename Container::const_iterator _it) |
| 1957 | 1957 |
: it(_it) {}
|
| 1958 | 1958 |
public: |
| 1959 | 1959 |
|
| 1960 | 1960 |
ValueIterator() {}
|
| 1961 | 1961 |
|
| 1962 | 1962 |
ValueIterator& operator++() { ++it; return *this; }
|
| 1963 | 1963 |
ValueIterator operator++(int) {
|
| 1964 | 1964 |
ValueIterator tmp(*this); |
| 1965 | 1965 |
operator++(); |
| 1966 | 1966 |
return tmp; |
| 1967 | 1967 |
} |
| 1968 | 1968 |
|
| 1969 | 1969 |
const Value& operator*() const { return it->first; }
|
| 1970 | 1970 |
const Value* operator->() const { return &(it->first); }
|
| 1971 | 1971 |
|
| 1972 | 1972 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
| 1973 | 1973 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
| 1974 | 1974 |
|
| 1975 | 1975 |
private: |
| 1976 | 1976 |
typename Container::const_iterator it; |
| ... | ... |
@@ -2251,941 +2251,950 @@ |
| 2251 | 2251 |
/// |
| 2252 | 2252 |
/// Gives back the \e RangeId of the item. |
| 2253 | 2253 |
int operator[](const Item& item) const {
|
| 2254 | 2254 |
return Map::operator[](item); |
| 2255 | 2255 |
} |
| 2256 | 2256 |
|
| 2257 | 2257 |
/// \brief Gives back the item belonging to a \e RangeId |
| 2258 | 2258 |
/// |
| 2259 | 2259 |
/// Gives back the item belonging to a \e RangeId. |
| 2260 | 2260 |
Item operator()(int id) const {
|
| 2261 | 2261 |
return _inv_map[id]; |
| 2262 | 2262 |
} |
| 2263 | 2263 |
|
| 2264 | 2264 |
private: |
| 2265 | 2265 |
|
| 2266 | 2266 |
typedef std::vector<Item> Container; |
| 2267 | 2267 |
Container _inv_map; |
| 2268 | 2268 |
|
| 2269 | 2269 |
public: |
| 2270 | 2270 |
|
| 2271 | 2271 |
/// \brief The inverse map type of RangeIdMap. |
| 2272 | 2272 |
/// |
| 2273 | 2273 |
/// The inverse map type of RangeIdMap. |
| 2274 | 2274 |
class InverseMap {
|
| 2275 | 2275 |
public: |
| 2276 | 2276 |
/// \brief Constructor |
| 2277 | 2277 |
/// |
| 2278 | 2278 |
/// Constructor of the InverseMap. |
| 2279 | 2279 |
explicit InverseMap(const RangeIdMap& inverted) |
| 2280 | 2280 |
: _inverted(inverted) {}
|
| 2281 | 2281 |
|
| 2282 | 2282 |
|
| 2283 | 2283 |
/// The value type of the InverseMap. |
| 2284 | 2284 |
typedef typename RangeIdMap::Key Value; |
| 2285 | 2285 |
/// The key type of the InverseMap. |
| 2286 | 2286 |
typedef typename RangeIdMap::Value Key; |
| 2287 | 2287 |
|
| 2288 | 2288 |
/// \brief Subscript operator. |
| 2289 | 2289 |
/// |
| 2290 | 2290 |
/// Subscript operator. It gives back the item |
| 2291 | 2291 |
/// that the descriptor currently belongs to. |
| 2292 | 2292 |
Value operator[](const Key& key) const {
|
| 2293 | 2293 |
return _inverted(key); |
| 2294 | 2294 |
} |
| 2295 | 2295 |
|
| 2296 | 2296 |
/// \brief Size of the map. |
| 2297 | 2297 |
/// |
| 2298 | 2298 |
/// Returns the size of the map. |
| 2299 | 2299 |
unsigned int size() const {
|
| 2300 | 2300 |
return _inverted.size(); |
| 2301 | 2301 |
} |
| 2302 | 2302 |
|
| 2303 | 2303 |
private: |
| 2304 | 2304 |
const RangeIdMap& _inverted; |
| 2305 | 2305 |
}; |
| 2306 | 2306 |
|
| 2307 | 2307 |
/// \brief Gives back the inverse of the map. |
| 2308 | 2308 |
/// |
| 2309 | 2309 |
/// Gives back the inverse of the map. |
| 2310 | 2310 |
const InverseMap inverse() const {
|
| 2311 | 2311 |
return InverseMap(*this); |
| 2312 | 2312 |
} |
| 2313 | 2313 |
}; |
| 2314 | 2314 |
|
| 2315 |
/// \brief Dynamic iterable bool map. |
|
| 2315 |
/// \brief Dynamic iterable \c bool map. |
|
| 2316 | 2316 |
/// |
| 2317 |
/// This class provides a special graph map type which can store for |
|
| 2318 |
/// each graph item(node, arc, edge, etc.) a bool value. For both |
|
| 2319 |
/// the true and the false values it is possible to iterate on the |
|
| 2320 |
/// keys. |
|
| 2317 |
/// This class provides a special graph map type which can store a |
|
| 2318 |
/// \c bool value for graph items (\c Node, \c Arc or \c Edge). |
|
| 2319 |
/// For both \c true and \c false values it is possible to iterate on |
|
| 2320 |
/// the keys. |
|
| 2321 | 2321 |
/// |
| 2322 |
/// \param GR The graph type. |
|
| 2323 |
/// \param ITEM One of the graph's item types, the key of the map. |
|
| 2324 |
|
|
| 2322 |
/// This type is a reference map, so it can be modified with the |
|
| 2323 |
/// subscription operator. |
|
| 2324 |
/// |
|
| 2325 |
/// \tparam GR The graph type. |
|
| 2326 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
|
| 2327 |
/// \c GR::Edge). |
|
| 2328 |
/// |
|
| 2329 |
/// \see IterableIntMap, IterableValueMap |
|
| 2330 |
/// \see CrossRefMap |
|
| 2331 |
template <typename GR, typename K> |
|
| 2325 | 2332 |
class IterableBoolMap |
| 2326 |
: protected ItemSetTraits<GR, |
|
| 2333 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
|
| 2327 | 2334 |
private: |
| 2328 | 2335 |
typedef GR Graph; |
| 2329 | 2336 |
|
| 2330 |
typedef typename ItemSetTraits<Graph, ITEM>::ItemIt KeyIt; |
|
| 2331 |
typedef typename ItemSetTraits<GR, ITEM>::template Map<int>::Type Parent; |
|
| 2332 |
|
|
| 2333 |
std::vector<ITEM> _array; |
|
| 2337 |
typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt; |
|
| 2338 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent; |
|
| 2339 |
|
|
| 2340 |
std::vector<K> _array; |
|
| 2334 | 2341 |
int _sep; |
| 2335 | 2342 |
|
| 2336 | 2343 |
public: |
| 2337 | 2344 |
|
| 2338 |
/// Indicates that the map |
|
| 2345 |
/// Indicates that the map is reference map. |
|
| 2339 | 2346 |
typedef True ReferenceMapTag; |
| 2340 | 2347 |
|
| 2341 | 2348 |
/// The key type |
| 2342 |
typedef |
|
| 2349 |
typedef K Key; |
|
| 2343 | 2350 |
/// The value type |
| 2344 | 2351 |
typedef bool Value; |
| 2345 | 2352 |
/// The const reference type. |
| 2346 | 2353 |
typedef const Value& ConstReference; |
| 2347 | 2354 |
|
| 2348 | 2355 |
private: |
| 2349 | 2356 |
|
| 2350 | 2357 |
int position(const Key& key) const {
|
| 2351 | 2358 |
return Parent::operator[](key); |
| 2352 | 2359 |
} |
| 2353 | 2360 |
|
| 2354 | 2361 |
public: |
| 2355 | 2362 |
|
| 2356 |
/// \brief |
|
| 2363 |
/// \brief Reference to the value of the map. |
|
| 2357 | 2364 |
/// |
| 2358 |
/// This class is similar to the bool type. It can be converted to |
|
| 2359 |
/// bool and it provides the same operators. |
|
| 2365 |
/// This class is similar to the \c bool type. It can be converted to |
|
| 2366 |
/// \c bool and it provides the same operators. |
|
| 2360 | 2367 |
class Reference {
|
| 2361 | 2368 |
friend class IterableBoolMap; |
| 2362 | 2369 |
private: |
| 2363 | 2370 |
Reference(IterableBoolMap& map, const Key& key) |
| 2364 | 2371 |
: _key(key), _map(map) {}
|
| 2365 | 2372 |
public: |
| 2366 | 2373 |
|
| 2367 | 2374 |
Reference& operator=(const Reference& value) {
|
| 2368 | 2375 |
_map.set(_key, static_cast<bool>(value)); |
| 2369 | 2376 |
return *this; |
| 2370 | 2377 |
} |
| 2371 | 2378 |
|
| 2372 | 2379 |
operator bool() const {
|
| 2373 | 2380 |
return static_cast<const IterableBoolMap&>(_map)[_key]; |
| 2374 | 2381 |
} |
| 2375 | 2382 |
|
| 2376 | 2383 |
Reference& operator=(bool value) {
|
| 2377 | 2384 |
_map.set(_key, value); |
| 2378 | 2385 |
return *this; |
| 2379 | 2386 |
} |
| 2380 | 2387 |
Reference& operator&=(bool value) {
|
| 2381 | 2388 |
_map.set(_key, _map[_key] & value); |
| 2382 | 2389 |
return *this; |
| 2383 | 2390 |
} |
| 2384 | 2391 |
Reference& operator|=(bool value) {
|
| 2385 | 2392 |
_map.set(_key, _map[_key] | value); |
| 2386 | 2393 |
return *this; |
| 2387 | 2394 |
} |
| 2388 | 2395 |
Reference& operator^=(bool value) {
|
| 2389 | 2396 |
_map.set(_key, _map[_key] ^ value); |
| 2390 | 2397 |
return *this; |
| 2391 | 2398 |
} |
| 2392 | 2399 |
private: |
| 2393 | 2400 |
Key _key; |
| 2394 | 2401 |
IterableBoolMap& _map; |
| 2395 | 2402 |
}; |
| 2396 | 2403 |
|
| 2397 | 2404 |
/// \brief Constructor of the map with a default value. |
| 2398 | 2405 |
/// |
| 2399 | 2406 |
/// Constructor of the map with a default value. |
| 2400 | 2407 |
explicit IterableBoolMap(const Graph& graph, bool def = false) |
| 2401 | 2408 |
: Parent(graph) {
|
| 2402 | 2409 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 2403 | 2410 |
Key it; |
| 2404 | 2411 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2405 | 2412 |
Parent::set(it, _array.size()); |
| 2406 | 2413 |
_array.push_back(it); |
| 2407 | 2414 |
} |
| 2408 | 2415 |
_sep = (def ? _array.size() : 0); |
| 2409 | 2416 |
} |
| 2410 | 2417 |
|
| 2411 | 2418 |
/// \brief Const subscript operator of the map. |
| 2412 | 2419 |
/// |
| 2413 | 2420 |
/// Const subscript operator of the map. |
| 2414 | 2421 |
bool operator[](const Key& key) const {
|
| 2415 | 2422 |
return position(key) < _sep; |
| 2416 | 2423 |
} |
| 2417 | 2424 |
|
| 2418 | 2425 |
/// \brief Subscript operator of the map. |
| 2419 | 2426 |
/// |
| 2420 | 2427 |
/// Subscript operator of the map. |
| 2421 | 2428 |
Reference operator[](const Key& key) {
|
| 2422 | 2429 |
return Reference(*this, key); |
| 2423 | 2430 |
} |
| 2424 | 2431 |
|
| 2425 | 2432 |
/// \brief Set operation of the map. |
| 2426 | 2433 |
/// |
| 2427 | 2434 |
/// Set operation of the map. |
| 2428 | 2435 |
void set(const Key& key, bool value) {
|
| 2429 | 2436 |
int pos = position(key); |
| 2430 | 2437 |
if (value) {
|
| 2431 | 2438 |
if (pos < _sep) return; |
| 2432 | 2439 |
Key tmp = _array[_sep]; |
| 2433 | 2440 |
_array[_sep] = key; |
| 2434 | 2441 |
Parent::set(key, _sep); |
| 2435 | 2442 |
_array[pos] = tmp; |
| 2436 | 2443 |
Parent::set(tmp, pos); |
| 2437 | 2444 |
++_sep; |
| 2438 | 2445 |
} else {
|
| 2439 | 2446 |
if (pos >= _sep) return; |
| 2440 | 2447 |
--_sep; |
| 2441 | 2448 |
Key tmp = _array[_sep]; |
| 2442 | 2449 |
_array[_sep] = key; |
| 2443 | 2450 |
Parent::set(key, _sep); |
| 2444 | 2451 |
_array[pos] = tmp; |
| 2445 | 2452 |
Parent::set(tmp, pos); |
| 2446 | 2453 |
} |
| 2447 | 2454 |
} |
| 2448 | 2455 |
|
| 2449 | 2456 |
/// \brief Set all items. |
| 2450 | 2457 |
/// |
| 2451 | 2458 |
/// Set all items in the map. |
| 2452 | 2459 |
/// \note Constant time operation. |
| 2453 | 2460 |
void setAll(bool value) {
|
| 2454 | 2461 |
_sep = (value ? _array.size() : 0); |
| 2455 | 2462 |
} |
| 2456 | 2463 |
|
| 2457 |
/// \brief Returns the number of the keys mapped to true. |
|
| 2464 |
/// \brief Returns the number of the keys mapped to \c true. |
|
| 2458 | 2465 |
/// |
| 2459 |
/// Returns the number of the keys mapped to true. |
|
| 2466 |
/// Returns the number of the keys mapped to \c true. |
|
| 2460 | 2467 |
int trueNum() const {
|
| 2461 | 2468 |
return _sep; |
| 2462 | 2469 |
} |
| 2463 | 2470 |
|
| 2464 |
/// \brief Returns the number of the keys mapped to false. |
|
| 2471 |
/// \brief Returns the number of the keys mapped to \c false. |
|
| 2465 | 2472 |
/// |
| 2466 |
/// Returns the number of the keys mapped to false. |
|
| 2473 |
/// Returns the number of the keys mapped to \c false. |
|
| 2467 | 2474 |
int falseNum() const {
|
| 2468 | 2475 |
return _array.size() - _sep; |
| 2469 | 2476 |
} |
| 2470 | 2477 |
|
| 2471 |
/// \brief Iterator for the keys mapped to true. |
|
| 2478 |
/// \brief Iterator for the keys mapped to \c true. |
|
| 2472 | 2479 |
/// |
| 2473 |
/// Iterator for the keys mapped to true. It works |
|
| 2474 |
/// like a graph item iterator in the map, it can be converted |
|
| 2480 |
/// Iterator for the keys mapped to \c true. It works |
|
| 2481 |
/// like a graph item iterator, it can be converted to |
|
| 2475 | 2482 |
/// the key type of the map, incremented with \c ++ operator, and |
| 2476 |
/// if the iterator |
|
| 2483 |
/// if the iterator leaves the last valid key, it will be equal to |
|
| 2477 | 2484 |
/// \c INVALID. |
| 2478 | 2485 |
class TrueIt : public Key {
|
| 2479 | 2486 |
public: |
| 2480 | 2487 |
typedef Key Parent; |
| 2481 | 2488 |
|
| 2482 | 2489 |
/// \brief Creates an iterator. |
| 2483 | 2490 |
/// |
| 2484 | 2491 |
/// Creates an iterator. It iterates on the |
| 2485 |
/// keys which mapped to true. |
|
| 2486 |
/// \param map The IterableIntMap |
|
| 2492 |
/// keys mapped to \c true. |
|
| 2493 |
/// \param map The IterableBoolMap. |
|
| 2487 | 2494 |
explicit TrueIt(const IterableBoolMap& map) |
| 2488 | 2495 |
: Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID), |
| 2489 | 2496 |
_map(&map) {}
|
| 2490 | 2497 |
|
| 2491 | 2498 |
/// \brief Invalid constructor \& conversion. |
| 2492 | 2499 |
/// |
| 2493 |
/// This constructor initializes the |
|
| 2500 |
/// This constructor initializes the iterator to be invalid. |
|
| 2494 | 2501 |
/// \sa Invalid for more details. |
| 2495 | 2502 |
TrueIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2496 | 2503 |
|
| 2497 | 2504 |
/// \brief Increment operator. |
| 2498 | 2505 |
/// |
| 2499 |
/// Increment |
|
| 2506 |
/// Increment operator. |
|
| 2500 | 2507 |
TrueIt& operator++() {
|
| 2501 | 2508 |
int pos = _map->position(*this); |
| 2502 | 2509 |
Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID); |
| 2503 | 2510 |
return *this; |
| 2504 | 2511 |
} |
| 2505 | 2512 |
|
| 2506 |
|
|
| 2507 | 2513 |
private: |
| 2508 | 2514 |
const IterableBoolMap* _map; |
| 2509 | 2515 |
}; |
| 2510 | 2516 |
|
| 2511 |
/// \brief Iterator for the keys mapped to false. |
|
| 2517 |
/// \brief Iterator for the keys mapped to \c false. |
|
| 2512 | 2518 |
/// |
| 2513 |
/// Iterator for the keys mapped to false. It works |
|
| 2514 |
/// like a graph item iterator in the map, it can be converted |
|
| 2519 |
/// Iterator for the keys mapped to \c false. It works |
|
| 2520 |
/// like a graph item iterator, it can be converted to |
|
| 2515 | 2521 |
/// the key type of the map, incremented with \c ++ operator, and |
| 2516 |
/// if the iterator |
|
| 2522 |
/// if the iterator leaves the last valid key, it will be equal to |
|
| 2517 | 2523 |
/// \c INVALID. |
| 2518 | 2524 |
class FalseIt : public Key {
|
| 2519 | 2525 |
public: |
| 2520 | 2526 |
typedef Key Parent; |
| 2521 | 2527 |
|
| 2522 | 2528 |
/// \brief Creates an iterator. |
| 2523 | 2529 |
/// |
| 2524 | 2530 |
/// Creates an iterator. It iterates on the |
| 2525 |
/// keys which mapped to false. |
|
| 2526 |
/// \param map The IterableIntMap |
|
| 2531 |
/// keys mapped to \c false. |
|
| 2532 |
/// \param map The IterableBoolMap. |
|
| 2527 | 2533 |
explicit FalseIt(const IterableBoolMap& map) |
| 2528 | 2534 |
: Parent(map._sep < int(map._array.size()) ? |
| 2529 | 2535 |
map._array.back() : INVALID), _map(&map) {}
|
| 2530 | 2536 |
|
| 2531 | 2537 |
/// \brief Invalid constructor \& conversion. |
| 2532 | 2538 |
/// |
| 2533 |
/// This constructor initializes the |
|
| 2539 |
/// This constructor initializes the iterator to be invalid. |
|
| 2534 | 2540 |
/// \sa Invalid for more details. |
| 2535 | 2541 |
FalseIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2536 | 2542 |
|
| 2537 | 2543 |
/// \brief Increment operator. |
| 2538 | 2544 |
/// |
| 2539 |
/// Increment |
|
| 2545 |
/// Increment operator. |
|
| 2540 | 2546 |
FalseIt& operator++() {
|
| 2541 | 2547 |
int pos = _map->position(*this); |
| 2542 | 2548 |
Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID); |
| 2543 | 2549 |
return *this; |
| 2544 | 2550 |
} |
| 2545 | 2551 |
|
| 2546 | 2552 |
private: |
| 2547 | 2553 |
const IterableBoolMap* _map; |
| 2548 | 2554 |
}; |
| 2549 | 2555 |
|
| 2550 | 2556 |
/// \brief Iterator for the keys mapped to a given value. |
| 2551 | 2557 |
/// |
| 2552 | 2558 |
/// Iterator for the keys mapped to a given value. It works |
| 2553 |
/// like a graph item iterator |
|
| 2559 |
/// like a graph item iterator, it can be converted to |
|
| 2554 | 2560 |
/// the key type of the map, incremented with \c ++ operator, and |
| 2555 |
/// if the iterator |
|
| 2561 |
/// if the iterator leaves the last valid key, it will be equal to |
|
| 2556 | 2562 |
/// \c INVALID. |
| 2557 | 2563 |
class ItemIt : public Key {
|
| 2558 | 2564 |
public: |
| 2559 | 2565 |
typedef Key Parent; |
| 2560 | 2566 |
|
| 2561 |
/// \brief Creates an iterator. |
|
| 2567 |
/// \brief Creates an iterator with a value. |
|
| 2562 | 2568 |
/// |
| 2563 |
/// Creates an iterator. It iterates on the |
|
| 2564 |
/// keys which mapped to false. |
|
| 2565 |
/// \param map The IterableIntMap |
|
| 2566 |
/// \param value Which elements should be iterated. |
|
| 2569 |
/// Creates an iterator with a value. It iterates on the |
|
| 2570 |
/// keys mapped to the given value. |
|
| 2571 |
/// \param map The IterableBoolMap. |
|
| 2572 |
/// \param value The value. |
|
| 2567 | 2573 |
ItemIt(const IterableBoolMap& map, bool value) |
| 2568 | 2574 |
: Parent(value ? |
| 2569 | 2575 |
(map._sep > 0 ? |
| 2570 | 2576 |
map._array[map._sep - 1] : INVALID) : |
| 2571 | 2577 |
(map._sep < int(map._array.size()) ? |
| 2572 | 2578 |
map._array.back() : INVALID)), _map(&map) {}
|
| 2573 | 2579 |
|
| 2574 | 2580 |
/// \brief Invalid constructor \& conversion. |
| 2575 | 2581 |
/// |
| 2576 |
/// This constructor initializes the |
|
| 2582 |
/// This constructor initializes the iterator to be invalid. |
|
| 2577 | 2583 |
/// \sa Invalid for more details. |
| 2578 | 2584 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2579 | 2585 |
|
| 2580 | 2586 |
/// \brief Increment operator. |
| 2581 | 2587 |
/// |
| 2582 |
/// Increment |
|
| 2588 |
/// Increment operator. |
|
| 2583 | 2589 |
ItemIt& operator++() {
|
| 2584 | 2590 |
int pos = _map->position(*this); |
| 2585 | 2591 |
int _sep = pos >= _map->_sep ? _map->_sep : 0; |
| 2586 | 2592 |
Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID); |
| 2587 | 2593 |
return *this; |
| 2588 | 2594 |
} |
| 2589 | 2595 |
|
| 2590 | 2596 |
private: |
| 2591 | 2597 |
const IterableBoolMap* _map; |
| 2592 | 2598 |
}; |
| 2593 | 2599 |
|
| 2594 | 2600 |
protected: |
| 2595 | 2601 |
|
| 2596 | 2602 |
virtual void add(const Key& key) {
|
| 2597 | 2603 |
Parent::add(key); |
| 2598 | 2604 |
Parent::set(key, _array.size()); |
| 2599 | 2605 |
_array.push_back(key); |
| 2600 | 2606 |
} |
| 2601 | 2607 |
|
| 2602 | 2608 |
virtual void add(const std::vector<Key>& keys) {
|
| 2603 | 2609 |
Parent::add(keys); |
| 2604 | 2610 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2605 | 2611 |
Parent::set(keys[i], _array.size()); |
| 2606 | 2612 |
_array.push_back(keys[i]); |
| 2607 | 2613 |
} |
| 2608 | 2614 |
} |
| 2609 | 2615 |
|
| 2610 | 2616 |
virtual void erase(const Key& key) {
|
| 2611 | 2617 |
int pos = position(key); |
| 2612 | 2618 |
if (pos < _sep) {
|
| 2613 | 2619 |
--_sep; |
| 2614 | 2620 |
Parent::set(_array[_sep], pos); |
| 2615 | 2621 |
_array[pos] = _array[_sep]; |
| 2616 | 2622 |
Parent::set(_array.back(), _sep); |
| 2617 | 2623 |
_array[_sep] = _array.back(); |
| 2618 | 2624 |
_array.pop_back(); |
| 2619 | 2625 |
} else {
|
| 2620 | 2626 |
Parent::set(_array.back(), pos); |
| 2621 | 2627 |
_array[pos] = _array.back(); |
| 2622 | 2628 |
_array.pop_back(); |
| 2623 | 2629 |
} |
| 2624 | 2630 |
Parent::erase(key); |
| 2625 | 2631 |
} |
| 2626 | 2632 |
|
| 2627 | 2633 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2628 | 2634 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2629 | 2635 |
int pos = position(keys[i]); |
| 2630 | 2636 |
if (pos < _sep) {
|
| 2631 | 2637 |
--_sep; |
| 2632 | 2638 |
Parent::set(_array[_sep], pos); |
| 2633 | 2639 |
_array[pos] = _array[_sep]; |
| 2634 | 2640 |
Parent::set(_array.back(), _sep); |
| 2635 | 2641 |
_array[_sep] = _array.back(); |
| 2636 | 2642 |
_array.pop_back(); |
| 2637 | 2643 |
} else {
|
| 2638 | 2644 |
Parent::set(_array.back(), pos); |
| 2639 | 2645 |
_array[pos] = _array.back(); |
| 2640 | 2646 |
_array.pop_back(); |
| 2641 | 2647 |
} |
| 2642 | 2648 |
} |
| 2643 | 2649 |
Parent::erase(keys); |
| 2644 | 2650 |
} |
| 2645 | 2651 |
|
| 2646 | 2652 |
virtual void build() {
|
| 2647 | 2653 |
Parent::build(); |
| 2648 | 2654 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 2649 | 2655 |
Key it; |
| 2650 | 2656 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2651 | 2657 |
Parent::set(it, _array.size()); |
| 2652 | 2658 |
_array.push_back(it); |
| 2653 | 2659 |
} |
| 2654 | 2660 |
_sep = 0; |
| 2655 | 2661 |
} |
| 2656 | 2662 |
|
| 2657 | 2663 |
virtual void clear() {
|
| 2658 | 2664 |
_array.clear(); |
| 2659 | 2665 |
_sep = 0; |
| 2660 | 2666 |
Parent::clear(); |
| 2661 | 2667 |
} |
| 2662 | 2668 |
|
| 2663 | 2669 |
}; |
| 2664 | 2670 |
|
| 2665 | 2671 |
|
| 2666 | 2672 |
namespace _maps_bits {
|
| 2667 | 2673 |
template <typename Item> |
| 2668 | 2674 |
struct IterableIntMapNode {
|
| 2669 | 2675 |
IterableIntMapNode() : value(-1) {}
|
| 2670 | 2676 |
IterableIntMapNode(int _value) : value(_value) {}
|
| 2671 | 2677 |
Item prev, next; |
| 2672 | 2678 |
int value; |
| 2673 | 2679 |
}; |
| 2674 | 2680 |
} |
| 2675 | 2681 |
|
| 2676 |
///\ingroup graph_maps |
|
| 2677 |
/// |
|
| 2678 | 2682 |
/// \brief Dynamic iterable integer map. |
| 2679 | 2683 |
/// |
| 2680 |
/// This class provides a special graph map type which can store |
|
| 2681 |
/// for each graph item(node, edge, etc.) an integer value. For each |
|
| 2682 |
/// non negative value it is possible to iterate on the keys which |
|
| 2683 |
/// mapped to the given value. |
|
| 2684 |
/// This class provides a special graph map type which can store an |
|
| 2685 |
/// integer value for graph items (\c Node, \c Arc or \c Edge). |
|
| 2686 |
/// For each non-negative value it is possible to iterate on the keys |
|
| 2687 |
/// mapped to the value. |
|
| 2684 | 2688 |
/// |
| 2685 |
/// |
|
| 2689 |
/// This type is a reference map, so it can be modified with the |
|
| 2690 |
/// subscription operator. |
|
| 2691 |
/// |
|
| 2692 |
/// \note The size of the data structure depends on the largest |
|
| 2686 | 2693 |
/// value in the map. |
| 2687 | 2694 |
/// |
| 2688 |
/// \param GR The graph type. |
|
| 2689 |
/// \param ITEM One of the graph's item type, the key of the map. |
|
| 2690 |
|
|
| 2695 |
/// \tparam GR The graph type. |
|
| 2696 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
|
| 2697 |
/// \c GR::Edge). |
|
| 2698 |
/// |
|
| 2699 |
/// \see IterableBoolMap, IterableValueMap |
|
| 2700 |
/// \see CrossRefMap |
|
| 2701 |
template <typename GR, typename K> |
|
| 2691 | 2702 |
class IterableIntMap |
| 2692 |
: protected ItemSetTraits<GR, ITEM>:: |
|
| 2693 |
template Map<_maps_bits::IterableIntMapNode<ITEM> >::Type {
|
|
| 2703 |
: protected ItemSetTraits<GR, K>:: |
|
| 2704 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type {
|
|
| 2694 | 2705 |
public: |
| 2695 |
typedef typename ItemSetTraits<GR, ITEM>:: |
|
| 2696 |
template Map<_maps_bits::IterableIntMapNode<ITEM> >::Type Parent; |
|
| 2706 |
typedef typename ItemSetTraits<GR, K>:: |
|
| 2707 |
template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent; |
|
| 2697 | 2708 |
|
| 2698 | 2709 |
/// The key type |
| 2699 |
typedef |
|
| 2710 |
typedef K Key; |
|
| 2700 | 2711 |
/// The value type |
| 2701 | 2712 |
typedef int Value; |
| 2702 | 2713 |
/// The graph type |
| 2703 | 2714 |
typedef GR Graph; |
| 2704 | 2715 |
|
| 2705 | 2716 |
/// \brief Constructor of the map. |
| 2706 | 2717 |
/// |
| 2707 |
/// Constructor of the map. It |
|
| 2718 |
/// Constructor of the map. It sets all values to -1. |
|
| 2708 | 2719 |
explicit IterableIntMap(const Graph& graph) |
| 2709 | 2720 |
: Parent(graph) {}
|
| 2710 | 2721 |
|
| 2711 | 2722 |
/// \brief Constructor of the map with a given value. |
| 2712 | 2723 |
/// |
| 2713 | 2724 |
/// Constructor of the map with a given value. |
| 2714 | 2725 |
explicit IterableIntMap(const Graph& graph, int value) |
| 2715 |
: Parent(graph, _maps_bits::IterableIntMapNode< |
|
| 2726 |
: Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
|
|
| 2716 | 2727 |
if (value >= 0) {
|
| 2717 | 2728 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
| 2718 | 2729 |
lace(it); |
| 2719 | 2730 |
} |
| 2720 | 2731 |
} |
| 2721 | 2732 |
} |
| 2722 | 2733 |
|
| 2723 | 2734 |
private: |
| 2724 | 2735 |
|
| 2725 | 2736 |
void unlace(const Key& key) {
|
| 2726 | 2737 |
typename Parent::Value& node = Parent::operator[](key); |
| 2727 | 2738 |
if (node.value < 0) return; |
| 2728 | 2739 |
if (node.prev != INVALID) {
|
| 2729 | 2740 |
Parent::operator[](node.prev).next = node.next; |
| 2730 | 2741 |
} else {
|
| 2731 | 2742 |
_first[node.value] = node.next; |
| 2732 | 2743 |
} |
| 2733 | 2744 |
if (node.next != INVALID) {
|
| 2734 | 2745 |
Parent::operator[](node.next).prev = node.prev; |
| 2735 | 2746 |
} |
| 2736 | 2747 |
while (!_first.empty() && _first.back() == INVALID) {
|
| 2737 | 2748 |
_first.pop_back(); |
| 2738 | 2749 |
} |
| 2739 | 2750 |
} |
| 2740 | 2751 |
|
| 2741 | 2752 |
void lace(const Key& key) {
|
| 2742 | 2753 |
typename Parent::Value& node = Parent::operator[](key); |
| 2743 | 2754 |
if (node.value < 0) return; |
| 2744 | 2755 |
if (node.value >= int(_first.size())) {
|
| 2745 | 2756 |
_first.resize(node.value + 1, INVALID); |
| 2746 | 2757 |
} |
| 2747 | 2758 |
node.prev = INVALID; |
| 2748 | 2759 |
node.next = _first[node.value]; |
| 2749 | 2760 |
if (node.next != INVALID) {
|
| 2750 | 2761 |
Parent::operator[](node.next).prev = key; |
| 2751 | 2762 |
} |
| 2752 | 2763 |
_first[node.value] = key; |
| 2753 | 2764 |
} |
| 2754 | 2765 |
|
| 2755 | 2766 |
public: |
| 2756 | 2767 |
|
| 2757 |
/// Indicates that the map |
|
| 2768 |
/// Indicates that the map is reference map. |
|
| 2758 | 2769 |
typedef True ReferenceMapTag; |
| 2759 | 2770 |
|
| 2760 |
/// \brief |
|
| 2771 |
/// \brief Reference to the value of the map. |
|
| 2761 | 2772 |
/// |
| 2762 |
/// This class is similar to the int type. It can |
|
| 2763 |
/// be converted to int and it has the same operators. |
|
| 2773 |
/// This class is similar to the \c int type. It can |
|
| 2774 |
/// be converted to \c int and it has the same operators. |
|
| 2764 | 2775 |
class Reference {
|
| 2765 | 2776 |
friend class IterableIntMap; |
| 2766 | 2777 |
private: |
| 2767 | 2778 |
Reference(IterableIntMap& map, const Key& key) |
| 2768 | 2779 |
: _key(key), _map(map) {}
|
| 2769 | 2780 |
public: |
| 2770 | 2781 |
|
| 2771 | 2782 |
Reference& operator=(const Reference& value) {
|
| 2772 | 2783 |
_map.set(_key, static_cast<const int&>(value)); |
| 2773 | 2784 |
return *this; |
| 2774 | 2785 |
} |
| 2775 | 2786 |
|
| 2776 | 2787 |
operator const int&() const {
|
| 2777 | 2788 |
return static_cast<const IterableIntMap&>(_map)[_key]; |
| 2778 | 2789 |
} |
| 2779 | 2790 |
|
| 2780 | 2791 |
Reference& operator=(int value) {
|
| 2781 | 2792 |
_map.set(_key, value); |
| 2782 | 2793 |
return *this; |
| 2783 | 2794 |
} |
| 2784 | 2795 |
Reference& operator++() {
|
| 2785 | 2796 |
_map.set(_key, _map[_key] + 1); |
| 2786 | 2797 |
return *this; |
| 2787 | 2798 |
} |
| 2788 | 2799 |
int operator++(int) {
|
| 2789 | 2800 |
int value = _map[_key]; |
| 2790 | 2801 |
_map.set(_key, value + 1); |
| 2791 | 2802 |
return value; |
| 2792 | 2803 |
} |
| 2793 | 2804 |
Reference& operator--() {
|
| 2794 | 2805 |
_map.set(_key, _map[_key] - 1); |
| 2795 | 2806 |
return *this; |
| 2796 | 2807 |
} |
| 2797 | 2808 |
int operator--(int) {
|
| 2798 | 2809 |
int value = _map[_key]; |
| 2799 | 2810 |
_map.set(_key, value - 1); |
| 2800 | 2811 |
return value; |
| 2801 | 2812 |
} |
| 2802 | 2813 |
Reference& operator+=(int value) {
|
| 2803 | 2814 |
_map.set(_key, _map[_key] + value); |
| 2804 | 2815 |
return *this; |
| 2805 | 2816 |
} |
| 2806 | 2817 |
Reference& operator-=(int value) {
|
| 2807 | 2818 |
_map.set(_key, _map[_key] - value); |
| 2808 | 2819 |
return *this; |
| 2809 | 2820 |
} |
| 2810 | 2821 |
Reference& operator*=(int value) {
|
| 2811 | 2822 |
_map.set(_key, _map[_key] * value); |
| 2812 | 2823 |
return *this; |
| 2813 | 2824 |
} |
| 2814 | 2825 |
Reference& operator/=(int value) {
|
| 2815 | 2826 |
_map.set(_key, _map[_key] / value); |
| 2816 | 2827 |
return *this; |
| 2817 | 2828 |
} |
| 2818 | 2829 |
Reference& operator%=(int value) {
|
| 2819 | 2830 |
_map.set(_key, _map[_key] % value); |
| 2820 | 2831 |
return *this; |
| 2821 | 2832 |
} |
| 2822 | 2833 |
Reference& operator&=(int value) {
|
| 2823 | 2834 |
_map.set(_key, _map[_key] & value); |
| 2824 | 2835 |
return *this; |
| 2825 | 2836 |
} |
| 2826 | 2837 |
Reference& operator|=(int value) {
|
| 2827 | 2838 |
_map.set(_key, _map[_key] | value); |
| 2828 | 2839 |
return *this; |
| 2829 | 2840 |
} |
| 2830 | 2841 |
Reference& operator^=(int value) {
|
| 2831 | 2842 |
_map.set(_key, _map[_key] ^ value); |
| 2832 | 2843 |
return *this; |
| 2833 | 2844 |
} |
| 2834 | 2845 |
Reference& operator<<=(int value) {
|
| 2835 | 2846 |
_map.set(_key, _map[_key] << value); |
| 2836 | 2847 |
return *this; |
| 2837 | 2848 |
} |
| 2838 | 2849 |
Reference& operator>>=(int value) {
|
| 2839 | 2850 |
_map.set(_key, _map[_key] >> value); |
| 2840 | 2851 |
return *this; |
| 2841 | 2852 |
} |
| 2842 | 2853 |
|
| 2843 | 2854 |
private: |
| 2844 | 2855 |
Key _key; |
| 2845 | 2856 |
IterableIntMap& _map; |
| 2846 | 2857 |
}; |
| 2847 | 2858 |
|
| 2848 | 2859 |
/// The const reference type. |
| 2849 | 2860 |
typedef const Value& ConstReference; |
| 2850 | 2861 |
|
| 2851 | 2862 |
/// \brief Gives back the maximal value plus one. |
| 2852 | 2863 |
/// |
| 2853 | 2864 |
/// Gives back the maximal value plus one. |
| 2854 | 2865 |
int size() const {
|
| 2855 | 2866 |
return _first.size(); |
| 2856 | 2867 |
} |
| 2857 | 2868 |
|
| 2858 | 2869 |
/// \brief Set operation of the map. |
| 2859 | 2870 |
/// |
| 2860 | 2871 |
/// Set operation of the map. |
| 2861 | 2872 |
void set(const Key& key, const Value& value) {
|
| 2862 | 2873 |
unlace(key); |
| 2863 | 2874 |
Parent::operator[](key).value = value; |
| 2864 | 2875 |
lace(key); |
| 2865 | 2876 |
} |
| 2866 | 2877 |
|
| 2867 | 2878 |
/// \brief Const subscript operator of the map. |
| 2868 | 2879 |
/// |
| 2869 | 2880 |
/// Const subscript operator of the map. |
| 2870 | 2881 |
const Value& operator[](const Key& key) const {
|
| 2871 | 2882 |
return Parent::operator[](key).value; |
| 2872 | 2883 |
} |
| 2873 | 2884 |
|
| 2874 | 2885 |
/// \brief Subscript operator of the map. |
| 2875 | 2886 |
/// |
| 2876 | 2887 |
/// Subscript operator of the map. |
| 2877 | 2888 |
Reference operator[](const Key& key) {
|
| 2878 | 2889 |
return Reference(*this, key); |
| 2879 | 2890 |
} |
| 2880 | 2891 |
|
| 2881 | 2892 |
/// \brief Iterator for the keys with the same value. |
| 2882 | 2893 |
/// |
| 2883 | 2894 |
/// Iterator for the keys with the same value. It works |
| 2884 |
/// like a graph item iterator |
|
| 2895 |
/// like a graph item iterator, it can be converted to |
|
| 2885 | 2896 |
/// the item type of the map, incremented with \c ++ operator, and |
| 2886 |
/// if the iterator |
|
| 2897 |
/// if the iterator leaves the last valid item, it will be equal to |
|
| 2887 | 2898 |
/// \c INVALID. |
| 2888 |
class ItemIt : public |
|
| 2899 |
class ItemIt : public Key {
|
|
| 2889 | 2900 |
public: |
| 2890 |
typedef |
|
| 2901 |
typedef Key Parent; |
|
| 2891 | 2902 |
|
| 2892 | 2903 |
/// \brief Invalid constructor \& conversion. |
| 2893 | 2904 |
/// |
| 2894 |
/// This constructor initializes the |
|
| 2905 |
/// This constructor initializes the iterator to be invalid. |
|
| 2895 | 2906 |
/// \sa Invalid for more details. |
| 2896 | 2907 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2897 | 2908 |
|
| 2898 | 2909 |
/// \brief Creates an iterator with a value. |
| 2899 | 2910 |
/// |
| 2900 | 2911 |
/// Creates an iterator with a value. It iterates on the |
| 2901 |
/// keys which have the given value. |
|
| 2902 |
/// \param map The IterableIntMap |
|
| 2903 |
/// |
|
| 2912 |
/// keys mapped to the given value. |
|
| 2913 |
/// \param map The IterableIntMap. |
|
| 2914 |
/// \param value The value. |
|
| 2904 | 2915 |
ItemIt(const IterableIntMap& map, int value) : _map(&map) {
|
| 2905 | 2916 |
if (value < 0 || value >= int(_map->_first.size())) {
|
| 2906 | 2917 |
Parent::operator=(INVALID); |
| 2907 | 2918 |
} else {
|
| 2908 | 2919 |
Parent::operator=(_map->_first[value]); |
| 2909 | 2920 |
} |
| 2910 | 2921 |
} |
| 2911 | 2922 |
|
| 2912 | 2923 |
/// \brief Increment operator. |
| 2913 | 2924 |
/// |
| 2914 |
/// Increment |
|
| 2925 |
/// Increment operator. |
|
| 2915 | 2926 |
ItemIt& operator++() {
|
| 2916 | 2927 |
Parent::operator=(_map->IterableIntMap::Parent:: |
| 2917 | 2928 |
operator[](static_cast<Parent&>(*this)).next); |
| 2918 | 2929 |
return *this; |
| 2919 | 2930 |
} |
| 2920 | 2931 |
|
| 2921 |
|
|
| 2922 | 2932 |
private: |
| 2923 | 2933 |
const IterableIntMap* _map; |
| 2924 | 2934 |
}; |
| 2925 | 2935 |
|
| 2926 | 2936 |
protected: |
| 2927 | 2937 |
|
| 2928 | 2938 |
virtual void erase(const Key& key) {
|
| 2929 | 2939 |
unlace(key); |
| 2930 | 2940 |
Parent::erase(key); |
| 2931 | 2941 |
} |
| 2932 | 2942 |
|
| 2933 | 2943 |
virtual void erase(const std::vector<Key>& keys) {
|
| 2934 | 2944 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 2935 | 2945 |
unlace(keys[i]); |
| 2936 | 2946 |
} |
| 2937 | 2947 |
Parent::erase(keys); |
| 2938 | 2948 |
} |
| 2939 | 2949 |
|
| 2940 | 2950 |
virtual void clear() {
|
| 2941 | 2951 |
_first.clear(); |
| 2942 | 2952 |
Parent::clear(); |
| 2943 | 2953 |
} |
| 2944 | 2954 |
|
| 2945 | 2955 |
private: |
| 2946 |
std::vector< |
|
| 2956 |
std::vector<Key> _first; |
|
| 2947 | 2957 |
}; |
| 2948 | 2958 |
|
| 2949 | 2959 |
namespace _maps_bits {
|
| 2950 | 2960 |
template <typename Item, typename Value> |
| 2951 | 2961 |
struct IterableValueMapNode {
|
| 2952 | 2962 |
IterableValueMapNode(Value _value = Value()) : value(_value) {}
|
| 2953 | 2963 |
Item prev, next; |
| 2954 | 2964 |
Value value; |
| 2955 | 2965 |
}; |
| 2956 | 2966 |
} |
| 2957 | 2967 |
|
| 2958 |
///\ingroup graph_maps |
|
| 2959 |
/// |
|
| 2960 | 2968 |
/// \brief Dynamic iterable map for comparable values. |
| 2961 | 2969 |
/// |
| 2962 |
/// This class provides a special graph map type which can store |
|
| 2963 |
/// for each graph item(node, edge, etc.) a value. For each |
|
| 2964 |
/// value it is possible to iterate on the keys which mapped to the |
|
| 2965 |
/// given value. The type stores for each value a linked list with |
|
| 2970 |
/// This class provides a special graph map type which can store an |
|
| 2971 |
/// comparable value for graph items (\c Node, \c Arc or \c Edge). |
|
| 2972 |
/// For each value it is possible to iterate on the keys mapped to |
|
| 2973 |
/// the value. |
|
| 2974 |
/// |
|
| 2975 |
/// The map stores for each value a linked list with |
|
| 2966 | 2976 |
/// the items which mapped to the value, and the values are stored |
| 2967 | 2977 |
/// in balanced binary tree. The values of the map can be accessed |
| 2968 | 2978 |
/// with stl compatible forward iterator. |
| 2969 | 2979 |
/// |
| 2970 |
/// This type is not reference map so it cannot be modified with |
|
| 2980 |
/// This type is not reference map, so it cannot be modified with |
|
| 2971 | 2981 |
/// the subscription operator. |
| 2972 | 2982 |
/// |
| 2973 |
/// \ |
|
| 2983 |
/// \tparam GR The graph type. |
|
| 2984 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
|
| 2985 |
/// \c GR::Edge). |
|
| 2986 |
/// \tparam V The value type of the map. It can be any comparable |
|
| 2987 |
/// value type. |
|
| 2974 | 2988 |
/// |
| 2975 |
/// \param GR The graph type. |
|
| 2976 |
/// \param ITEM One of the graph's item type, the key of the map. |
|
| 2977 |
/// \param VAL Any comparable value type. |
|
| 2978 |
template <typename GR, typename ITEM, typename VAL> |
|
| 2989 |
/// \see IterableBoolMap, IterableIntMap |
|
| 2990 |
/// \see CrossRefMap |
|
| 2991 |
template <typename GR, typename K, typename V> |
|
| 2979 | 2992 |
class IterableValueMap |
| 2980 |
: protected ItemSetTraits<GR, ITEM>:: |
|
| 2981 |
template Map<_maps_bits::IterableValueMapNode<ITEM, VAL> >::Type {
|
|
| 2993 |
: protected ItemSetTraits<GR, K>:: |
|
| 2994 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
|
|
| 2982 | 2995 |
public: |
| 2983 |
typedef typename ItemSetTraits<GR, ITEM>:: |
|
| 2984 |
template Map<_maps_bits::IterableValueMapNode<ITEM, VAL> >::Type Parent; |
|
| 2996 |
typedef typename ItemSetTraits<GR, K>:: |
|
| 2997 |
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent; |
|
| 2985 | 2998 |
|
| 2986 | 2999 |
/// The key type |
| 2987 |
typedef |
|
| 3000 |
typedef K Key; |
|
| 2988 | 3001 |
/// The value type |
| 2989 |
typedef |
|
| 3002 |
typedef V Value; |
|
| 2990 | 3003 |
/// The graph type |
| 2991 | 3004 |
typedef GR Graph; |
| 2992 | 3005 |
|
| 2993 | 3006 |
public: |
| 2994 | 3007 |
|
| 2995 |
/// \brief Constructor of the |
|
| 3008 |
/// \brief Constructor of the map with a given value. |
|
| 2996 | 3009 |
/// |
| 2997 |
/// Constructor of the |
|
| 3010 |
/// Constructor of the map with a given value. |
|
| 2998 | 3011 |
explicit IterableValueMap(const Graph& graph, |
| 2999 | 3012 |
const Value& value = Value()) |
| 3000 |
: Parent(graph, _maps_bits::IterableValueMapNode< |
|
| 3013 |
: Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
|
|
| 3001 | 3014 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
| 3002 | 3015 |
lace(it); |
| 3003 | 3016 |
} |
| 3004 | 3017 |
} |
| 3005 | 3018 |
|
| 3006 | 3019 |
protected: |
| 3007 | 3020 |
|
| 3008 | 3021 |
void unlace(const Key& key) {
|
| 3009 | 3022 |
typename Parent::Value& node = Parent::operator[](key); |
| 3010 | 3023 |
if (node.prev != INVALID) {
|
| 3011 | 3024 |
Parent::operator[](node.prev).next = node.next; |
| 3012 | 3025 |
} else {
|
| 3013 | 3026 |
if (node.next != INVALID) {
|
| 3014 | 3027 |
_first[node.value] = node.next; |
| 3015 | 3028 |
} else {
|
| 3016 | 3029 |
_first.erase(node.value); |
| 3017 | 3030 |
} |
| 3018 | 3031 |
} |
| 3019 | 3032 |
if (node.next != INVALID) {
|
| 3020 | 3033 |
Parent::operator[](node.next).prev = node.prev; |
| 3021 | 3034 |
} |
| 3022 | 3035 |
} |
| 3023 | 3036 |
|
| 3024 | 3037 |
void lace(const Key& key) {
|
| 3025 | 3038 |
typename Parent::Value& node = Parent::operator[](key); |
| 3026 | 3039 |
typename std::map<Value, Key>::iterator it = _first.find(node.value); |
| 3027 | 3040 |
if (it == _first.end()) {
|
| 3028 | 3041 |
node.prev = node.next = INVALID; |
| 3029 |
if (node.next != INVALID) {
|
|
| 3030 |
Parent::operator[](node.next).prev = key; |
|
| 3031 |
} |
|
| 3032 | 3042 |
_first.insert(std::make_pair(node.value, key)); |
| 3033 | 3043 |
} else {
|
| 3034 | 3044 |
node.prev = INVALID; |
| 3035 | 3045 |
node.next = it->second; |
| 3036 | 3046 |
if (node.next != INVALID) {
|
| 3037 | 3047 |
Parent::operator[](node.next).prev = key; |
| 3038 | 3048 |
} |
| 3039 | 3049 |
it->second = key; |
| 3040 | 3050 |
} |
| 3041 | 3051 |
} |
| 3042 | 3052 |
|
| 3043 | 3053 |
public: |
| 3044 | 3054 |
|
| 3045 | 3055 |
/// \brief Forward iterator for values. |
| 3046 | 3056 |
/// |
| 3047 | 3057 |
/// This iterator is an stl compatible forward |
| 3048 | 3058 |
/// iterator on the values of the map. The values can |
| 3049 |
/// be accessed in the [beginValue, endValue) range. |
|
| 3050 |
/// |
|
| 3059 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range. |
|
| 3051 | 3060 |
class ValueIterator |
| 3052 | 3061 |
: public std::iterator<std::forward_iterator_tag, Value> {
|
| 3053 | 3062 |
friend class IterableValueMap; |
| 3054 | 3063 |
private: |
| 3055 | 3064 |
ValueIterator(typename std::map<Value, Key>::const_iterator _it) |
| 3056 | 3065 |
: it(_it) {}
|
| 3057 | 3066 |
public: |
| 3058 | 3067 |
|
| 3059 | 3068 |
ValueIterator() {}
|
| 3060 | 3069 |
|
| 3061 | 3070 |
ValueIterator& operator++() { ++it; return *this; }
|
| 3062 | 3071 |
ValueIterator operator++(int) {
|
| 3063 | 3072 |
ValueIterator tmp(*this); |
| 3064 | 3073 |
operator++(); |
| 3065 | 3074 |
return tmp; |
| 3066 | 3075 |
} |
| 3067 | 3076 |
|
| 3068 | 3077 |
const Value& operator*() const { return it->first; }
|
| 3069 | 3078 |
const Value* operator->() const { return &(it->first); }
|
| 3070 | 3079 |
|
| 3071 | 3080 |
bool operator==(ValueIterator jt) const { return it == jt.it; }
|
| 3072 | 3081 |
bool operator!=(ValueIterator jt) const { return it != jt.it; }
|
| 3073 | 3082 |
|
| 3074 | 3083 |
private: |
| 3075 | 3084 |
typename std::map<Value, Key>::const_iterator it; |
| 3076 | 3085 |
}; |
| 3077 | 3086 |
|
| 3078 | 3087 |
/// \brief Returns an iterator to the first value. |
| 3079 | 3088 |
/// |
| 3080 | 3089 |
/// Returns an stl compatible iterator to the |
| 3081 | 3090 |
/// first value of the map. The values of the |
| 3082 |
/// map can be accessed in the [beginValue, endValue) |
|
| 3091 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
|
| 3083 | 3092 |
/// range. |
| 3084 | 3093 |
ValueIterator beginValue() const {
|
| 3085 | 3094 |
return ValueIterator(_first.begin()); |
| 3086 | 3095 |
} |
| 3087 | 3096 |
|
| 3088 | 3097 |
/// \brief Returns an iterator after the last value. |
| 3089 | 3098 |
/// |
| 3090 | 3099 |
/// Returns an stl compatible iterator after the |
| 3091 | 3100 |
/// last value of the map. The values of the |
| 3092 |
/// map can be accessed in the [beginValue, endValue) |
|
| 3101 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
|
| 3093 | 3102 |
/// range. |
| 3094 | 3103 |
ValueIterator endValue() const {
|
| 3095 | 3104 |
return ValueIterator(_first.end()); |
| 3096 | 3105 |
} |
| 3097 | 3106 |
|
| 3098 | 3107 |
/// \brief Set operation of the map. |
| 3099 | 3108 |
/// |
| 3100 | 3109 |
/// Set operation of the map. |
| 3101 | 3110 |
void set(const Key& key, const Value& value) {
|
| 3102 | 3111 |
unlace(key); |
| 3103 | 3112 |
Parent::operator[](key).value = value; |
| 3104 | 3113 |
lace(key); |
| 3105 | 3114 |
} |
| 3106 | 3115 |
|
| 3107 | 3116 |
/// \brief Const subscript operator of the map. |
| 3108 | 3117 |
/// |
| 3109 | 3118 |
/// Const subscript operator of the map. |
| 3110 | 3119 |
const Value& operator[](const Key& key) const {
|
| 3111 | 3120 |
return Parent::operator[](key).value; |
| 3112 | 3121 |
} |
| 3113 | 3122 |
|
| 3114 | 3123 |
/// \brief Iterator for the keys with the same value. |
| 3115 | 3124 |
/// |
| 3116 | 3125 |
/// Iterator for the keys with the same value. It works |
| 3117 |
/// like a graph item iterator |
|
| 3126 |
/// like a graph item iterator, it can be converted to |
|
| 3118 | 3127 |
/// the item type of the map, incremented with \c ++ operator, and |
| 3119 |
/// if the iterator |
|
| 3128 |
/// if the iterator leaves the last valid item, it will be equal to |
|
| 3120 | 3129 |
/// \c INVALID. |
| 3121 |
class ItemIt : public |
|
| 3130 |
class ItemIt : public Key {
|
|
| 3122 | 3131 |
public: |
| 3123 |
typedef |
|
| 3132 |
typedef Key Parent; |
|
| 3124 | 3133 |
|
| 3125 | 3134 |
/// \brief Invalid constructor \& conversion. |
| 3126 | 3135 |
/// |
| 3127 |
/// This constructor initializes the |
|
| 3136 |
/// This constructor initializes the iterator to be invalid. |
|
| 3128 | 3137 |
/// \sa Invalid for more details. |
| 3129 | 3138 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 3130 | 3139 |
|
| 3131 | 3140 |
/// \brief Creates an iterator with a value. |
| 3132 | 3141 |
/// |
| 3133 | 3142 |
/// Creates an iterator with a value. It iterates on the |
| 3134 | 3143 |
/// keys which have the given value. |
| 3135 | 3144 |
/// \param map The IterableValueMap |
| 3136 | 3145 |
/// \param value The value |
| 3137 | 3146 |
ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
|
| 3138 | 3147 |
typename std::map<Value, Key>::const_iterator it = |
| 3139 | 3148 |
map._first.find(value); |
| 3140 | 3149 |
if (it == map._first.end()) {
|
| 3141 | 3150 |
Parent::operator=(INVALID); |
| 3142 | 3151 |
} else {
|
| 3143 | 3152 |
Parent::operator=(it->second); |
| 3144 | 3153 |
} |
| 3145 | 3154 |
} |
| 3146 | 3155 |
|
| 3147 | 3156 |
/// \brief Increment operator. |
| 3148 | 3157 |
/// |
| 3149 | 3158 |
/// Increment Operator. |
| 3150 | 3159 |
ItemIt& operator++() {
|
| 3151 | 3160 |
Parent::operator=(_map->IterableValueMap::Parent:: |
| 3152 | 3161 |
operator[](static_cast<Parent&>(*this)).next); |
| 3153 | 3162 |
return *this; |
| 3154 | 3163 |
} |
| 3155 | 3164 |
|
| 3156 | 3165 |
|
| 3157 | 3166 |
private: |
| 3158 | 3167 |
const IterableValueMap* _map; |
| 3159 | 3168 |
}; |
| 3160 | 3169 |
|
| 3161 | 3170 |
protected: |
| 3162 | 3171 |
|
| 3163 | 3172 |
virtual void add(const Key& key) {
|
| 3164 | 3173 |
Parent::add(key); |
| 3165 | 3174 |
unlace(key); |
| 3166 | 3175 |
} |
| 3167 | 3176 |
|
| 3168 | 3177 |
virtual void add(const std::vector<Key>& keys) {
|
| 3169 | 3178 |
Parent::add(keys); |
| 3170 | 3179 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 3171 | 3180 |
lace(keys[i]); |
| 3172 | 3181 |
} |
| 3173 | 3182 |
} |
| 3174 | 3183 |
|
| 3175 | 3184 |
virtual void erase(const Key& key) {
|
| 3176 | 3185 |
unlace(key); |
| 3177 | 3186 |
Parent::erase(key); |
| 3178 | 3187 |
} |
| 3179 | 3188 |
|
| 3180 | 3189 |
virtual void erase(const std::vector<Key>& keys) {
|
| 3181 | 3190 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 3182 | 3191 |
unlace(keys[i]); |
| 3183 | 3192 |
} |
| 3184 | 3193 |
Parent::erase(keys); |
| 3185 | 3194 |
} |
| 3186 | 3195 |
|
| 3187 | 3196 |
virtual void build() {
|
| 3188 | 3197 |
Parent::build(); |
| 3189 | 3198 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
| 3190 | 3199 |
lace(it); |
| 3191 | 3200 |
} |
| 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-2009 |
| 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 <deque> |
| 20 | 20 |
#include <set> |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/concept_check.h> |
| 23 | 23 |
#include <lemon/concepts/maps.h> |
| 24 | 24 |
#include <lemon/maps.h> |
| 25 |
#include <lemon/smart_graph.h> |
|
| 25 | 26 |
|
| 26 | 27 |
#include "test_tools.h" |
| 27 | 28 |
|
| 28 | 29 |
using namespace lemon; |
| 29 | 30 |
using namespace lemon::concepts; |
| 30 | 31 |
|
| 31 | 32 |
struct A {};
|
| 32 | 33 |
inline bool operator<(A, A) { return true; }
|
| 33 | 34 |
struct B {};
|
| 34 | 35 |
|
| 35 | 36 |
class C {
|
| 36 | 37 |
int x; |
| 37 | 38 |
public: |
| 38 | 39 |
C(int _x) : x(_x) {}
|
| 39 | 40 |
}; |
| 40 | 41 |
|
| 41 | 42 |
class F {
|
| 42 | 43 |
public: |
| 43 | 44 |
typedef A argument_type; |
| 44 | 45 |
typedef B result_type; |
| 45 | 46 |
|
| 46 | 47 |
B operator()(const A&) const { return B(); }
|
| 47 | 48 |
private: |
| 48 | 49 |
F& operator=(const F&); |
| 49 | 50 |
}; |
| 50 | 51 |
|
| 51 | 52 |
int func(A) { return 3; }
|
| 52 | 53 |
|
| 53 | 54 |
int binc(int a, B) { return a+1; }
|
| 54 | 55 |
|
| 55 | 56 |
typedef ReadMap<A, double> DoubleMap; |
| 56 | 57 |
typedef ReadWriteMap<A, double> DoubleWriteMap; |
| 57 | 58 |
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap; |
| 58 | 59 |
|
| 59 | 60 |
typedef ReadMap<A, bool> BoolMap; |
| 60 | 61 |
typedef ReadWriteMap<A, bool> BoolWriteMap; |
| 61 | 62 |
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap; |
| 62 | 63 |
|
| 63 | 64 |
int main() |
| 64 | 65 |
{
|
| 65 | 66 |
// Map concepts |
| 66 | 67 |
checkConcept<ReadMap<A,B>, ReadMap<A,B> >(); |
| 67 | 68 |
checkConcept<ReadMap<A,C>, ReadMap<A,C> >(); |
| 68 | 69 |
checkConcept<WriteMap<A,B>, WriteMap<A,B> >(); |
| 69 | 70 |
checkConcept<WriteMap<A,C>, WriteMap<A,C> >(); |
| 70 | 71 |
checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >(); |
| 71 | 72 |
checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >(); |
| 72 | 73 |
checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >(); |
| 73 | 74 |
checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >(); |
| 74 | 75 |
|
| 75 | 76 |
// NullMap |
| 76 | 77 |
{
|
| 77 | 78 |
checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >(); |
| 78 | 79 |
NullMap<A,B> map1; |
| 79 | 80 |
NullMap<A,B> map2 = map1; |
| 80 | 81 |
map1 = nullMap<A,B>(); |
| 81 | 82 |
} |
| 82 | 83 |
|
| 83 | 84 |
// ConstMap |
| 84 | 85 |
{
|
| 85 | 86 |
checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >(); |
| 86 | 87 |
checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >(); |
| 87 | 88 |
ConstMap<A,B> map1; |
| 88 | 89 |
ConstMap<A,B> map2 = B(); |
| ... | ... |
@@ -294,247 +295,247 @@ |
| 294 | 295 |
{
|
| 295 | 296 |
checkConcept<BoolMap, TrueMap<A> >(); |
| 296 | 297 |
checkConcept<BoolMap, FalseMap<A> >(); |
| 297 | 298 |
checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >(); |
| 298 | 299 |
checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >(); |
| 299 | 300 |
checkConcept<BoolMap, NotMap<BoolMap> >(); |
| 300 | 301 |
checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >(); |
| 301 | 302 |
checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >(); |
| 302 | 303 |
checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >(); |
| 303 | 304 |
|
| 304 | 305 |
TrueMap<int> tm; |
| 305 | 306 |
FalseMap<int> fm; |
| 306 | 307 |
RangeMap<bool> rm(2); |
| 307 | 308 |
rm[0] = true; rm[1] = false; |
| 308 | 309 |
check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && |
| 309 | 310 |
!andMap(fm,rm)[0] && !andMap(fm,rm)[1], |
| 310 | 311 |
"Something is wrong with AndMap"); |
| 311 | 312 |
check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && |
| 312 | 313 |
orMap(fm,rm)[0] && !orMap(fm,rm)[1], |
| 313 | 314 |
"Something is wrong with OrMap"); |
| 314 | 315 |
check(!notMap(rm)[0] && notMap(rm)[1], |
| 315 | 316 |
"Something is wrong with NotMap"); |
| 316 | 317 |
check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], |
| 317 | 318 |
"Something is wrong with NotWriteMap"); |
| 318 | 319 |
|
| 319 | 320 |
ConstMap<int, double> cm(2.0); |
| 320 | 321 |
IdentityMap<int> im; |
| 321 | 322 |
ConvertMap<IdentityMap<int>, double> id(im); |
| 322 | 323 |
check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3], |
| 323 | 324 |
"Something is wrong with LessMap"); |
| 324 | 325 |
check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3], |
| 325 | 326 |
"Something is wrong with EqualMap"); |
| 326 | 327 |
} |
| 327 | 328 |
|
| 328 | 329 |
// LoggerBoolMap |
| 329 | 330 |
{
|
| 330 | 331 |
typedef std::vector<int> vec; |
| 331 | 332 |
vec v1; |
| 332 | 333 |
vec v2(10); |
| 333 | 334 |
LoggerBoolMap<std::back_insert_iterator<vec> > |
| 334 | 335 |
map1(std::back_inserter(v1)); |
| 335 | 336 |
LoggerBoolMap<vec::iterator> map2(v2.begin()); |
| 336 | 337 |
map1.set(10, false); |
| 337 | 338 |
map1.set(20, true); map2.set(20, true); |
| 338 | 339 |
map1.set(30, false); map2.set(40, false); |
| 339 | 340 |
map1.set(50, true); map2.set(50, true); |
| 340 | 341 |
map1.set(60, true); map2.set(60, true); |
| 341 | 342 |
check(v1.size() == 3 && v2.size() == 10 && |
| 342 | 343 |
v1[0]==20 && v1[1]==50 && v1[2]==60 && |
| 343 | 344 |
v2[0]==20 && v2[1]==50 && v2[2]==60, |
| 344 | 345 |
"Something is wrong with LoggerBoolMap"); |
| 345 | 346 |
|
| 346 | 347 |
int i = 0; |
| 347 | 348 |
for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin(); |
| 348 | 349 |
it != map2.end(); ++it ) |
| 349 | 350 |
check(v1[i++] == *it, "Something is wrong with LoggerBoolMap"); |
| 350 | 351 |
} |
| 351 | 352 |
|
| 352 | 353 |
// Iterable bool map |
| 353 | 354 |
{
|
| 354 | 355 |
typedef SmartGraph Graph; |
| 355 | 356 |
typedef SmartGraph::Node Item; |
| 356 | 357 |
|
| 357 | 358 |
typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm; |
| 358 |
checkConcept< |
|
| 359 |
checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>(); |
|
| 359 | 360 |
|
| 360 | 361 |
const int num = 10; |
| 361 | 362 |
Graph g; |
| 362 | 363 |
std::vector<Item> items; |
| 363 | 364 |
for (int i = 0; i < num; ++i) {
|
| 364 | 365 |
items.push_back(g.addNode()); |
| 365 | 366 |
} |
| 366 | 367 |
|
| 367 | 368 |
Ibm map1(g, true); |
| 368 | 369 |
int n = 0; |
| 369 | 370 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
|
| 370 | 371 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt"); |
| 371 | 372 |
++n; |
| 372 | 373 |
} |
| 373 | 374 |
check(n == num, "Wrong number"); |
| 374 | 375 |
|
| 375 | 376 |
n = 0; |
| 376 | 377 |
for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
|
| 377 | 378 |
check(map1[static_cast<Item>(it)], "Wrong ItemIt for true"); |
| 378 | 379 |
++n; |
| 379 | 380 |
} |
| 380 | 381 |
check(n == num, "Wrong number"); |
| 381 | 382 |
check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt"); |
| 382 | 383 |
check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false"); |
| 383 | 384 |
|
| 384 | 385 |
map1[items[5]] = true; |
| 385 | 386 |
|
| 386 | 387 |
n = 0; |
| 387 | 388 |
for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
|
| 388 | 389 |
check(map1[static_cast<Item>(it)], "Wrong ItemIt for true"); |
| 389 | 390 |
++n; |
| 390 | 391 |
} |
| 391 | 392 |
check(n == num, "Wrong number"); |
| 392 | 393 |
|
| 393 | 394 |
map1[items[num / 2]] = false; |
| 394 | 395 |
check(map1[items[num / 2]] == false, "Wrong map value"); |
| 395 | 396 |
|
| 396 | 397 |
n = 0; |
| 397 | 398 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
|
| 398 | 399 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt for true"); |
| 399 | 400 |
++n; |
| 400 | 401 |
} |
| 401 | 402 |
check(n == num - 1, "Wrong number"); |
| 402 | 403 |
|
| 403 | 404 |
n = 0; |
| 404 | 405 |
for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
|
| 405 | 406 |
check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true"); |
| 406 | 407 |
++n; |
| 407 | 408 |
} |
| 408 | 409 |
check(n == 1, "Wrong number"); |
| 409 | 410 |
|
| 410 | 411 |
map1[items[0]] = false; |
| 411 | 412 |
check(map1[items[0]] == false, "Wrong map value"); |
| 412 | 413 |
|
| 413 | 414 |
map1[items[num - 1]] = false; |
| 414 | 415 |
check(map1[items[num - 1]] == false, "Wrong map value"); |
| 415 | 416 |
|
| 416 | 417 |
n = 0; |
| 417 | 418 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
|
| 418 | 419 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt for true"); |
| 419 | 420 |
++n; |
| 420 | 421 |
} |
| 421 | 422 |
check(n == num - 3, "Wrong number"); |
| 422 | 423 |
check(map1.trueNum() == num - 3, "Wrong number"); |
| 423 | 424 |
|
| 424 | 425 |
n = 0; |
| 425 | 426 |
for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
|
| 426 | 427 |
check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true"); |
| 427 | 428 |
++n; |
| 428 | 429 |
} |
| 429 | 430 |
check(n == 3, "Wrong number"); |
| 430 | 431 |
check(map1.falseNum() == 3, "Wrong number"); |
| 431 | 432 |
} |
| 432 | 433 |
|
| 433 | 434 |
// Iterable int map |
| 434 | 435 |
{
|
| 435 | 436 |
typedef SmartGraph Graph; |
| 436 | 437 |
typedef SmartGraph::Node Item; |
| 437 | 438 |
typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim; |
| 438 | 439 |
|
| 439 |
checkConcept< |
|
| 440 |
checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>(); |
|
| 440 | 441 |
|
| 441 | 442 |
const int num = 10; |
| 442 | 443 |
Graph g; |
| 443 | 444 |
std::vector<Item> items; |
| 444 | 445 |
for (int i = 0; i < num; ++i) {
|
| 445 | 446 |
items.push_back(g.addNode()); |
| 446 | 447 |
} |
| 447 | 448 |
|
| 448 | 449 |
Iim map1(g); |
| 449 | 450 |
check(map1.size() == 0, "Wrong size"); |
| 450 | 451 |
|
| 451 | 452 |
for (int i = 0; i < num; ++i) {
|
| 452 | 453 |
map1[items[i]] = i; |
| 453 | 454 |
} |
| 454 | 455 |
check(map1.size() == num, "Wrong size"); |
| 455 | 456 |
|
| 456 | 457 |
for (int i = 0; i < num; ++i) {
|
| 457 | 458 |
Iim::ItemIt it(map1, i); |
| 458 | 459 |
check(static_cast<Item>(it) == items[i], "Wrong value"); |
| 459 | 460 |
++it; |
| 460 | 461 |
check(static_cast<Item>(it) == INVALID, "Wrong value"); |
| 461 | 462 |
} |
| 462 | 463 |
|
| 463 | 464 |
for (int i = 0; i < num; ++i) {
|
| 464 | 465 |
map1[items[i]] = i % 2; |
| 465 | 466 |
} |
| 466 | 467 |
check(map1.size() == 2, "Wrong size"); |
| 467 | 468 |
|
| 468 | 469 |
int n = 0; |
| 469 | 470 |
for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
|
| 470 |
check(map1[static_cast<Item>(it)] == 0, "Wrong |
|
| 471 |
check(map1[static_cast<Item>(it)] == 0, "Wrong value"); |
|
| 471 | 472 |
++n; |
| 472 | 473 |
} |
| 473 | 474 |
check(n == (num + 1) / 2, "Wrong number"); |
| 474 | 475 |
|
| 475 | 476 |
for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
|
| 476 |
check(map1[static_cast<Item>(it)] == 1, "Wrong |
|
| 477 |
check(map1[static_cast<Item>(it)] == 1, "Wrong value"); |
|
| 477 | 478 |
++n; |
| 478 | 479 |
} |
| 479 | 480 |
check(n == num, "Wrong number"); |
| 480 | 481 |
|
| 481 | 482 |
} |
| 482 | 483 |
|
| 483 | 484 |
// Iterable value map |
| 484 | 485 |
{
|
| 485 | 486 |
typedef SmartGraph Graph; |
| 486 | 487 |
typedef SmartGraph::Node Item; |
| 487 | 488 |
typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm; |
| 488 | 489 |
|
| 489 | 490 |
checkConcept<ReadWriteMap<Item, double>, Ivm>(); |
| 490 | 491 |
|
| 491 | 492 |
const int num = 10; |
| 492 | 493 |
Graph g; |
| 493 | 494 |
std::vector<Item> items; |
| 494 | 495 |
for (int i = 0; i < num; ++i) {
|
| 495 | 496 |
items.push_back(g.addNode()); |
| 496 | 497 |
} |
| 497 | 498 |
|
| 498 | 499 |
Ivm map1(g, 0.0); |
| 499 | 500 |
check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size"); |
| 500 | 501 |
check(*map1.beginValue() == 0.0, "Wrong value"); |
| 501 | 502 |
|
| 502 | 503 |
for (int i = 0; i < num; ++i) {
|
| 503 | 504 |
map1.set(items[i], static_cast<double>(i)); |
| 504 | 505 |
} |
| 505 | 506 |
check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size"); |
| 506 | 507 |
|
| 507 | 508 |
for (int i = 0; i < num; ++i) {
|
| 508 | 509 |
Ivm::ItemIt it(map1, static_cast<double>(i)); |
| 509 | 510 |
check(static_cast<Item>(it) == items[i], "Wrong value"); |
| 510 | 511 |
++it; |
| 511 | 512 |
check(static_cast<Item>(it) == INVALID, "Wrong value"); |
| 512 | 513 |
} |
| 513 | 514 |
|
| 514 | 515 |
for (Ivm::ValueIterator vit = map1.beginValue(); |
| 515 | 516 |
vit != map1.endValue(); ++vit) {
|
| 516 | 517 |
check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit, |
| 517 | 518 |
"Wrong ValueIterator"); |
| 518 | 519 |
} |
| 519 | 520 |
|
| 520 | 521 |
for (int i = 0; i < num; ++i) {
|
| 521 | 522 |
map1.set(items[i], static_cast<double>(i % 2)); |
| 522 | 523 |
} |
| 523 | 524 |
check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size"); |
| 524 | 525 |
|
| 525 | 526 |
int n = 0; |
| 526 | 527 |
for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
|
| 527 |
check(map1[static_cast<Item>(it)] == 0.0, "Wrong |
|
| 528 |
check(map1[static_cast<Item>(it)] == 0.0, "Wrong value"); |
|
| 528 | 529 |
++n; |
| 529 | 530 |
} |
| 530 | 531 |
check(n == (num + 1) / 2, "Wrong number"); |
| 531 | 532 |
|
| 532 | 533 |
for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
|
| 533 |
check(map1[static_cast<Item>(it)] == 1.0, "Wrong |
|
| 534 |
check(map1[static_cast<Item>(it)] == 1.0, "Wrong value"); |
|
| 534 | 535 |
++n; |
| 535 | 536 |
} |
| 536 | 537 |
check(n == num, "Wrong number"); |
| 537 | 538 |
|
| 538 | 539 |
} |
| 539 | 540 |
return 0; |
| 540 | 541 |
} |
0 comments (0 inline)