gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Improvements for graph maps (#302) - Add a count() function to CrossRefMap. - Add tests for IdMap and RangeIdMap. - Extend tests for CrossRefMap. - Improve the doc.
0 2 0
default
2 files changed with 123 insertions and 5 deletions:
↑ Collapse diff ↑
Show white space 192 line context
... ...
@@ -1733,193 +1733,193 @@
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
  /// class \c InverseMap or with the \c operator() member.
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 1905
  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1906 1906
  /// and if a key is set to a new value, then stores it in the inverse map.
1907 1907
  /// The values of the map can be accessed
1908 1908
  /// with stl compatible forward iterator.
1909 1909
  ///
1910 1910
  /// This type is not reference map, so it cannot be modified with
1911 1911
  /// the subscript operator.
1912 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;
... ...
@@ -1941,296 +1941,304 @@
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
    /// They are considered with multiplicity, so each value is
1953 1953
    /// traversed for each item it is assigned to.
1954 1954
    class ValueIterator
1955 1955
      : public std::iterator<std::forward_iterator_tag, Value> {
1956 1956
      friend class CrossRefMap;
1957 1957
    private:
1958 1958
      ValueIterator(typename Container::const_iterator _it)
1959 1959
        : it(_it) {}
1960 1960
    public:
1961 1961

	
1962 1962
      ValueIterator() {}
1963 1963

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

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

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

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

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

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

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

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

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

	
2037
    /// \brief Returns the number of items with the given value.
2038
    ///
2039
    /// This function returns the number of items with the given value
2040
    /// associated with it.
2041
    int count(const Value &val) const {
2042
      return _inv_map.count(val);
2043
    }
2044

	
2037 2045
  protected:
2038 2046

	
2039 2047
    /// \brief Erase the key from the map and the inverse map.
2040 2048
    ///
2041 2049
    /// Erase the key from the map and the inverse map. It is called by the
2042 2050
    /// \c AlterationNotifier.
2043 2051
    virtual void erase(const Key& key) {
2044 2052
      Value val = Map::operator[](key);
2045 2053
      typename Container::iterator it;
2046 2054
      for (it = _inv_map.equal_range(val).first;
2047 2055
           it != _inv_map.equal_range(val).second; ++it) {
2048 2056
        if (it->second == key) {
2049 2057
          _inv_map.erase(it);
2050 2058
          break;
2051 2059
        }
2052 2060
      }
2053 2061
      Map::erase(key);
2054 2062
    }
2055 2063

	
2056 2064
    /// \brief Erase more keys from the map and the inverse map.
2057 2065
    ///
2058 2066
    /// Erase more keys from the map and the inverse map. It is called by the
2059 2067
    /// \c AlterationNotifier.
2060 2068
    virtual void erase(const std::vector<Key>& keys) {
2061 2069
      for (int i = 0; i < int(keys.size()); ++i) {
2062 2070
        Value val = Map::operator[](keys[i]);
2063 2071
        typename Container::iterator it;
2064 2072
        for (it = _inv_map.equal_range(val).first;
2065 2073
             it != _inv_map.equal_range(val).second; ++it) {
2066 2074
          if (it->second == keys[i]) {
2067 2075
            _inv_map.erase(it);
2068 2076
            break;
2069 2077
          }
2070 2078
        }
2071 2079
      }
2072 2080
      Map::erase(keys);
2073 2081
    }
2074 2082

	
2075 2083
    /// \brief Clear the keys from the map and the inverse map.
2076 2084
    ///
2077 2085
    /// Clear the keys from the map and the inverse map. It is called by the
2078 2086
    /// \c AlterationNotifier.
2079 2087
    virtual void clear() {
2080 2088
      _inv_map.clear();
2081 2089
      Map::clear();
2082 2090
    }
2083 2091

	
2084 2092
  public:
2085 2093

	
2086 2094
    /// \brief The inverse map type.
2087 2095
    ///
2088 2096
    /// The inverse of this map. The subscript operator of the map
2089 2097
    /// gives back the item that was last assigned to the value.
2090 2098
    class InverseMap {
2091 2099
    public:
2092 2100
      /// \brief Constructor
2093 2101
      ///
2094 2102
      /// Constructor of the InverseMap.
2095 2103
      explicit InverseMap(const CrossRefMap& inverted)
2096 2104
        : _inverted(inverted) {}
2097 2105

	
2098 2106
      /// The value type of the InverseMap.
2099 2107
      typedef typename CrossRefMap::Key Value;
2100 2108
      /// The key type of the InverseMap.
2101 2109
      typedef typename CrossRefMap::Value Key;
2102 2110

	
2103 2111
      /// \brief Subscript operator.
2104 2112
      ///
2105 2113
      /// Subscript operator. It gives back an item
2106 2114
      /// that is assigned to the given value or \c INVALID
2107 2115
      /// if no such item exists.
2108 2116
      Value operator[](const Key& key) const {
2109 2117
        return _inverted(key);
2110 2118
      }
2111 2119

	
2112 2120
    private:
2113 2121
      const CrossRefMap& _inverted;
2114 2122
    };
2115 2123

	
2116 2124
    /// \brief It gives back the read-only inverse map.
2117 2125
    ///
2118 2126
    /// It gives back the read-only inverse map.
2119 2127
    InverseMap inverse() const {
2120 2128
      return InverseMap(*this);
2121 2129
    }
2122 2130

	
2123 2131
  };
2124 2132

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

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

	
2153 2161
  public:
2154 2162
    /// The graph type of RangeIdMap.
2155 2163
    typedef GR Graph;
2156 2164
    typedef GR Digraph;
2157 2165
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2158 2166
    typedef K Item;
2159 2167
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2160 2168
    typedef K Key;
2161 2169
    /// The value type of RangeIdMap.
2162 2170
    typedef int Value;
2163 2171

	
2164 2172
    /// \brief Constructor.
2165 2173
    ///
2166 2174
    /// Constructor.
2167 2175
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
2168 2176
      Item it;
2169 2177
      const typename Map::Notifier* nf = Map::notifier();
2170 2178
      for (nf->first(it); it != INVALID; nf->next(it)) {
2171 2179
        Map::set(it, _inv_map.size());
2172 2180
        _inv_map.push_back(it);
2173 2181
      }
2174 2182
    }
