14# define ZIP_INLINE __inline
16# define ZIP_INLINE inline
19#if defined(__GNUC__) || defined(__clang__)
20# define ZIP_UNUSED __attribute__((unused))
44#define ZIP_HEAD(name, type) \
49#define ZIP_ENTRY(type) \
68#define ZIP_INIT(head) do { (head)->root = NULL; } while (0)
69#define ZIP_ROOT(head) (head)->root
70#define ZIP_LEFT(elm, field) (elm)->field.left
71#define ZIP_RIGHT(elm, field) (elm)->field.right
72#define ZIP_INSERT(name, head, elm) name##_ZIP_INSERT(head, elm)
73#define ZIP_FIND(name, head, key) name##_ZIP_FIND(head, key)
74#define ZIP_MIN(name, head) name##_ZIP_MIN(head)
75#define ZIP_MAX(name, head) name##_ZIP_MAX(head)
78#define ZIP_REMOVE(name, head, elm) name##_ZIP_REMOVE(head, elm)
82#define ZIP_ZIP(name, left, right) name##_ZIP_ZIP(left, right)
83#define ZIP_UNZIP(name, head, key, left, right) \
84 name##_ZIP_UNZIP(head, key, left, right)
93typedef void * (*zip_iter_cb)(
void *context,
void *elm);
94#define ZIP_ITER(name, head, cb, ctx) name##_ZIP_ITER(head, cb, ctx)
97#define ZIP_ITER_KEY(name, head, key, cb, ctx) name##_ZIP_ITER_KEY(head, key, cb, ctx)
100#define ZIP_FUNCTIONS(name, type, field, keytype, keyfield, cmp) \
102ZIP_UNUSED static ZIP_INLINE void \
103name##_ZIP_INSERT(struct name *head, struct type *el) { \
104 __ZIP_INSERT(head, (zip_cmp_cb)cmp, offsetof(struct type, field), \
105 offsetof(struct type, keyfield), el); \
108ZIP_UNUSED static ZIP_INLINE struct type * \
109name##_ZIP_REMOVE(struct name *head, struct type *elm) { \
110 return (struct type*) \
111 __ZIP_REMOVE(head, (zip_cmp_cb)cmp, \
112 offsetof(struct type, field), \
113 offsetof(struct type, keyfield), elm); \
116ZIP_UNUSED static ZIP_INLINE struct type * \
117name##_ZIP_FIND(struct name *head, const keytype *key) { \
118 struct type *cur = ZIP_ROOT(head); \
120 enum ZIP_CMP eq = cmp(key, &cur->keyfield); \
121 if(eq == ZIP_CMP_EQ) \
123 if(eq == ZIP_CMP_LESS) \
124 cur = ZIP_LEFT(cur, field); \
126 cur = ZIP_RIGHT(cur, field); \
131ZIP_UNUSED static ZIP_INLINE struct type * \
132name##_ZIP_MIN(struct name *head) { \
133 struct type *cur = ZIP_ROOT(head); \
136 while(ZIP_LEFT(cur, field)) { \
137 cur = ZIP_LEFT(cur, field); \
142ZIP_UNUSED static ZIP_INLINE struct type * \
143name##_ZIP_MAX(struct name *head) { \
144 struct type *cur = ZIP_ROOT(head); \
147 while(ZIP_RIGHT(cur, field)) { \
148 cur = ZIP_RIGHT(cur, field); \
153typedef void * (*name##_cb)(void *context, struct type *elm); \
155ZIP_UNUSED static ZIP_INLINE void * \
156name##_ZIP_ITER(struct name *head, name##_cb cb, void *context) { \
157 return __ZIP_ITER(offsetof(struct type, field), (zip_iter_cb)cb, \
158 context, ZIP_ROOT(head)); \
161ZIP_UNUSED static ZIP_INLINE void * \
162name##_ZIP_ITER_KEY(struct name *head, const keytype *key, \
163 name##_cb cb, void *context) { \
164 return __ZIP_ITER_KEY((zip_cmp_cb)cmp, offsetof(struct type, field), \
165 offsetof(struct type, keyfield), key, \
166 (zip_iter_cb)cb, context, ZIP_ROOT(head)); \
169ZIP_UNUSED static ZIP_INLINE struct type * \
170name##_ZIP_ZIP(struct type *left, struct type *right) { \
171 return (struct type*) \
172 __ZIP_ZIP(offsetof(struct type, field), left, right); \
175ZIP_UNUSED static ZIP_INLINE void \
176name##_ZIP_UNZIP(struct name *head, const keytype *key, \
177 struct name *left, struct name *right) { \
178 __ZIP_UNZIP((zip_cmp_cb)cmp, offsetof(struct type, field), \
179 offsetof(struct type, keyfield), key, \
180 head, left, right); \
187 unsigned short keyoffset,
void *elm);
191 unsigned short keyoffset,
void *elm);
195 void *context,
void *elm);
199 unsigned short keyoffset,
const void *key,
203__ZIP_ZIP(
unsigned short fieldoffset,
void *left,
void *right);
207 unsigned short keyoffset,
const void *key,
208 void *h,
void *l,
void *r);
void * __ZIP_ITER(unsigned short fieldoffset, zip_iter_cb cb, void *context, void *elm)
enum ZIP_CMP(* zip_cmp_cb)(const void *key1, const void *key2)
The comparison method "cmp" for a zip tree has the signature.
void __ZIP_INSERT(void *h, zip_cmp_cb cmp, unsigned short fieldoffset, unsigned short keyoffset, void *elm)
Internal definitions.
void __ZIP_UNZIP(zip_cmp_cb cmp, unsigned short fieldoffset, unsigned short keyoffset, const void *key, void *h, void *l, void *r)
void * __ZIP_ZIP(unsigned short fieldoffset, void *left, void *right)
void * __ZIP_ITER_KEY(zip_cmp_cb cmp, unsigned short fieldoffset, unsigned short keyoffset, const void *key, zip_iter_cb cb, void *context, void *elm)
void *(* zip_iter_cb)(void *context, void *elm)
ZIP_ITER uses in-order traversal of the tree (in the order of the keys).
void * __ZIP_REMOVE(void *h, zip_cmp_cb cmp, unsigned short fieldoffset, unsigned short keyoffset, void *elm)