libsapling  0.8.0
typed_avl.h
Go to the documentation of this file.
1 #ifndef _LIBSAPLING_TYPED_AVL_H_
2 #define _LIBSAPLING_TYPED_AVL_H_
3 
13 
14 #define IMPLEMENT_TYPED_AVL(SYM, TYPE, CMP, EQU, FPF) \
15  \
16 IMPLEMENT_TYPED_ADAPTERS(SYM, TYPE, avl) \
17  \
18 static \
19 int SYM##__cmp_predicate(const TYPE *data, void *info) \
20 { \
21  struct info_insert *ins = info; \
22  \
23  return CMP(ins->data, data) < 0 ? 1 : 0; \
24 } \
25  \
26 static \
27 void SYM##__access(enum qt qt, node_t *ref, void *info, \
28  SYM##__predicate_t predicate, SYM##__apply_t apply) \
29 { \
30  TYPE *data = info; \
31  struct SYM##__adapt a = { sizeof(TYPE), data, info, predicate, apply }; \
32  avl__access(qt, ref, &a, SYM##__predicate_adapter, SYM##__apply_adapter, \
33  CMP); \
34 } \
35  \
36 static \
37 void SYM##__insert(node_t *ref, TYPE val, SYM##__predicate_t predicate) \
38 { \
39  struct SYM##__adapt a = { sizeof(TYPE), &val, NULL, predicate, NULL }; \
40  avl__insert(ref, &a, SYM##__predicate_adapter, CMP); \
41 } \
42  \
43 static \
44 void SYM##__delete(node_t *ref, TYPE val, SYM##__predicate_t predicate) \
45 { \
46  struct SYM##__adapt a = { sizeof(TYPE), &val, NULL, predicate, NULL }; \
47  avl__delete(ref, &a, SYM##__predicate_adapter, CMP); \
48 } \
49  \
50 static \
51 void SYM##__print(FILE *stream, node_t *ref) \
52 { \
53  avl__print(stream, ref, FPF); \
54 } \
55  \
56 static \
57 void SYM##__dump_dot(FILE *stream, node_t *ref) \
58 { \
59  avl__dump_dot(stream, ref, FPF); \
60 } \
61  \
62 static \
63 int SYM##__length(const node_t *ref) \
64 { \
65  return avl__length(ref); \
66 } \
67  \
68 static \
69 int SYM##__equ_predicate(const TYPE *data, void *info) \
70 { \
71  struct info_insert *ins = info; \
72  \
73  return EQU(ins->data, data); \
74 } \
75 static \
76 void SYM##__a_in(UNUSED TYPE *data, void *info) \
77 { \
78  CAST_USER_INFO(int *, in, info); \
79  \
80  *in = 1; \
81 } \
82  \
83 static \
84 int SYM##__in(node_t *ref, TYPE val) \
85 { \
86  int in = 0; \
87  struct SYM##__adapt a = { sizeof(TYPE), &val, &in, SYM##__equ_predicate, \
88  SYM##__a_in }; \
89  avl__access(E_QT, ref, &a, SYM##__predicate_adapter, \
90  SYM##__apply_adapter, CMP); \
91  return in; \
92 }
93 
94 #endif
Implementation of typed adapters for predicate and apply functions.