dimval 0.2.0
Modern C++23 header-only library of dimensional values (units, measures, ranges)
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <dimval/measure.hpp>
7#include <dimval/range.hpp>
8#include <dimval/traits.hpp>
9#include <dimval/unit.hpp>
10
11#include <cstddef>
12#include <functional>
13#include <string_view>
14
15namespace dimval::detail {
16
17[[nodiscard]] inline std::size_t hash_combine(std::size_t seed, const std::size_t v) noexcept {
18 // boost::hash_combine constants — well-distributed mixing for 64-bit seeds.
19 seed ^= v + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2);
20 return seed;
21}
22
23} // namespace dimval::detail
24
25template <dimval::UnitLike U, dimval::NumericValue T>
26struct std::hash<dimval::UnitValue<U, T>> {
27 [[nodiscard]] std::size_t operator()(const dimval::UnitValue<U, T>& v) const noexcept {
28 std::size_t s = std::hash<std::string_view>{}(U::id);
29 s = dimval::detail::hash_combine(s, std::hash<T>{}(v.v));
30 return s;
31 }
32};
33
34template <dimval::MeasureLike M, dimval::NumericValue T>
35struct std::hash<dimval::MeasureValue<M, T>> {
36 [[nodiscard]] std::size_t operator()(const dimval::MeasureValue<M, T>& v) const noexcept {
37 std::size_t s = std::hash<std::string_view>{}(M::id);
38 s = dimval::detail::hash_combine(s, std::hash<T>{}(v.v));
39 return s;
40 }
41};
42
43template <dimval::UnitLike U, dimval::NumericValue T>
44struct std::hash<dimval::UnitRangeValue<U, T>> {
45 [[nodiscard]] std::size_t operator()(const dimval::UnitRangeValue<U, T>& r) const noexcept {
46 std::size_t s = std::hash<std::string_view>{}(U::id);
47 s = dimval::detail::hash_combine(s, std::hash<T>{}(r.min().v));
48 s = dimval::detail::hash_combine(s, std::hash<T>{}(r.max().v));
49 s = dimval::detail::hash_combine(s, static_cast<std::size_t>(r.inclusion().lower));
50 s = dimval::detail::hash_combine(s, static_cast<std::size_t>(r.inclusion().upper));
51 return s;
52 }
53};
54
55template <dimval::MeasureLike M, dimval::NumericValue T>
56struct std::hash<dimval::MeasureRangeValue<M, T>> {
57 [[nodiscard]] std::size_t operator()(const dimval::MeasureRangeValue<M, T>& r) const noexcept {
58 std::size_t s = std::hash<std::string_view>{}(M::id);
59 s = dimval::detail::hash_combine(s, std::hash<T>{}(r.min().v));
60 s = dimval::detail::hash_combine(s, std::hash<T>{}(r.max().v));
61 s = dimval::detail::hash_combine(s, static_cast<std::size_t>(r.inclusion().lower));
62 s = dimval::detail::hash_combine(s, static_cast<std::size_t>(r.inclusion().upper));
63 return s;
64 }
65};
Closed/open interval over a MeasureValue<M,T>.
Definition range.hpp:196
Closed/open interval over a UnitValue<U,T>.
Definition range.hpp:45
MeasureValue<M,T>: a UnitValue tagged with an additional semantic measure.
UnitRangeValue and MeasureRangeValue — closed/open intervals of dimensional values.
A value carrying both a measure tag and a unit tag.
Definition measure.hpp:31
A value paired at the type level with a unit tag.
Definition unit.hpp:36
Concepts and helper accessors that drive the static side of dimval.
UnitValue<U,T>: a strongly-typed value carrying a compile-time unit tag.