/** * @file include/ActivMiddleware/Misc/Detail/UnorderedMapDetail.h * * @brief Detail classes for UnorderedMap. */ #if (!defined ACTIV_MIDDLEWARE_MISC_DETAIL_UNORDERED_MAP_DETAIL_H) #define ACTIV_MIDDLEWARE_MISC_DETAIL_UNORDERED_MAP_DETAIL_H #include "ActivMiddleware/ActivMiddleware.h" #include "ActivMiddleware/ActivBase/IntrusiveSetHelper.h" #include ACTIV_BOOST_INCLUDE(pool/pool.hpp) //! Activ namespace. namespace Activ { //! Detail namespace. namespace Detail { //! Pool type used by UnorderedMap. typedef Boost::pool<> UnorderedMapPool; // --------------------------------------------------------------------------------------------------------------------------------- //! Convert provided bucket count to a non-zero power of 2. inline size_t UnorderedMapSanitiseBucketCount(const size_t bucketCount) { if (0 == bucketCount) return 1; if (IsPowerOfTwo(bucketCount)) return bucketCount; // Round up to nearest power of 2. const size_t highestBitPosition = HighestBitPosition()(bucketCount); // static_cast for c++03 warning fix. if (highestBitPosition < static_cast(std::numeric_limits::digits)) return size_t(1) << highestBitPosition; else return size_t(1) << (highestBitPosition - 1); } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template class UnorderedMapNode : public IntrusiveSetHelper::UnorderedSetBaseHookStoreHash, public std::pair { public: //! Default constructor, used by operator[]. UnorderedMapNode() { } //! Construct from key and single argument for T, used by emplace. template UnorderedMapNode(const Key &key, const A0 &a0) : std::pair(key, a0) { } //! Construct from a pair, used by insert. UnorderedMapNode(const std::pair &value) : std::pair(value) { } //! Disposer class passed to IntrusiveSetHelper to return nodes back to the pool. class Disposer { public: //! Constructor. Disposer(UnorderedMapPool &pool) : m_pool(pool) { } //! Returns the node back to the pool. void operator()(UnorderedMapNode *pNode) { // Call the destructor and return to the pool. pNode->~UnorderedMapNode(); m_pool.free(pNode); } UnorderedMapPool & m_pool; }; //! Functor to fetch the key from the node, used by IntrusiveSetHelper. struct GetKey { typedef Key type; const type &operator()(const UnorderedMapNode &node) const { return node.first; } }; }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template struct UnorderedMapDefaultHash { typedef typename IntrusiveSetHelper::Detail::HashSelector< typename Detail::UnorderedMapNode, Key, typename Detail::UnorderedMapNode::GetKey>::type type; }; } // namespace Detail } // namespace Activ #endif // (!defined ACTIV_MIDDLEWARE_MISC_DETAIL_UNORDERED_MAP_DETAIL_H)