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
|
#ifndef BTPD_HASHTABLE_H
#define BTPD_HASHTABLE_H
struct htbl_iter {
struct _htbl *tbl;
size_t bi;
size_t cnt;
struct _any *obj;
};
struct _htbl *_htbl_create(int (*equal)(const void *, const void *),
uint32_t (*hash)(const void *), size_t keyoff, size_t chainoff);
void _htbl_free(struct _htbl *tbl);
void _htbl_insert(struct _htbl *tbl, struct _any *o);
struct _any *_htbl_remove(struct _htbl *tbl, const void *key);
struct _any *_htbl_find(struct _htbl *tbl, const void *key);
void _htbl_tov(struct _htbl *tb, struct _any **v);
size_t _htbl_size(struct _htbl *tbl);
void _htbl_iter_init(struct _htbl *tbl, struct htbl_iter *it);
struct _any *_htbl_iter_next(struct htbl_iter *it);
#define HTBL_ENTRY(name) struct _any *name
#define HTBL_TYPE(name, type, ktype, kname, cname) \
__attribute__((always_inline)) static inline struct name * \
name##_create(int (*equal)(const void *, const void *), \
uint32_t (*hash)(const void *)) \
{ \
return (struct name *) \
_htbl_create(equal, hash, offsetof(struct type, kname), \
offsetof(struct type, cname)); \
} \
\
__attribute__((always_inline)) static inline struct type * \
name##_find(struct name *tbl, const ktype *key) \
{ \
return (struct type *)_htbl_find((struct _htbl *)tbl, key); \
} \
\
__attribute__((always_inline)) static inline struct type * \
name##_remove(struct name *tbl, const ktype *key) \
{ \
return (struct type *)_htbl_remove((struct _htbl *)tbl, key); \
} \
\
__attribute__((always_inline)) static inline void \
name##_free(struct name *tbl) \
{ \
_htbl_free((struct _htbl *)tbl); \
} \
\
__attribute__((always_inline)) static inline void \
name##_insert(struct name *tbl, struct type *o) \
{ \
_htbl_insert((struct _htbl *)tbl, (struct _any *)o); \
} \
__attribute__((always_inline)) static inline void \
name##_tov(struct name *tbl, struct type **v) \
{ \
_htbl_tov((struct _htbl *)tbl, (struct _any **)v); \
} \
\
__attribute__((always_inline)) static inline size_t \
name##_size(struct name *tbl) \
{ \
return _htbl_size((struct _htbl *)tbl); \
} \
\
__attribute__((always_inline)) static inline void \
name##_iter_init(struct name *tbl, struct htbl_iter *it) \
{ \
_htbl_iter_init((struct _htbl *)tbl, it); \
} \
\
__attribute__((always_inline)) static inline struct type * \
name##_iter_next(struct htbl_iter *it) \
{ \
return (struct type *)_htbl_iter_next(it); \
}
#endif
|