Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ThibaultDECO
Copy link
Contributor

Using Szudzik's pairing function (more efficient than the previous approach or Cantor's)

Using Szudzik's pairing function (more efficient than the previous approach or Cantor's)
@ThibaultDECO
Copy link
Contributor Author

As discussed in issue #648.

@tfussell
Copy link
Owner

tfussell commented Dec 3, 2022

I didn't know about that algorithm--it's pretty cool! Could you explain how it improves performance though? It seems like the operations are about the same (bit shift ~= multiplication, addition ~= boolean OR).

@tfussell
Copy link
Owner

tfussell commented Dec 3, 2022

If the pairing function is a perfect hash function, maybe we could remove the call to std::hash<size_t> entirely and simply return the 1-d value as the result of the custom hash. I could see that being much faster.

@ThibaultDECO
Copy link
Contributor Author

The Szudzik function has 100% value packing efficiency, i.e. no overflow. With Szudzik, pairing two 32-bits numbers results in a 64-bits number, which is not the case for Cantor or the current implementation. For example:
cantor(9, 9) = 200
szudzik(9, 9) = 99
You will find more details and a benchmark on performance here.
We could indeed remove the call to std::hash<size_t>, but only if size_t is 64-bits, which is unfortunately not always the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants