From 48c126cf9c8cbb76b6fe1bb54158d1e7b4635fce Mon Sep 17 00:00:00 2001 From: Teebonne <80053070+Teebonne@users.noreply.github.com> Date: Sat, 10 Sep 2022 21:35:48 +0100 Subject: [PATCH] Improving the hash function Using Szudzik's pairing function (more efficient than the previous approach or Cantor's) --- include/xlnt/cell/cell_reference.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/xlnt/cell/cell_reference.hpp b/include/xlnt/cell/cell_reference.hpp index c8efcb4b3..d551b6ab3 100644 --- a/include/xlnt/cell/cell_reference.hpp +++ b/include/xlnt/cell/cell_reference.hpp @@ -262,11 +262,13 @@ namespace std { template <> struct hash { - size_t operator()(const xlnt::cell_reference &x) const + size_t operator()(const xlnt::cell_reference &x) const // using Szudzik's pairing function { - static_assert(std::is_same::value, "this hash function expects both row and column to be 32-bit numbers"); - static_assert(std::is_same::value, "this hash function expects both row and column to be 32-bit numbers"); - return hash{}(x.row() | static_cast(x.column_index()) << 32); + if (x.column_index() >= x.row()) { + return hash{}(x.column_index() * x.column_index() + x.column_index() + x.row()); + } else { + return hash{}(x.column_index() + x.row() * x.row()); + }; } }; } // namespace std