| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- module;
- #include <cstdio>
- #include <cstring>
- export module Core.HashedString;
- export import Core.Types;
- export namespace Core {
- struct StringHash {
- size_t length = 0;
- size_t hash = 0;
- };
- constexpr StringHash hashString(const char* s) {
- StringHash h;
- while(*s != '\0') {
- h.length++;
- h.hash = 2'120'251'889lu * h.hash + static_cast<size_t>(*(s++));
- }
- return h;
- }
- template<size_t N>
- class HashedString {
- StringHash hash;
- char data[N];
- public:
- HashedString() : hash(), data{} {
- }
- HashedString(const char* s) : hash(hashString(s)) {
- snprintf(data, N, "%s", s);
- }
- bool operator==(const HashedString& other) const {
- return hash.length == other.hash.length &&
- hash.hash == other.hash.hash &&
- strcmp(data, other.data) == 0;
- }
- bool operator!=(const HashedString& other) const {
- return !(*this == other);
- }
- size_t getLength() const {
- return hash.length;
- }
- constexpr size_t getCapacity() const {
- return N - 1;
- }
- size_t hashCode() const {
- return hash.hash;
- }
- operator const char*() const {
- return data;
- }
- };
- }
|