Search code examples
c++hashsetunordered-set

Unique key for 3D interger coordinates to insert into a std::unordered_set


I have a stream of 3D integer coordinates that correspond to voxels and thus are aligned on a grid. I want to figure out if the current processed triplet is already existing in order to filter duplicates. I was able to build a simple solution to my problem with a std::set. Let x y z be 3 int and registry be a std::set< std::array<int, 3> >. I made a function that return a bool like that

std::array<int, 3> key = {x, y, z};
return registry.insert(key).second;

But this is far to be optimized in term of computation time. Reading documentation and SO topics I understand that an unordered_set should be more appropriated. Indeed there is no need to sort anything here. In addition I guess that using a array<int,3> as key is not efficient for comparison at insert time.

An unordered_set requires a hash function. Studying hash functions I found boost::hash_combine as well as other options.

How can I use an unordered_set efficiently in my situation? The key point is being as fast as possible. I don't need to access to values and I don't need to make any special computations.


Solution

  • I answer my own question. My initial question was ill-formed but thank to @Damien I understood how the hash was used into an std::unordered_*. I used boost

    #include <boost/functional/hash.hpp>
    

    And I defined my registry as follow

    typedef std::array<I32,3> Array;
    std::unordered_set<Array, boost::hash<Array> >
    

    And I gained ~33% of computation time.