hash实现

来源:互联网 发布:中科院分词系统算法 编辑:程序博客网 时间:2024/06/06 01:06

hash_pub.h

#ifdef __cplusplus#if __cplusplusextern "C"{#endif #endif #ifndef __INC_HASH_PUB_H__#define __INC_HASH_PUB_H__#define  MATCH_NOT_FOUND    0#define  INSERT_PRIORTO     1#define  INSERT_NEXTTO      2#define HASH_NODE_S   DLL_NODE_S #define HASH_BUCKET_S DLL_Stypedef  struct HASH_TABLE_S{         U32             ulHashSize;                U32             (*pInsertFunc)();           U32              NodeNum;               HASH_BUCKET_S  HashList[1];       }HASH_TABLE_S;#define HASH_BUCKET_INIT   DLL_Init#define HASH_ADD_NODE      DLL_Add#define HASH_DELETE_NODE   dll_delete#define HASH_INSERT_NODE   dll_insert#define HASH_FIRST_NODE    DLL_First#define HASH_LAST_NODE     DLL_Last#define HASH_NEXT_NODE     DLL_Next#define HASH_INIT_NODE     DLL_Init_Node#define HASH_SCAN_BUCKET   DLL_Scan#define HASH_Scan_Bucket(pHashTab,ulHashIndex,pNodePtr,TypeCast) \        HASH_SCAN_BUCKET(&(pHashTab)->HashList[(ulHashIndex)],(pNodePtr),TypeCast)#define HASH_Scan_Table(pHashTab,ulHashIndex) \        for((ulHashIndex) = 0; (ulHashIndex) < (pHashTab)->ulHashSize; (ulHashIndex) = (ulHashIndex) +1)#define HASH_Get_First_Bucket_Node(pHashTab,ulHashIndex) \        HASH_FIRST_NODE(&pHashTab->HashList[ulHashIndex])#define HASH_Get_Next_Bucket_Node(pHashTab,ulHashIndex,pNode) \        HASH_NEXT_NODE(&pHashTab->HashList[ulHashIndex],pNode)#define HASH_Bucket_Count(pHashTab,ulHashIndex) \        DLL_Count(&((pHashTab)->HashList[(ulHashIndex)]))#define HASH_Bucket_FreeAll(pHashTab,ulHashIndex,fnFree) \dll_free_all(&((pHashTab)->HashList[(ulHashIndex)]), fnFree)    #define HASH_Init_Node(pNode) HASH_INIT_NODE(pNode)HASH_TABLE_S *hash_create_table (U32 ulHashSize, U32 (*pInsertFunc)());HASH_NODE_S *hash_find_node(HASH_TABLE_S *pHashTab, U32 ulIndex, VOID *pKey, S32 (*fnValCmp)(VOID *, HASH_NODE_S *));VOID hash_add_node (HASH_TABLE_S *pHashTab,HASH_NODE_S *pNode,U32 ulHashIndex,U8 *pu1InsertFuncParam);VOID hash_delete_node (HASH_TABLE_S *pHashTab,HASH_NODE_S *pNode,U32 ulHashIndex);VOID hash_delete_table (HASH_TABLE_S *pHashTab,VOID (*pFreeNodeMemFunc)(VOID *));VOID hash_walk_bucket (HASH_TABLE_S *pHashTab, U32 ulHashIndex, VOID (*fnVisit)(HASH_NODE_S *));VOID hash_walk_table (HASH_TABLE_S *pHashTab, VOID (*fnVisit)(HASH_NODE_S *));VOID hash_free_allbucket (HASH_TABLE_S *pHashTab, VOID  (*pMemFreeFunc)(VOID *));#endif#ifdef __cplusplus#if __cplusplus}#endif #endif 

ssp_hash.c

