example of a hash - superfulous code
<p>I found this snippet <a href="http://www.algolist.net/Data_structures/Hash_table/Simple_example" rel="nofollow">here</a> as an example of the simplest kind of hash. It kind of makes since..basically use the modulus operator to reduce a data set from DATA_SIZE(implied) to TABLE_SIZE(defined).</p>

<p>However, if a collision occurs, i.e. the array element is already taken, it moves to </p>

<pre><code>hash = (hash + 1) % TABLE_SIZE
</code></pre>

<p>But if you already did a modulus using TABLE SIZE this line will always produce (hash+1) b.c. x % y always equals x when x &lt; y.  Question: Is this a bug...I mean it should work but the % TABLE_SIZE is extra right?  It does nothing in the code below.</p>

<pre><code>const int TABLE_SIZE = 128;

class HashEntry 
  {
  private:
    int key;
    int value;
  public:
    HashEntry(int key, int value) 
      {
      this-&gt;key = key;
      this-&gt;value = value;
      }
    int getKey() 
      {
      return key;
      }
    int getValue() 
      {
      return value;
      }
  }; 

class HashMap 
  {
  private:
    HashEntry **table;
  public:
    HashMap() 
      {
      table = new HashEntry*[TABLE_SIZE];
      for (int i = 0; i &lt; TABLE_SIZE; i++) table[i] = NULL;
      }
    int get(int key) 
      {
      int hash = (key % TABLE_SIZE);
      while (table[hash] != NULL &amp;&amp; table[hash]-&gt;getKey() != key) hash = (hash + 1) % TABLE_SIZE;
      if (table[hash] == NULL)return -1;
      else return table[hash]-&gt;getValue();
      }
    void put(int key, int value) 
      {
      int hash = (key % TABLE_SIZE);
      while (table[hash] != NULL &amp;&amp; table[hash]-&gt;getKey() != key) hash = (hash + 1) % TABLE_SIZE;
      if (table[hash] != NULL) delete table[hash];
      table[hash] = new HashEntry(key, value);
      }     
    ~HashMap() 
      {
      for (int i = 0; i &lt; TABLE_SIZE; i++)
         if (table[i] != NULL) delete table[i];
      delete[] table;
      }
  };
</code></pre>
