From a89f27e0520985eae75946a523c939a72b7ecf68 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 11 Feb 2024 08:39:22 +0700 Subject: [PATCH] remove hashed_string class --- develop/singles/headers/meta.hpp/meta_all.hpp | 61 +---------- .../untests/meta_base/hashed_string_tests.cpp | 100 ------------------ headers/meta.hpp/meta_base.hpp | 1 - headers/meta.hpp/meta_base/hash_composer.hpp | 3 +- headers/meta.hpp/meta_base/hashed_string.hpp | 69 ------------ 5 files changed, 2 insertions(+), 232 deletions(-) delete mode 100644 develop/untests/meta_base/hashed_string_tests.cpp delete mode 100644 headers/meta.hpp/meta_base/hashed_string.hpp diff --git a/develop/singles/headers/meta.hpp/meta_all.hpp b/develop/singles/headers/meta.hpp/meta_all.hpp index c098359..3ff8e64 100644 --- a/develop/singles/headers/meta.hpp/meta_all.hpp +++ b/develop/singles/headers/meta.hpp/meta_all.hpp @@ -792,65 +792,6 @@ namespace meta_hpp::detail } } -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_; - } - - [[nodiscard]] constexpr bool operator==(hashed_string other) const noexcept { - return hash_ == other.hash_; - } - - [[nodiscard]] constexpr std::strong_ordering operator<=>(hashed_string other) const noexcept { - return hash_ <=> other.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, std::string_view r) noexcept { - return l == hashed_string{r}; - } - - [[nodiscard]] constexpr std::strong_ordering operator<=>(hashed_string l, std::string_view r) noexcept { - return l <=> hashed_string{r}; - } -} - -namespace std -{ - template <> - struct hash { - size_t operator()(meta_hpp::detail::hashed_string hs) const noexcept { - return hs.get_hash(); - } - }; -} - namespace meta_hpp::detail { template < std::size_t SizeBytes = sizeof(std::size_t) > @@ -880,7 +821,7 @@ namespace meta_hpp::detail } constexpr hash_composer operator<<(std::string_view value) noexcept { - hash = combine(hash, hashed_string{value}.get_hash()); + hash = combine(hash, fnv1a_hash(value.data(), value.size())); return *this; } diff --git a/develop/untests/meta_base/hashed_string_tests.cpp b/develop/untests/meta_base/hashed_string_tests.cpp deleted file mode 100644 index d468f15..0000000 --- a/develop/untests/meta_base/hashed_string_tests.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/******************************************************************************* - * 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-2024, by Matvey Cherevko (blackmatov@gmail.com) - ******************************************************************************/ - -#include -#include - -TEST_CASE("meta/meta_base/hashed_string") { - namespace meta = meta_hpp; - using meta::detail::hashed_string; - - SUBCASE("ctor/0") { - constexpr hashed_string hs{}; - static_assert(hs == hashed_string{""}); - static_assert(hs.get_hash() == hashed_string{""}.get_hash()); - - CHECK(hs == hashed_string{std::string{}}); - CHECK(hs == hashed_string{std::string_view{}}); - } - - SUBCASE("ctor/1") { - constexpr hashed_string hs{"hello"}; - static_assert(hs.get_hash() == meta::detail::fnv1a_hash("hello", 5)); - - CHECK(hs == hashed_string{std::string{"hello"}}); - CHECK(hs == hashed_string{std::string_view{"hello"}}); - } - - SUBCASE("copy_ctor") { - constexpr hashed_string hs{"hello"}; - constexpr hashed_string hs2{hs}; - static_assert(hs == hs2); - static_assert(hs.get_hash() == hs2.get_hash()); - } - - SUBCASE("move_ctor") { - constexpr hashed_string hs{"hello"}; - constexpr hashed_string hs2{std::move(hs)}; - static_assert(hs == hs2); - static_assert(hs.get_hash() == hs2.get_hash()); - } - - SUBCASE("operator=/copy") { - constexpr hashed_string hs{"hello"}; - constexpr hashed_string hs2 = [&hs](){ - hashed_string r; - r = hs; - return r; - }(); - static_assert(hs == hs2); - static_assert(hs.get_hash() == hs2.get_hash()); - } - - SUBCASE("operator=/move") { - constexpr hashed_string hs{"hello"}; - constexpr hashed_string hs2 = [&hs](){ - hashed_string r; - r = std::move(hs); - return r; - }(); - static_assert(hs == hs2); - static_assert(hs.get_hash() == hs2.get_hash()); - } - - SUBCASE("get_hash") { - constexpr hashed_string hs1{"hello"}; - constexpr hashed_string hs2{"world"}; - static_assert(hs1.get_hash() == hashed_string{"hello"}.get_hash()); - static_assert(hs1.get_hash() != hs2.get_hash()); - CHECK(std::hash{}(hs1) == hs1.get_hash()); - } - - SUBCASE("operator<") { - constexpr hashed_string hs1{"hello"}; - constexpr hashed_string hs2{"hello"}; - static_assert(!(hs1 < hs2) && !(hs2 < hs1)); - static_assert(hs1 < hashed_string{"world"} || hashed_string{"world"} < hs1); - static_assert((hs1 < "world" || hs1 > "world")); - static_assert(("world" < hs1 || "world" > hs1)); - } - - SUBCASE("operator==") { - constexpr hashed_string hs1{"hello"}; - static_assert(hs1 == hashed_string{"hello"}); - static_assert(hs1 != hashed_string{"world"}); - static_assert(hs1 == "hello"); - static_assert("hello" == hs1); - static_assert("world" != hs1); - } - - SUBCASE("swap") { - hashed_string hs1{"hello"}; - hashed_string hs2{"world"}; - swap(hs1, hs2); - CHECK(hs1 == hashed_string{"world"}); - CHECK(hs2 == hashed_string{"hello"}); - } -} diff --git a/headers/meta.hpp/meta_base.hpp b/headers/meta.hpp/meta_base.hpp index d511ce2..2118cb1 100644 --- a/headers/meta.hpp/meta_base.hpp +++ b/headers/meta.hpp/meta_base.hpp @@ -14,7 +14,6 @@ #include "meta_base/fixed_function.hpp" #include "meta_base/fnv1a_hash.hpp" #include "meta_base/hash_composer.hpp" -#include "meta_base/hashed_string.hpp" #include "meta_base/inline_vector.hpp" #include "meta_base/insert_or_assign.hpp" #include "meta_base/is_in_place_type.hpp" diff --git a/headers/meta.hpp/meta_base/hash_composer.hpp b/headers/meta.hpp/meta_base/hash_composer.hpp index 31a3e60..5080f9e 100644 --- a/headers/meta.hpp/meta_base/hash_composer.hpp +++ b/headers/meta.hpp/meta_base/hash_composer.hpp @@ -9,7 +9,6 @@ #include "base.hpp" #include "bitflags.hpp" #include "fnv1a_hash.hpp" -#include "hashed_string.hpp" namespace meta_hpp::detail { @@ -40,7 +39,7 @@ namespace meta_hpp::detail } constexpr hash_composer operator<<(std::string_view value) noexcept { - hash = combine(hash, hashed_string{value}.get_hash()); + hash = combine(hash, fnv1a_hash(value.data(), value.size())); return *this; } diff --git a/headers/meta.hpp/meta_base/hashed_string.hpp b/headers/meta.hpp/meta_base/hashed_string.hpp deleted file mode 100644 index 37c2878..0000000 --- a/headers/meta.hpp/meta_base/hashed_string.hpp +++ /dev/null @@ -1,69 +0,0 @@ -/******************************************************************************* - * 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-2024, 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_; - } - - [[nodiscard]] constexpr bool operator==(hashed_string other) const noexcept { - return hash_ == other.hash_; - } - - [[nodiscard]] constexpr std::strong_ordering operator<=>(hashed_string other) const noexcept { - return hash_ <=> other.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, std::string_view r) noexcept { - return l == hashed_string{r}; - } - - [[nodiscard]] constexpr std::strong_ordering operator<=>(hashed_string l, std::string_view r) noexcept { - return l <=> hashed_string{r}; - } -} - -namespace std -{ - template <> - struct hash { - size_t operator()(meta_hpp::detail::hashed_string hs) const noexcept { - return hs.get_hash(); - } - }; -}