-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ecs): use EntityHash instead of std::hash
- Loading branch information
Showing
14 changed files
with
107 additions
and
68 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
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,22 @@ | ||
/// @file | ||
/// @brief Struct @ref cubos::core::ecs::EntityHash. | ||
/// @ingroup core-ecs | ||
|
||
#pragma once | ||
|
||
#include <cstddef> | ||
|
||
namespace cubos::core::ecs | ||
{ | ||
struct Entity; | ||
|
||
/// @brief Used to hash @ref Entity objects. | ||
/// | ||
/// Can be used to allow @ref Entity objects to be used as keys in an `std::unordered_map`. | ||
/// | ||
/// @ingroup core-ecs | ||
struct EntityHash | ||
{ | ||
std::size_t operator()(const Entity& entity) const noexcept; | ||
}; | ||
} // namespace cubos::core::ecs |
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
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,20 @@ | ||
#include <cubos/core/ecs/entity/entity.hpp> | ||
#include <cubos/core/ecs/entity/hash.hpp> | ||
|
||
using cubos::core::ecs::EntityHash; | ||
|
||
std::size_t EntityHash::operator()(const Entity& entity) const noexcept | ||
{ | ||
// If std::size_t is 64 bits or more, we can fit both the index and generation in it. | ||
// Otherwise, we'll just use the index as the hash, which means we'll always get collisions | ||
// for entities with the same index but different generations. That shouldn't be a problem | ||
// though, since that use case is very unlikely. | ||
|
||
if constexpr (sizeof(std::size_t) < sizeof(uint32_t) * 2) | ||
{ | ||
return static_cast<std::size_t>(entity.index); | ||
} | ||
|
||
return static_cast<std::size_t>(entity.index) | | ||
(static_cast<std::size_t>(entity.generation) << (sizeof(uint32_t) * 8)); | ||
} |
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
Oops, something went wrong.