From 9e2dce4ed0f21092113e5e958155d2abc7c9107b Mon Sep 17 00:00:00 2001 From: Qianqian Fang Date: Fri, 22 Nov 2024 18:36:30 -0500 Subject: [PATCH] [feat] speed up facenb computation using std::unordered_map --- src/mmc_highorder.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/mmc_highorder.cpp b/src/mmc_highorder.cpp index 1d1e852..bf37f04 100644 --- a/src/mmc_highorder.cpp +++ b/src/mmc_highorder.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include #include @@ -108,21 +108,34 @@ void mesh_10nodetet(tetmesh* mesh, mcconfig* cfg) { } +class Vec3Hash { + public: + std::size_t operator()(std::array const& vec) const { + std::size_t seed = 3; + + for (int i = 0; i < 3; i++) { + seed ^= vec[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2); + } + + return seed; + } +}; + #ifdef __cplusplus extern "C" #endif void mesh_getfacenb(tetmesh* mesh, mcconfig* cfg) { - std::map< std::vector, std::pair > facenb; - std::map< std::vector, std::pair > ::iterator it; + std::unordered_map< std::array, std::pair, Vec3Hash > facenb; + std::unordered_map< std::array, std::pair, Vec3Hash > ::iterator it; for (int i = 0; i < mesh->ne; i++) { int* ee = mesh->elem + i * mesh->elemlen; for (int j = 0; j < 4; j++) { - std::vector facevec = { ee[facelist[j][0]], ee[facelist[j][1]], ee[facelist[j][2]] }; + std::array facevec = { ee[facelist[j][0]], ee[facelist[j][1]], ee[facelist[j][2]] }; std::sort (facevec.begin(), facevec.end()); - if (facenb.count(facevec)) { + if (facenb.find(facevec) != facenb.end()) { facenb[facevec].second = (i << 2) + j + 1; } else { facenb[facevec] = std::make_pair((i << 2) + j, 0);