1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #ifndef CORE_HASHED_STRING_HPP
- #define CORE_HASHED_STRING_HPP
- #include <string.h>
- #include "core/utils/Types.hpp"
- namespace Core {
- struct StringHash {
- i32 length = 0;
- u32 hash = 0;
- };
- constexpr StringHash hashString(const char* s) {
- StringHash h;
- while(*s != '\0') {
- h.length++;
- h.hash = 2120251889u * h.hash + static_cast<u32>(*(s++));
- }
- return h;
- }
- template<i32 N>
- class HashedString {
- static_assert(N > 0, "length of hashed string must be positive");
- StringHash hash;
- char data[N];
- public:
- HashedString() : hash() {
- }
- HashedString(const char* s) : hash(hashString(s)) {
- strncpy(data, s, N);
- }
- 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);
- }
- i32 getLength() const {
- return hash.length;
- }
- constexpr i32 getCapacity() const {
- return N - 1;
- }
- u32 hashCode() const {
- return hash.hash;
- }
- operator const char*() const {
- return data;
- }
- };
- }
- #endif
|