124 lines
3.3 KiB
C++
124 lines
3.3 KiB
C++
/**
|
|
* @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<size_t>()(bucketCount);
|
|
|
|
// static_cast for c++03 warning fix.
|
|
if (highestBitPosition < static_cast<size_t>(std::numeric_limits<size_t>::digits))
|
|
return size_t(1) << highestBitPosition;
|
|
else
|
|
return size_t(1) << (highestBitPosition - 1);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Key, typename T>
|
|
class UnorderedMapNode :
|
|
public IntrusiveSetHelper::UnorderedSetBaseHookStoreHash,
|
|
public std::pair<const Key, T>
|
|
{
|
|
public:
|
|
//! Default constructor, used by operator[].
|
|
UnorderedMapNode()
|
|
{
|
|
}
|
|
|
|
//! Construct from key and single argument for T, used by emplace.
|
|
template <typename A0>
|
|
UnorderedMapNode(const Key &key, const A0 &a0) :
|
|
std::pair<const Key, T>(key, a0)
|
|
{
|
|
}
|
|
|
|
//! Construct from a pair, used by insert.
|
|
UnorderedMapNode(const std::pair<const Key, T> &value) :
|
|
std::pair<const Key, T>(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<Key, T> *pNode)
|
|
{
|
|
// Call the destructor and return to the pool.
|
|
pNode->~UnorderedMapNode<Key, T>();
|
|
|
|
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<Key, T> &node) const
|
|
{
|
|
return node.first;
|
|
}
|
|
};
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Key, typename T>
|
|
struct UnorderedMapDefaultHash
|
|
{
|
|
typedef typename IntrusiveSetHelper::Detail::HashSelector<
|
|
typename Detail::UnorderedMapNode<Key, T>, Key, typename Detail::UnorderedMapNode<Key, T>::GetKey>::type type;
|
|
};
|
|
|
|
} // namespace Detail
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MIDDLEWARE_MISC_DETAIL_UNORDERED_MAP_DETAIL_H)
|