From 7458072df2cfafbecd757f863804cd5b2e182768 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sun, 12 Sep 2021 18:14:42 +0200 Subject: [PATCH] hashed_string (close #781): * operator< and operator<= * operator> and operator>= --- src/entt/core/hashed_string.hpp | 55 ++++++++++++++++++++++++++++++++ test/entt/core/hashed_string.cpp | 42 ++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/src/entt/core/hashed_string.hpp b/src/entt/core/hashed_string.hpp index db3a384f8..c799d8f6f 100644 --- a/src/entt/core/hashed_string.hpp +++ b/src/entt/core/hashed_string.hpp @@ -233,6 +233,61 @@ template } +/** + * @brief Compares two hashed strings. + * @tparam Char Character type. + * @param lhs A valid hashed string. + * @param rhs A valid hashed string. + * @return True if the first element is less than the second, false otherwise. + */ +template +[[nodiscard]] constexpr bool operator<(const basic_hashed_string &lhs, const basic_hashed_string &rhs) ENTT_NOEXCEPT { + return lhs.value() < rhs.value(); +} + + +/** + * @brief Compares two hashed strings. + * @tparam Char Character type. + * @param lhs A valid hashed string. + * @param rhs A valid hashed string. + * @return True if the first element is less than or equal to the second, false + * otherwise. + */ +template +[[nodiscard]] constexpr bool operator<=(const basic_hashed_string &lhs, const basic_hashed_string &rhs) ENTT_NOEXCEPT { + return lhs.value() <= rhs.value(); +} + + +/** + * @brief Compares two hashed strings. + * @tparam Char Character type. + * @param lhs A valid hashed string. + * @param rhs A valid hashed string. + * @return True if the first element is greater than the second, false + * otherwise. + */ +template +[[nodiscard]] constexpr bool operator>(const basic_hashed_string &lhs, const basic_hashed_string &rhs) ENTT_NOEXCEPT { + return !(lhs <= rhs); +} + + +/** + * @brief Compares two hashed strings. + * @tparam Char Character type. + * @param lhs A valid hashed string. + * @param rhs A valid hashed string. + * @return True if the first element is greater than or equal to the second, + * false otherwise. + */ +template +[[nodiscard]] constexpr bool operator>=(const basic_hashed_string &lhs, const basic_hashed_string &rhs) ENTT_NOEXCEPT { + return !(lhs < rhs); +} + + /*! @brief Aliases for common character types. */ using hashed_string = basic_hashed_string; diff --git a/test/entt/core/hashed_string.cpp b/test/entt/core/hashed_string.cpp index 75ed34305..66850e9e6 100644 --- a/test/entt/core/hashed_string.cpp +++ b/test/entt/core/hashed_string.cpp @@ -79,6 +79,21 @@ TEST(HashedString, Correctness) { ASSERT_EQ(entt::hashed_string::value(view.data(), view.size()), foobar_v); } +TEST(HashedString, Order) { + using namespace entt::literals; + const entt::hashed_string lhs = "foo"_hs; + const entt::hashed_string rhs = "bar"_hs; + + ASSERT_FALSE(lhs < lhs); + ASSERT_FALSE(rhs < rhs); + + ASSERT_LT(rhs, lhs); + ASSERT_LE(rhs, lhs); + + ASSERT_GT(lhs, rhs); + ASSERT_GE(lhs, rhs); +} + TEST(HashedString, Constexprness) { using namespace entt::literals; constexpr std::string_view view{"foobar__", 6}; @@ -91,6 +106,12 @@ TEST(HashedString, Constexprness) { static_assert(entt::hashed_string::value("quux", 4) == "quux"_hs); static_assert(entt::hashed_string::value(view.data(), view.size()) == foobar_v); + + static_assert(entt::hashed_string{"bar"} < "foo"_hs); + static_assert(entt::hashed_string{"bar"} <= "bar"_hs); + + static_assert(entt::hashed_string{"foo"} > "bar"_hs); + static_assert(entt::hashed_string{"foo"} >= "foo"_hs); } TEST(HashedWString, Functionalities) { @@ -138,6 +159,21 @@ TEST(HashedWString, Correctness) { ASSERT_EQ(entt::hashed_wstring::value(view.data(), view.size()), foobar_v); } +TEST(HashedWString, Order) { + using namespace entt::literals; + const entt::hashed_wstring lhs = L"foo"_hws; + const entt::hashed_wstring rhs = L"bar"_hws; + + ASSERT_FALSE(lhs < lhs); + ASSERT_FALSE(rhs < rhs); + + ASSERT_LT(rhs, lhs); + ASSERT_LE(rhs, lhs); + + ASSERT_GT(lhs, rhs); + ASSERT_GE(lhs, rhs); +} + TEST(HashedWString, Constexprness) { using namespace entt::literals; constexpr std::wstring_view view{L"foobar__", 6}; @@ -150,4 +186,10 @@ TEST(HashedWString, Constexprness) { static_assert(entt::hashed_wstring::value(L"quux", 4) == L"quux"_hws); static_assert(entt::hashed_wstring::value(view.data(), view.size()) == foobar_v); + + static_assert(entt::hashed_wstring{L"bar"} < L"foo"_hws); + static_assert(entt::hashed_wstring{L"bar"} <= L"bar"_hws); + + static_assert(entt::hashed_wstring{L"foo"} > L"bar"_hws); + static_assert(entt::hashed_wstring{L"foo"} >= L"foo"_hws); }