Convexhull-3D-Implementation-of-incremental-convexhull-algorithm icon indicating copy to clipboard operation
Convexhull-3D-Implementation-of-incremental-convexhull-algorithm copied to clipboard

Wrong usage of unordered map

Open milasudril opened this issue 2 years ago • 0 comments

In case you have a hash collision when computing the hash of the pair of points, you will overwrite existing entries. Instead of using the hash as a key, you should use the actual pair, and use an associated hash function, ie:

    struct PointHash
    {
      size_t operator()(Point3D a) const
      {
        return std::hash<double>{}(a.x) ^ std::hash<double>{}(a.y) ^ std::hash<double>{}(a.z);
      }
    };

    struct EdgeEndpoints
    {
      Point3D p1;
      Point3D p2;

      constexpr bool operator==(const EdgeEndpoints&) const = default;
      constexpr bool operator!=(const EdgeEndpoints&) const = default;
    };

    struct EdgeEndpointHash
    {
      size_t operator()(EdgeEndpoints const& a) const
      {
        return PointHash{}(a.p1) ^ PointHash{}(a.p2);
      }
    };

    std::unordered_map<EdgeEndpoints, Edge*, EdgeEndpointHash> map_edges;

Also, please do not convert to string when computing hashes.

milasudril avatar Apr 08 '23 18:04 milasudril