1 2 3 4 5 6 7 8 9 10 11 12 13
|
using namespace std;
template <class HASH, class intT>
class Table {
private:
typedef typename HASH::eType eType;
typedef typename HASH::kType kType;
intT m;
intT mask;
eType empty;
HASH hashStruct;
eType* TA;
intT* compactL;
|
This is simple definition of the class ( only some part of it), the problem is that data member defined above
HASH hashStruct has called some functions where as it is a generic data member only, for example within a class it is used to call a function like
1 2 3 4
|
intT firstIndex(kType v) {
return hashToRange(hashStruct.hash(v));
}
|
Here hashStruct has called a function within the class Table , but the function called are described in the three structures which are given below( which are defined outside the definition of class)
3 different places below in the code are the structures containing the functions which are called by
hashStruct, which are as follows:
1:
1 2 3 4 5 6 7 8 9 10
|
template <class intT>
struct hashInt {
typedef intT eType;
typedef intT kType;
eType empty() {return -1;}
kType getKey(eType v) {return v;}
uintT hash(kType v) {return utils::hash(v);}
int cmp(kType v, kType b) {return (v > b) ? 1 : ((v == b) ? 0 : -1);}
bool replaceQ(eType v, eType b) {return 0;}
};
|
2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
struct hashStr {
typedef char* eType;
typedef char* kType;
eType empty() {return NULL;}
kType getKey(eType v) {return v;}
uintT hash(kType s) {
uintT hash = 0;
while (*s) hash = *s++ + (hash << 6) + (hash << 16) - hash;
return hash;
}
int cmp(kType s, kType s2) {
while (*s && *s==*s2) {s++; s2++;};
return (*s > *s2) ? 1 : ((*s == *s2) ? 0 : -1);
}
bool replaceQ(eType s, eType s2) {return 0;}
};
|
3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
template <class KEYHASH, class DTYPE>
struct hashPair {
KEYHASH keyHash;
typedef typename KEYHASH::kType kType;
typedef pair<kType,DTYPE>* eType;
eType empty() {return NULL;}
hashPair(KEYHASH _k) : keyHash(_k) {}
kType getKey(eType v) { return v->first; }
uintT hash(kType s) { return keyHash.hash(s);}
int cmp(kType s, kType s2) { return keyHash.cmp(s, s2);}
bool replaceQ(eType s, eType s2) {
return s->second > s2->second;}
};
|
These all functions are called at different places by
hashStruct in the code, I have highlighted only one in the start.
I want to know some sound explanation how it is able to call functions, the logic behind it , the all code is placed in the header file. Because as far as I know the objects of class can call functions through
"." operator
How can generic data member.