Reputation: 133
I am creating a hash table and need to have this chained hash table tested with different hash functions. I have hash structs such as
struct Hasher {
virtual int hash(std::string s, int N) = 0;
};
struct SumHasher : Hasher {
int hash(std::string s, int N){
int result = 0;
for (int i=0; i<s.size(); ++i)
result += s[i];
return int (std::abs((int)result)) % N;
}
};
struct ProdHasher : Hasher {
int hash(std::string s, int N) {
int result = 1;
for (int i=0; i<s.size(); ++i)
result *= s[i];
return int (std::abs((int)result)) % N;
}
};
struct ShiftHasher : Hasher {
int hash(std::string s, int N){
const int shift = 6; unsigned z = 0;
const int mask = ~z >> (32-shift); // lower 6 bits on
int result = 0;
for (int i = 0; i < s.length(); i++)
result = (result << shift) | (s[i] & mask);
return int (std::abs((int)result)) % N;
}
};
Now how can I use this functions within the Hashtable class by creating a struct hash type then passing that object to the constructor
class ChainedHashTable
{
ListNode **T; // array of linked lists
int capacity;
public:
Hasher *myhash;
int info;
ChainedHashTable(int numberOfChains, Hasher *myHasher){
myhash = hasher;
capacity = numberOfChains;
T = new ListNode* [capacity];
for (int i=0; i<capacity; ++i)
T[i] = NULL;
}
.......
void ChainedHashTable::insert(std::string key, int info){
int h = myhash::hash(key, capacity);
T[h] = ListNode::make(key, info, T[h]);
}
Upvotes: 0
Views: 79