Files
meta.hpp/headers/meta.hpp/meta_base/hashed_string.hpp
2023-02-02 14:40:02 +07:00

54 lines
1.9 KiB
C++

/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/meta.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2021-2023, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include "base.hpp"
#include "fnv1a_hash.hpp"
namespace meta_hpp::detail
{
class hashed_string final {
public:
hashed_string() = default;
~hashed_string() = default;
hashed_string(hashed_string&&) = default;
hashed_string(const hashed_string&) = default;
hashed_string& operator=(hashed_string&&) = default;
hashed_string& operator=(const hashed_string&) = default;
constexpr hashed_string(std::string_view str) noexcept
: hash_{fnv1a_hash(str.data(), str.size())} {}
constexpr void swap(hashed_string& other) noexcept {
std::swap(hash_, other.hash_);
}
[[nodiscard]] constexpr std::size_t get_hash() const noexcept {
return hash_;
}
private:
std::size_t hash_{fnv1a_hash("", 0)};
};
constexpr void swap(hashed_string& l, hashed_string& r) noexcept { l.swap(r); }
[[nodiscard]] constexpr bool operator<(hashed_string l, hashed_string r) noexcept { return l.get_hash() < r.get_hash(); }
[[nodiscard]] constexpr bool operator==(hashed_string l, hashed_string r) noexcept { return l.get_hash() == r.get_hash(); }
[[nodiscard]] constexpr bool operator!=(hashed_string l, hashed_string r) noexcept { return l.get_hash() != r.get_hash(); }
}
namespace std
{
template <>
struct hash<meta_hpp::detail::hashed_string> {
size_t operator()(meta_hpp::detail::hashed_string hs) const noexcept {
return hs.get_hash();
}
};
}