gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Add creator functions for IdMap and RangeIdMap (#302)
0 1 0
default
1 file changed with 17 insertions and 0 deletions:
↑ Collapse diff ↑
Show white space 384 line context
... ...
@@ -1710,384 +1710,392 @@
1710 1710
  }
1711 1711

	
1712 1712
  /// @}
1713 1713

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

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

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

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

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

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

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

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

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

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

	
1812 1812
  /// @}
1813 1813

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

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

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

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

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

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

	
1866 1866
  public:
1867 1867

	
1868 1868
    /// \brief The inverse map type of IdMap.
1869 1869
    ///
1870 1870
    /// The inverse map type of IdMap. The subscript operator gives back
1871 1871
    /// an item by its id.
1872 1872
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1873 1873
    /// \see inverse()
1874 1874
    class InverseMap {
1875 1875
    public:
1876 1876

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

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

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

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

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

	
1902
  /// \brief Returns an \c IdMap class.
1903
  ///
1904
  /// This function just returns an \c IdMap class.
1905
  /// \relates IdMap
1906
  template <typename K, typename GR>
1907
  inline IdMap<GR, K> idMap(const GR& graph) {
1908
    return IdMap<GR, K>(graph);
1909
  }
1902 1910

	
1903 1911
  /// \brief General cross reference graph map type.
1904 1912

	
1905 1913
  /// This class provides simple invertable graph maps.
1906 1914
  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1907 1915
  /// and if a key is set to a new value, then stores it in the inverse map.
1908 1916
  /// The graph items can be accessed by their values either using
1909 1917
  /// \c InverseMap or \c operator()(), and the values of the map can be
1910 1918
  /// accessed with an STL compatible forward iterator (\c ValueIt).
1911 1919
  /// 
1912 1920
  /// This map is intended to be used when all associated values are
1913 1921
  /// different (the map is actually invertable) or there are only a few
1914 1922
  /// items with the same value.
1915 1923
  /// Otherwise consider to use \c IterableValueMap, which is more 
1916 1924
  /// suitable and more efficient for such cases. It provides iterators
1917 1925
  /// to traverse the items with the same associated value, however
1918 1926
  /// it does not have \c InverseMap.
1919 1927
  ///
1920 1928
  /// This type is not reference map, so it cannot be modified with
1921 1929
  /// the subscript operator.
1922 1930
  ///
1923 1931
  /// \tparam GR The graph type.
1924 1932
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1925 1933
  /// \c GR::Edge).
1926 1934
  /// \tparam V The value type of the map.
1927 1935
  ///
1928 1936
  /// \see IterableValueMap
1929 1937
  template <typename GR, typename K, typename V>
1930 1938
  class CrossRefMap
1931 1939
    : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1932 1940
  private:
1933 1941

	
1934 1942
    typedef typename ItemSetTraits<GR, K>::
1935 1943
      template Map<V>::Type Map;
1936 1944

	
1937 1945
    typedef std::multimap<V, K> Container;
1938 1946
    Container _inv_map;
1939 1947

	
1940 1948
  public:
1941 1949

	
1942 1950
    /// The graph type of CrossRefMap.
1943 1951
    typedef GR Graph;
1944 1952
    typedef GR Digraph;
1945 1953
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1946 1954
    typedef K Item;
1947 1955
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1948 1956
    typedef K Key;
1949 1957
    /// The value type of CrossRefMap.
1950 1958
    typedef V Value;
1951 1959

	
1952 1960
    /// \brief Constructor.
1953 1961
    ///
1954 1962
    /// Construct a new CrossRefMap for the given graph.
1955 1963
    explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1956 1964

	
1957 1965
    /// \brief Forward iterator for values.
1958 1966
    ///
1959 1967
    /// This iterator is an STL compatible forward
1960 1968
    /// iterator on the values of the map. The values can
1961 1969
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1962 1970
    /// They are considered with multiplicity, so each value is
1963 1971
    /// traversed for each item it is assigned to.
1964 1972
    class ValueIt
1965 1973
      : public std::iterator<std::forward_iterator_tag, Value> {
1966 1974
      friend class CrossRefMap;
1967 1975
    private:
1968 1976
      ValueIt(typename Container::const_iterator _it)
1969 1977
        : it(_it) {}
1970 1978
    public:
1971 1979

	
1972 1980
      /// Constructor
1973 1981
      ValueIt() {}
1974 1982

	
1975 1983
      /// \e
1976 1984
      ValueIt& operator++() { ++it; return *this; }
1977 1985
      /// \e
1978 1986
      ValueIt operator++(int) {
1979 1987
        ValueIt tmp(*this);
1980 1988
        operator++();
1981 1989
        return tmp;
1982 1990
      }
1983 1991

	
1984 1992
      /// \e
1985 1993
      const Value& operator*() const { return it->first; }
1986 1994
      /// \e
1987 1995
      const Value* operator->() const { return &(it->first); }
1988 1996

	
1989 1997
      /// \e
1990 1998
      bool operator==(ValueIt jt) const { return it == jt.it; }
1991 1999
      /// \e
1992 2000
      bool operator!=(ValueIt jt) const { return it != jt.it; }
1993 2001

	
1994 2002
    private:
1995 2003
      typename Container::const_iterator it;
1996 2004
    };
1997 2005
    
1998 2006
    /// Alias for \c ValueIt
1999 2007
    typedef ValueIt ValueIterator;
2000 2008

	
2001 2009
    /// \brief Returns an iterator to the first value.
2002 2010
    ///
2003 2011
    /// Returns an STL compatible iterator to the
2004 2012
    /// first value of the map. The values of the
2005 2013
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
2006 2014
    /// range.
2007 2015
    ValueIt beginValue() const {
2008 2016
      return ValueIt(_inv_map.begin());
2009 2017
    }
2010 2018

	
2011 2019
    /// \brief Returns an iterator after the last value.
2012 2020
    ///
2013 2021
    /// Returns an STL compatible iterator after the
2014 2022
    /// last value of the map. The values of the
2015 2023
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
2016 2024
    /// range.
2017 2025
    ValueIt endValue() const {
2018 2026
      return ValueIt(_inv_map.end());
2019 2027
    }
2020 2028

	
2021 2029
    /// \brief Sets the value associated with the given key.
2022 2030
    ///
2023 2031
    /// Sets the value associated with the given key.
2024 2032
    void set(const Key& key, const Value& val) {
2025 2033
      Value oldval = Map::operator[](key);
2026 2034
      typename Container::iterator it;
2027 2035
      for (it = _inv_map.equal_range(oldval).first;
2028 2036
           it != _inv_map.equal_range(oldval).second; ++it) {
2029 2037
        if (it->second == key) {
2030 2038
          _inv_map.erase(it);
2031 2039
          break;
2032 2040
        }
2033 2041
      }
2034 2042
      _inv_map.insert(std::make_pair(val, key));
2035 2043
      Map::set(key, val);
2036 2044
    }
2037 2045

	
2038 2046
    /// \brief Returns the value associated with the given key.
2039 2047
    ///
2040 2048
    /// Returns the value associated with the given key.
2041 2049
    typename MapTraits<Map>::ConstReturnValue
2042 2050
    operator[](const Key& key) const {
2043 2051
      return Map::operator[](key);
2044 2052
    }
2045 2053

	
2046 2054
    /// \brief Gives back an item by its value.
2047 2055
    ///
2048 2056
    /// This function gives back an item that is assigned to
2049 2057
    /// the given value or \c INVALID if no such item exists.
2050 2058
    /// If there are more items with the same associated value,
2051 2059
    /// only one of them is returned.
2052 2060
    Key operator()(const Value& val) const {
2053 2061
      typename Container::const_iterator it = _inv_map.find(val);
2054 2062
      return it != _inv_map.end() ? it->second : INVALID;
2055 2063
    }
2056 2064
    
2057 2065
    /// \brief Returns the number of items with the given value.
2058 2066
    ///
2059 2067
    /// This function returns the number of items with the given value
2060 2068
    /// associated with it.
2061 2069
    int count(const Value &val) const {
2062 2070
      return _inv_map.count(val);
2063 2071
    }
2064 2072

	
2065 2073
  protected:
2066 2074

	
2067 2075
    /// \brief Erase the key from the map and the inverse map.
2068 2076
    ///
2069 2077
    /// Erase the key from the map and the inverse map. It is called by the
2070 2078
    /// \c AlterationNotifier.
2071 2079
    virtual void erase(const Key& key) {
2072 2080
      Value val = Map::operator[](key);
2073 2081
      typename Container::iterator it;
2074 2082
      for (it = _inv_map.equal_range(val).first;
2075 2083
           it != _inv_map.equal_range(val).second; ++it) {
2076 2084
        if (it->second == key) {
2077 2085
          _inv_map.erase(it);
2078 2086
          break;
2079 2087
        }
2080 2088
      }
2081 2089
      Map::erase(key);
2082 2090
    }
2083 2091

	
2084 2092
    /// \brief Erase more keys from the map and the inverse map.
2085 2093
    ///
2086 2094
    /// Erase more keys from the map and the inverse map. It is called by the
2087 2095
    /// \c AlterationNotifier.
2088 2096
    virtual void erase(const std::vector<Key>& keys) {
2089 2097
      for (int i = 0; i < int(keys.size()); ++i) {
2090 2098
        Value val = Map::operator[](keys[i]);
2091 2099
        typename Container::iterator it;
2092 2100
        for (it = _inv_map.equal_range(val).first;
2093 2101
             it != _inv_map.equal_range(val).second; ++it) {
... ...
@@ -2173,384 +2181,393 @@
2173 2181
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2174 2182
  /// \c GR::Edge).
2175 2183
  ///
2176 2184
  /// \see IdMap
2177 2185
  template <typename GR, typename K>
2178 2186
  class RangeIdMap
2179 2187
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2180 2188

	
2181 2189
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2182 2190

	
2183 2191
  public:
2184 2192
    /// The graph type of RangeIdMap.
2185 2193
    typedef GR Graph;
2186 2194
    typedef GR Digraph;
2187 2195
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2188 2196
    typedef K Item;
2189 2197
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2190 2198
    typedef K Key;
2191 2199
    /// The value type of RangeIdMap.
2192 2200
    typedef int Value;
2193 2201

	
2194 2202
    /// \brief Constructor.
2195 2203
    ///
2196 2204
    /// Constructor.
2197 2205
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
2198 2206
      Item it;
2199 2207
      const typename Map::Notifier* nf = Map::notifier();
2200 2208
      for (nf->first(it); it != INVALID; nf->next(it)) {
2201 2209
        Map::set(it, _inv_map.size());
2202 2210
        _inv_map.push_back(it);
2203 2211
      }
2204 2212
    }
2205 2213

	
2206 2214
  protected:
2207 2215

	
2208 2216
    /// \brief Adds a new key to the map.
2209 2217
    ///
2210 2218
    /// Add a new key to the map. It is called by the
2211 2219
    /// \c AlterationNotifier.
2212 2220
    virtual void add(const Item& item) {
2213 2221
      Map::add(item);
2214 2222
      Map::set(item, _inv_map.size());
2215 2223
      _inv_map.push_back(item);
2216 2224
    }
2217 2225

	
2218 2226
    /// \brief Add more new keys to the map.
2219 2227
    ///
2220 2228
    /// Add more new keys to the map. It is called by the
2221 2229
    /// \c AlterationNotifier.
2222 2230
    virtual void add(const std::vector<Item>& items) {
2223 2231
      Map::add(items);
2224 2232
      for (int i = 0; i < int(items.size()); ++i) {
2225 2233
        Map::set(items[i], _inv_map.size());
2226 2234
        _inv_map.push_back(items[i]);
2227 2235
      }
2228 2236
    }
2229 2237

	
2230 2238
    /// \brief Erase the key from the map.
2231 2239
    ///
2232 2240
    /// Erase the key from the map. It is called by the
2233 2241
    /// \c AlterationNotifier.
2234 2242
    virtual void erase(const Item& item) {
2235 2243
      Map::set(_inv_map.back(), Map::operator[](item));
2236 2244
      _inv_map[Map::operator[](item)] = _inv_map.back();
2237 2245
      _inv_map.pop_back();
2238 2246
      Map::erase(item);
2239 2247
    }
2240 2248

	
2241 2249
    /// \brief Erase more keys from the map.
2242 2250
    ///
2243 2251
    /// Erase more keys from the map. It is called by the
2244 2252
    /// \c AlterationNotifier.
2245 2253
    virtual void erase(const std::vector<Item>& items) {
2246 2254
      for (int i = 0; i < int(items.size()); ++i) {
2247 2255
        Map::set(_inv_map.back(), Map::operator[](items[i]));
2248 2256
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
2249 2257
        _inv_map.pop_back();
2250 2258
      }
2251 2259
      Map::erase(items);
2252 2260
    }
2253 2261

	
2254 2262
    /// \brief Build the unique map.
2255 2263
    ///
2256 2264
    /// Build the unique map. It is called by the
2257 2265
    /// \c AlterationNotifier.
2258 2266
    virtual void build() {
2259 2267
      Map::build();
2260 2268
      Item it;
2261 2269
      const typename Map::Notifier* nf = Map::notifier();
2262 2270
      for (nf->first(it); it != INVALID; nf->next(it)) {
2263 2271
        Map::set(it, _inv_map.size());
2264 2272
        _inv_map.push_back(it);
2265 2273
      }
2266 2274
    }
2267 2275

	
2268 2276
    /// \brief Clear the keys from the map.
2269 2277
    ///
2270 2278
    /// Clear the keys from the map. It is called by the
2271 2279
    /// \c AlterationNotifier.
2272 2280
    virtual void clear() {
2273 2281
      _inv_map.clear();
2274 2282
      Map::clear();
2275 2283
    }
2276 2284

	
2277 2285
  public:
2278 2286

	
2279 2287
    /// \brief Returns the maximal value plus one.
2280 2288
    ///
2281 2289
    /// Returns the maximal value plus one in the map.
2282 2290
    unsigned int size() const {
2283 2291
      return _inv_map.size();
2284 2292
    }
2285 2293

	
2286 2294
    /// \brief Swaps the position of the two items in the map.
2287 2295
    ///
2288 2296
    /// Swaps the position of the two items in the map.
2289 2297
    void swap(const Item& p, const Item& q) {
2290 2298
      int pi = Map::operator[](p);
2291 2299
      int qi = Map::operator[](q);
2292 2300
      Map::set(p, qi);
2293 2301
      _inv_map[qi] = p;
2294 2302
      Map::set(q, pi);
2295 2303
      _inv_map[pi] = q;
2296 2304
    }
2297 2305

	
2298 2306
    /// \brief Gives back the \e range \e id of the item
2299 2307
    ///
2300 2308
    /// Gives back the \e range \e id of the item.
2301 2309
    int operator[](const Item& item) const {
2302 2310
      return Map::operator[](item);
2303 2311
    }
2304 2312

	
2305 2313
    /// \brief Gives back the item belonging to a \e range \e id
2306 2314
    ///
2307 2315
    /// Gives back the item belonging to the given \e range \e id.
2308 2316
    Item operator()(int id) const {
2309 2317
      return _inv_map[id];
2310 2318
    }
2311 2319

	
2312 2320
  private:
2313 2321

	
2314 2322
    typedef std::vector<Item> Container;
2315 2323
    Container _inv_map;
2316 2324

	
2317 2325
  public:
2318 2326

	
2319 2327
    /// \brief The inverse map type of RangeIdMap.
2320 2328
    ///
2321 2329
    /// The inverse map type of RangeIdMap. The subscript operator gives
2322 2330
    /// back an item by its \e range \e id.
2323 2331
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2324 2332
    class InverseMap {
2325 2333
    public:
2326 2334
      /// \brief Constructor
2327 2335
      ///
2328 2336
      /// Constructor of the InverseMap.
2329 2337
      explicit InverseMap(const RangeIdMap& inverted)
2330 2338
        : _inverted(inverted) {}
2331 2339

	
2332 2340

	
2333 2341
      /// The value type of the InverseMap.
2334 2342
      typedef typename RangeIdMap::Key Value;
2335 2343
      /// The key type of the InverseMap.
2336 2344
      typedef typename RangeIdMap::Value Key;
2337 2345

	
2338 2346
      /// \brief Subscript operator.
2339 2347
      ///
2340 2348
      /// Subscript operator. It gives back the item
2341 2349
      /// that the given \e range \e id currently belongs to.
2342 2350
      Value operator[](const Key& key) const {
2343 2351
        return _inverted(key);
2344 2352
      }
2345 2353

	
2346 2354
      /// \brief Size of the map.
2347 2355
      ///
2348 2356
      /// Returns the size of the map.
2349 2357
      unsigned int size() const {
2350 2358
        return _inverted.size();
2351 2359
      }
2352 2360

	
2353 2361
    private:
2354 2362
      const RangeIdMap& _inverted;
2355 2363
    };
2356 2364

	
2357 2365
    /// \brief Gives back the inverse of the map.
2358 2366
    ///
2359 2367
    /// Gives back the inverse of the RangeIdMap.
2360 2368
    const InverseMap inverse() const {
2361 2369
      return InverseMap(*this);
2362 2370
    }
2363 2371
  };
2364 2372

	
2373
  /// \brief Returns a \c RangeIdMap class.
2374
  ///
2375
  /// This function just returns an \c RangeIdMap class.
2376
  /// \relates RangeIdMap
2377
  template <typename K, typename GR>
2378
  inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
2379
    return RangeIdMap<GR, K>(graph);
2380
  }
2381
  
2365 2382
  /// \brief Dynamic iterable \c bool map.
2366 2383
  ///
2367 2384
  /// This class provides a special graph map type which can store a
2368 2385
  /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
2369 2386
  /// For both \c true and \c false values it is possible to iterate on
2370 2387
  /// the keys mapped to the value.
2371 2388
  ///
2372 2389
  /// This type is a reference map, so it can be modified with the
2373 2390
  /// subscript operator.
2374 2391
  ///
2375 2392
  /// \tparam GR The graph type.
2376 2393
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2377 2394
  /// \c GR::Edge).
2378 2395
  ///
2379 2396
  /// \see IterableIntMap, IterableValueMap
2380 2397
  /// \see CrossRefMap
2381 2398
  template <typename GR, typename K>
2382 2399
  class IterableBoolMap
2383 2400
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2384 2401
  private:
2385 2402
    typedef GR Graph;
2386 2403

	
2387 2404
    typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
2388 2405
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
2389 2406

	
2390 2407
    std::vector<K> _array;
2391 2408
    int _sep;
2392 2409

	
2393 2410
  public:
2394 2411

	
2395 2412
    /// Indicates that the map is reference map.
2396 2413
    typedef True ReferenceMapTag;
2397 2414

	
2398 2415
    /// The key type
2399 2416
    typedef K Key;
2400 2417
    /// The value type
2401 2418
    typedef bool Value;
2402 2419
    /// The const reference type.
2403 2420
    typedef const Value& ConstReference;
2404 2421

	
2405 2422
  private:
2406 2423

	
2407 2424
    int position(const Key& key) const {
2408 2425
      return Parent::operator[](key);
2409 2426
    }
2410 2427

	
2411 2428
  public:
2412 2429

	
2413 2430
    /// \brief Reference to the value of the map.
2414 2431
    ///
2415 2432
    /// This class is similar to the \c bool type. It can be converted to
2416 2433
    /// \c bool and it provides the same operators.
2417 2434
    class Reference {
2418 2435
      friend class IterableBoolMap;
2419 2436
    private:
2420 2437
      Reference(IterableBoolMap& map, const Key& key)
2421 2438
        : _key(key), _map(map) {}
2422 2439
    public:
2423 2440

	
2424 2441
      Reference& operator=(const Reference& value) {
2425 2442
        _map.set(_key, static_cast<bool>(value));
2426 2443
         return *this;
2427 2444
      }
2428 2445

	
2429 2446
      operator bool() const {
2430 2447
        return static_cast<const IterableBoolMap&>(_map)[_key];
2431 2448
      }
2432 2449

	
2433 2450
      Reference& operator=(bool value) {
2434 2451
        _map.set(_key, value);
2435 2452
        return *this;
2436 2453
      }
2437 2454
      Reference& operator&=(bool value) {
2438 2455
        _map.set(_key, _map[_key] & value);
2439 2456
        return *this;
2440 2457
      }
2441 2458
      Reference& operator|=(bool value) {
2442 2459
        _map.set(_key, _map[_key] | value);
2443 2460
        return *this;
2444 2461
      }
2445 2462
      Reference& operator^=(bool value) {
2446 2463
        _map.set(_key, _map[_key] ^ value);
2447 2464
        return *this;
2448 2465
      }
2449 2466
    private:
2450 2467
      Key _key;
2451 2468
      IterableBoolMap& _map;
2452 2469
    };
2453 2470

	
2454 2471
    /// \brief Constructor of the map with a default value.
2455 2472
    ///
2456 2473
    /// Constructor of the map with a default value.
2457 2474
    explicit IterableBoolMap(const Graph& graph, bool def = false)
2458 2475
      : Parent(graph) {
2459 2476
      typename Parent::Notifier* nf = Parent::notifier();
2460 2477
      Key it;
2461 2478
      for (nf->first(it); it != INVALID; nf->next(it)) {
2462 2479
        Parent::set(it, _array.size());
2463 2480
        _array.push_back(it);
2464 2481
      }
2465 2482
      _sep = (def ? _array.size() : 0);
2466 2483
    }
2467 2484

	
2468 2485
    /// \brief Const subscript operator of the map.
2469 2486
    ///
2470 2487
    /// Const subscript operator of the map.
2471 2488
    bool operator[](const Key& key) const {
2472 2489
      return position(key) < _sep;
2473 2490
    }
2474 2491

	
2475 2492
    /// \brief Subscript operator of the map.
2476 2493
    ///
2477 2494
    /// Subscript operator of the map.
2478 2495
    Reference operator[](const Key& key) {
2479 2496
      return Reference(*this, key);
2480 2497
    }
2481 2498

	
2482 2499
    /// \brief Set operation of the map.
2483 2500
    ///
2484 2501
    /// Set operation of the map.
2485 2502
    void set(const Key& key, bool value) {
2486 2503
      int pos = position(key);
2487 2504
      if (value) {
2488 2505
        if (pos < _sep) return;
2489 2506
        Key tmp = _array[_sep];
2490 2507
        _array[_sep] = key;
2491 2508
        Parent::set(key, _sep);
2492 2509
        _array[pos] = tmp;
2493 2510
        Parent::set(tmp, pos);
2494 2511
        ++_sep;
2495 2512
      } else {
2496 2513
        if (pos >= _sep) return;
2497 2514
        --_sep;
2498 2515
        Key tmp = _array[_sep];
2499 2516
        _array[_sep] = key;
2500 2517
        Parent::set(key, _sep);
2501 2518
        _array[pos] = tmp;
2502 2519
        Parent::set(tmp, pos);
2503 2520
      }
2504 2521
    }
2505 2522

	
2506 2523
    /// \brief Set all items.
2507 2524
    ///
2508 2525
    /// Set all items in the map.
2509 2526
    /// \note Constant time operation.
2510 2527
    void setAll(bool value) {
2511 2528
      _sep = (value ? _array.size() : 0);
2512 2529
    }
2513 2530

	
2514 2531
    /// \brief Returns the number of the keys mapped to \c true.
2515 2532
    ///
2516 2533
    /// Returns the number of the keys mapped to \c true.
2517 2534
    int trueNum() const {
2518 2535
      return _sep;
2519 2536
    }
2520 2537

	
2521 2538
    /// \brief Returns the number of the keys mapped to \c false.
2522 2539
    ///
2523 2540
    /// Returns the number of the keys mapped to \c false.
2524 2541
    int falseNum() const {
2525 2542
      return _array.size() - _sep;
2526 2543
    }
2527 2544

	
2528 2545
    /// \brief Iterator for the keys mapped to \c true.
2529 2546
    ///
2530 2547
    /// Iterator for the keys mapped to \c true. It works
2531 2548
    /// like a graph item iterator, it can be converted to
2532 2549
    /// the key type of the map, incremented with \c ++ operator, and
2533 2550
    /// if the iterator leaves the last valid key, it will be equal to
2534 2551
    /// \c INVALID.
2535 2552
    class TrueIt : public Key {
2536 2553
    public:
2537 2554
      typedef Key Parent;
2538 2555

	
2539 2556
      /// \brief Creates an iterator.
2540 2557
      ///
2541 2558
      /// Creates an iterator. It iterates on the
2542 2559
      /// keys mapped to \c true.
2543 2560
      /// \param map The IterableBoolMap.
2544 2561
      explicit TrueIt(const IterableBoolMap& map)
2545 2562
        : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
2546 2563
          _map(&map) {}
2547 2564

	
2548 2565
      /// \brief Invalid constructor \& conversion.
2549 2566
      ///
2550 2567
      /// This constructor initializes the iterator to be invalid.
2551 2568
      /// \sa Invalid for more details.
2552 2569
      TrueIt(Invalid) : Parent(INVALID), _map(0) {}
2553 2570

	
2554 2571
      /// \brief Increment operator.
2555 2572
      ///
2556 2573
      /// Increment operator.
0 comments (0 inline)