gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fix the implementation and doc of CrossRefMap (#302) - Handle multiple values correctly with std::multimap. - Clarify the problematic points in the doc. - Add some basic tests for the class.
0 2 0
default
2 files changed with 75 insertions and 21 deletions:
↑ Collapse diff ↑
Ignore white space 384 line context
... ...
@@ -1713,568 +1713,587 @@
1713 1713
  /// @}
1714 1714

	
1715 1715
  /// \addtogroup maps
1716 1716
  /// @{
1717 1717

	
1718 1718
  /// \brief Writable bool map for logging each \c true assigned element
1719 1719
  ///
1720 1720
  /// A \ref concepts::WriteMap "writable" bool map for logging
1721 1721
  /// each \c true assigned element, i.e it copies subsequently each
1722 1722
  /// keys set to \c true to the given iterator.
1723 1723
  /// The most important usage of it is storing certain nodes or arcs
1724 1724
  /// that were marked \c true by an algorithm.
1725 1725
  ///
1726 1726
  /// There are several algorithms that provide solutions through bool
1727 1727
  /// maps and most of them assign \c true at most once for each key.
1728 1728
  /// In these cases it is a natural request to store each \c true
1729 1729
  /// assigned elements (in order of the assignment), which can be
1730 1730
  /// easily done with LoggerBoolMap.
1731 1731
  ///
1732 1732
  /// The simplest way of using this map is through the loggerBoolMap()
1733 1733
  /// function.
1734 1734
  ///
1735 1735
  /// \tparam IT The type of the iterator.
1736 1736
  /// \tparam KEY The key type of the map. The default value set
1737 1737
  /// according to the iterator type should work in most cases.
1738 1738
  ///
1739 1739
  /// \note The container of the iterator must contain enough space
1740 1740
  /// for the elements or the iterator should be an inserter iterator.
1741 1741
#ifdef DOXYGEN
1742 1742
  template <typename IT, typename KEY>
1743 1743
#else
1744 1744
  template <typename IT,
1745 1745
            typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1746 1746
#endif
1747 1747
  class LoggerBoolMap : public MapBase<KEY, bool> {
1748 1748
  public:
1749 1749

	
1750 1750
    ///\e
1751 1751
    typedef KEY Key;
1752 1752
    ///\e
1753 1753
    typedef bool Value;
1754 1754
    ///\e
1755 1755
    typedef IT Iterator;
1756 1756

	
1757 1757
    /// Constructor
1758 1758
    LoggerBoolMap(Iterator it)
1759 1759
      : _begin(it), _end(it) {}
1760 1760

	
1761 1761
    /// Gives back the given iterator set for the first key
1762 1762
    Iterator begin() const {
1763 1763
      return _begin;
1764 1764
    }
1765 1765

	
1766 1766
    /// Gives back the the 'after the last' iterator
1767 1767
    Iterator end() const {
1768 1768
      return _end;
1769 1769
    }
1770 1770

	
1771 1771
    /// The set function of the map
1772 1772
    void set(const Key& key, Value value) {
1773 1773
      if (value) {
1774 1774
        *_end++ = key;
1775 1775
      }
1776 1776
    }
1777 1777

	
1778 1778
  private:
1779 1779
    Iterator _begin;
1780 1780
    Iterator _end;
1781 1781
  };
1782 1782

	
1783 1783
  /// Returns a \c LoggerBoolMap class
1784 1784

	
1785 1785
  /// This function just returns a \c LoggerBoolMap class.
1786 1786
  ///
1787 1787
  /// The most important usage of it is storing certain nodes or arcs
1788 1788
  /// that were marked \c true by an algorithm.
1789 1789
  /// For example it makes easier to store the nodes in the processing
1790 1790
  /// order of Dfs algorithm, as the following examples show.
1791 1791
  /// \code
1792 1792
  ///   std::vector<Node> v;
1793 1793
  ///   dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1794 1794
  /// \endcode
1795 1795
  /// \code
1796 1796
  ///   std::vector<Node> v(countNodes(g));
1797 1797
  ///   dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1798 1798
  /// \endcode
1799 1799
  ///
1800 1800
  /// \note The container of the iterator must contain enough space
1801 1801
  /// for the elements or the iterator should be an inserter iterator.
1802 1802
  ///
1803 1803
  /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1804 1804
  /// it cannot be used when a readable map is needed, for example as
1805 1805
  /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1806 1806
  ///
1807 1807
  /// \relates LoggerBoolMap
1808 1808
  template<typename Iterator>
1809 1809
  inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1810 1810
    return LoggerBoolMap<Iterator>(it);
1811 1811
  }
1812 1812

	
1813 1813
  /// @}
1814 1814

	
1815 1815
  /// \addtogroup graph_maps
1816 1816
  /// @{
1817 1817

	
1818 1818
  /// \brief Provides an immutable and unique id for each item in a graph.
1819 1819
  ///
1820 1820
  /// IdMap provides a unique and immutable id for each item of the
1821 1821
  /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is 
1822 1822
  ///  - \b unique: different items get different ids,
1823 1823
  ///  - \b immutable: the id of an item does not change (even if you
1824 1824
  ///    delete other nodes).
1825 1825
  ///
1826 1826
  /// Using this map you get access (i.e. can read) the inner id values of
1827 1827
  /// the items stored in the graph, which is returned by the \c id()
1828 1828
  /// function of the graph. This map can be inverted with its member
1829 1829
  /// class \c InverseMap or with the \c operator() member.
1830 1830
  ///
1831 1831
  /// \tparam GR The graph type.
1832 1832
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1833 1833
  /// \c GR::Edge).
1834 1834
  ///
1835 1835
  /// \see RangeIdMap
1836 1836
  template <typename GR, typename K>
1837 1837
  class IdMap : public MapBase<K, int> {
1838 1838
  public:
1839 1839
    /// The graph type of IdMap.
1840 1840
    typedef GR Graph;
1841 1841
    typedef GR Digraph;
1842 1842
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1843 1843
    typedef K Item;
1844 1844
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1845 1845
    typedef K Key;
1846 1846
    /// The value type of IdMap.
1847 1847
    typedef int Value;
1848 1848

	
1849 1849
    /// \brief Constructor.
1850 1850
    ///
1851 1851
    /// Constructor of the map.
1852 1852
    explicit IdMap(const Graph& graph) : _graph(&graph) {}
1853 1853

	
1854 1854
    /// \brief Gives back the \e id of the item.
1855 1855
    ///
1856 1856
    /// Gives back the immutable and unique \e id of the item.
1857 1857
    int operator[](const Item& item) const { return _graph->id(item);}
1858 1858

	
1859 1859
    /// \brief Gives back the \e item by its id.
1860 1860
    ///
1861 1861
    /// Gives back the \e item by its id.
1862 1862
    Item operator()(int id) { return _graph->fromId(id, Item()); }
1863 1863

	
1864 1864
  private:
1865 1865
    const Graph* _graph;
1866 1866

	
1867 1867
  public:
1868 1868

	
1869 1869
    /// \brief This class represents the inverse of its owner (IdMap).
1870 1870
    ///
1871 1871
    /// This class represents the inverse of its owner (IdMap).
1872 1872
    /// \see inverse()
1873 1873
    class InverseMap {
1874 1874
    public:
1875 1875

	
1876 1876
      /// \brief Constructor.
1877 1877
      ///
1878 1878
      /// Constructor for creating an id-to-item map.
1879 1879
      explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1880 1880

	
1881 1881
      /// \brief Constructor.
1882 1882
      ///
1883 1883
      /// Constructor for creating an id-to-item map.
1884 1884
      explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1885 1885

	
1886 1886
      /// \brief Gives back the given item from its id.
1887 1887
      ///
1888 1888
      /// Gives back the given item from its id.
1889 1889
      Item operator[](int id) const { return _graph->fromId(id, Item());}
1890 1890

	
1891 1891
    private:
1892 1892
      const Graph* _graph;
1893 1893
    };
1894 1894

	
1895 1895
    /// \brief Gives back the inverse of the map.
1896 1896
    ///
1897 1897
    /// Gives back the inverse of the IdMap.
1898 1898
    InverseMap inverse() const { return InverseMap(*_graph);}
1899 1899
  };
1900 1900

	
1901 1901

	
1902 1902
  /// \brief General cross reference graph map type.
1903 1903

	
1904 1904
  /// This class provides simple invertable graph maps.
1905
  /// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap"
1906
  /// and if a key is set to a new value then store it
1907
  /// in the inverse map.
1908
  ///
1905
  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1906
  /// and if a key is set to a new value, then stores it in the inverse map.
1909 1907
  /// The values of the map can be accessed
1910 1908
  /// with stl compatible forward iterator.
