forked from clibs/hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.h
More file actions
106 lines (78 loc) · 1.59 KB
/
hash.h
File metadata and controls
106 lines (78 loc) · 1.59 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// hash.h
//
// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
//
#ifndef HASH
#define HASH
#include "khash.h"
// pointer hash
KHASH_MAP_INIT_STR(ptr, void *);
/*
* Hash type.
*/
typedef khash_t(ptr) hash_t;
/*
* Allocate a new hash.
*/
#define hash_new() kh_init(ptr)
/*
* Destroy the hash.
*/
#define hash_free(self) kh_destroy(ptr, self)
/*
* Hash size.
*/
#define hash_size kh_size
/*
* Remove all pairs in the hash.
*/
#define hash_clear(self) kh_clear(ptr, self)
/*
* Iterate hash keys and ptrs, populating
* `key` and `val`.
*/
#define hash_each(self, block) { \
const char *key; \
void *val; \
for (khiter_t k = kh_begin(self); k < kh_end(self); ++k) { \
if (!kh_exist(self, k)) continue; \
key = kh_key(self, k); \
val = kh_value(self, k); \
block; \
} \
}
/*
* Iterate hash keys, populating `key`.
*/
#define hash_each_key(self, block) { \
const char *key; \
for (khiter_t k = kh_begin(self); k < kh_end(self); ++k) { \
if (!kh_exist(self, k)) continue; \
key = kh_key(self, k); \
block; \
} \
}
/*
* Iterate hash ptrs, populating `val`.
*/
#define hash_each_val(self, block) { \
void *val; \
for (khiter_t k = kh_begin(self); k < kh_end(self); ++k) { \
if (!kh_exist(self, k)) continue; \
val = kh_value(self, k); \
block; \
} \
}
// protos
void
hash_set(hash_t *self, char *key, void *val);
void *
hash_get(hash_t *self, char *key);
int
hash_has(hash_t *self, char *key);
void
hash_del(hash_t *self, char *key);
void
hash_clear(hash_t *self);
#endif /* HASH */