1
0
Fork 0

Codechange: introduce transparent hash lookup for strings

pull/14132/head
Rubidium 2025-04-27 15:49:10 +02:00 committed by rubidium42
parent 7bbf380931
commit 4e3e3d5be6
3 changed files with 15 additions and 4 deletions

View File

@ -12,6 +12,7 @@
#include "../core/string_consumer.hpp"
#include "../language.h"
#include "../string_type.h"
#include "../3rdparty/fmt/format.h"
#include <unordered_map>
@ -41,7 +42,7 @@ struct LangString {
/** Information about the currently known strings. */
struct StringData {
std::vector<std::shared_ptr<LangString>> strings; ///< List of all known strings.
std::unordered_map<std::string, std::shared_ptr<LangString>> name_to_string; ///< Lookup table for the strings.
std::unordered_map<std::string, std::shared_ptr<LangString>, StringHash, std::equal_to<>> name_to_string; ///< Lookup table for the strings.
size_t tabs; ///< The number of 'tabs' of strings.
size_t max_strings; ///< The maximum number of strings.
size_t next_string_id;///< The next string ID to allocate.
@ -49,7 +50,7 @@ struct StringData {
StringData(size_t tabs);
void FreeTranslation();
void Add(std::shared_ptr<LangString> ls);
LangString *Find(const std::string &s);
LangString *Find(std::string_view s);
uint32_t Version() const;
size_t CountInUse(size_t tab) const;
};

View File

@ -100,7 +100,7 @@ void StringData::Add(std::shared_ptr<LangString> ls)
* @param s The string name to search on.
* @return The LangString or nullptr if it is not known.
*/
LangString *StringData::Find(const std::string &s)
LangString *StringData::Find(std::string_view s)
{
auto it = this->name_to_string.find(s);
if (it == this->name_to_string.end()) return nullptr;
@ -527,7 +527,7 @@ void StringReader::HandleString(std::string_view src)
}
/* Check if this string already exists.. */
LangString *ent = this->data.Find(std::string(str_name));
LangString *ent = this->data.Find(str_name);
if (this->master) {
if (casep.has_value()) {

View File

@ -59,4 +59,14 @@ using StringValidationSettings = EnumBitSet<StringValidationSetting, uint8_t>;
/** Type for a list of strings. */
typedef std::vector<std::string> StringList;
/** Helper to provide transparent hashing for string types in e.g. std::unordered_map. */
struct StringHash {
using hash_type = std::hash<std::string_view>;
using is_transparent = void;
std::size_t operator()(const char *str) const { return hash_type{}(str); }
std::size_t operator()(std::string_view str) const { return hash_type{}(str); }
std::size_t operator()(const std::string &str) const { return hash_type{}(str); }
};
#endif /* STRING_TYPE_H */