-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsugar.c
126 lines (102 loc) · 8.69 KB
/
sugar.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @file
* @brief Wrapper functions to fill pre-allocated iterplus structs and turn them into Iterables.
*
* See `sugar.h`.
*/
#include "sugar.h"
#include "common.h"
#include "impls.h"
#include <stdint.h>
/* Utility functions to fill pre-allocated iterplus structs and turn them into iterables */
/* Macro to define a function that turns a pre-allocated IterTake struct into iterable */
#define prep_tk(T, Name, implfunc) \
Iterable(T) Name(IterTake(T) * tk, Iterable(T) x) \
{ \
tk->src = x; \
return implfunc(tk); \
}
/* Macro to define a function that turns a pre-allocated IterTake struct into iterable */
#define prep_drp(T, Name, implfunc) \
Iterable(T) Name(IterDrop(T) * tk, Iterable(T) x) \
{ \
tk->src = x; \
return implfunc(tk); \
}
/* Macro to define a function that turns a pre-allocated IterMap struct into iterable */
#define prep_mp(A, B, Name, implfunc) \
Iterable(B) Name(IterMap(A, B) * mp, Iterable(A) x, B(*fn)(A)) \
{ \
mp->f = fn; \
mp->src = x; \
return implfunc(mp); \
}
/* Macro to define a function that turns a pre-allocated IterFilt struct into iterable */
#define prep_flt(T, Name, implfunc) \
Iterable(T) Name(IterFilt(T) * flt, Iterable(T) x, bool (*pred)(T)) \
{ \
flt->pred = pred; \
flt->src = x; \
return implfunc(flt); \
}
/* Macro to define a function that turns a pre-allocated IterFiltMap struct into iterable */
#define prep_fltmap(A, B, Name, implfunc) \
Iterable(B) Name(IterFiltMap(A, B) * fltmp, Iterable(A) x, Maybe(B)(*fn)(A)) \
{ \
fltmp->f = fn; \
fltmp->src = x; \
return implfunc(fltmp); \
}
/* Macro to define a function that turns a pre-allocated IterChain struct into iterable */
#define prep_chn(T, Name, implfunc) \
Iterable(T) Name(IterChain(T) * chn, Iterable(T) x, Iterable(T) y) \
{ \
chn->curr = x; \
chn->nxt = y; \
return implfunc(chn); \
}
/* Macro to define a function that turns a pre-allocated IterTakeWhile struct into iterable */
#define prep_tkwhl(T, Name, implfunc) \
Iterable(T) Name(IterTakeWhile(T) * tkwhl, Iterable(T) x, bool (*pred)(T)) \
{ \
tkwhl->pred = pred; \
tkwhl->src = x; \
return implfunc(tkwhl); \
}
/* Macro to define a function that turns a pre-allocated IterTakeWhile struct into iterable */
#define prep_drpwhl(T, Name, implfunc) \
Iterable(T) Name(IterDropWhile(T) * drpwhl, Iterable(T) x, bool (*pred)(T)) \
{ \
drpwhl->pred = pred; \
drpwhl->src = x; \
return implfunc(drpwhl); \
}
/* Macro to define a function that turns a pre-allocated IterEnumr struct into iterable */
#define prep_enumr(T, Name, implfunc) \
Iterable(Pair(size_t, T)) Name(IterEnumr(T) * enumr, Iterable(T) x) \
{ \
enumr->src = x; \
return implfunc(enumr); \
}
#define prep_zip(T, U, Name, implfunc) \
Iterable(Pair(T, U)) Name(IterZip(T, U) * zipstrct, Iterable(T) x, Iterable(U) y) \
{ \
zipstrct->asrc = x; \
zipstrct->bsrc = y; \
return implfunc(zipstrct); \
}
// clang-format off
prep_tk(uint32_t, prep_u32tk, u32tk_to_itr)
prep_tk(NumType, prep_numtypetk, numtypetk_to_itr)
prep_tk(string, prep_strtk, strtk_to_itr)
prep_drp(uint32_t, prep_u32drp, u32drp_to_itr)
prep_mp(uint32_t, NumType, prep_u32numtypemap, u32numtypemap_to_itr)
prep_flt(uint32_t, prep_u32filt, u32filt_to_itr)
prep_flt(string, prep_strfilt, strfilt_to_itr)
prep_fltmap(string, uint32_t, prep_stru32fltmap, stru32filtmap_to_itr)
prep_fltmap(string, NumType, prep_strnumtypefltmap, strnumtypefiltmap_to_itr)
prep_chn(uint32_t, prep_u32chn, u32chn_to_itr)
prep_tkwhl(uint32_t, prep_u32tkwhl, u32tkwhl_to_itr)
prep_drpwhl(uint32_t, prep_u32drpwhl, u32drpwhl_to_itr)
prep_enumr(uint32_t, prep_u32enumr, u32enumr_to_itr)
prep_zip(uint32_t, uint32_t, prep_u32u32zip, u32u32zip_to_itr)