Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
HashedString.hpp
1#pragma once
2
3#include <map>
4
5#include "String.hpp"
6#include "Hash.hpp"
7
8namespace Grindstone {
9 class HashedString {
10 public:
11 using HashMap = std::map<Grindstone::HashValue, Grindstone::String>;
12 static void CreateHashMap();
13 static HashMap* GetHashedStringMap();
14 static void SetHashMap(HashMap* hashMap);
15 HashedString();
16 explicit HashedString(const char* inStringRef);
17 explicit HashedString(const String& inString);
18 explicit HashedString(StringRef inStringRef);
19
20 void Create(StringRef inStringRef);
21 uint64_t GetHash() const;
22
23 operator bool() const noexcept;
24 bool operator==(const Grindstone::HashedString& other) const noexcept;
25
26 bool operator>(const HashedString& other) const {
27 return hash > other.hash;
28 }
29
30 bool operator<(const HashedString& other) const {
31 return hash < other.hash;
32 }
33
34 bool operator>=(const HashedString& other) const {
35 return hash >= other.hash;
36 }
37
38 bool operator<=(const HashedString& other) const {
39 return hash <= other.hash;
40 }
41
42 operator std::size_t() const {
43 return static_cast<std::size_t>(hash);
44 }
45
46
47 const String& ToString() const;
48
49 protected:
50 Grindstone::HashValue hash;
51 };
52
53 class ConstHashedString {
54 public:
55 constexpr ConstHashedString(const char* str)
56 : hash(Hash::MurmurOAAT64(str)), literal(str) {
57 }
58
59 bool operator==(const Grindstone::HashedString& other) const noexcept {
60 return hash == other.GetHash();
61 }
62
63 bool operator==(const Grindstone::ConstHashedString& other) const noexcept {
64 return hash == other.GetHash();
65 }
66
67 friend bool operator==(const Grindstone::HashedString& a, const Grindstone::ConstHashedString& b) noexcept {
68 return a.GetHash() == b.GetHash();
69 }
70
71 uint64_t GetHash() const noexcept { return hash; }
72 operator uint64_t() const noexcept { return hash; }
73 HashedString GetHashedString() const noexcept { return HashedString(literal); }
74 operator HashedString() const noexcept { return HashedString(literal); }
75
76 const char* ToString() const {
77 auto map = Grindstone::HashedString::GetHashedStringMap();
78 if (map != nullptr) {
79 auto it = map->find(hash);
80 if (it == map->end()) {
81 map->emplace(hash, Grindstone::String(literal));
82 }
83 }
84
85 return literal;
86 }
87
88 private:
89 uint64_t hash;
90 const char* literal;
91 };
92}
93
94namespace std {
95 template <>
96 struct hash<Grindstone::HashedString> {
97 std::size_t operator()(const Grindstone::HashedString& c) const {
98 uint64_t hash = c.GetHash();
99 return static_cast<std::size_t>(hash);
100 }
101 };
102}
Definition HashedString.hpp:53
Definition HashedString.hpp:9