#include <openssl/lhash.h>
#define DATA_SIZE 256
typedef struct _MY_HASH_NODE_
{
long unsigned lHashValue;
char psValue[256];
int nCount;
}MyHashNode;
unsigned long MyHash(const MyHashNode *ptNode)
{
}
int MyCompare(const void *arg1, const void *arg2)
{
MyHashNode *ptNode1 = (MyHashNode *)arg1;
MyHashNode *ptNode2 = (MyHashNode *)arg2;
if(ptNode1->lHashValue == ptNode2->lHashValue)
return EXIT_SUCCESS;
else if(strcmp(ptNode1->psValue,ptNode2->psValue) == 0)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
void MyDoAll(MyHashNode *ptNode)
{
printf("hash: %lu, %s, %d\n", ptNode->lHashValue, ptNode->psValue, ptNode->nCount);
free(ptNode);
}
int MyHashInsert(LHASH *ptHash, char *psData)
{
MyHashNode *ptNode = (MyHashNode *)calloc(1, sizeof(MyHashNode));
if(NULL == ptNode)
{
perror("calloc failed.");
return -1;
}
ptNode->lHashValue = lh_strhash(psData);
strncpy(ptNode->psValue, psData, DATA_SIZE);
MyHashNode *ptRetrieve = lh_retrieve(ptHash, ptNode);
if(NULL == ptRetrieve)
{
lh_insert(ptHash, ptNode);
}
else
{
ptRetrieve->nCount++;
free(ptNode);
}
return 0;
}
static IMPLEMENT_LHASH_HASH_FN(MyHash, const MyHashNode *);
static IMPLEMENT_LHASH_COMP_FN(MyCompare, const MyHashNode *);
static IMPLEMENT_LHASH_DOALL_FN(MyDoAll, MyHashNode *);
int main()
{
LHASH *ptHash = lh_new(LHASH_HASH_FN(MyHash),LHASH_COMP_FN(MyCompare));
MyHashInsert(ptHash, "xuzhh1");
MyHashInsert(ptHash, "hello");
MyHashInsert(ptHash, "hello");
MyHashInsert(ptHash, "hello");
MyHashInsert(ptHash, "hello");
MyHashInsert(ptHash, "hello");
MyHashInsert(ptHash, "hello");
MyHashInsert(ptHash, "world");
lh_doall(ptHash, LHASH_DOALL_FN(MyDoAll));
lh_free(ptHash);
return 0;
}