1911 1909
  ///
1910
  /// This type is not reference map, so it cannot be modified with
1911
  /// the subscript operator.
1912
  ///
1912 1913
  /// \tparam GR The graph type.
1913 1914
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1914 1915
  /// \c GR::Edge).
1915 1916
  /// \tparam V The value type of the map.
1916 1917
  ///
1917 1918
  /// \see IterableValueMap
1918 1919
  template <typename GR, typename K, typename V>
1919 1920
  class CrossRefMap
1920 1921
    : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1921 1922
  private:
1922 1923

	
1923 1924
    typedef typename ItemSetTraits<GR, K>::
1924 1925
      template Map<V>::Type Map;
1925 1926

	
1926
    typedef std::map<V, K> Container;
1927
    typedef std::multimap<V, K> Container;
1927 1928
    Container _inv_map;
1928 1929

	
1929 1930
  public:
1930 1931

	
1931 1932
    /// The graph type of CrossRefMap.
1932 1933
    typedef GR Graph;
1933 1934
    typedef GR Digraph;
1934 1935
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1935 1936
    typedef K Item;
1936 1937
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1937 1938
    typedef K Key;
1938 1939
    /// The value type of CrossRefMap.
1939 1940
    typedef V Value;
1940 1941

	
1941 1942
    /// \brief Constructor.
1942 1943
    ///
1943 1944
    /// Construct a new CrossRefMap for the given graph.
1944 1945
    explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1945 1946

	
1946 1947
    /// \brief Forward iterator for values.
1947 1948
    ///
1948 1949
    /// This iterator is an stl compatible forward
1949 1950
    /// iterator on the values of the map. The values can
1950 1951
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1952
    /// They are considered with multiplicity, so each value is
1953
    /// traversed for each item it is assigned to.
1951 1954
    class ValueIterator
1952 1955
      : public std::iterator<std::forward_iterator_tag, Value> {
1953 1956
      friend class CrossRefMap;
1954 1957
    private:
1955 1958
      ValueIterator(typename Container::const_iterator _it)
1956 1959
        : it(_it) {}
1957 1960
    public:
1958 1961

	
1959 1962
      ValueIterator() {}
1960 1963

	
1961 1964
      ValueIterator& operator++() { ++it; return *this; }
1962 1965
      ValueIterator operator++(int) {
1963 1966
        ValueIterator tmp(*this);
1964 1967
        operator++();
1965 1968
        return tmp;
1966 1969
      }
1967 1970

	
1968 1971
      const Value& operator*() const { return it->first; }
1969 1972
      const Value* operator->() const { return &(it->first); }
1970 1973

	
1971 1974
      bool operator==(ValueIterator jt) const { return it == jt.it; }
1972 1975
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
1973 1976

	
1974 1977
    private:
1975 1978
      typename Container::const_iterator it;
1976 1979
    };
1977 1980

	
1978 1981
    /// \brief Returns an iterator to the first value.
1979 1982
    ///
1980 1983
    /// Returns an stl compatible iterator to the
1981 1984
    /// first value of the map. The values of the
1982 1985
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1983 1986
    /// range.
1984 1987
    ValueIterator beginValue() const {
1985 1988
      return ValueIterator(_inv_map.begin());
1986 1989
    }
1987 1990

	
1988 1991
    /// \brief Returns an iterator after the last value.
1989 1992
    ///
1990 1993
    /// Returns an stl compatible iterator after the
1991 1994
    /// last value of the map. The values of the
1992 1995
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1993 1996
    /// range.
1994 1997
    ValueIterator endValue() const {
1995 1998
      return ValueIterator(_inv_map.end());
1996 1999
    }
1997 2000

	
1998 2001
    /// \brief Sets the value associated with the given key.
1999 2002
    ///
2000 2003
    /// Sets the value associated with the given key.
