Files
spark/src/lib/core/HashPhaser.h
T

221 lines
7.1 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
namespace bts::core {
/**
* 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 <class T, class... NextT>
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 <class Iter>
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 <class Iter, class Hash>
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 <class T, class HashOp, class... NextT>
static std::size_t custom_phase(T value, HashOp hash_op, NextT... next);
private:
template <class T, class... NextT>
static void _auto_phase(
std::size_t &hash, std::uint16_t &depth, T value, NextT... next);
template <class T>
static void _auto_phase(std::size_t &hash, std::uint16_t &depth, T value);
template <class T, class HashOp, class... NextT>
static void _custom_phase(
std::size_t &hash, std::uint16_t &depth, T value, HashOp, NextT... next);
template <class T, class HashOp>
static void _custom_phase(
std::size_t &hash, std::uint16_t &depth, T value, HashOp);
/**
* Get the result of std::hash<T>()(value) without having to declare the
* template parameter T explicitly (helpful when using 'auto' variables).
*/
template <class T>
static std::size_t _default_hash(T value);
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
template <class T, class HashOp, class... NextT>
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 <class T, class... NextT>
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 <class T>
inline std::size_t HashPhaser::_default_hash(T value) {
return std::hash<T>()(value);
}
template <class Iter>
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<std::uint16_t>::max()
? max_els
: std::max(static_cast<std::uint16_t>(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 <class Iter, class Hash>
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<std::uint16_t>::max()
? max_els
: std::max(static_cast<std::uint16_t>(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 <class T, class... NextT>
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<T>()(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 <class T>
void HashPhaser::_auto_phase(
std::size_t &composite_hash, std::uint16_t &depth, T value) {
depth++;
std::size_t val_hash = std::hash<T>()(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);
}
template <class T, class HashOp, class... NextT>
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 <class T, class HashOp>
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