matrix_property_map.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_MATRIX_PROPERTY_MAP_HPP
  7. #define BOOST_GRAPH_MATRIX_PROPERTY_MAP_HPP
  8. #include <boost/graph/property_maps/container_property_map.hpp>
  9. namespace boost
  10. {
  11. // This property map is built specifically for property maps over
  12. // matrices. Like the basic property map over a container, this builds
  13. // the property abstraction over a matrix (usually a vector of vectors)
  14. // and returns property maps over the nested containers.
  15. template <typename Graph, typename Key, typename Matrix>
  16. struct matrix_property_map
  17. : boost::put_get_helper<
  18. container_property_map<Graph, Key, typename Matrix::value_type>,
  19. matrix_property_map<Graph, Key, Matrix> >
  20. {
  21. // abstract the indexing keys
  22. typedef typename detail::choose_indexer<Graph, Key>::indexer_type indexer_type;
  23. // aliases for the nested container and its corresponding map
  24. typedef typename Matrix::value_type container_type;
  25. typedef container_property_map<Graph, Key, container_type> map_type;
  26. typedef Key key_type;
  27. // This property map doesn't really provide access to nested containers,
  28. // but returns property maps over them. Since property maps are all
  29. // copy-constructible (or should be anyways), we never return references.
  30. // As such, this property is only readable, but not writable. Curiously,
  31. // the inner property map is actually an lvalue pmap.
  32. typedef map_type value_type;
  33. typedef map_type reference;
  34. typedef readable_property_map_tag category;
  35. matrix_property_map()
  36. : m_matrix(0), m_graph(0)
  37. { }
  38. matrix_property_map(Matrix& m, const Graph& g)
  39. : m_matrix(&m), m_graph(const_cast<Graph*>(&g))
  40. { }
  41. matrix_property_map(const matrix_property_map& x)
  42. : m_matrix(x.m_matrix), m_graph(x.m_graph)
  43. { }
  44. inline reference operator [](key_type k) const
  45. {
  46. typedef typename indexer_type::value_type Index;
  47. Index x = indexer_type::index(k, *m_graph);
  48. return map_type((*m_matrix)[x], *m_graph);
  49. }
  50. private:
  51. mutable Matrix* m_matrix;
  52. mutable Graph* m_graph;
  53. };
  54. }
  55. #endif