EzC
ezc_list.h
Go to the documentation of this file.
1 /* ezc_list.h
2  *
3  * Copyright (c) 2018 Kirk Lange <github.com/kirklange>
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 
22 #ifndef EZC_LIST_H
23 #define EZC_LIST_H
24 
33 #ifdef __cplusplus
34 extern C
35 {
36 #endif
37 
38 #include "ezc/ezc_macro.h"
39 #include <stdarg.h>
40 #include <stddef.h>
41 
42 
43 
50 typedef struct ezc_list
51 {
53  void *data;
54 
56  struct ezc_list *next;
57 }
58 ezc_list;
59 
60 
61 
72 #define ezc_list_new(self, ...) \
73  (ezc_list_new__((self), ##__VA_ARGS__, NULL))
74 
75 ezc_list* ezc_list_new__(void const *data, ...);
76 
77 
78 
86 #define ezc_list_copy(orig) \
87  (ezc_list_copy__((orig)))
88 
89 ezc_list* ezc_list_copy__(ezc_list const *orig);
90 
91 
92 
101 #define ezc_list_swap(a, b) \
102  (ezc_list_swap__((a), (b)))
103 
104 void ezc_list_swap__(ezc_list *a, ezc_list *b);
105 
106 
107 
117 #define ezc_list_cat(self, ...) \
118  (ezc_list_cat__((self), ##__VA_ARGS__, NULL))
119 
120 ezc_list* ezc_list_cat__(ezc_list *self, ...);
121 
122 
123 
132 #define ezc_list_delete(self, ...) \
133  (ezc_list_delete__((self), ##__VA_ARGS__, NULL), \
134  SST_MAP(EZC_TO_ZERO, (self), ##__VA_ARGS__))
135 
136 void ezc_list_delete__(ezc_list *self, ...);
137 
138 
139 
146 #define ezc_list_length(self) \
147  (ezc_list_length__((self)))
148 
149 long ezc_list_length__(ezc_list const *self);
150 
151 
152 
165 #define ezc_list_map(self, fn, ...) \
166  do { ezc_list *iter = (self); while (iter != NULL) { \
167  (fn)(iter->data, ##__VA_ARGS__); iter = iter->next; \
168  } } while(0)
169 
170 
171 
182 #define ezc_list_get_index_of(self, head) \
183  (ezc_list_get_index_of__((self), (head)))
184 
185 long ezc_list_get_index_of__(ezc_list const *self, ezc_list const *head);
186 
187 
188 
189 
197 #define ezc_list_get_at(self, n) \
198  (ezc_list_get_at__((self), (n)))
199 
200 ezc_list* ezc_list_get_at__(ezc_list const *self, long n);
201 
202 
203 
213 #define ezc_list_get_match(self, data) \
214  (ezc_list_get_match_fn__((self), NULL, (data)))
215 
216 
217 
232 #define ezc_list_get_match_fn(self, neq, data) \
233  (ezc_list_get_match_fn__((self), (neq), (data)))
234 
235 ezc_list* ezc_list_get_match_fn__(ezc_list const *self,
236  int (*neq)(void const *, void const *),
237  void const *data);
238 
239 
240 
251 #define ezc_list_push_at(self, n, ...) \
252  do { \
253  if ((self) == NULL) (self) = ezc_list_new__(__VA_ARGS__, NULL); \
254  else ezc_list_push_at__((self), (n), ##__VA_ARGS__, NULL); \
255  } while (0)
256 
257 void ezc_list_push_at__(ezc_list *self, long n, ...);
258 
259 
260 
268 #define ezc_list_push_front(self, ...) \
269  do { \
270  if ((self) == NULL) (self) = ezc_list_new__(__VA_ARGS__, NULL); \
271  else ezc_list_push_at__((self), 0, ##__VA_ARGS__, NULL); \
272  } while (0)
273 
274 
275 
284 #define ezc_list_push_back(self, ...) \
285  do { \
286  if ((self) == NULL) (self) = ezc_list_new__(__VA_ARGS__, NULL); \
287  else ezc_list_push_at__((self), \
288  ezc_list_length((self)), \
289  ##__VA_ARGS__, \
290  NULL); \
291  } while (0)
292 
293 
294 
295 
306 #define ezc_list_pop_at(self, n) \
307  (ezc_list_pop_at__(&(self), (n)))
308 
309 ezc_list* ezc_list_pop_at__(ezc_list **self, long n);
310 
311 
312 
319 #define ezc_list_pop_front(self) \
320  (ezc_list_pop_at__(&(self), 0))
321 
322 
323 
330 #define ezc_list_pop_back(self) \
331  (ezc_list_pop_at__(&(self), ezc_list_length__((self))-1))
332 
333 
334 
345 #define ezc_list_pop_match(self, data) \
346  (ezc_list_pop_match_fn__((self), NULL, (data)))
347 
348 
349 
365 #define ezc_list_pop_match_fn(self, neq, data, ...) \
366  (ezc_list_pop_match_fn__((self), (neq), (data)))
367 
368 ezc_list* ezc_list_pop_match_fn__(ezc_list const *self,
369  int (*neq)(void const *, void const *),
370  void const *data);
371 
372 
373 
380 #define ezc_list_erase_at(self, n) \
381  (ezc_list_delete__(ezc_list_pop_at__(&(self), (n)), NULL))
382 
383 
384 
390 #define ezc_list_erase_front(self) \
391  (ezc_list_delete__(ezc_list_pop_at__(&(self), 0), NULL))
392 
393 
394 
401 #define ezc_list_erase_back(self) \
402  (ezc_list_delete__( \
403  ezc_list_pop_at__(&(self), ezc_list_length__((self))-1), \
404  NULL))
405 
406 
407 
416 #define ezc_list_erase_match(self, data) \
417  (ezc_list_delete__(ezc_list_pop_match_fn__((self), NULL, (data)), NULL))
418 
419 
420 
435 #define ezc_list_erase_match_fn(self, neq, data, ...) \
436  (ezc_list_delete__(ezc_list_pop_match_fn__((self), (neq), (data)), NULL))
437 
438 
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 #endif /* EZC_LIST_H */
Macros for helping you write your own featureful macros.
List item structure.
Definition: ezc_list.h:50
void * data
Definition: ezc_list.h:53
struct ezc_list * next
Definition: ezc_list.h:56
struct ezc_list ezc_list
List item structure.