local_property_map.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2004-2006 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. // The placement of this #include probably looks very odd relative to
  8. // the #ifndef/#define pair below. However, this placement is
  9. // extremely important to allow the various property map headers to be
  10. // included in any order.
  11. #include <boost/property_map/property_map.hpp>
  12. #ifndef BOOST_PARALLEL_LOCAL_PROPERTY_MAP_HPP
  13. #define BOOST_PARALLEL_LOCAL_PROPERTY_MAP_HPP
  14. #include <boost/assert.hpp>
  15. namespace boost {
  16. /** Property map that accesses an underlying, local property map
  17. * using a subset of the global keys.
  18. */
  19. template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
  20. class local_property_map
  21. {
  22. typedef typename property_traits<GlobalMap>::value_type owner_local_pair;
  23. public:
  24. typedef ProcessGroup process_group_type;
  25. typedef typename property_traits<StorageMap>::value_type value_type;
  26. typedef typename property_traits<GlobalMap>::key_type key_type;
  27. typedef typename property_traits<StorageMap>::reference reference;
  28. typedef typename property_traits<StorageMap>::category category;
  29. local_property_map() { }
  30. local_property_map(const ProcessGroup& process_group,
  31. const GlobalMap& global, const StorageMap& storage)
  32. : process_group_(process_group), global_(global), storage(storage) { }
  33. reference operator[](const key_type& key)
  34. {
  35. owner_local_pair p = get(global_, key);
  36. BOOST_ASSERT(p.first == process_id(process_group_));
  37. return storage[p.second];
  38. }
  39. GlobalMap& global() const { return global_; }
  40. StorageMap& base() const { return storage; }
  41. ProcessGroup& process_group() { return process_group_; }
  42. const ProcessGroup& process_group() const { return process_group_; }
  43. private:
  44. ProcessGroup process_group_;
  45. mutable GlobalMap global_;
  46. mutable StorageMap storage;
  47. };
  48. template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
  49. inline
  50. typename local_property_map<ProcessGroup, GlobalMap, StorageMap>::reference
  51. get(const local_property_map<ProcessGroup, GlobalMap, StorageMap>& pm,
  52. typename local_property_map<ProcessGroup, GlobalMap, StorageMap>::key_type
  53. const & key)
  54. {
  55. typename property_traits<GlobalMap>::value_type p = get(pm.global(), key);
  56. return get(pm.base(), p.second);
  57. }
  58. template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
  59. inline void
  60. put(const local_property_map<ProcessGroup, GlobalMap, StorageMap>& pm,
  61. typename local_property_map<ProcessGroup, GlobalMap, StorageMap>
  62. ::key_type const & key,
  63. typename local_property_map<ProcessGroup, GlobalMap, StorageMap>
  64. ::value_type const& v)
  65. {
  66. typename property_traits<GlobalMap>::value_type p = get(pm.global(), key);
  67. BOOST_ASSERT(p.first == process_id(pm.process_group()));
  68. put(pm.base(), p.second, v);
  69. }
  70. } // end namespace boost
  71. #endif // BOOST_PARALLEL_LOCAL_PROPERTY_MAP_HPP