Double hashing issue.

FindPos function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
static const int R_VALUE = 55603;
template <typename HashedObj>
int DoubleHashTable<HashedObj>::FindPos(const HashedObj & x) {
  unsigned int current_pos = InternalHash(x);
  
  while (array_[current_pos].info_ != kEmpty && array_[current_pos].element_ != x ) {
    // Compute ith probe.
    current_pos = R_VALUE - (current_pos % R_VALUE);
    ++collisions_; //increment collisions because we hit a non-empty bucket
    if (current_pos >= array_.size()) {
      current_pos -= array_.size( );
    }
  }
  probe_count_ = current_pos; //probes used
  return current_pos;
}


Insert function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename HashedObj>
bool DoubleHashTable<HashedObj>::Insert(const HashedObj & x) {
  // Insert x as active.
  int current_pos = FindPos(x);
  if (IsActive(current_pos)) {
    return false;
  }
  
  array_[current_pos].element_ = x;
  array_[current_pos].info_ = kActive; //flag the bucket as "active" (occupied)
  
  if (++current_size_ > array_.size() / 2) {
    Rehash();
  }
  
  return true;
}


The above is my FindPos function for my hash table. Its purpose is to find a position in the hash table that isn't occupied.

1) InternalHash calculates the initial hash value (current_pos).
2) If the initial hash value's spot is empty, it will insert it. Else, the while loop runs until an empty spot is found.
3) In the while loop, my double hash implementation is "current_pos = R_VALUE - (current_pos % R_VALUE")" (a formula that I took from my textbook).
4) The function returns current_pos, which is then used in my Insert function. My table rehashes if it gets close to fullness. array_ (of type vector) is where the contents are being stored.

Now, the issue. My R_VALUE is equal to 55,603, which is one prime less than my table size (55,609). When I execute my program, it crashes.

Is my above implementation wrong/missing something? Is my R_VALUE too big?
Last edited on
Run it under a debugger. Look at the values of variables at the point of crashing.
Topic archived. No new replies allowed.