diff options
author | Sune Vuorela <sune@vuorela.dk> | 2024-12-19 13:26:18 +0100 |
---|---|---|
committer | Albert Astals Cid <aacid@kde.org> | 2024-12-20 10:53:05 +0000 |
commit | 99c9fe76ed537939be665c183cfd31a3e7925a74 (patch) | |
tree | 4b2729a65450739edadf343b58c13456266ba27d | |
parent | 1509b915ef1c826d40b6dccabb7f1fabf62e5f84 (diff) |
NameToCharCode: remove custom hashmap
Using std::unordered_map makes the code quite a bit simpler
-rw-r--r-- | poppler/NameToCharCode.cc | 100 | ||||
-rw-r--r-- | poppler/NameToCharCode.h | 22 |
2 files changed, 15 insertions, 107 deletions
diff --git a/poppler/NameToCharCode.cc b/poppler/NameToCharCode.cc index 73c9f1c1..78aa6bee 100644 --- a/poppler/NameToCharCode.cc +++ b/poppler/NameToCharCode.cc @@ -23,110 +23,18 @@ #include <config.h> #include <cstring> -#include "goo/gmem.h" #include "NameToCharCode.h" -//------------------------------------------------------------------------ - -struct NameToCharCodeEntry -{ - char *name; - CharCode c; -}; - -//------------------------------------------------------------------------ - -NameToCharCode::NameToCharCode() -{ - int i; - - size = 31; - len = 0; - tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry)); - for (i = 0; i < size; ++i) { - tab[i].name = nullptr; - } -} - -NameToCharCode::~NameToCharCode() -{ - int i; - - for (i = 0; i < size; ++i) { - if (tab[i].name) { - gfree(tab[i].name); - } - } - gfree(tab); -} - void NameToCharCode::add(const char *name, CharCode c) { - NameToCharCodeEntry *oldTab; - int h, i, oldSize; - - // expand the table if necessary - if (len >= size / 2) { - oldSize = size; - oldTab = tab; - size = 2 * size + 1; - tab = (NameToCharCodeEntry *)gmallocn(size, sizeof(NameToCharCodeEntry)); - for (h = 0; h < size; ++h) { - tab[h].name = nullptr; - } - for (i = 0; i < oldSize; ++i) { - if (oldTab[i].name) { - h = hash(oldTab[i].name); - while (tab[h].name) { - if (++h == size) { - h = 0; - } - } - tab[h] = oldTab[i]; - } - } - gfree(oldTab); - } - - // add the new name - h = hash(name); - while (tab[h].name && (strcmp(tab[h].name, name) != 0)) { - if (++h == size) { - h = 0; - } - } - if (!tab[h].name) { - tab[h].name = copyString(name); - } - tab[h].c = c; - - ++len; + tab.insert_or_assign(std::string { name }, c); } CharCode NameToCharCode::lookup(const char *name) const { - int h; - - h = hash(name); - while (tab[h].name) { - if (!strcmp(tab[h].name, name)) { - return tab[h].c; - } - if (++h == size) { - h = 0; - } + auto it = tab.find(name); + if (it != tab.end()) { + return it->second; } return 0; } - -int NameToCharCode::hash(const char *name) const -{ - const char *p; - unsigned int h; - - h = 0; - for (p = name; *p; ++p) { - h = 17 * h + (int)(*p & 0xff); - } - return (int)(h % size); -} diff --git a/poppler/NameToCharCode.h b/poppler/NameToCharCode.h index 53b8972e..3b74e354 100644 --- a/poppler/NameToCharCode.h +++ b/poppler/NameToCharCode.h @@ -24,6 +24,8 @@ #define NAMETOCHARCODE_H #include "CharTypes.h" +#include <unordered_map> +#include <string> struct NameToCharCodeEntry; @@ -32,21 +34,19 @@ struct NameToCharCodeEntry; class NameToCharCode { public: - NameToCharCode(); - ~NameToCharCode(); - - NameToCharCode(const NameToCharCode &) = delete; - NameToCharCode &operator=(const NameToCharCode &) = delete; - void add(const char *name, CharCode c); CharCode lookup(const char *name) const; private: - int hash(const char *name) const; - - NameToCharCodeEntry *tab; - int size; - int len; + /*This is a helper struct to allow looking up by char* into a std string unordered_map*/ + struct string_hasher + { + using is_transparent = void; + [[nodiscard]] size_t operator()(const char *txt) const { return std::hash<std::string_view> {}(txt); } + [[nodiscard]] size_t operator()(const std::string &txt) const { return std::hash<std::string> {}(txt); } + }; + + std::unordered_map<std::string, CharCode, string_hasher, std::equal_to<>> tab; }; #endif |