container_property_map.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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< typename Container::iterator,
  20. typename property_map< Graph,
  21. typename detail::choose_indexer< Graph,
  22. Key >::index_type >::type >::reference,
  23. container_property_map< Graph, Key, Container > >
  24. {
  25. typedef typename detail::choose_indexer< Graph, Key >::indexer_type
  26. indexer_type;
  27. typedef typename indexer_type::index_type index_type;
  28. typedef iterator_property_map< typename Container::iterator,
  29. typename property_map< Graph, index_type >::type >
  30. map_type;
  31. typedef typename map_type::key_type key_type;
  32. typedef typename map_type::value_type value_type;
  33. typedef typename map_type::reference reference;
  34. typedef typename map_type::category category;
  35. // The default constructor will *probably* crash if its actually
  36. // used for referencing vertices since the underlying iterator
  37. // map points past the end of an unknown container.
  38. inline container_property_map() : m_map() {}
  39. // This is the preferred constructor. It is invoked over the container
  40. // and the graph explicitly. This requires that the underlying iterator
  41. // map use the indices of the vertices in g rather than the default
  42. // identity map.
  43. //
  44. // Note the const-cast this ensures the reference type of the
  45. // vertex index map is non-const, which happens to be an
  46. // artifact of passing const graph references.
  47. inline container_property_map(Container& c, const Graph& g)
  48. : m_map(c.begin(), indexer_type::index_map(const_cast< Graph& >(g)))
  49. {
  50. }
  51. // Typical copy constructor.
  52. inline container_property_map(const container_property_map& x)
  53. : m_map(x.m_map)
  54. {
  55. }
  56. // The [] operator delegates to the underlying map/
  57. inline reference operator[](const key_type& k) const { return m_map[k]; }
  58. map_type m_map;
  59. };
  60. }
  61. #endif