mirror of https://github.com/OpenTTD/OpenTTD
Codechange: introduce transparent hash lookup for strings
parent
7bbf380931
commit
4e3e3d5be6
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
#include "../core/string_consumer.hpp"
|
#include "../core/string_consumer.hpp"
|
||||||
#include "../language.h"
|
#include "../language.h"
|
||||||
|
#include "../string_type.h"
|
||||||
#include "../3rdparty/fmt/format.h"
|
#include "../3rdparty/fmt/format.h"
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
@ -41,7 +42,7 @@ struct LangString {
|
||||||
/** Information about the currently known strings. */
|
/** Information about the currently known strings. */
|
||||||
struct StringData {
|
struct StringData {
|
||||||
std::vector<std::shared_ptr<LangString>> strings; ///< List of all known strings.
|
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 tabs; ///< The number of 'tabs' of strings.
|
||||||
size_t max_strings; ///< The maximum number of strings.
|
size_t max_strings; ///< The maximum number of strings.
|
||||||
size_t next_string_id;///< The next string ID to allocate.
|
size_t next_string_id;///< The next string ID to allocate.
|
||||||
|
@ -49,7 +50,7 @@ struct StringData {
|
||||||
StringData(size_t tabs);
|
StringData(size_t tabs);
|
||||||
void FreeTranslation();
|
void FreeTranslation();
|
||||||
void Add(std::shared_ptr<LangString> ls);
|
void Add(std::shared_ptr<LangString> ls);
|
||||||
LangString *Find(const std::string &s);
|
LangString *Find(std::string_view s);
|
||||||
uint32_t Version() const;
|
uint32_t Version() const;
|
||||||
size_t CountInUse(size_t tab) const;
|
size_t CountInUse(size_t tab) const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -100,7 +100,7 @@ void StringData::Add(std::shared_ptr<LangString> ls)
|
||||||
* @param s The string name to search on.
|
* @param s The string name to search on.
|
||||||
* @return The LangString or nullptr if it is not known.
|
* @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);
|
auto it = this->name_to_string.find(s);
|
||||||
if (it == this->name_to_string.end()) return nullptr;
|
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.. */
|
/* 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 (this->master) {
|
||||||
if (casep.has_value()) {
|
if (casep.has_value()) {
|
||||||
|
|
|
@ -59,4 +59,14 @@ using StringValidationSettings = EnumBitSet<StringValidationSetting, uint8_t>;
|
||||||
/** Type for a list of strings. */
|
/** Type for a list of strings. */
|
||||||
typedef std::vector<std::string> StringList;
|
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 */
|
#endif /* STRING_TYPE_H */
|
||||||
|
|
Loading…
Reference in New Issue