matrix_property_map.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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
  23. indexer_type;
  24. // aliases for the nested container and its corresponding map
  25. typedef typename Matrix::value_type container_type;
  26. typedef container_property_map< Graph, Key, container_type > map_type;
  27. typedef Key key_type;
  28. // This property map doesn't really provide access to nested containers,
  29. // but returns property maps over them. Since property maps are all
  30. // copy-constructible (or should be anyways), we never return references.
  31. // As such, this property is only readable, but not writable. Curiously,
  32. // the inner property map is actually an lvalue pmap.
  33. typedef map_type value_type;
  34. typedef map_type reference;
  35. typedef readable_property_map_tag category;
  36. matrix_property_map() : m_matrix(0), m_graph(0) {}
  37. matrix_property_map(Matrix& m, const Graph& g)
  38. : m_matrix(&m), m_graph(const_cast< Graph* >(&g))
  39. {
  40. }
  41. matrix_property_map(const matrix_property_map& x)
  42. : m_matrix(x.m_matrix), m_graph(x.m_graph)
  43. {
  44. }
  45. inline reference operator[](key_type k) const
  46. {
  47. typedef typename indexer_type::value_type Index;
  48. Index x = indexer_type::index(k, *m_graph);
  49. return map_type((*m_matrix)[x], *m_graph);
  50. }
  51. private:
  52. mutable Matrix* m_matrix;
  53. mutable Graph* m_graph;
  54. };
  55. }
  56. #endif