Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hash_combine #281

Merged
merged 11 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions engine/source/runtime/core/base/hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <cstddef>
#include <functional>

template<typename T>
inline void hash_combine(std::size_t& seed, const T& v)
{
seed ^= std::hash<T> {}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

template<typename T, typename... Ts>
inline void hash_combine(std::size_t& seed, const T& v, Ts... rest)
{
hash_combine(seed, v);
if constexpr (sizeof...(Ts) > 1)
{
hash_combine(seed, rest...);
}
}
19 changes: 7 additions & 12 deletions engine/source/runtime/function/physics/collision_detection.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "runtime/core/base/hash.h"
#include "runtime/core/math/axis_aligned.h"
#include "runtime/core/math/transform.h"

Expand Down Expand Up @@ -32,22 +33,16 @@ namespace Piccolo
m_contact_point.m_penetration = penetration;
}

bool operator<(const CollisionInfo& other_info) const
bool operator<(const CollisionInfo& rhs) const
{
size_t other_hash = (size_t)other_info.m_id_a + ((size_t)other_info.m_id_b << 8);
size_t this_hash = (size_t)m_id_a + ((size_t)m_id_b << 8);
size_t other_hash = 0;
hash_combine(other_hash, rhs.m_id_a, rhs.m_id_b);
size_t this_hash = 0;
hash_combine(this_hash, m_id_a, m_id_b);
return this_hash < other_hash;
}

bool operator==(const CollisionInfo& other_info) const
{
if (other_info.m_id_a == m_id_a && other_info.m_id_b == m_id_b)
{
return true;
}

return false;
}
bool operator==(const CollisionInfo& rhs) const { return rhs.m_id_a == m_id_a && rhs.m_id_b == m_id_b; }
};

class CollisionDetection
Expand Down
23 changes: 15 additions & 8 deletions engine/source/runtime/function/render/render_type.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "runtime/core/base/hash.h"

#include <cstdint>
#include <memory>
#include <string>
Expand Down Expand Up @@ -117,17 +119,22 @@ namespace Piccolo
bool operator==(const MaterialSourceDesc& rhs) const
{
return m_base_color_file == rhs.m_base_color_file &&
m_metallic_roughness_file == rhs.m_metallic_roughness_file && m_normal_file == rhs.m_normal_file &&
m_occlusion_file == rhs.m_occlusion_file && m_emissive_file == rhs.m_emissive_file;
m_metallic_roughness_file == rhs.m_metallic_roughness_file &&
m_normal_file == rhs.m_normal_file &&
m_occlusion_file == rhs.m_occlusion_file &&
m_emissive_file == rhs.m_emissive_file;
}

size_t getHashValue() const
{
size_t h0 = std::hash<std::string> {}(m_base_color_file);
size_t h1 = std::hash<std::string> {}(m_metallic_roughness_file);
size_t h2 = std::hash<std::string> {}(m_normal_file);
size_t h3 = std::hash<std::string> {}(m_occlusion_file);
size_t h4 = std::hash<std::string> {}(m_emissive_file);
return (((h0 ^ (h1 << 1)) ^ (h2 << 1)) ^ (h3 << 1)) ^ (h4 << 1);
size_t hash = 0;
hash_combine(hash,
m_base_color_file,
m_metallic_roughness_file,
m_normal_file,
m_occlusion_file,
m_emissive_file);
return hash;
}
};

Expand Down