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