Tangleman
Tangleman

Reputation: 133

Use function from another class or struct in a function within this class

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

Answers (1)

Dinesh
Dinesh

Reputation: 2204

you should use:

myhash->hash(key, capacity)

Upvotes: 1

Related Questions