#pragma once #include #include #include #include namespace bts::core { #ifndef __CHAR_BIT__ #define __CHAR_BIT__ 8 #endif /** * Provides facilities to combine multiple hash codes into a single hash code. * * @remarks Employs techniques, such as modular arithmetic and bitwise shifts, * to minimize and evenly distribute information loss to the [multiple] input * hash codes in the [singular] output hash code. */ class HashPhaser { public: /** * Combines the hash codes of several parameters into a single hash code. * * @param value One of the parameters whose hash to store * @param next The other parameters whose hash to store * * @remarks A well-chosen modulus of each parameter's hash is stored in * equally sized blocks of bits. */ template static std::size_t auto_phase(T value, NextT... next); /** * Combines the hash codes of a limited number of elements into a single hash * code. * * @param first Iterator pointing to the first element whose hash to store * @param last Iterator pointing just after the last element whose hash to * store * * @remarks A well-chosen modulus of each value's hash is stored in equally * sized blocks of bits */ template static std::size_t auto_phase_range(Iter first, Iter last); /** * Combines the hash codes of a limited number of elements into a single hash * code. * * @param first Iterator pointing to the first element whose hash to store * @param last Iterator pointing just after the last element whose hash to * store * @param hash The hash op to use * * @overload */ template static std::size_t auto_phase_range(Iter first, Iter last, Hash hash); /** * Combines the hash codes of several parameters into a single composite hash * code. * * @param value One of the parameters whose hash to store * @param hash_op The hash code operator to use for the 'value' parameter * @param next The other parameters whose hash to store * * @remarks A well-chosen modulus of each parameter's hash is stored in * equally sized blocks of bits. */ template static std::size_t custom_phase(T value, HashOp hash_op, NextT... next); private: template static void _auto_phase( std::size_t &hash, std::uint16_t &depth, T value, NextT... next); template static void _auto_phase(std::size_t &hash, std::uint16_t &depth, T value); template static void _custom_phase( std::size_t &hash, std::uint16_t &depth, T value, HashOp, NextT... next); template static void _custom_phase( std::size_t &hash, std::uint16_t &depth, T value, HashOp); /** * Get the result of std::hash()(value) without having to declare the * template parameter T explicitly (helpful when using 'auto' variables). */ template static std::size_t _default_hash(T value); }; ////////////////////////////////////////////////// // Definition ////////////////////////////////////////////////// template std::size_t HashPhaser::custom_phase(T value, HashOp hash_op, NextT... next) { std::size_t res = 0; std::uint16_t depth = 0; HashPhaser::_custom_phase(res, depth, value, hash_op, next...); return res; } template std::size_t HashPhaser::auto_phase(T value, NextT... next) { std::size_t res = 0; std::uint16_t depth = 0; HashPhaser::_auto_phase(res, depth, value, next...); return res; } template inline std::size_t HashPhaser::_default_hash(T value) { return std::hash()(value); } template std::size_t HashPhaser::auto_phase_range(Iter first, Iter last) { if (first == last) { return 0; } const std::uint16_t max_els = 8; auto num_vals = std::distance(first, last); std::uint16_t el_count = num_vals > (std::numeric_limits::max)() ? max_els : std::max(static_cast(num_vals), max_els); auto bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / el_count; std::size_t phase = 1U << (bits_per_hash - 1); std::size_t composite_hash = 0; auto el_rem = el_count; while (first != last) { std::size_t val_hash = _default_hash(*first); composite_hash |= (val_hash % phase) << ((el_count - 1) * bits_per_hash); if (--el_rem == 0) { break; } } return composite_hash; } template std::size_t HashPhaser::auto_phase_range(Iter first, Iter last, Hash hash) { if (first == last) { return 0; } const std::uint16_t max_els = 8; auto num_vals = std::distance(first, last); std::uint16_t el_count = num_vals > (std::numeric_limits::max)() ? max_els : std::max(static_cast(num_vals), max_els); auto bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / el_count; std::size_t phase = 1U << (bits_per_hash - 1); std::size_t composite_hash = 0; auto el_rem = el_count; while (first != last) { std::size_t val_hash = hash(*first); composite_hash |= (val_hash % phase) << ((el_count - 1) * bits_per_hash); if (--el_rem == 0) { break; } } return composite_hash; } template void HashPhaser::_auto_phase( std::size_t &composite_hash, std::uint16_t &depth, T value, NextT... next) { auto m_depth = depth++; _auto_phase(composite_hash, depth, next...); auto val_hash = std::hash()(value); std::size_t bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / depth; std::size_t phase = std::size_t(1) << static_cast(bits_per_hash - 1); composite_hash |= (val_hash % phase) << (m_depth * bits_per_hash); } template void HashPhaser::_auto_phase( std::size_t &composite_hash, std::uint16_t &depth, T value) { depth++; std::size_t val_hash = std::hash()(value); std::size_t bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / depth; std::size_t phase = std::size_t(1) << static_cast(bits_per_hash - 1); composite_hash |= (val_hash % phase) << ((depth - 1) * bits_per_hash); } template void HashPhaser::_custom_phase( std::size_t &composite_hash, std::uint16_t &depth, T value, HashOp hash_op, NextT... next) { auto m_depth = depth++; _custom_phase(composite_hash, depth, next...); auto val_hash = hash_op(value); auto bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / depth; std::size_t phase = 1U << (bits_per_hash - 1); composite_hash |= (val_hash % phase) << (m_depth * bits_per_hash); } template void HashPhaser::_custom_phase( std::size_t &composite_hash, std::uint16_t &depth, T value, HashOp hash_op) { depth++; std::size_t val_hash = hash_op(value); auto bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / depth; std::size_t phase = 1U << (bits_per_hash - 1); composite_hash |= (val_hash % phase) << ((depth - 1) * bits_per_hash); } } // namespace bts::core