-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhash.h
62 lines (49 loc) · 1.48 KB
/
hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*!
* @file libxutils/src/data/hash.h
*
* This source is part of "libxutils" project
* 2015-2020 Sun Dro (s.kalatoz@gmail.com)
*
* @brief Implementation of hash tables
*/
#ifndef __XUTILS_HASH_H__
#define __XUTILS_HASH_H__
#ifdef __cplusplus
extern "C" {
#endif
#define XHASH_MIX(num, range) ((num ^ (num >> 8) ^ (num >> 16)) % range)
#include "xstd.h"
#include "list.h"
#ifdef _XUTILS_HASH_MODULES
#define XHASH_MODULES _XUTILS_HASH_MODULES
#else
#define XHASH_MODULES 32
#endif
typedef struct XHashData {
void* pData;
size_t nSize;
int nKey;
} xhash_pair_t;
typedef void(*xhash_clearcb_t)(void *, void*, int);
typedef int(*xhash_itfunc_t)(xhash_pair_t*, void*);
typedef struct XHash {
xhash_clearcb_t clearCb;
void *pUserContext;
size_t nPairCount;
xlist_t tables[XHASH_MODULES];
} xhash_t;
int XHash_Init(xhash_t *pHash, xhash_clearcb_t clearCb, void *pCtx);
void XHash_Iterate(xhash_t *pHash, xhash_itfunc_t itfunc, void *pCtx);
void XHash_Destroy(xhash_t *pHash);
xhash_pair_t* XHash_GetPair(xhash_t *pHash, int nKey);
xlist_t* XHash_GetNode(xhash_t *pHash, int nKey);
void* XHash_GetData(xhash_t *pHash, int nKey);
int XHash_GetSize(xhash_t *pHash, int nKey);
xhash_pair_t* XHash_NewPair(void* pData, size_t nSize, int nKey);
int XHash_InsertPair(xhash_t *pHash, xhash_pair_t *pData);
int XHash_Insert(xhash_t *pHash, void *pData, size_t nSize, int nKey);
int XHash_Delete(xhash_t *pHash, int nKey);
#ifdef __cplusplus
}
#endif
#endif /* __XUTILS_HASH_H__ */