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 
7 #ifndef BOOST_GRAPH_CONTAINER_PROPERTY_MAP_HPP
8 #define BOOST_GRAPH_CONTAINER_PROPERTY_MAP_HPP
9 
10 #include <boost/graph/detail/index.hpp>
11 #include <boost/property_map/property_map.hpp>
12 
13 namespace boost
14 {
15 // This is an adapter built over the iterator property map with
16 // more useful uniform construction semantics. Specifically, this
17 // requires the container rather than the iterator and the graph
18 // rather than the optional index map.
19 template < typename Graph, typename Key, typename Container >
20 struct container_property_map
21 : public boost::put_get_helper<
22       typename iterator_property_map< typename Container::iterator,
23           typename property_map< Graph,
24               typename detail::choose_indexer< Graph,
25                   Key >::index_type >::type >::reference,
26       container_property_map< Graph, Key, Container > >
27 {
28     typedef typename detail::choose_indexer< Graph, Key >::indexer_type
29         indexer_type;
30     typedef typename indexer_type::index_type index_type;
31     typedef iterator_property_map< typename Container::iterator,
32         typename property_map< Graph, index_type >::type >
33         map_type;
34     typedef typename map_type::key_type key_type;
35     typedef typename map_type::value_type value_type;
36     typedef typename map_type::reference reference;
37     typedef typename map_type::category category;
38 
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.
container_property_mapboost::container_property_map42     inline container_property_map() : m_map() {}
43 
44     // This is the preferred constructor. It is invoked over the container
45     // and the graph explicitly. This requires that the underlying iterator
46     // map use the indices of the vertices in g rather than the default
47     // identity map.
48     //
49     // Note the const-cast this ensures the reference type of the
50     // vertex index map is non-const, which happens to be an
51     // artifact of passing const graph references.
container_property_mapboost::container_property_map52     inline container_property_map(Container& c, const Graph& g)
53     : m_map(c.begin(), indexer_type::index_map(const_cast< Graph& >(g)))
54     {
55     }
56 
57     // Typical copy constructor.
container_property_mapboost::container_property_map58     inline container_property_map(const container_property_map& x)
59     : m_map(x.m_map)
60     {
61     }
62 
63     // The [] operator delegates to the underlying map/
operator []boost::container_property_map64     inline reference operator[](const key_type& k) const { return m_map[k]; }
65 
66     map_type m_map;
67 };
68 }
69 
70 #endif
71