| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- export module Core.HashedString;
- export import Core.Types;
- import Core.Std;
- import Core.ToString;
- export namespace Core {
- struct StringHash {
- size_t length = 0;
- size_t hash = 0;
- };
- constexpr StringHash hashString(const char* s) noexcept {
- 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() noexcept : hash(), data{} {
- }
- HashedString(const char* s) noexcept : hash(hashString(s)) {
- Core::StringBase(data, N).add(s);
- }
- bool operator==(const HashedString& other) const noexcept {
- return hash.length == other.hash.length &&
- hash.hash == other.hash.hash &&
- strcmp(data, other.data) == 0;
- }
- bool operator!=(const HashedString& other) const noexcept {
- return !(*this == other);
- }
- size_t getLength() const noexcept {
- return hash.length;
- }
- constexpr size_t getCapacity() const noexcept {
- return N - 1;
- }
- size_t hashCode() const noexcept {
- return hash.hash;
- }
- operator const char*() const noexcept {
- return data;
- }
- };
- }
|