inc/ecu/dlist.h Source File

ECU: inc/ecu/dlist.h Source File
ECU
dlist.h
Go to the documentation of this file.
1 
14 #ifndef ECU_DLIST_H_
15 #define ECU_DLIST_H_
16 
17 /*------------------------------------------------------------*/
18 /*------------------------- INCLUDES -------------------------*/
19 /*------------------------------------------------------------*/
20 
21 /* STDLib. */
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 
26 /* ECU. */
27 #include "ecu/object_id.h"
28 #include "ecu/utils.h"
29 
30 /*------------------------------------------------------------*/
31 /*---------------------- DEFINES AND MACROS ------------------*/
32 /*------------------------------------------------------------*/
33 
39 #define ECU_DNODE_OBJ_UNUSED \
40  ((void *)0)
41 
47 #define ECU_DNODE_DESTROY_UNUSED \
48  ((void (*)(struct ecu_dnode *, ecu_object_id_t))0)
49 
62 #define ECU_DNODE_GET_ENTRY(ptr_, type_, member_) \
63  ECU_CONTAINER_OF(ptr_, type_, member_)
64 
78 #define ECU_DNODE_GET_CONST_ENTRY(ptr_, type_, member_) \
79  ECU_CONST_CONTAINER_OF(ptr_, type_, member_)
80 
97 #define ECU_DLIST_AT_FOR_EACH(var_, iter_, list_, start_) \
98  for (struct ecu_dnode *var_ = ecu_dlist_iterator_at(iter_, list_, start_); \
99  var_ != ecu_dlist_iterator_end(iter_); \
100  var_ = ecu_dlist_iterator_next(iter_))
101 
116 #define ECU_DLIST_CONST_AT_FOR_EACH(var_, citer_, list_, start_) \
117  for (const struct ecu_dnode *var_ = ecu_dlist_iterator_cat(citer_, list_, start_); \
118  var_ != ecu_dlist_iterator_cend(citer_); \
119  var_ = ecu_dlist_iterator_cnext(citer_))
120 
133 #define ECU_DLIST_FOR_EACH(var_, iter_, list_) \
134  for (struct ecu_dnode *var_ = ecu_dlist_iterator_begin(iter_, list_); \
135  var_ != ecu_dlist_iterator_end(iter_); \
136  var_ = ecu_dlist_iterator_next(iter_))
137 
149 #define ECU_DLIST_CONST_FOR_EACH(var_, citer_, list_) \
150  for (const struct ecu_dnode *var_ = ecu_dlist_iterator_cbegin(citer_, list_); \
151  var_ != ecu_dlist_iterator_cend(citer_); \
152  var_ = ecu_dlist_iterator_cnext(citer_))
153 
154 /*------------------------------------------------------------*/
155 /*-------------------------- DLIST ---------------------------*/
156 /*------------------------------------------------------------*/
157 
165 struct ecu_dnode
166 {
168  struct ecu_dnode *next;
169 
171  struct ecu_dnode *prev;
172 
176  void (*destroy)(struct ecu_dnode *me, ecu_object_id_t id);
177 
181 };
182 
189 struct ecu_dlist
190 {
193  struct ecu_dnode head;
194 };
195 
196 /*------------------------------------------------------------*/
197 /*--------------------- DLIST ITERATORS ----------------------*/
198 /*------------------------------------------------------------*/
199 
207 {
210  struct ecu_dlist *list;
211 
214 
218  struct ecu_dnode *next;
219 };
220 
228 {
231  const struct ecu_dlist *list;
232 
234  const struct ecu_dnode *current;
235 
239  const struct ecu_dnode *next;
240 };
241 
242 /*------------------------------------------------------------*/
243 /*------------------ DNODE MEMBER FUNCTIONS ------------------*/
244 /*------------------------------------------------------------*/
245 
246 #ifdef __cplusplus
247 extern "C" {
248 #endif
249 
273 extern void ecu_dnode_ctor(struct ecu_dnode *me,
274  void (*destroy)(struct ecu_dnode *me, ecu_object_id_t id),
275  ecu_object_id_t id);
276 
291 extern void ecu_dnode_destroy(struct ecu_dnode *me);
305 extern ecu_object_id_t ecu_dnode_id(const struct ecu_dnode *me);
306 
313 extern bool ecu_dnode_in_list(const struct ecu_dnode *me);
314 
324 extern void ecu_dnode_insert_after(struct ecu_dnode *pos, struct ecu_dnode *node);
325 
335 extern void ecu_dnode_insert_before(struct ecu_dnode *pos, struct ecu_dnode *node);
336 
345 extern struct ecu_dnode *ecu_dnode_next(struct ecu_dnode *me);
346 
354 extern const struct ecu_dnode *ecu_dnode_cnext(const struct ecu_dnode *me);
355 
364 extern struct ecu_dnode *ecu_dnode_prev(struct ecu_dnode *me);
365 
373 extern const struct ecu_dnode *ecu_dnode_cprev(const struct ecu_dnode *me);
374 
383 extern void ecu_dnode_remove(struct ecu_dnode *me);
384 
392 extern bool ecu_dnode_valid(const struct ecu_dnode *me);
395 /*------------------------------------------------------------*/
396 /*------------------ DLIST MEMBER FUNCTIONS ------------------*/
397 /*------------------------------------------------------------*/
398 
412 extern void ecu_dlist_ctor(struct ecu_dlist *me);
413 
429 extern void ecu_dlist_destroy(struct ecu_dlist *me);
443 extern struct ecu_dnode *ecu_dlist_back(struct ecu_dlist *me);
444 
452 extern const struct ecu_dnode *ecu_dlist_cback(const struct ecu_dlist *me);
453 
461 extern void ecu_dlist_clear(struct ecu_dlist *me);
462 
472 extern bool ecu_dlist_empty(const struct ecu_dlist *me);
473 
481 extern struct ecu_dnode *ecu_dlist_front(struct ecu_dlist *me);
482 
490 extern const struct ecu_dnode *ecu_dlist_cfront(const struct ecu_dlist *me);
491 
512 extern void ecu_dlist_insert_before(struct ecu_dlist *me,
513  struct ecu_dnode *node,
514  bool (*condition)(const struct ecu_dnode *node, const struct ecu_dnode *position, void *data),
515  void *data);
516 
526 extern void ecu_dlist_push_back(struct ecu_dlist *me, struct ecu_dnode *node);
527 
537 extern void ecu_dlist_push_front(struct ecu_dlist *me, struct ecu_dnode *node);
538 
546 extern struct ecu_dnode *ecu_dlist_pop_back(struct ecu_dlist *me);
547 
555 extern struct ecu_dnode *ecu_dlist_pop_front(struct ecu_dlist *me);
556 
563 extern size_t ecu_dlist_size(const struct ecu_dlist *me);
564 
577 extern void ecu_dlist_sort(struct ecu_dlist *me,
578  bool (*lhs_less_than_rhs)(const struct ecu_dnode *lhs, const struct ecu_dnode *rhs, void *data),
579  void *data);
580 
589 extern void ecu_dlist_swap(struct ecu_dlist *me, struct ecu_dlist *other);
590 
597 extern bool ecu_dlist_valid(const struct ecu_dlist *me);
600 /*------------------------------------------------------------*/
601 /*----------- NON-CONST ITERATOR MEMBER FUNCTIONS ------------*/
602 /*------------------------------------------------------------*/
603 
624  struct ecu_dlist *list,
625  struct ecu_dnode *start);
626 
640 extern struct ecu_dnode *ecu_dlist_iterator_begin(struct ecu_dlist_iterator *me, struct ecu_dlist *list);
641 
654 
665 
666 /*------------------------------------------------------------*/
667 /*------------- CONST ITERATOR MEMBER FUNCTIONS --------------*/
668 /*------------------------------------------------------------*/
669 
686 extern const struct ecu_dnode *ecu_dlist_iterator_cat(struct ecu_dlist_citerator *me,
687  const struct ecu_dlist *list,
688  const struct ecu_dnode *start);
689 
705  const struct ecu_dlist *list);
706 
720 extern const struct ecu_dnode *ecu_dlist_iterator_cend(struct ecu_dlist_citerator *me);
721 
733 extern const struct ecu_dnode *ecu_dlist_iterator_cnext(struct ecu_dlist_citerator *me);
736 #ifdef __cplusplus
737 }
738 #endif
739 
740 #endif /* ECU_DLIST_H_ */
const struct ecu_dnode * ecu_dnode_cprev(const struct ecu_dnode *me)
Const-qualified version of ecu_dnode_prev(). Returned node is read-only.
bool ecu_dlist_empty(const struct ecu_dlist *me)
Returns true if the list is empty. False otherwise.
struct ecu_dnode * ecu_dlist_pop_front(struct ecu_dlist *me)
Removes the front node from the list and returns it. If the list is empty, returns NULL.
struct ecu_dnode * ecu_dlist_iterator_begin(struct ecu_dlist_iterator *me, struct ecu_dlist *list)
Initializes iterator and returns first node in the list. Returns first user-defined node if list is n...
struct ecu_dnode * ecu_dlist_iterator_at(struct ecu_dlist_iterator *me, struct ecu_dlist *list, struct ecu_dnode *start)
Initializes iterator at the supplied starting node's position. The supplied starting node is returned...
bool ecu_dnode_in_list(const struct ecu_dnode *me)
Returns true if the node is in a list. False otherwise.
void ecu_dlist_insert_before(struct ecu_dlist *me, struct ecu_dnode *node, bool(*condition)(const struct ecu_dnode *node, const struct ecu_dnode *position, void *data), void *data)
Inserts a node before the position specified by a condition function. Starting from HEAD,...
bool ecu_dlist_valid(const struct ecu_dlist *me)
Returns true if the supplied list has been constructed via ecu_dlist_ctor(). False otherwise.
const struct ecu_dnode * ecu_dlist_iterator_cnext(struct ecu_dlist_citerator *me)
Const-qualified version of ecu_dlist_iterator_next(). Returns the next node in the iteration.
size_t ecu_dlist_size(const struct ecu_dlist *me)
Returns the number of nodes in a list. Returns 0 if the list is empty.
const struct ecu_dnode * ecu_dlist_cback(const struct ecu_dlist *me)
Const-qualified version of ecu_dlist_back(). Returned node is read-only.
struct ecu_dnode * ecu_dlist_pop_back(struct ecu_dlist *me)
Removes the tail node from the list and returns it. If the list is empty, returns NULL.
void ecu_dlist_sort(struct ecu_dlist *me, bool(*lhs_less_than_rhs)(const struct ecu_dnode *lhs, const struct ecu_dnode *rhs, void *data), void *data)
Merge sorts all nodes in the list. The sorting condition is defined by a user-supplied function.
void ecu_dlist_ctor(struct ecu_dlist *me)
List constructor.
const struct ecu_dnode * ecu_dlist_cfront(const struct ecu_dlist *me)
Const-qualified version of ecu_dlist_front(). Returned node is read-only.
struct ecu_dnode * ecu_dlist_back(struct ecu_dlist *me)
Returns the tail node but does not remove it. If the list is empty, NULL is returned.
void ecu_dlist_swap(struct ecu_dlist *me, struct ecu_dlist *other)
Swaps nodes between two lists. If one list is empty, the swapped list will become empty:
void ecu_dlist_push_back(struct ecu_dlist *me, struct ecu_dnode *node)
Inserts node to the back of the list.
void ecu_dnode_insert_before(struct ecu_dnode *pos, struct ecu_dnode *node)
Inserts a node before the specified position.
const struct ecu_dnode * ecu_dlist_iterator_cat(struct ecu_dlist_citerator *me, const struct ecu_dlist *list, const struct ecu_dnode *start)
Const-qualified version of ecu_dlist_iterator_at(). Initializes iterator at the supplied starting nod...
const struct ecu_dnode * ecu_dnode_cnext(const struct ecu_dnode *me)
Const-qualified version of ecu_dnode_next(). Returned node is read-only.
struct ecu_dnode * ecu_dlist_iterator_end(struct ecu_dlist_iterator *me)
Returns list's terminal node, which is HEAD (ecu_dlist::head).
const struct ecu_dnode * ecu_dlist_iterator_cbegin(struct ecu_dlist_citerator *me, const struct ecu_dlist *list)
Const-qualified version of ecu_dlist_iterator_begin(). Initializes iterator and returns first node in...
void ecu_dnode_remove(struct ecu_dnode *me)
Removes the node from a list. It can be reused and added to another list without reconstruction....
void ecu_dnode_destroy(struct ecu_dnode *me)
Node destructor. Removes node if it is in a list. Executes the user-defined destructor if one was sup...
void ecu_dnode_insert_after(struct ecu_dnode *pos, struct ecu_dnode *node)
Inserts a node after the specified position.
void ecu_dlist_destroy(struct ecu_dlist *me)
List destructor. Destroys the list and all nodes within the list. All destroyed objects must be recon...
struct ecu_dnode * ecu_dlist_front(struct ecu_dlist *me)
Returns the front node in the list but does not remove it. If the list is empty, returns NULL.
bool ecu_dnode_valid(const struct ecu_dnode *me)
Returns true if the supplied node has been constructed via ecu_dnode_ctor(). Returns false if supplie...
ecu_object_id_t ecu_dnode_id(const struct ecu_dnode *me)
Returns node ID. Used to identity different user-defined types stored in the same list.
struct ecu_dnode * ecu_dnode_prev(struct ecu_dnode *me)
Returns the node previous to (left of) me. NULL is returned if me is the first node in the list (one ...
struct ecu_dnode * ecu_dnode_next(struct ecu_dnode *me)
Returns the node next to (right of) me. NULL is returned if me is the last node in the list or if me ...
void ecu_dnode_ctor(struct ecu_dnode *me, void(*destroy)(struct ecu_dnode *me, ecu_object_id_t id), ecu_object_id_t id)
Node constructor.
const struct ecu_dnode * ecu_dlist_iterator_cend(struct ecu_dlist_citerator *me)
Const-qualified version of ecu_dlist_iterator_end(). Returns list's terminal node,...
struct ecu_dnode * ecu_dlist_iterator_next(struct ecu_dlist_iterator *me)
Returns the next node in the iteration.
void ecu_dlist_clear(struct ecu_dlist *me)
Removes all nodes from the list. List and nodes can be reused without reconstruction.
void ecu_dlist_push_front(struct ecu_dlist *me, struct ecu_dnode *node)
Inserts node to the front of the list.
int32_t ecu_object_id_t
Object ID type. Used so the library can implicitly typecast between this value, ecu_reserved_object_i...
Definition: object_id.h:64
Const list iterator.
Definition: dlist.h:228
const struct ecu_dnode * next
Next position in the list. Allows user to safely add and remove nodes in the middle of an iteration.
Definition: dlist.h:239
const struct ecu_dlist * list
List that is being iterated. List's HEAD is used as a delimiter.
Definition: dlist.h:231
const struct ecu_dnode * current
Current position in list.
Definition: dlist.h:234
Non-const list iterator.
Definition: dlist.h:207
struct ecu_dnode * current
Current position in list.
Definition: dlist.h:213
struct ecu_dlist * list
List that is being iterated. List's HEAD is used as a delimiter.
Definition: dlist.h:210
struct ecu_dnode * next
Next position in the list. Allows user to safely add and remove nodes in the middle of an iteration.
Definition: dlist.h:218
Intrusive, doubly-linked list.
Definition: dlist.h:190
struct ecu_dnode head
Dummy node used as delimiter to represent start and end of list. Not apart of user's list.
Definition: dlist.h:193
Single node within list. Intrusive, so user-defined types contain this node as a member.
Definition: dlist.h:166
struct ecu_dnode * prev
Previous node in list.
Definition: dlist.h:171
ecu_object_id_t id
Optional node ID. Helps user identify different types stored in the same list.
Definition: dlist.h:180
void(* destroy)(struct ecu_dnode *me, ecu_object_id_t id)
Optional user-defined node destructor. Executes when ecu_dlist_destroy() or ecu_dnode_destroy() are c...
Definition: dlist.h:176
struct ecu_dnode * next
Next node in list.
Definition: dlist.h:168