I am currently working on a project in C that involves writing polymorphic data structures. I have already created a static hash table which just takes a void pointer to data void *data as input for insert, lookup and delete functions. In order to hash the data I have a function pointer typedef int (*hash_t)(size_t, void*); as a parameter when creating a new table. Initially I used the hash table to build a graph where it didn't really make sense to separate key and value, with a hash function like this:
int
hash_data(size_t size, void *graph_node) {
return ((graph_node_t*)node)->id % size;
}
But now I was looking at reusing the hash table to record the frequencies of integers in a text file where <key, value> approach would make a lot more sense, so I'm trying to build a dictionary data structure which builds off the hash table by having a <key, value> struct:
typedef struct {
void *key;
void *value;
} key_value_t;
Where once again I'd have a function pointer typedef int (*hash_t)(size_t, void*); as a parameter when creating a new dictionary, where ideally I'd pass a function pointer to something like this to my hashtable:
int
hash_key_value(size_t size, void *key_value) {
return hash_key(size, ((pair_t*)pair)->key);
}
The problem is I'm not sure how to have a function like this where it itself calls another function pointer, being called from within my hash table insert and lookup functions.
I have tried having the function pointer as a parameter such as:
int
hash_pair(size_t size, void *pair, hash_key_t hash_key) {
return hash_key(size, ((pair_t*)pair)->key);
}
but this doesn't seem to be helpful as it requires the my hash code has a pointer to my hashKey function which is provided by code two layers less abstract.
I've considered having a 'fill in the blank' kind of function in my dictionary.c file like:
int
hash_key(size_t size, void *key) {
/* some code */
}
But this seems less elegant than allowing the dictionary to be used without modifying the file at all, and is inconsistent with the function pointer approach I have already used.
Another alternative would be to make the dictionary only allow integer keys as perhaps I'm taking too much of an OOP approach to the problem.