Line 101: you leave num_entries uninitialized
Line 110: you set num_entries here, but the call to put() at line 114 increments it too.
Table::put(unsigned int key, std::string data) always increments num_entries, but it also calls put(Entry) which sometimes increments num_entries.
Yuck. My advice is to decide who will increment num_entries and then fix to code to keep with your policy.
put(Entry): Suppose the hash bucket is not empty, but the Entry isn't in there. In that case you will never insert it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void Table::put(Entry e) {
if (!A[hashkey(e.get_key())].empty()) {
// if (find(e) != 0) {
for(int i = 0; i < A[hashkey(e.get_key())].size(); i++) {
if(A[hashkey(e.get_key())][i].get_key() == e.get_key()){
A[hashkey(e.get_key())][i] = e;
return;
}
}
}
else {
A[hashkey(e.get_key())].push_back(e);
num_entries++;
}
}
|
The underlined value doesn't change so do yourself a favor and just put it in a local variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void Table::put(Entry e) {
unsigned int hash = hashkey(e.get_key());
if (!A[hash].empty()) {
// if (find(e) != 0) {
for(int i = 0; i < A[hash].size(); i++) {
if(A[hash][i].get_key() == e.get_key()){
A[hash][i] = e;
return;
}
}
}
else {
A[hash].push_back(e);
num_entries++;
}
}
|
The same comment applies in other places too.
Lines 160-167: The first time through the loop, the function will return at line 162, or it will return at line 165. What if the value you seek is the second one in the hash bucket?
Your remove() and find() functions seem to have the same problem: a loop that contains an if/else statement that returns in both branches. So the loop only ever executes once.