wangzhengquan
2021-02-05 14c345b38d57fd814f217eb8465963a08ca79f7e
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
#ifndef __HASHTABLE_H__
#define __HASHTABLE_H__
 
#include <sys/queue.h>
#include <functional>
#include <set>
 
#define MAPSIZE 1024
 
// 创建Queue数量的上限
#define QUEUE_COUNT_LIMIT 300
 
typedef struct hashtable_t
{
 struct tailq_header_t* array[MAPSIZE];
 int mutex;
 int queueCount;
 int currentKey; // 当前分配的key
 // int wlock;
 // int cond;
 // size_t readcnt;
 
 
 
} hashtable_t;
typedef void (*hashtable_foreach_cb)(int key, void *value);
 
void hashtable_init(hashtable_t *hashtable);
void hashtable_destroy(hashtable_t *hashtable);
void *hashtable_get(hashtable_t *hashtable, int key);
void hashtable_put(hashtable_t *hashtable, int key, void *value) ;
bool  hashtable_check_put(hashtable_t *hashtable, int key, void *value, bool overwrite) ;
 
void *hashtable_remove(hashtable_t *hashtable, int key);
void hashtable_removeall(hashtable_t *hashtable);
 
int hashtable_lock(hashtable_t *hashtable);
int hashtable_unlock(hashtable_t *hashtable);
 
int hashtable_get_queue_count(hashtable_t *hashtable) ;
/** 
 * 遍历hash_table
 * @demo 
 * hashtable_foreach(&hashtable, [&](int key, void * value){
 *    printf("%d, %p\n", key, value);
 * });
 * 
*/
void hashtable_foreach(hashtable_t *hashtable, std::function<void(int, void *)>  cb);
 
// void hashtable_printall(hashtable_t *hashtable);
 
int hashtable_alloc_key(hashtable_t *hashtable);
 
std::set<int> * hashtable_keyset(hashtable_t *hashtable) ;
#endif