#include "syscfg.h"#ifdef __cplusplus#if __cplusplusextern "C"{#endif #endif #include "aos.h"#define HASH_MALLOC(ulSize)  aos_smem_alloc( MPE_SYS,SID_HASH,ulSize)#define HASH_FREE(pData)          AOS_ASSERT(0);VOID hash_free_allbucket (HASH_TABLE_S *pHashTab, VOID  (*pMemFreeFunc)(VOID *));HASH_TABLE_S *hash_create_table (U32 ulHashSize, U32 (*pInsertFunc)()){ HASH_TABLE_S *pHashTab; U32           ulHashIndex,ulHashMemSize; ulHashMemSize = sizeof(HASH_TABLE_S)+(ulHashSize-1)*sizeof(HASH_BUCKET_S); pHashTab = (HASH_TABLE_S *)HASH_MALLOC(ulHashMemSize); if(pHashTab != (HASH_TABLE_S *)NULL){    pHashTab->ulHashSize      = ulHashSize;    pHashTab->pInsertFunc      = pInsertFunc;    for(ulHashIndex = 0; ulHashIndex < ulHashSize; ulHashIndex++){        HASH_BUCKET_INIT(&pHashTab->HashList[ulHashIndex]);    } } return(pHashTab);}HASH_NODE_S *hash_find_node(HASH_TABLE_S *pHashTab, U32 ulIndex, VOID *pKey, S32 (*fnValCmp)(VOID *, HASH_NODE_S *)){HASH_NODE_S *pNode;HASH_Scan_Bucket(pHashTab, ulIndex, pNode, HASH_NODE_S *){if (!fnValCmp(pKey, pNode))return pNode;}return NULL;}VOID hash_add_node (HASH_TABLE_S *pHashTab, HASH_NODE_S *pNode, U32 ulHashIndex, U8 *pu1InsertFuncParam){ U8 u1Found = FALSE; HASH_NODE_S *pTmpNodePtr; HASH_NODE_S *pPrevNodePtr = NULL; S32   i4InsertPos = MATCH_NOT_FOUND; if(pHashTab->pInsertFunc == NULL){HASH_ADD_NODE(&pHashTab->HashList[ulHashIndex],pNode); } else{    HASH_Scan_Bucket(pHashTab,ulHashIndex,pTmpNodePtr,HASH_NODE_S *){       i4InsertPos= (S32)(*pHashTab->pInsertFunc)(pTmpNodePtr,pu1InsertFuncParam);       if ((i4InsertPos == INSERT_NEXTTO) ||             (i4InsertPos == INSERT_PRIORTO)) {          u1Found = TRUE;          break;       }       pPrevNodePtr = pTmpNodePtr;    }    if(u1Found == FALSE) pTmpNodePtr = HASH_LAST_NODE(&pHashTab->HashList[ulHashIndex]);            if (i4InsertPos == INSERT_PRIORTO) {       if (pPrevNodePtr)          HASH_INSERT_NODE(&pHashTab->HashList[ulHashIndex], pPrevNodePtr, pNode);       else            HASH_INSERT_NODE(&pHashTab->HashList[ulHashIndex], NULL, pNode);    }     else           HASH_INSERT_NODE(&pHashTab->HashList[ulHashIndex], pTmpNodePtr, pNode); }}VOID hash_delete_node (HASH_TABLE_S *pHashTab, HASH_NODE_S  *pNode, U32 ulHashIndex) { HASH_DELETE_NODE(&pHashTab->HashList[ulHashIndex],pNode);}VOID hash_free_allbucket (HASH_TABLE_S *pHashTab, VOID  (*pMemFreeFunc)(VOID *)){ U32           ulHashIndex;  HASH_Scan_Table(pHashTab,ulHashIndex){   HASH_Bucket_FreeAll(pHashTab, ulHashIndex, pMemFreeFunc); }}VOID hash_delete_table (HASH_TABLE_S *pHashTab, VOID  (*pMemFreeFunc)(VOID *)){hash_free_allbucket(pHashTab, pMemFreeFunc);HASH_FREE(pHashTab);}VOID hash_walk_bucket (HASH_TABLE_S *pHashTab, U32 ulHashIndex, VOID (*fnVisit)(HASH_NODE_S *)){HASH_NODE_S *pNode;HASH_Scan_Bucket(pHashTab, ulHashIndex, pNode, HASH_NODE_S *){fnVisit(pNode);}}VOID hash_walk_table (HASH_TABLE_S *pHashTab, VOID (*fnVisit)(HASH_NODE_S *)){U32 ulHashIndex;HASH_NODE_S *pNode;HASH_Scan_Table(pHashTab, ulHashIndex){HASH_Scan_Bucket(pHashTab, ulHashIndex, pNode, HASH_NODE_S *){fnVisit(pNode);}}}#ifdef __cplusplus#if __cplusplus}#endif #endif 
原创粉丝点击