Macro implementation of universal hash storage method with quick access.
The class is designed to provide hash organized array of items. All items having the same hash value (integer) are stored in unordered manner with linear search, so it's better to increase hash base to improve access performance in case you are planning to store great number of items in the hash.
Specification:
typedef struct
{
#define AnyType your-item-type
#define AnyHash your-hash-class
#define AnyKeyType your-custom-type-for-key
#define AnyItem2Key(x) extract-key-from-item
#define AnyKey2Hash(k) convert-key-to-number
#define AnyKeyCmp(k,k_) compare-two-keys
#include <mix/AnyHash.H>
Example 1:
typedef struct
{
int val;
float data;
} Record;
#define AnyType Record
#define AnyHash RecordHash
#define AnyKeyType int
#define AnyItem2Key(x) (x).val
#define AnyKey2Hash(k) (k)
#define AnyKeyCmp(k,k_) COMPAR0((k)-(k_))
#include <mix/AnyHash.H>
Example 2:
class Record
{
private:
char *name;
float data;
public:
const char* key () const {
return name;
}
static int hash (const char* s) {
int h = 0, i = 0;
while(s[i] != '\0')
h += s[i++];
return h;
}
};
#define AnyType Record
#define AnyHash RecordHash
#define AnyKeyType const char*
#define AnyItem2Key(x) (x).key()
#define AnyKey2Hash(k) Record::hash(k)
#define AnyKeyCmp(k,k_) strcmp((k),(k_))
#include <mix/AnyHash.H>