unordered_map_index.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP
  11. #define BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/detail/workaround.hpp>
  14. #include <functional>
  15. #include <utility>
  16. #include <boost/unordered_map.hpp>
  17. #include <boost/interprocess/detail/utilities.hpp>
  18. #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
  19. //!\file
  20. //!Describes index adaptor of boost::unordered_map container, to use it
  21. //!as name/shared memory index
  22. namespace boost {
  23. namespace interprocess {
  24. ///@cond
  25. //!Helper class to define typedefs from
  26. //!IndexTraits
  27. template <class MapConfig>
  28. struct unordered_map_index_aux
  29. {
  30. typedef typename MapConfig::key_type key_type;
  31. typedef typename MapConfig::mapped_type mapped_type;
  32. typedef std::equal_to<key_type> key_equal;
  33. typedef std::pair<const key_type, mapped_type> value_type;
  34. typedef private_adaptive_pool
  35. <value_type,
  36. typename MapConfig::
  37. segment_manager_base> allocator_type;
  38. struct hasher
  39. : std::unary_function<key_type, std::size_t>
  40. {
  41. std::size_t operator()(const key_type &val) const
  42. {
  43. typedef typename key_type::char_type char_type;
  44. const char_type *beg = ipcdetail::to_raw_pointer(val.mp_str),
  45. *end = beg + val.m_len;
  46. return boost::hash_range(beg, end);
  47. }
  48. };
  49. typedef unordered_map<key_type, mapped_type, hasher,
  50. key_equal, allocator_type> index_t;
  51. };
  52. ///@endcond
  53. //!Index type based in unordered_map. Just derives from unordered_map and
  54. //!defines the interface needed by managed memory segments
  55. template <class MapConfig>
  56. class unordered_map_index
  57. //Derive class from unordered_map specialization
  58. : public unordered_map_index_aux<MapConfig>::index_t
  59. {
  60. /// @cond
  61. typedef unordered_map_index_aux<MapConfig> index_aux;
  62. typedef typename index_aux::index_t base_type;
  63. typedef typename
  64. MapConfig::segment_manager_base segment_manager_base;
  65. /// @endcond
  66. public:
  67. //!Constructor. Takes a pointer to the
  68. //!segment manager. Can throw
  69. unordered_map_index(segment_manager_base *segment_mngr)
  70. : base_type(0,
  71. typename index_aux::hasher(),
  72. typename index_aux::key_equal(),
  73. segment_mngr){}
  74. //!This reserves memory to optimize the insertion of n
  75. //!elements in the index
  76. void reserve(typename segment_manager_base::size_type n)
  77. { base_type::rehash(n); }
  78. //!This tries to free previously allocate
  79. //!unused memory.
  80. void shrink_to_fit()
  81. { base_type::rehash(base_type::size()); }
  82. };
  83. /// @cond
  84. //!Trait class to detect if an index is a node
  85. //!index. This allows more efficient operations
  86. //!when deallocating named objects.
  87. template<class MapConfig>
  88. struct is_node_index
  89. <boost::interprocess::unordered_map_index<MapConfig> >
  90. {
  91. static const bool value = true;
  92. };
  93. /// @endcond
  94. }} //namespace boost { namespace interprocess {
  95. #include <boost/interprocess/detail/config_end.hpp>
  96. #endif //#ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP