-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_iterable.h
52 lines (39 loc) · 1.76 KB
/
list_iterable.h
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
#ifndef IT_LIST_ITRBLE_H
#define IT_LIST_ITRBLE_H
#include "func_iter.h"
#include <stdlib.h>
#define Nil NULL
typedef struct int_node
{
int val;
struct int_node* next;
} IntNode, *IntList;
/*
The `T` passed to `ListIter`, `DefineListIterOf` and so on need to be alphanumeric.
So here's an alphanumeric alias to `IntNode const*`.
*/
typedef IntNode const* ConstIntList;
#define ListIter(T) T##ListIter
#define DefineListIterOf(T) \
typedef struct \
{ \
T curr; \
} ListIter(T)
/* Macro to consistently name the arriter -> iterable functions based on element type */
#define prep_listiter_of(T) prep_##T##_itr
/*
Take in a source list and its element type, build an `ListIter` from it, and call the wrapper function to
turn it into an `Iterable`
*/
#define list_into_iter(head, T) prep_listiter_of(T)(&(ListIter(T)){.curr = head})
/* Define `ListIter` struct for an int list */
DefineListIterOf(ConstIntList);
/* Create and prepend an IntNode to given IntList and return the new list */
IntList prepend_intnode(int val, IntList list);
/* Print the given int list */
void print_intlist(ConstIntList head);
/* Free the given IntList */
IntList free_intlist(IntList head);
/* Convert a pointer to an `ListIter(ConstIntList)` to an `Iterable(int)` */
Iterable(int) prep_listiter_of(ConstIntList)(ListIter(ConstIntList) * x);
#endif /* !IT_LIST_ITRBLE_H */