2001 2004
    void set(const Key& key, const Value& val) {
2002 2005
      Value oldval = Map::operator[](key);
2003
      typename Container::iterator it = _inv_map.find(oldval);
2004
      if (it != _inv_map.end() && it->second == key) {
2005
        _inv_map.erase(it);
2006
      typename Container::iterator it;
2007
      for (it = _inv_map.equal_range(oldval).first;
2008
           it != _inv_map.equal_range(oldval).second; ++it) {
2009
        if (it->second == key) {
2010
          _inv_map.erase(it);
2011
          break;
2012
        }
2006 2013
      }
2007
      _inv_map.insert(make_pair(val, key));
2014
      _inv_map.insert(std::make_pair(val, key));
2008 2015
      Map::set(key, val);
2009 2016
    }
2010 2017

	
2011 2018
    /// \brief Returns the value associated with the given key.
2012 2019
    ///
2013 2020
    /// Returns the value associated with the given key.
2014 2021
    typename MapTraits<Map>::ConstReturnValue
2015 2022
    operator[](const Key& key) const {
2016 2023
      return Map::operator[](key);
2017 2024
    }
2018 2025

	
2019
    /// \brief Gives back the item by its value.
2026
    /// \brief Gives back an item by its value.
2020 2027
    ///
2021
    /// Gives back the item by its value.
2022
    Key operator()(const Value& key) const {
2023
      typename Container::const_iterator it = _inv_map.find(key);
2028
    /// This function gives back an item that is assigned to
2029
    /// the given value or \c INVALID if no such item exists.
2030
    /// If there are more items with the same associated value,
2031
    /// only one of them is returned.
2032
    Key operator()(const Value& val) const {
2033
      typename Container::const_iterator it = _inv_map.find(val);
2024 2034
      return it != _inv_map.end() ? it->second : INVALID;
2025 2035
    }
2026 2036

	
2027 2037
  protected:
2028 2038

	
2029 2039
    /// \brief Erase the key from the map and the inverse map.
2030 2040
    ///
2031 2041
    /// Erase the key from the map and the inverse map. It is called by the
2032 2042
    /// \c AlterationNotifier.
2033 2043
    virtual void erase(const Key& key) {
2034 2044
      Value val = Map::operator[](key);
2035
      typename Container::iterator it = _inv_map.find(val);
2036
      if (it != _inv_map.end() && it->second == key) {
2037
        _inv_map.erase(it);
2045
      typename Container::iterator it;
2046
      for (it = _inv_map.equal_range(val).first;
2047
           it != _inv_map.equal_range(val).second; ++it) {
2048
        if (it->second == key) {
2049
          _inv_map.erase(it);
2050
          break;
2051
        }
2038 2052
      }
2039 2053
      Map::erase(key);
2040 2054
    }
2041 2055

	
2042 2056
    /// \brief Erase more keys from the map and the inverse map.
2043 2057
    ///
2044 2058
    /// Erase more keys from the map and the inverse map. It is called by the
2045 2059
    /// \c AlterationNotifier.
2046 2060
    virtual void erase(const std::vector<Key>& keys) {
2047 2061
      for (int i = 0; i < int(keys.size()); ++i) {
2048 2062
        Value val = Map::operator[](keys[i]);
2049
        typename Container::iterator it = _inv_map.find(val);
2050
        if (it != _inv_map.end() && it->second == keys[i]) {
2051
          _inv_map.erase(it);
2063
        typename Container::iterator it;
2064
        for (it = _inv_map.equal_range(val).first;
2065
             it != _inv_map.equal_range(val).second; ++it) {
2066
          if (it->second == keys[i]) {
2067
            _inv_map.erase(it);
2068
            break;
2069
          }
2052 2070
        }
2053 2071
      }
2054 2072
      Map::erase(keys);
2055 2073
    }
2056 2074

	
2057 2075
    /// \brief Clear the keys from the map and the inverse map.
2058 2076
    ///
2059 2077
    /// Clear the keys from the map and the inverse map. It is called by the
2060 2078
    /// \c AlterationNotifier.
2061 2079
    virtual void clear() {
2062 2080
      _inv_map.clear();
2063 2081
      Map::clear();
2064 2082
    }
2065 2083

	
2066 2084
  public:
2067 2085

	
2068 2086
    /// \brief The inverse map type.
2069 2087
    ///
2070 2088
    /// The inverse of this map. The subscript operator of the map
2071 2089
    /// gives back the item that was last assigned to the value.
2072 2090
    class InverseMap {
2073 2091
    public:
2074 2092
      /// \brief Constructor
2075 2093
      ///
2076 2094
      /// Constructor of the InverseMap.
2077 2095
      explicit InverseMap(const CrossRefMap& inverted)
2078 2096
        : _inverted(inverted) {}
2079 2097

	
2080 2098
      /// The value type of the InverseMap.
2081 2099
      typedef typename CrossRefMap::Key Value;
2082 2100
      /// The key type of the InverseMap.
2083 2101
      typedef typename CrossRefMap::Value Key;
2084 2102

	
2085 2103
      /// \brief Subscript operator.
2086 2104
      ///
2087
      /// Subscript operator. It gives back the item
2088
      /// that was last assigned to the given value.
2105
      /// Subscript operator. It gives back an item
2106
      /// that is assigned to the given value or \c INVALID
2107
      /// if no such item exists.
2089 2108
      Value operator[](const Key& key) const {
2090 2109
        return _inverted(key);
2091 2110
      }
2092 2111

	
2093 2112
    private:
2094 2113
      const CrossRefMap& _inverted;
2095 2114
    };
2096 2115

	
2097 2116
    /// \brief It gives back the read-only inverse map.
2098 2117
    ///
2099 2118
    /// It gives back the read-only inverse map.
2100 2119
    InverseMap inverse() const {
2101 2120
      return InverseMap(*this);
2102 2121
    }
2103 2122

	
2104 2123
  };
2105 2124

	
2106 2125
  /// \brief Provides continuous and unique ID for the
2107 2126
  /// items of a graph.
2108 2127
  ///
2109 2128
  /// RangeIdMap provides a unique and continuous
2110 2129
  /// ID for each item of a given type (\c Node, \c Arc or
2111 2130
  /// \c Edge) in a graph. This id is
2112 2131
  ///  - \b unique: different items get different ids,
2113 2132
  ///  - \b continuous: the range of the ids is the set of integers
2114 2133
  ///    between 0 and \c n-1, where \c n is the number of the items of
2115 2134
  ///    this type (\c Node, \c Arc or \c Edge).
2116 2135
  ///  - So, the ids can change when deleting an item of the same type.
2117 2136
  ///
2118 2137
  /// Thus this id is not (necessarily) the same as what can get using
2119 2138
  /// the \c id() function of the graph or \ref IdMap.
2120 2139
  /// This map can be inverted with its member class \c InverseMap,
2121 2140
  /// or with the \c operator() member.
2122 2141
  ///
2123 2142
  /// \tparam GR The graph type.
2124 2143
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2125 2144
  /// \c GR::Edge).
2126 2145
  ///
2127 2146
  /// \see IdMap
2128 2147
  template <typename GR, typename K>
2129 2148
  class RangeIdMap
2130 2149
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2131 2150

	
2132 2151
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2133 2152

	
2134 2153
  public:
2135 2154
    /// The graph type of RangeIdMap.
2136 2155
    typedef GR Graph;
2137 2156
    typedef GR Digraph;
2138 2157
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2139 2158
    typedef K Item;
2140 2159
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2141 2160
    typedef K Key;
2142 2161
    /// The value type of RangeIdMap.
2143 2162
    typedef int Value;
2144 2163

	
2145 2164
    /// \brief Constructor.
2146 2165
    ///
2147 2166
    /// Constructor.
2148 2167
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
2149 2168
      Item it;
2150 2169
      const typename Map::Notifier* nf = Map::notifier();
2151 2170
      for (nf->first(it); it != INVALID; nf->next(it)) {
2152 2171
        Map::set(it, _inv_map.size());
2153 2172
        _inv_map.push_back(it);
2154 2173
      }
2155 2174
    }
2156 2175

	
2157 2176
  protected:
2158 2177

	
2159 2178
    /// \brief Adds a new key to the map.
2160 2179
    ///
2161 2180
    /// Add a new key to the map. It is called by the
2162 2181
    /// \c AlterationNotifier.
2163 2182
    virtual void add(const Item& item) {
2164 2183
      Map::add(item);
2165 2184
      Map::set(item, _inv_map.size());
2166 2185
      _inv_map.push_back(item);
2167 2186
    }
2168 2187

	
2169 2188
    /// \brief Add more new keys to the map.
2170 2189
    ///
2171 2190
    /// Add more new keys to the map. It is called by the
2172 2191
    /// \c AlterationNotifier.
2173 2192
    virtual void add(const std::vector<Item>& items) {
2174 2193
      Map::add(items);
2175 2194
      for (int i = 0; i < int(items.size()); ++i) {
2176 2195
        Map::set(items[i], _inv_map.size());
2177 2196
        _inv_map.push_back(items[i]);
2178 2197
      }
2179 2198
    }
2180 2199

	
2181 2200
    /// \brief Erase the key from the map.
2182 2201
    ///
2183 2202
    /// Erase the key from the map. It is called by the
2184 2203
    /// \c AlterationNotifier.
2185 2204
    virtual void erase(const Item& item) {
2186 2205
      Map::set(_inv_map.back(), Map::operator[](item));
2187 2206
      _inv_map[Map::operator[](item)] = _inv_map.back();
2188 2207
      _inv_map.pop_back();
2189 2208
      Map::erase(item);
2190 2209
    }
2191 2210

	
2192 2211
    /// \brief Erase more keys from the map.
2193 2212
    ///
2194 2213
    /// Erase more keys from the map. It is called by the
2195 2214
    /// \c AlterationNotifier.
2196 2215
    virtual void erase(const std::vector<Item>& items) {
2197 2216
      for (int i = 0; i < int(items.size()); ++i) {
2198 2217
        Map::set(_inv_map.back(), Map::operator[](items[i]));
2199 2218
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
2200 2219
        _inv_map.pop_back();
2201 2220
      }
2202 2221
      Map::erase(items);
2203 2222
    }
2204 2223

	
2205 2224
    /// \brief Build the unique map.
2206 2225
    ///
2207 2226
    /// Build the unique map. It is called by the
2208 2227
    /// \c AlterationNotifier.
2209 2228
    virtual void build() {
2210 2229
      Map::build();
2211 2230
      Item it;
2212 2231
      const typename Map::Notifier* nf = Map::notifier();
2213 2232
      for (nf->first(it); it != INVALID; nf->next(it)) {
2214 2233
        Map::set(it, _inv_map.size());
2215 2234
        _inv_map.push_back(it);
2216 2235
      }
2217 2236
    }
2218 2237

	
2219 2238
    /// \brief Clear the keys from the map.
2220 2239
    ///
2221 2240
    /// Clear the keys from the map. It is called by the
2222 2241
    /// \c AlterationNotifier.
2223 2242
    virtual void clear() {
2224 2243
      _inv_map.clear();
2225 2244
      Map::clear();
2226 2245
    }
2227 2246

	
2228 2247
  public:
2229 2248

	
2230 2249
    /// \brief Returns the maximal value plus one.
2231 2250
    ///
2232 2251
    /// Returns the maximal value plus one in the map.
2233 2252
    unsigned int size() const {
2234 2253
      return _inv_map.size();
2235 2254
    }
2236 2255

	
2237 2256
    /// \brief Swaps the position of the two items in the map.
2238 2257
    ///
2239 2258
    /// Swaps the position of the two items in the map.
2240 2259
    void swap(const Item& p, const Item& q) {
2241 2260
      int pi = Map::operator[](p);
2242 2261
      int qi = Map::operator[](q);
2243 2262
      Map::set(p, qi);
2244 2263
      _inv_map[qi] = p;
2245 2264
      Map::set(q, pi);
2246 2265
      _inv_map[pi] = q;
2247 2266
    }
2248 2267

	
2249 2268
    /// \brief Gives back the \e RangeId of the item
2250 2269
    ///
2251 2270
    /// Gives back the \e RangeId of the item.
2252 2271
    int operator[](const Item& item) const {
2253 2272
      return Map::operator[](item);
2254 2273
    }
2255 2274

	
2256 2275
    /// \brief Gives back the item belonging to a \e RangeId
2257 2276
    /// 
2258 2277
    /// Gives back the item belonging to a \e RangeId.
2259 2278
    Item operator()(int id) const {
2260 2279
      return _inv_map[id];
2261 2280
    }
2262 2281

	
2263 2282
  private:
2264 2283

	
2265 2284
    typedef std::vector<Item> Container;
2266 2285
    Container _inv_map;
2267 2286

	
2268 2287
  public:
2269 2288

	
2270 2289
    /// \brief The inverse map type of RangeIdMap.
2271 2290
    ///
2272 2291
    /// The inverse map type of RangeIdMap.
2273 2292
    class InverseMap {
2274 2293
    public:
2275 2294
      /// \brief Constructor
2276 2295
      ///
2277 2296
      /// Constructor of the InverseMap.
2278 2297
      explicit InverseMap(const RangeIdMap& inverted)
2279 2298
        : _inverted(inverted) {}
2280 2299

	
Ignore white space 384 line context
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/list_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();
89 90
    ConstMap<A,B> map3 = map1;
90 91
    map1 = constMap<A>(B());
91 92
    map1 = constMap<A,B>();
92 93
    map1.setAll(B());
93 94
    ConstMap<A,C> map4(C(1));
94 95
    ConstMap<A,C> map5 = map4;
95 96
    map4 = constMap<A>(C(2));
96 97
    map4.setAll(C(3));
97 98

	
98 99
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
99 100
    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
100 101

	
101 102
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
102 103
    ConstMap<A,Const<int,10> > map6;
103 104
    ConstMap<A,Const<int,10> > map7 = map6;
104 105
    map6 = constMap<A,int,10>();
105 106
    map7 = constMap<A,Const<int,10> >();
106 107
    check(map6[A()] == 10 && map7[A()] == 10,
107 108
          "Something is wrong with ConstMap");
108 109
  }
109 110

	
110 111
  // IdentityMap
111 112
  {
112 113
    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
113 114
    IdentityMap<A> map1;
114 115
    IdentityMap<A> map2 = map1;
115 116
    map1 = identityMap<A>();
116 117

	
117 118
    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
118 119
    check(identityMap<double>()[1.0] == 1.0 &&
119 120
          identityMap<double>()[3.14] == 3.14,
120 121
          "Something is wrong with IdentityMap");
121 122
  }
122 123

	
123 124
  // RangeMap
124 125
  {
125 126
    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
126 127
    RangeMap<B> map1;
127 128
    RangeMap<B> map2(10);
128 129
    RangeMap<B> map3(10,B());
129 130
    RangeMap<B> map4 = map1;
130 131
    RangeMap<B> map5 = rangeMap<B>();
131 132
    RangeMap<B> map6 = rangeMap<B>(10);
132 133
    RangeMap<B> map7 = rangeMap(10,B());
133 134

	
134 135
    checkConcept< ReferenceMap<int, double, double&, const double&>,
135 136
                  RangeMap<double> >();
136 137
    std::vector<double> v(10, 0);
137 138
    v[5] = 100;
138 139
    RangeMap<double> map8(v);
139 140
    RangeMap<double> map9 = rangeMap(v);
140 141
    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
141 142
          "Something is wrong with RangeMap");
142 143
  }
143 144

	
144 145
  // SparseMap
145 146
  {
146 147
    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
147 148
    SparseMap<A,B> map1;
148 149
    SparseMap<A,B> map2 = B();
149 150
    SparseMap<A,B> map3 = sparseMap<A,B>();
150 151
    SparseMap<A,B> map4 = sparseMap<A>(B());
151 152

	
152 153
    checkConcept< ReferenceMap<double, int, int&, const int&>,
153 154
                  SparseMap<double, int> >();
154 155
    std::map<double, int> m;
155 156
    SparseMap<double, int> map5(m);
156 157
    SparseMap<double, int> map6(m,10);
157 158
    SparseMap<double, int> map7 = sparseMap(m);
158 159
    SparseMap<double, int> map8 = sparseMap(m,10);
159 160

	
160 161
    check(map5[1.0] == 0 && map5[3.14] == 0 &&
161 162
          map6[1.0] == 10 && map6[3.14] == 10,
162 163
          "Something is wrong with SparseMap");
163 164
    map5[1.0] = map6[3.14] = 100;
164 165
    check(map5[1.0] == 100 && map5[3.14] == 0 &&
165 166
          map6[1.0] == 10 && map6[3.14] == 100,
166 167
          "Something is wrong with SparseMap");
167 168
  }
168 169

	
169 170
  // ComposeMap
170 171
  {
171 172
    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
172 173
    checkConcept<ReadMap<B,double>, CompMap>();
173 174
    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
174 175
    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
175 176

	
176 177
    SparseMap<double, bool> m1(false); m1[3.14] = true;
177 178
    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
178 179
    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
179 180
          "Something is wrong with ComposeMap")
180 181
  }
