Skip to content

Wrong usage of unordered map #3

Open
@milasudril

Description

@milasudril

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions