title: Best way to hash a vertex
body: <p>I have the following code which I would like to input in an unordered_set for the fastest access possible based on its 3D coordinates.</p>
  
  <pre><code>struct MeshVertex {
    float x;
    float y;
    float z;
  
    std::vector&lt;MeshVertex*&gt; links;
  
    float&amp; operator [](int i) {
      switch (i) {
      case 0:
        return x;
      case 1:
        return y;
      case 2:
        return z;
      default:
        throw std::out_of_range("");
      }
    }
  
    MeshVertex&amp; operator=(const STLFileVertex&amp; other)
    {
      x = other.x;
      y = other.y;
      z = other.z;
      return *this;
    }
  
    bool operator&lt;(const MeshVertex&amp; other) const
    {
      if (x == other.x) {
        if (y == other.y)
          return z &lt; other.z;
        return y &lt; other.y;
      }
      return x &lt; other.x;
    }
  
    bool operator==(const MeshVertex&amp; other) const {
      if (x == other.x &amp;&amp; y == other.y &amp;&amp; z == other.z)
        return true;
      return false;
    }
  
    bool operator!=(const MeshVertex&amp; other) const {
      if (x == other.x &amp;&amp; y == other.y &amp;&amp; z == other.z)
        return false;
      return true;
    }
  
    double distance(const MeshVertex&amp; other) const {
      return hypot(hypot(x - other.x, y - other.y), z - other.z);
    }
  };
  </code></pre>
  
  <p>As expected, I get the following error:</p>
  
  <pre><code>The C++ Standard doesn't provide a hash for this type.
  </code></pre>
  
  <p>How do I implement a <strong>well performing</strong> hash for this? It only has to contain the members x, y and z and <strong>the combination of hash and comparison</strong> must be <strong>100% free of collisions</strong>, meaning a vertex can only be replaced if the newly inserted one has exactly the same values. Please consider that the vertexes are represented by <code>float</code> type. Which means a normal comparison could be misleading.</p>
  
  <p>Edit:</p>
  
  <p>What I'm really doing is reading an STL (stereolithography) binary file. Those familiar with the STL format will know that triangles are written like this in the file:</p>
  
  <pre><code>float normals[3];
  float vertex[3][3];
  uint16_t attributes;
  </code></pre>
  
  <p>That means vertexes will be duplicated more than often when reading the file. I want to normalize vertexes (removing duplicates) and apply links to recreate the triangles.</p>
  
  <p>My goal is mapping all the vertexes in a graph, through <strong>Breadth-first search</strong>. The links, as previously mentioned, are available in the triangles. After getting all the links, the triangles become disposable.</p>
  
  <p>If I don't have a <strong>perfect</strong> mean to remove duplicated vertexes, links will become broken and my mapping will fail.</p>