2175 2183

	
2176 2184
  protected:
2177 2185

	
2178 2186
    /// \brief Adds a new key to the map.
2179 2187
    ///
2180 2188
    /// Add a new key to the map. It is called by the
2181 2189
    /// \c AlterationNotifier.
2182 2190
    virtual void add(const Item& item) {
2183 2191
      Map::add(item);
2184 2192
      Map::set(item, _inv_map.size());
2185 2193
      _inv_map.push_back(item);
2186 2194
    }
2187 2195

	
2188 2196
    /// \brief Add more new keys to the map.
2189 2197
    ///
2190 2198
    /// Add more new keys to the map. It is called by the
2191 2199
    /// \c AlterationNotifier.
2192 2200
    virtual void add(const std::vector<Item>& items) {
2193 2201
      Map::add(items);
2194 2202
      for (int i = 0; i < int(items.size()); ++i) {
2195 2203
        Map::set(items[i], _inv_map.size());
2196 2204
        _inv_map.push_back(items[i]);
2197 2205
      }
2198 2206
    }
2199 2207

	
2200 2208
    /// \brief Erase the key from the map.
2201 2209
    ///
2202 2210
    /// Erase the key from the map. It is called by the
2203 2211
    /// \c AlterationNotifier.
2204 2212
    virtual void erase(const Item& item) {
2205 2213
      Map::set(_inv_map.back(), Map::operator[](item));
2206 2214
      _inv_map[Map::operator[](item)] = _inv_map.back();
2207 2215
      _inv_map.pop_back();
2208 2216
      Map::erase(item);
2209 2217
    }
2210 2218

	
2211 2219
    /// \brief Erase more keys from the map.
2212 2220
    ///
2213 2221
    /// Erase more keys from the map. It is called by the
2214 2222
    /// \c AlterationNotifier.
2215 2223
    virtual void erase(const std::vector<Item>& items) {
2216 2224
      for (int i = 0; i < int(items.size()); ++i) {
2217 2225
        Map::set(_inv_map.back(), Map::operator[](items[i]));
2218 2226
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
2219 2227
        _inv_map.pop_back();
2220 2228
      }
2221 2229
      Map::erase(items);
2222 2230
    }
2223 2231

	
2224 2232
    /// \brief Build the unique map.
2225 2233
    ///
2226 2234
    /// Build the unique map. It is called by the
2227 2235
    /// \c AlterationNotifier.
2228 2236
    virtual void build() {
2229 2237
      Map::build();
2230 2238
      Item it;
2231 2239
      const typename Map::Notifier* nf = Map::notifier();
2232 2240
      for (nf->first(it); it != INVALID; nf->next(it)) {
2233 2241
        Map::set(it, _inv_map.size());
2234 2242
        _inv_map.push_back(it);
2235 2243
      }
2236 2244
    }
Show white space 192 line context
... ...
@@ -236,153 +236,263 @@
236 236
    map2.set(5, 10);
237 237
    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
238 238
          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
239 239
          "Something is wrong with ForkMap");
240 240
  }
241 241

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

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

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

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

	
290 290
  // Logical maps:
291 291
  // - TrueMap, FalseMap
292 292
  // - AndMap, OrMap
293 293
  // - NotMap, NotWriteMap
294 294
  // - EqualMap, LessMap
295 295
  {
296 296
    checkConcept<BoolMap, TrueMap<A> >();
297 297
    checkConcept<BoolMap, FalseMap<A> >();
298 298
    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
299 299
    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
300 300
    checkConcept<BoolMap, NotMap<BoolMap> >();
301 301
    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
302 302
    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
303 303
    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
304 304

	
305 305
    TrueMap<int> tm;
306 306
    FalseMap<int> fm;
307 307
    RangeMap<bool> rm(2);
308 308
    rm[0] = true; rm[1] = false;
309 309
    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
310 310
          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
311 311
          "Something is wrong with AndMap");
312 312
    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
313 313
          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
314 314
          "Something is wrong with OrMap");
315 315
    check(!notMap(rm)[0] && notMap(rm)[1],
316 316
          "Something is wrong with NotMap");
317 317
    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
318 318
          "Something is wrong with NotWriteMap");
319 319

	
320 320
    ConstMap<int, double> cm(2.0);
321 321
    IdentityMap<int> im;
322 322
    ConvertMap<IdentityMap<int>, double> id(im);
323 323
    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
324 324
          "Something is wrong with LessMap");
325 325
    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
326 326
          "Something is wrong with EqualMap");
327 327
  }
328 328

	
329 329
  // LoggerBoolMap
330 330
  {
331 331
    typedef std::vector<int> vec;
332
    checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
333
    checkConcept<WriteMap<int, bool>,
334
                 LoggerBoolMap<std::back_insert_iterator<vec> > >();
335

	
332 336
    vec v1;
333 337
    vec v2(10);
334 338
    LoggerBoolMap<std::back_insert_iterator<vec> >
335 339
      map1(std::back_inserter(v1));
336 340
    LoggerBoolMap<vec::iterator> map2(v2.begin());
337 341
    map1.set(10, false);
338 342
    map1.set(20, true);   map2.set(20, true);
339 343
    map1.set(30, false);  map2.set(40, false);
340 344
    map1.set(50, true);   map2.set(50, true);
341 345
    map1.set(60, true);   map2.set(60, true);
342 346
    check(v1.size() == 3 && v2.size() == 10 &&
343 347
          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
344 348
          v2[0]==20 && v2[1]==50 && v2[2]==60,
345 349
          "Something is wrong with LoggerBoolMap");
346 350

	
347 351
    int i = 0;
348 352
    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
349 353
          it != map2.end(); ++it )
350 354
      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
351 355
  }
352 356
  
357
  // IdMap, RangeIdMap
358
  {
359
    typedef ListDigraph Graph;
360
    DIGRAPH_TYPEDEFS(Graph);
361

	
362
    checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
363
    checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
364
    checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
365
    checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
366
    
367
    Graph gr;
368
    IdMap<Graph, Node> nmap(gr);
369
    IdMap<Graph, Arc> amap(gr);
370
    RangeIdMap<Graph, Node> nrmap(gr);
371
    RangeIdMap<Graph, Arc> armap(gr);
372
    
373
    Node n0 = gr.addNode();
374
    Node n1 = gr.addNode();
375
    Node n2 = gr.addNode();
376
    
377
    Arc a0 = gr.addArc(n0, n1);
378
    Arc a1 = gr.addArc(n0, n2);
379
    Arc a2 = gr.addArc(n2, n1);
380
    Arc a3 = gr.addArc(n2, n0);
381
    
382
    check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
383
    check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
384
    check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
385

	
386
    check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
387
    check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
388
    check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
389
    check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
390

	
391
    check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
392
    check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
393
    
394
    check(nrmap.size() == 3 && armap.size() == 4,
395
          "Wrong RangeIdMap::size()");
396

	
397
    check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
398
    check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
399
    check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
400
    
401
    check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
402
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
403
    check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
404
    check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
405

	
406
    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
407
    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
408
    
409
    gr.erase(n1);
410
    
411
    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
412
    nrmap.swap(n2, n0);
413
    if (armap[a1] == 1) armap.swap(a1, a3);
414
    armap.swap(a3, a1);
415
    
416
    check(nrmap.size() == 2 && armap.size() == 2,
417
          "Wrong RangeIdMap::size()");
418

	
419
    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
420
    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
421
    
422
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
423
    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
424

	
425
    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
426
    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
427
  }
428
  
353 429
  // CrossRefMap
354 430
  {
355 431
    typedef ListDigraph Graph;
356 432
    DIGRAPH_TYPEDEFS(Graph);
357 433

	
358 434
    checkConcept<ReadWriteMap<Node, int>,
359 435
                 CrossRefMap<Graph, Node, int> >();
436
    checkConcept<ReadWriteMap<Node, bool>,
437
                 CrossRefMap<Graph, Node, bool> >();
438
    checkConcept<ReadWriteMap<Node, double>,
439
                 CrossRefMap<Graph, Node, double> >();
360 440
    
361 441
    Graph gr;
362 442
    typedef CrossRefMap<Graph, Node, char> CRMap;
363 443
    typedef CRMap::ValueIterator ValueIt;
364 444
    CRMap map(gr);
365 445
    
366 446
    Node n0 = gr.addNode();
367 447
    Node n1 = gr.addNode();
368 448
    Node n2 = gr.addNode();
369 449
    
370 450
    map.set(n0, 'A');
371 451
    map.set(n1, 'B');
372 452
    map.set(n2, 'C');
453
    
454
    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
455
          "Wrong CrossRefMap");
456
    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
457
          "Wrong CrossRefMap");
458
    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
459
          "Wrong CrossRefMap");
460
    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
461
          "Wrong CrossRefMap::count()");
462
    
463
    ValueIt it = map.beginValue();
464
    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
465
          it == map.endValue(), "Wrong value iterator");
466
    
373 467
    map.set(n2, 'A');
468

	
469
    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
470
          "Wrong CrossRefMap");
471
    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
472
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
473
    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
474
          "Wrong CrossRefMap");
475
    check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
476
          "Wrong CrossRefMap::count()");
477

	
478
    it = map.beginValue();
479
    check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
480
          it == map.endValue(), "Wrong value iterator");
481

	
374 482
    map.set(n0, 'C');
375 483

	
376 484
    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
377 485
          "Wrong CrossRefMap");
378 486
    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
379 487
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
380 488
    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
489
    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
490
          "Wrong CrossRefMap::count()");
381 491

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

	
387 497
  return 0;
388 498
}
0 comments (0 inline)