title: Double Hash function resulting in infinite loop
body: <p>I'm working on a data structures assignment and my attempt to increment a double hash function is stuck in an infinite loop.</p>
  
  <p>My book defines a strategy to double hash as</p>
  
  <blockquote>
    <p>h′(k) = q−(k mod q), for some prime number q &lt; N. Also, N
    should be a prime.</p>
  </blockquote>
  
  <p>I've identified that the double hash increment is causing the issue, as switching to linear probing runs fine. </p>
  
  <pre><code>private int findSlot(int h, K k) {
      totalProbes = 0;
      int avail = -1;                               // no slot available (thus far)
      int j = h;                                    // index while scanning table
      do {
          totalProbes++;
          if (totalProbes &gt; maxProbes) maxProbes = totalProbes;
          if (isAvailable(j)) {                       // may be either empty or defunct
              if (avail == -1) avail = j;               // this is the first available slot!
              if (table[j] == null) {
                  break;
              }                                           // if empty, search fails immediately
          } else if (table[j].getKey().equals(k))
              return j;                                 // successful match
         //j = (j + 1) % capacity;                       // keep looking (cyclically)
          j = hashTwo(k); //increment using double hash
      } while (j != h);                             // stop if we return to the start
      return -(avail + 1);                          // search has failed
  }
  
  private int hashTwo(K key) {
      String keyString = key.toString(); //convert generic -&gt; string -&gt; int
      int keyInt = Integer.parseInt(keyString);
      return 7 - (keyInt % 7);
  }
  </code></pre>
  
  <p>There is some ugliness with the hash 2 function - namely converting from a generic to an integer, but besides that it follows the same instructions as the book.</p>