1998-12-27 00:33:51 +08:00
|
|
|
#ifndef H_HASH
|
|
|
|
#define H_HASH
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
/** \file lib/hash.h
|
2000-08-28 03:27:03 +08:00
|
|
|
* Hash table implemenation.
|
2000-03-23 23:49:50 +08:00
|
|
|
*/
|
|
|
|
|
1998-12-27 00:33:51 +08:00
|
|
|
typedef struct hashTable_s * hashTable;
|
|
|
|
|
1999-07-14 05:37:57 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
/** */
|
2000-08-23 20:59:48 +08:00
|
|
|
typedef unsigned int (*hashFunctionType) (const void * string) /*@*/;
|
2000-03-23 23:49:50 +08:00
|
|
|
/** */
|
2000-08-23 20:59:48 +08:00
|
|
|
typedef int (*hashEqualityType) (const void * key1, const void * key2) /*@*/;
|
1998-12-27 02:31:45 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
/**
|
|
|
|
* Return hash value of a string
|
|
|
|
* @param string string on which to calculate hash value
|
|
|
|
* @return hash value
|
|
|
|
*/
|
2000-08-23 20:59:48 +08:00
|
|
|
unsigned int hashFunctionString(const void * string) /*@*/;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two hash table entries for equality.
|
|
|
|
* @param key1 entry 1
|
|
|
|
* @param key2 entry 2
|
|
|
|
* @return 0 if entries are equal
|
|
|
|
*/
|
2000-08-23 20:59:48 +08:00
|
|
|
int hashEqualityString(const void * key1, const void * key2) /*@*/;
|
1998-12-27 02:31:45 +08:00
|
|
|
|
2000-03-23 23:49:50 +08:00
|
|
|
/**
|
|
|
|
* Create hash table.
|
|
|
|
* If keySize > 0, the key is duplicated within the table (which costs
|
|
|
|
* memory, but may be useful anyway.
|
|
|
|
* @param numBuckets number of hash buckets
|
|
|
|
* @param keySize size of key (0 if unknown)
|
|
|
|
* @param freeData should data be freed when table is destroyed?
|
|
|
|
* @param fn function to generate hash value for key
|
|
|
|
* @param eq function to compare hash keys for equality
|
|
|
|
* @return pointer to initialized hash table
|
|
|
|
*/
|
1999-10-30 00:06:01 +08:00
|
|
|
hashTable htCreate(int numBuckets, int keySize, int freeData,
|
2000-08-23 20:59:48 +08:00
|
|
|
hashFunctionType fn, hashEqualityType eq) /*@*/;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy hash table.
|
|
|
|
* @param ht pointer to hash table
|
|
|
|
*/
|
1999-10-05 04:18:48 +08:00
|
|
|
void htFree( /*@only@*/ hashTable ht);
|
2000-03-23 23:49:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add item to hash table.
|
|
|
|
* @param ht pointer to hash table
|
|
|
|
* @param key pointer to key
|
|
|
|
* @param data pointer to data value
|
|
|
|
*/
|
2000-08-23 20:59:48 +08:00
|
|
|
void htAddEntry(hashTable ht, /*@owned@*/ const void * key,
|
|
|
|
/*@owned@*/ const void * data) /*@modifies ht */;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve item from hash table.
|
|
|
|
* @param ht pointer to hash table
|
|
|
|
* @param key pointer to key value
|
|
|
|
* @retval data address to store data value from bucket
|
|
|
|
* @retval dataCount address to store data value size from bucket
|
|
|
|
* @retval tableKey address to store key value from bucket (may be NULL)
|
|
|
|
* @return 0 on success, 1 if the item is not found.
|
|
|
|
*/
|
1999-10-05 04:18:48 +08:00
|
|
|
int htGetEntry(hashTable ht, const void * key, /*@out@*/ const void *** data,
|
2000-08-23 20:59:48 +08:00
|
|
|
/*@out@*/ int * dataCount, /*@out@*/ const void ** tableKey)
|
|
|
|
/*@modifies *data, *dataCount, *tableKey @*/;
|
2000-03-23 23:49:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for key in hash table.
|
|
|
|
* @param ht pointer to hash table
|
|
|
|
* @param key pointer to key value
|
|
|
|
* @return 1 if the key is present, 0 otherwise
|
|
|
|
*/
|
2000-08-23 20:59:48 +08:00
|
|
|
int htHasEntry(hashTable ht, const void * key) /*@*/;
|
1998-12-27 00:33:51 +08:00
|
|
|
|
1999-07-14 05:37:57 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-12-27 00:33:51 +08:00
|
|
|
#endif
|