From 36a274c9a8bca944234589220def025d3920b3ef Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 28 Jul 2026 19:19:32 +0200 Subject: Enforce case label uniqueness and constness --- boot/result.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'boot/result.cc') diff --git a/boot/result.cc b/boot/result.cc index 86b0e7a..0eb5c12 100644 --- a/boot/result.cc +++ b/boot/result.cc @@ -17,6 +17,8 @@ along with GCC; see the file COPYING3. If not see #include "elna/boot/result.h" +#include + namespace elna::boot { location::location(const std::size_t line, const std::size_t column) @@ -137,6 +139,49 @@ namespace elna::boot { return this->m_exported; } + + std::size_t constant_value_hash::operator()(const elna::boot::constant_value& value) const noexcept + { + return std::visit([](auto&& alternative) -> std::size_t { + using T = std::decay_t; + + return std::hash{}(alternative); + }, value); + } + + bool constant_value_hash::operator()(const elna::boot::constant_value& lhs, + const elna::boot::constant_value& rhs) const noexcept + { + return std::visit([](auto&& first, auto&& second) -> bool { + using T = std::decay_t; + using U = std::decay_t; + + if constexpr (std::is_same_v) + { + return first == second; + } + else + { + return false; + } + }, lhs, rhs); + } + + hash_accumulator hash_accumulator::operator+(const std::size_t& that) const + { + hash_accumulator result{}; + + result.m_seed ^= that + hash_accumulator::golden_ratio + // NOLINTNEXTLINE(readability-magic-numbers) + + (this->m_seed << 6) + (this->m_seed >> 2); + + return result; + } + + std::size_t hash_accumulator::seed() const + { + return this->m_seed; + } } std::size_t std::hash::operator()( @@ -144,3 +189,33 @@ std::size_t std::hash::operator()( { return std::hash{}(key.name()); } + +std::size_t std::hash::operator()( + const elna::boot::global_address& key) const noexcept +{ + return std::hash{}(key.name); +} + +std::size_t std::hash>::operator()( + const elna::boot::constant_aggregate& key) const noexcept +{ + const elna::boot::constant_value_hash hasher{}; + auto hash = std::accumulate(key->begin(), key->end(), elna::boot::hash_accumulator{}, + [&hasher](const auto& accumulator, const auto& element) { + return accumulator + hasher(element); + }); + + return hash.seed(); +} + +std::size_t std::hash>::operator()( + const elna::boot::constant_aggregate& key) const noexcept +{ + const elna::boot::constant_value_hash hasher{}; + auto hash = std::accumulate(key->begin(), key->end(), elna::boot::hash_accumulator{}, + [&hasher](const auto& accumulator, const auto& element) { + return accumulator + std::hash{}(element.first) + hasher(element.second); + }); + + return hash.seed(); +} -- cgit v1.2.3