container_property_map.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // (C) Copyright 2007-2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_CONTAINER_PROPERTY_MAP_HPP
  7. #define BOOST_GRAPH_CONTAINER_PROPERTY_MAP_HPP
  8. #include <boost/graph/detail/index.hpp>
  9. #include <boost/property_map/property_map.hpp>
  10. namespace boost
  11. {
  12. // This is an adapter built over the iterator property map with
  13. // more useful uniform construction semantics. Specifically, this
  14. // requires the container rather than the iterator and the graph
  15. // rather than the optional index map.
  16. template <typename Graph, typename Key, typename Container>
  17. struct container_property_map
  18. : public boost::put_get_helper<
  19. typename iterator_property_map<
  20. typename Container::iterator,
  21. typename property_map<
  22. Graph,
  23. typename detail::choose_indexer<Graph, Key>::index_type
  24. >::type
  25. >::reference,
  26. container_property_map<Graph, Key, Container>
  27. >
  28. {
  29. typedef typename detail::choose_indexer<Graph, Key>::indexer_type indexer_type;
  30. typedef typename indexer_type::index_type index_type;
  31. typedef iterator_property_map<
  32. typename Container::iterator,
  33. typename property_map<Graph, index_type>::type
  34. > map_type;
  35. typedef typename map_type::key_type key_type;
  36. typedef typename map_type::value_type value_type;
  37. typedef typename map_type::reference reference;
  38. typedef typename map_type::category category;
  39. // The default constructor will *probably* crash if its actually
  40. // used for referencing vertices since the underlying iterator
  41. // map points past the end of an unknown container.
  42. inline container_property_map()
  43. : m_map()
  44. { }
  45. // This is the preferred constructor. It is invoked over the container
  46. // and the graph explicitly. This requires that the underlying iterator
  47. // map use the indices of the vertices in g rather than the default
  48. // identity map.
  49. //
  50. // Note the const-cast this ensures the reference type of the
  51. // vertex index map is non-const, which happens to be an
  52. // artifact of passing const graph references.
  53. inline container_property_map(Container& c, const Graph& g)
  54. : m_map(c.begin(), indexer_type::index_map(const_cast<Graph&>(g)))
  55. { }
  56. // Typical copy constructor.
  57. inline container_property_map(const container_property_map& x)
  58. : m_map(x.m_map)
  59. { }
  60. // The [] operator delegates to the underlying map/
  61. inline reference operator [](const key_type& k) const
  62. { return m_map[k]; }
  63. map_type m_map;
  64. };
  65. }
  66. #endif