Skip to content

Commit

Permalink
Add utils/string_view_hash.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Aug 6, 2024
1 parent a41e60a commit 13947ea
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
19 changes: 2 additions & 17 deletions Source/utils/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "utils/file_util.h"
#include "utils/log.hpp"
#include "utils/paths.h"
#include "utils/string_view_hash.hpp"

#ifdef USE_SDL1
#include "utils/sdl2_to_1_2_backports.h"
Expand All @@ -37,23 +38,7 @@ std::unique_ptr<char[]> translationValues;

using TranslationRef = uint32_t;

struct StringHash {
using is_avalanching = void;

[[nodiscard]] uint64_t operator()(const char *str) const noexcept
{
return ankerl::unordered_dense::hash<std::string_view> {}(str);
}
};

struct StringEq {
bool operator()(const char *lhs, const char *rhs) const noexcept
{
return std::string_view(lhs) == std::string_view(rhs);
}
};

std::vector<ankerl::unordered_dense::map<const char *, TranslationRef, StringHash, StringEq>> translation = { {}, {} };
std::vector<ankerl::unordered_dense::map<const char *, TranslationRef, StringViewHash, StringViewEquals>> translation = { {}, {} };

constexpr uint32_t TranslationRefOffsetBits = 19;
constexpr uint32_t TranslationRefSizeBits = 32 - TranslationRefOffsetBits; // 13
Expand Down
47 changes: 47 additions & 0 deletions Source/utils/string_view_hash.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include <cstdint>
#include <string>
#include <string_view>

#include <ankerl/unordered_dense.h>

namespace devilution {

// A hash functor that enables heterogenous lookup for `unordered_set/map`.
struct StringViewHash {
using is_transparent = void;
using is_avalanching = void;

[[nodiscard]] uint64_t operator()(std::string_view str) const noexcept
{
return ankerl::unordered_dense::hash<std::string_view> {}(str);
}

[[nodiscard]] uint64_t operator()(const char *str) const noexcept
{
return (*this)(std::string_view { str });
}

[[nodiscard]] uint64_t operator()(const std::string &str) const noexcept
{
return (*this)(std::string_view { str });
}
};

// Usually we'd use `std::equal_to<>` instead but the latter
// does not link on the libcxx that comes with Xbox NXDK as of Aug 2024,
struct StringViewEquals {
using is_transparent = void;

[[nodiscard]] bool operator()(std::string_view a, std::string_view b) const { return a == b; }
[[nodiscard]] bool operator()(std::string_view a, const std::string &b) const { return a == b; }
[[nodiscard]] bool operator()(const std::string &a, std::string_view &b) const { return a == b; }
[[nodiscard]] bool operator()(const char *a, const std::string &b) const { return a == b; }
[[nodiscard]] bool operator()(const std::string &a, const char *b) const { return a == b; }
[[nodiscard]] bool operator()(std::string_view a, const char *b) const { return a == b; }
[[nodiscard]] bool operator()(const char *a, std::string_view b) const { return a == b; }
[[nodiscard]] bool operator()(const char *a, const char *b) const { return std::string_view { a } == b; }
};

} // namespace devilution

0 comments on commit 13947ea

Please sign in to comment.