template<typename... T>
struct carta::Key< T >
Class for grouping helper functions for shared key functionality.
Child struct constructors must pass their specific field values into the parent constructor to define the tuple representation of the struct that will be used to check for equality and to construct a combined hash. The tuple elements don't have to be ordered in the same way as the struct fields, and don't have to include all of the child properties (if some are not relevant). The tuple elements should be ordered by the size of their likely range, from smallest to largest: this reduces the likelihood of hash collisions. For example, a polarization index should be sorted before a Z index, but X and Y coordinates can be passed in either order.
template<typename... T>
| std::size_t carta::Key< T >::CombinedHash |
( |
| ) |
const |
|
inline |
Generic function for creating a hash for a key object.
This helper function implements hashes for custom key objects using their tuple representations. At time of writing there is no clean way for an object to inherit a std::hash specialization, so specializations must be defined individually for children, and should call this function. This algorithm multiplies component hashes by increasing powers of a prime number and sums them. For example, for a tuple with values a1, a2, a3 that hash to h1, h2, h3, it returns h1 + 31 * h2 + pow(31,
2) * h3. This produces a better hash distribution than XORing bit-shifted component hashes, even for quite large integer values of components (relevant for X / Y / Z coordinates). 31 was selected as the prime multiplier because it can be implemented efficiently as a shift and a subtraction.