1 /**
2  * @file lv_ll.c
3  * Handle linked lists. The nodes are dynamically allocated by the 'lv_mem' module.
4  */
5 
6 #ifndef LV_LL_H
7 #define LV_LL_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "lv_mem.h"
17 #include <stdint.h>
18 #include <stddef.h>
19 #include <stdbool.h>
20 
21 /*********************
22  *      DEFINES
23  *********************/
24 
25 /**********************
26  *      TYPEDEFS
27  **********************/
28 
29 /** Dummy type to make handling easier*/
30 typedef uint8_t lv_ll_node_t;
31 
32 /** Description of a linked list*/
33 typedef struct {
34     uint32_t n_size;
35     lv_ll_node_t * head;
36     lv_ll_node_t * tail;
37 } lv_ll_t;
38 
39 /**********************
40  * GLOBAL PROTOTYPES
41  **********************/
42 
43 /**
44  * Initialize linked list
45  * @param ll_dsc pointer to ll_dsc variable
46  * @param node_size the size of 1 node in bytes
47  */
48 void _lv_ll_init(lv_ll_t * ll_p, uint32_t node_size);
49 
50 /**
51  * Add a new head to a linked list
52  * @param ll_p pointer to linked list
53  * @return pointer to the new head
54  */
55 void * _lv_ll_ins_head(lv_ll_t * ll_p);
56 
57 /**
58  * Insert a new node in front of the n_act node
59  * @param ll_p pointer to linked list
60  * @param n_act pointer a node
61  * @return pointer to the new head
62  */
63 void * _lv_ll_ins_prev(lv_ll_t * ll_p, void * n_act);
64 
65 /**
66  * Add a new tail to a linked list
67  * @param ll_p pointer to linked list
68  * @return pointer to the new tail
69  */
70 void * _lv_ll_ins_tail(lv_ll_t * ll_p);
71 
72 /**
73  * Remove the node 'node_p' from 'll_p' linked list.
74  * It does not free the the memory of node.
75  * @param ll_p pointer to the linked list of 'node_p'
76  * @param node_p pointer to node in 'll_p' linked list
77  */
78 void _lv_ll_remove(lv_ll_t * ll_p, void * node_p);
79 
80 /**
81  * Remove and free all elements from a linked list. The list remain valid but become empty.
82  * @param ll_p pointer to linked list
83  */
84 void _lv_ll_clear(lv_ll_t * ll_p);
85 
86 /**
87  * Move a node to a new linked list
88  * @param ll_ori_p pointer to the original (old) linked list
89  * @param ll_new_p pointer to the new linked list
90  * @param node pointer to a node
91  * @param head true: be the head in the new list
92  *             false be the head in the new list
93  */
94 void _lv_ll_chg_list(lv_ll_t * ll_ori_p, lv_ll_t * ll_new_p, void * node, bool head);
95 
96 /**
97  * Return with head node of the linked list
98  * @param ll_p pointer to linked list
99  * @return pointer to the head of 'll_p'
100  */
101 void * _lv_ll_get_head(const lv_ll_t * ll_p);
102 
103 /**
104  * Return with tail node of the linked list
105  * @param ll_p pointer to linked list
106  * @return pointer to the head of 'll_p'
107  */
108 void * _lv_ll_get_tail(const lv_ll_t * ll_p);
109 
110 /**
111  * Return with the pointer of the next node after 'n_act'
112  * @param ll_p pointer to linked list
113  * @param n_act pointer a node
114  * @return pointer to the next node
115  */
116 void * _lv_ll_get_next(const lv_ll_t * ll_p, const void * n_act);
117 
118 /**
119  * Return with the pointer of the previous node after 'n_act'
120  * @param ll_p pointer to linked list
121  * @param n_act pointer a node
122  * @return pointer to the previous node
123  */
124 void * _lv_ll_get_prev(const lv_ll_t * ll_p, const void * n_act);
125 
126 /**
127  * Return the length of the linked list.
128  * @param ll_p pointer to linked list
129  * @return length of the linked list
130  */
131 uint32_t _lv_ll_get_len(const lv_ll_t * ll_p);
132 
133 /**
134  * TODO
135  * @param ll_p
136  * @param n1_p
137  * @param n2_p
138 void lv_ll_swap(lv_ll_t * ll_p, void * n1_p, void * n2_p);
139  */
140 
141 /**
142  * Move a node before an other node in the same linked list
143  * @param ll_p pointer to a linked list
144  * @param n_act pointer to node to move
145  * @param n_after pointer to a node which should be after `n_act`
146  */
147 void _lv_ll_move_before(lv_ll_t * ll_p, void * n_act, void * n_after);
148 
149 /**
150  * Check if a linked list is empty
151  * @param ll_p pointer to a linked list
152  * @return true: the linked list is empty; false: not empty
153  */
154 bool _lv_ll_is_empty(lv_ll_t * ll_p);
155 
156 /**********************
157  *      MACROS
158  **********************/
159 
160 #define _LV_LL_READ(list, i) for(i = _lv_ll_get_head(&list); i != NULL; i = _lv_ll_get_next(&list, i))
161 
162 #define _LV_LL_READ_BACK(list, i) for(i = _lv_ll_get_tail(&list); i != NULL; i = _lv_ll_get_prev(&list, i))
163 
164 #ifdef __cplusplus
165 } /* extern "C" */
166 #endif
167 
168 #endif
169