title: Using unordered_map on array of doubles
body: <p>My main data object is a array of doubles of a length that depends on a specific instantiation of my class.  I would like to construct a very simple hash table to store/retrieve these objects, and we can assume that the numbers are generated in a way that is free of numerical error. </p>
  
  <pre><code>int main() {
    std::tr1::unordered_map&lt;double*, double*&gt; cache;
  
    double x1[] = { 1.0, 3.14 };
    double x2[] = { 1.0, 3.14 };
  
    cache[x1] = x1;
  
    std::cout &lt;&lt; "x1: " &lt;&lt; cache.count(x1) &lt;&lt; std::endl;
    std::cout &lt;&lt; "x2: " &lt;&lt; cache.count(x2) &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>The above obviously only compares the pointers, giving the output:</p>
  
  <pre><code>&gt; ./tmp
  x1: 1
  x2: 0
  </code></pre>
  
  <p>When I really want to see:</p>
  
  <pre><code>&gt; ./tmp
  x1: 1
  x2: 1
  </code></pre>
  
  <p>It's pretty clear how to create custom hashing and equality functions when the size of the arrays are <strong>fixed at compile time</strong> but I do not know how to make custom functions that depend on a specific instantiation... I created a class below, but I'm not sure if it's useful, or how it could be used.</p>
  
  <pre><code>class Hash_double_vec {
  
  public:
    int dim;
    Hash_double_vec(int d) { dim = d; }
  
    size_t operator()(const double *x) const{
      std::tr1::hash&lt;double&gt; hash_fn;
      size_t r = hash_fn(x[0]);
      for(int i=1;i&lt;dim;i++) r ^= hash_fn(x[i]);
      return r;
    }
  
    bool operator()(const double *x, const double *y) const{
      for(int i=1;i&lt;dim;i++) if (fabs(x[i]-y[i]) &gt; 1e-10) return false;
      return true;
    }
  };
  </code></pre>