181 182

	
182 183
  // CombineMap
183 184
  {
184 185
    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
185 186
    checkConcept<ReadMap<A,double>, CombMap>();
186 187
    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
187 188
    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
188 189

	
189 190
    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
190 191
          "Something is wrong with CombineMap");
191 192
  }
192 193

	
193 194
  // FunctorToMap, MapToFunctor
194 195
  {
195 196
    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
196 197
    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
197 198
    FunctorToMap<F> map1;
198 199
    FunctorToMap<F> map2 = FunctorToMap<F>(F());
199 200
    B b = functorToMap(F())[A()];
200 201

	
201 202
    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
202 203
    MapToFunctor<ReadMap<A,B> > map = MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
203 204

	
204 205
    check(functorToMap(&func)[A()] == 3,
205 206
          "Something is wrong with FunctorToMap");
206 207
    check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
207 208
          "Something is wrong with MapToFunctor");
208 209
    check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
209 210
          mapToFunctor(functorToMap(&func))[A()] == 3,
210 211
          "Something is wrong with FunctorToMap or MapToFunctor");
211 212
    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
212 213
          "Something is wrong with FunctorToMap or MapToFunctor");
213 214
  }
214 215

	
215 216
  // ConvertMap
216 217
  {
217 218
    checkConcept<ReadMap<double,double>,
218 219
      ConvertMap<ReadMap<double, int>, double> >();
219 220
    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
220 221
    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
221 222
  }
222 223

	
223 224
  // ForkMap
224 225
  {
225 226
    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
226 227

	
227 228
    typedef RangeMap<double> RM;
228 229
    typedef SparseMap<int, double> SM;
229 230
    RM m1(10, -1);
230 231
    SM m2(-1);
231 232
    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
232 233
    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
233 234
    ForkMap<RM, SM> map1(m1,m2);
234 235
    ForkMap<SM, RM> map2 = forkMap(m2,m1);
235 236
    map2.set(5, 10);
236 237
    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
237 238
          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
238 239
          "Something is wrong with ForkMap");
239 240
  }
240 241

	
241 242
  // Arithmetic maps:
242 243
  // - AddMap, SubMap, MulMap, DivMap
243 244
  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
244 245
  // - NegMap, NegWriteMap, AbsMap
245 246
  {
246 247
    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
247 248
    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
248 249
    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
249 250
    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
250 251

	
251 252
    ConstMap<int, double> c1(1.0), c2(3.14);
252 253
    IdentityMap<int> im;
253 254
    ConvertMap<IdentityMap<int>, double> id(im);
254 255
    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
255 256
          "Something is wrong with AddMap");
256 257
    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
257 258
          "Something is wrong with SubMap");
258 259
    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
259 260
          "Something is wrong with MulMap");
260 261
    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
261 262
          "Something is wrong with DivMap");
262 263

	
263 264
    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
264 265
    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
265 266
    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
266 267
    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
267 268
    checkConcept<DoubleMap, NegMap<DoubleMap> >();
268 269
    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
269 270
    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
270 271

	
271 272
    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
272 273
          "Something is wrong with ShiftMap");
273 274
    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
274 275
          shiftWriteMap(id, 2.0)[10] == 12.0,
275 276
          "Something is wrong with ShiftWriteMap");
276 277
    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
277 278
          "Something is wrong with ScaleMap");
278 279
    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
279 280
          scaleWriteMap(id, 2.0)[10] == 20.0,
280 281
          "Something is wrong with ScaleWriteMap");
281 282
    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
282 283
          "Something is wrong with NegMap");
283 284
    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
284 285
          "Something is wrong with NegWriteMap");
285 286
    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
286 287
          "Something is wrong with AbsMap");
287 288
  }
288 289

	
289 290
  // Logical maps:
290 291
  // - TrueMap, FalseMap
291 292
  // - AndMap, OrMap
292 293
  // - NotMap, NotWriteMap
293 294
  // - EqualMap, LessMap
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
  }
352
  
353
  // CrossRefMap
354
  {
355
    typedef ListDigraph Graph;
356
    DIGRAPH_TYPEDEFS(Graph);
357

	
358
    checkConcept<ReadWriteMap<Node, int>,
359
                 CrossRefMap<Graph, Node, int> >();
360
    
361
    Graph gr;
362
    typedef CrossRefMap<Graph, Node, char> CRMap;
363
    typedef CRMap::ValueIterator ValueIt;
364
    CRMap map(gr);
365
    
366
    Node n0 = gr.addNode();
367
    Node n1 = gr.addNode();
368
    Node n2 = gr.addNode();
369
    
370
    map.set(n0, 'A');
371
    map.set(n1, 'B');
372
    map.set(n2, 'C');
373
    map.set(n2, 'A');
374
    map.set(n0, 'C');
375

	
376
    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
377
          "Wrong CrossRefMap");
378
    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
379
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
380
    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
381

	
382
    ValueIt it = map.beginValue();
383
    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
384
          it == map.endValue(), "Wrong value iterator");
385
  }
351 386

	
352 387
  return 0;
353 388
}
0 comments (0 inline)