Skip to content

Add custom hash function for std::pairs #1694

@jcs15c

Description

@jcs15c

Although the equality operator is supported for generic std::pair<U, V> objects, they cannot be used as keys in either std::unordered_map or axom::FlatMap due to a lack of a built-in hash function. It's easy enough to make one on the fly as needed, but it seems like a common enough use-case that it might be useful to have a standard definition in core that can be reused.

So far, I've gotten around this using the slower std::map implementation in both core/numerics/quadrature.hpp (where 1D quadrature points are keyed by npts and allocatorID) and in primal/operators/detail/winding_number_2d/3d_memoization.hpp (where intermediate values for GWN calculation are keyed by pairs of refinementLevel and refinementIndex). If a standard hash function for pairs is created, it should be used in all 3 cases.

Here's a simple example of what a pair hash function could look like, but I assume there are more sophisticated/robust algorithms out there:

template <typename FirstType, typename SecondType>
struct SimplePairHash
{
  using argument_type = std::pair<FirstType, SecondType>;
  using result_type = axom::IndexType;

  axom::IndexType operator()(std::pair<FirstType, SecondType> p) const
  {
    return static_cast<axom::IndexType>(
      detail::flat_map::HashMixer64<FirstType, DeviceHash> {}(p.first) ^
      (detail::flat_map::HashMixer64<SecondType, DeviceHash> {}(p.second) << 1));
  }
};

Metadata

Metadata

Labels

CoreIssues related to Axom's 'core' componentReviewedenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions