Distributed File System 1
Distributed file system in C
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1/* lib/list.h
2 *
3 * Generic C List implementation.
4 * For more info, please visit: https://tangled.org/@stau.space/c-list.
5 *
6 */
7#ifndef LIST_H_
8#define LIST_H_
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14/* ----- Includes ----- */
15
16#include "lib.h"
17
18/* ----- Types ----- */
19
21typedef void* Opaque;
22
24struct NodeOpaque {
26 struct NodeOpaque* rest;
27};
28
30typedef struct NodeOpaque* ListOpaque;
31
32/* ----- Functions ----- */
33
38
45
52
62
69
77
83
92
93#define DefList(type) \
94struct Node##type { \
95 type head; \
96 struct Node##type* rest; \
97}; \
98typedef struct Node##type* List##type; \
99void List##type##_deinit(List##type list); \
100type List##type##_car(struct Node##type node); \
101struct Node##type* List##type##_cdr(struct Node##type node); \
102List##type List##type##_cons(type a, List##type list); \
103List##type List##type##_rev(List##type list, List##type a); \
104List##type List##type##_reverse(List##type list); \
105size_t List##type##_length(List##type list); \
106type List##type##_at(List##type list, size_t index); \
107List##type List##type##_drop(List##type list, size_t n); \
108List##type List##type##_drop(List##type list, size_t n);
109
110#define ImplList(type) \
111void List##type##_deinit(List##type list) { \
112 if (list != NULL) { \
113 List##type##_deinit(list->rest); \
114 free(list); \
115 } \
116} \
117 \
118type List##type##_car(struct Node##type node) { \
119 List##type##_deinit(node.rest); \
120 return node.head; \
121} \
122 \
123struct Node##type* List##type##_cdr(struct Node##type node) { \
124 return node.rest; \
125} \
126 \
127List##type List##type##_cons(type a, List##type list) { \
128 struct Node##type* node = (struct Node##type*)calloc(1, sizeof(struct Node##type)); \
129 exists(node); \
130 *node = (struct Node##type){ .head = a, .rest = list }; \
131 return (List##type)node; \
132} \
133 \
134List##type List##type##_rev(List##type list, List##type a) { \
135 if (list != NULL) { \
136 List##type out = List##type##_rev(list->rest, List##type##_cons(list->head, a)); \
137 free(list); \
138 return out; \
139 } else return a; \
140} \
141 \
142List##type List##type##_reverse(List##type list) { \
143 return List##type##_rev(list, NULL); \
144} \
145 \
146size_t List##type##_length(List##type list) { \
147 if (list != NULL) return List##type##_length(list->rest) + 1; \
148 else return 0; \
149} \
150 \
151type List##type##_at(List##type list, size_t index) { \
152 if (list != NULL) return index == 1 ? list->head : List##type##_at(list->rest, index - 1); \
153 else exists(NULL); \
154 return (type){0}; \
155} \
156 \
157List##type List##type##_take(List##type list, size_t n) { \
158 if (list != NULL) { \
159 if (n != 0) \
160 return List##type##_cons(list->head, List##type##_take(list->rest, n - 1)); \
161 else { \
162 List##type##_deinit(list->rest); \
163 return NULL; \
164 } \
165 } else { \
166 log(FATAL, "Took too many elements from List\n"); \
167 exit(1); \
168 } \
169} \
170 \
171List##type List##type##_drop(List##type list, size_t n) { \
172 if (list != NULL) { \
173 if (n != 0) { \
174 List##type kod = list; \
175 list = list-> rest; \
176 free(kod); \
177 return List##type##_take(list, n - 1); \
178 } else \
179 return list->rest; \
180 } else { \
181 log(FATAL, "Took too many elements from List\n"); \
182 exit(1); \
183 } \
184} \
185
186#ifdef __cplusplus
187}
188#endif
189
190#endif // LIST_H_
struct NodeOpaque * ListOpaque
This is the actual type of the List.
Definition list.h:30
void * Opaque
An opaque type.
Definition list.h:21
size_t ListOpaque_length(ListOpaque list)
Returns the length of the List.
ListOpaque ListOpaque_cons(Opaque fst, ListOpaque list)
Prepend an element to the List.
ListOpaque ListOpaque_rev(ListOpaque list, ListOpaque acc)
Helper function for ListOpaque_reverse.
Opaque ListOpaque_at(ListOpaque list, size_t index)
The indexing function for a List.
Opaque ListOpaque_car(struct NodeOpaque node)
Returns the element at the head of the List.
struct NodeOpaque * ListOpaque_cdr(struct NodeOpaque node)
Returns the rest of the List, ignoring the first element.
ListOpaque ListOpaque_reverse(ListOpaque list)
Reverses a List.
void ListOpaque_deinit(ListOpaque list)
Deinitialize the List.
void list(Enchufe cliente)
Definition meta-data.c:14
This is the data at each node on a List.
Definition list.h:24
struct NodeOpaque * rest
the rest of the List
Definition list.h:26
Opaque head
the data at each index of the List
Definition list.h:25