-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from psiberx/master
Add NodeRef
- Loading branch information
Showing
3 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include <RED4ext/Common.hpp> | ||
#include <RED4ext/HashMap.hpp> | ||
|
||
namespace RED4ext | ||
{ | ||
struct NodeRef | ||
{ | ||
static constexpr auto GlobalRoot = FNV1a64("$"); // NodeRef("$") | ||
static constexpr auto RelativeRoot = FNV1a64("~"); // NodeRef("~") | ||
|
||
constexpr NodeRef(uint64_t aHash = 0) noexcept | ||
: hash(aHash) | ||
{ | ||
} | ||
|
||
constexpr NodeRef(const char* aNodeRefStr) noexcept | ||
: hash(Hash(aNodeRefStr)) | ||
{ | ||
} | ||
|
||
constexpr operator uint64_t() const noexcept | ||
{ | ||
return hash; | ||
} | ||
|
||
constexpr size_t operator()(const NodeRef& aNodeRef) const | ||
{ | ||
return aNodeRef.hash; | ||
} | ||
|
||
constexpr NodeRef& operator=(const uint64_t aRhs) noexcept | ||
{ | ||
hash = aRhs; | ||
return *this; | ||
} | ||
|
||
constexpr NodeRef& operator=(const char* aRhs) noexcept | ||
{ | ||
*this = NodeRef(aRhs); | ||
return *this; | ||
} | ||
|
||
constexpr NodeRef& operator=(const NodeRef& aRhs) noexcept | ||
{ | ||
hash = aRhs.hash; | ||
return *this; | ||
} | ||
|
||
constexpr bool operator==(const NodeRef& aRhs) const noexcept | ||
{ | ||
return hash == aRhs.hash; | ||
} | ||
|
||
constexpr bool operator!=(const NodeRef& aRhs) const noexcept | ||
{ | ||
return !(*this == aRhs); | ||
} | ||
|
||
constexpr bool operator==(const uint64_t aRhs) const noexcept | ||
{ | ||
return hash == aRhs; | ||
} | ||
|
||
constexpr bool operator!=(const uint64_t aRhs) const noexcept | ||
{ | ||
return hash != aRhs; | ||
} | ||
|
||
[[nodiscard]] constexpr bool IsEmpty() const noexcept | ||
{ | ||
return hash == 0; | ||
} | ||
|
||
static constexpr uint64_t Hash(const char* aNodeRefStr) | ||
{ | ||
constexpr uint64_t prime = 0x100000001b3; | ||
constexpr uint64_t seed = 0xCBF29CE484222325; | ||
|
||
uint64_t hash = seed; | ||
|
||
while (aNodeRefStr && *aNodeRefStr) | ||
{ | ||
if (*aNodeRefStr == '#') | ||
{ | ||
++aNodeRefStr; | ||
continue; | ||
} | ||
|
||
if (*aNodeRefStr == ';') | ||
{ | ||
++aNodeRefStr; | ||
|
||
while (*aNodeRefStr && *aNodeRefStr != '/') | ||
++aNodeRefStr; | ||
|
||
if (!*aNodeRefStr) | ||
break; | ||
} | ||
|
||
hash ^= *aNodeRefStr; | ||
hash *= prime; | ||
|
||
++aNodeRefStr; | ||
} | ||
|
||
return hash == seed ? 0 : hash; | ||
} | ||
|
||
uint64_t hash; | ||
}; | ||
RED4EXT_ASSERT_SIZE(NodeRef, 0x8); | ||
|
||
template<typename T> | ||
struct HashMapHash<T, std::enable_if_t<std::is_same_v<T, NodeRef>>> | ||
{ | ||
uint32_t operator()(const T& aKey) const noexcept | ||
{ | ||
return static_cast<uint32_t>(aKey) ^ ((aKey >> 32) & 0xFFFFFFFF); | ||
} | ||
}; | ||
} // namespace RED4ext |