-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralize transparent string hashing to TransparentStringViewHasher
1 parent
9467b80
commit bb8d313
Showing
8 changed files
with
94 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <string_view> | ||
|
||
namespace osc | ||
{ | ||
// a `std::hash`-like object that can transparently hash any object that is | ||
// implicitly convertible to a `std::string_view` | ||
struct TransparentStringViewHasher final { | ||
|
||
// important: this is how associative containers check that the hasher | ||
// doesn't need to create keys at runtime (e.g. using a `std::string_view` | ||
// to `.find` an `std::unordered_map<std::string, T>` won't require | ||
// constructing a `std::string`) | ||
using is_transparent = void; | ||
|
||
size_t operator()(std::string_view sv) const noexcept | ||
{ | ||
return std::hash<std::string_view>{}(sv); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <oscar/Utils/TransparentStringViewHasher.h> | ||
|
||
#include <oscar/Utils/CStringView.h> | ||
#include <oscar/Utils/StringName.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include <algorithm> | ||
#include <array> | ||
#include <functional> | ||
#include <string> | ||
#include <string_view> | ||
#include <unordered_map> | ||
|
||
using namespace osc; | ||
namespace rgs = std::ranges; | ||
|
||
namespace | ||
{ | ||
using TransparentMap = std::unordered_map<std::string, int, TransparentStringViewHasher, std::equal_to<>>; | ||
} | ||
|
||
TEST(TransparentStringViewHasher, can_construct_std_unordered_map_that_uses_transparent_string_hasher) | ||
{ | ||
[[maybe_unused]] TransparentMap map; // this should work | ||
} | ||
|
||
TEST(TransparentStringViewHasher, transparent_unordered_map_enables_std_string_view_lookups) | ||
{ | ||
TransparentMap map; | ||
[[maybe_unused]] const auto it = map.find(std::string_view{"i don't need to be converted into a std::string :)"}); | ||
} | ||
|
||
TEST(TransparentStringViewHasher, transparent_unordered_map_enables_CStringView_lookups) | ||
{ | ||
TransparentMap map; | ||
[[maybe_unused]] const auto it = map.find(CStringView{"i don't need to be converted into a std::string :)"}); | ||
} | ||
|
||
TEST(TransparentStringViewHasher, transparent_unordered_map_enables_StringName_lookups) | ||
{ | ||
TransparentMap map; | ||
[[maybe_unused]] const auto it = map.find(StringName{"i don't need to be converted into a std::string :)"}); | ||
} | ||
|
||
TEST(TransparentStringViewHasher, produces_same_hash_for_all_of_OSCs_string_types) | ||
{ | ||
for (const char* str : {"", "some string", "why not try three?"}) { | ||
const auto hashes = std::to_array({ | ||
TransparentStringViewHasher{}(str), | ||
TransparentStringViewHasher{}(std::string_view{str}), | ||
TransparentStringViewHasher{}(CStringView{str}), | ||
TransparentStringViewHasher{}(std::string{str}), | ||
TransparentStringViewHasher{}(StringName{str}), | ||
}); | ||
ASSERT_TRUE(rgs::equal_range(hashes, hashes.front())); | ||
} | ||
} |