*/
#include "system.h"
+#include <stdio.h>
#include "debug.h"
#define Bucket JOIN(HASHTYPE,Buket)
* Find entry in hash table.
* @param ht pointer to hash table
* @param key pointer to key value
+ * @param keyHash key hash
* @return pointer to hash bucket of key (or NULL)
*/
static
-Bucket HASHPREFIX(findEntry)(HASHTYPE ht, HTKEYTYPE key)
+Bucket HASHPREFIX(findEntry)(HASHTYPE ht, HTKEYTYPE key, unsigned int keyHash)
{
- unsigned int hash;
- Bucket b;
-
- hash = ht->fn(key) % ht->numBuckets;
- b = ht->buckets[hash];
+ unsigned int hash = keyHash % ht->numBuckets;
+ Bucket b = ht->buckets[hash];
while (b && ht->eq(b->key, key))
b = b->next;
return ht;
}
-static HASHPREFIX(Resize)(HASHTYPE ht, int numBuckets) {
+static void HASHPREFIX(Resize)(HASHTYPE ht, int numBuckets) {
Bucket * buckets = xcalloc(numBuckets, sizeof(*ht->buckets));
for (int i=0; i<ht->numBuckets; i++) {
ht->numBuckets = numBuckets;
}
-void HASHPREFIX(AddEntry)(HASHTYPE ht, HTKEYTYPE key
+unsigned int HASHPREFIX(KeyHash)(HASHTYPE ht, HTKEYTYPE key)
+{
+ return ht->fn(key);
+}
+
+void HASHPREFIX(AddHEntry)(HASHTYPE ht, HTKEYTYPE key, unsigned int keyHash
#ifdef HTDATATYPE
, HTDATATYPE data
#endif
)
{
- unsigned int hash;
- Bucket b;
- Bucket * b_addr;
-
- hash = ht->fn(key) % ht->numBuckets;
- b = ht->buckets[hash];
- b_addr = ht->buckets + hash;
+ unsigned int hash = keyHash % ht->numBuckets;
+ Bucket b = ht->buckets[hash];
+#ifdef HTDATATYPE
+ Bucket * b_addr = ht->buckets + hash;
+#endif
if (b == NULL) {
ht->bucketCount += 1;
}
while (b && ht->eq(b->key, key)) {
+#ifdef HTDATATYPE
b_addr = &(b->next);
+#endif
b = b->next;
}
}
}
+void HASHPREFIX(AddEntry)(HASHTYPE ht, HTKEYTYPE key
+#ifdef HTDATATYPE
+, HTDATATYPE data
+#endif
+)
+{
+#ifdef HTDATATYPE
+ HASHPREFIX(AddHEntry)(ht, key, ht->fn(key), data);
+#else
+ HASHPREFIX(AddHEntry)(ht, key, ht->fn(key));
+#endif
+}
+
void HASHPREFIX(Empty)( HASHTYPE ht)
{
Bucket b, n;
return NULL;
}
-int HASHPREFIX(HasEntry)(HASHTYPE ht, HTKEYTYPE key)
+int HASHPREFIX(HasHEntry)(HASHTYPE ht, HTKEYTYPE key, unsigned int keyHash)
{
Bucket b;
- if (!(b = HASHPREFIX(findEntry)(ht, key))) return 0; else return 1;
+ if (!(b = HASHPREFIX(findEntry)(ht, key, keyHash))) return 0; else return 1;
}
-#ifdef HTDATATYPE
+int HASHPREFIX(HasEntry)(HASHTYPE ht, HTKEYTYPE key)
+{
+ return HASHPREFIX(HasHEntry)(ht, key, ht->fn(key));
+}
-int HASHPREFIX(GetEntry)(HASHTYPE ht, HTKEYTYPE key, HTDATATYPE** data,
- int * dataCount, HTKEYTYPE* tableKey)
+int HASHPREFIX(GetHEntry)(HASHTYPE ht, HTKEYTYPE key, unsigned int keyHash,
+#ifdef HTDATATYPE
+ HTDATATYPE** data, int * dataCount,
+#endif
+ HTKEYTYPE* tableKey)
{
Bucket b;
- int rc = ((b = HASHPREFIX(findEntry)(ht, key)) != NULL);
+ int rc = ((b = HASHPREFIX(findEntry)(ht, key, keyHash)) != NULL);
+#ifdef HTDATATYPE
if (data)
*data = rc ? b->data : NULL;
if (dataCount)
*dataCount = rc ? b->dataCount : 0;
+#endif
if (tableKey && rc)
*tableKey = b->key;
return rc;
}
+int HASHPREFIX(GetEntry)(HASHTYPE ht, HTKEYTYPE key,
+#ifdef HTDATATYPE
+ HTDATATYPE** data, int * dataCount,
#endif
-
+ HTKEYTYPE* tableKey)
+{
+ return HASHPREFIX(GetHEntry)(ht, key, ht->fn(key),
+#ifdef HTDATATYPE
+ data, dataCount,
+#endif
+ tableKey);
+}
unsigned int HASHPREFIX(NumBuckets)(HASHTYPE ht) {
return ht->numBuckets;