Skip to content

Commit dfbf3e5

Browse files
committed
add Vector2Hash<T> for all fundamental type, also replace fge::AStar::Vector2iHash with it
1 parent 7e07571 commit dfbf3e5

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

includes/FastEngine/C_vector.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ using Vector3i = Vector3<int32_t>;
4343
using Vector3u = Vector3<uint32_t>;
4444
using Vector3f = Vector3<float>;
4545

46+
template<class T>
47+
struct Vector2Hash
48+
{
49+
static_assert(std::is_fundamental_v<T>, "Vector2Hash only works with fundamental types !");
50+
51+
inline std::size_t operator()(fge::Vector2<T> const& value) const
52+
{
53+
if constexpr (std::is_fundamental_v<T>)
54+
{
55+
return std::hash<T>{}(value.x) ^ std::hash<T>{}(value.y);
56+
}
57+
else
58+
{
59+
static_assert(false, "Vector2Hash only works with fundamental types !");
60+
}
61+
}
62+
};
63+
64+
using Vector2iHash = Vector2Hash<int32_t>;
65+
using Vector2uHash = Vector2Hash<uint32_t>;
66+
using Vector2fHash = Vector2Hash<float>;
67+
using Vector2sizeHash = Vector2Hash<std::size_t>;
68+
4669
} // namespace fge
4770

4871
namespace glm

includes/FastEngine/extra/extra_pathFinding.hpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,7 @@ namespace fge::AStar
3939
using HeuristicFunction = unsigned int (*)(fge::Vector2i, fge::Vector2i);
4040
using CoordinateList = std::vector<fge::Vector2i>;
4141

42-
struct Vector2iHash
43-
{
44-
static_assert(sizeof(fge::Vector2i) == 8, "bad fge::Vector2i size, should be 8 !");
45-
46-
inline std::size_t operator()(fge::Vector2i const& coord) const
47-
{
48-
return std::hash<uint64_t>()(*reinterpret_cast<uint64_t const*>(&coord));
49-
}
50-
};
51-
52-
using CoordinateSet = std::unordered_set<fge::Vector2i, fge::AStar::Vector2iHash>;
42+
using CoordinateSet = std::unordered_set<fge::Vector2i, fge::Vector2iHash>;
5343

5444
struct FGE_API Node
5545
{
@@ -61,7 +51,7 @@ struct FGE_API Node
6151
std::optional<fge::Vector2i> _parent;
6252
};
6353

64-
using NodeMap = std::unordered_map<fge::Vector2i, fge::AStar::Node, fge::AStar::Vector2iHash>;
54+
using NodeMap = std::unordered_map<fge::Vector2i, fge::AStar::Node, fge::Vector2iHash>;
6555

6656
class FGE_API Generator
6757
{

0 commit comments

Comments
 (0)