px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_link_list.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2008 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_link_list.h : Link List
14 Author(s): Pieter Conradie
15 Creation Date: 2008-11-27
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20
21/* _____PROJECT INCLUDES_____________________________________________________ */
22#include "px_link_list.h"
23
24/* _____LOCAL DEFINITIONS____________________________________________________ */
25
26/* _____MACROS_______________________________________________________________ */
27
28/* _____GLOBAL VARIABLES_____________________________________________________ */
29
30/* _____LOCAL VARIABLES______________________________________________________ */
31
32/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
33
34/* _____LOCAL FUNCTIONS______________________________________________________ */
35
36/* _____GLOBAL FUNCTIONS_____________________________________________________ */
38 size_t max_nr_of_items)
39{
40 list->first = NULL;
41 list->last = NULL;
42 list->item_count = 0;
43 list->items_max = max_nr_of_items;
44}
45
48{
49 item->prev = NULL;
50 item->next = NULL;
51}
52
54{
55 if(list->first == NULL)
56 {
57 return true;
58 }
59 else
60 {
61 return false;
62 }
63}
64
66{
67 if(list->items_max == 0)
68 {
69 return false;
70 }
71
72 if(list->item_count < list->items_max)
73 {
74 return false;
75 }
76 else
77 {
78 return true;
79 }
80}
81
83{
84 return list->item_count;
85}
86
88{
89 return list->first;
90}
91
93{
94 return list->last;
95}
96
99{
100 return item->next;
101}
102
104 px_link_list_item_t * item)
105{
106 return item->prev;
107}
108
110 px_link_list_item_t * item)
111{
112 if(px_link_list_is_full(list))
113 {
114 return false;
115 }
116 if(px_link_list_is_empty(list))
117 {
118 // Add first item
119 list->first = item;
120 list->last = item;
121 item->next = NULL;
122 item->prev = NULL;
123 }
124 else
125 {
126 // Insert new item before first item
127 item->prev = NULL;
128 item->next = list->first;
129 list->first->prev = item;
130 list->first = item;
131 }
132 // Increment item count
133 list->item_count++;
134
135 return true;
136}
137
139 px_link_list_item_t * item)
140{
141 if(px_link_list_is_full(list))
142 {
143 return false;
144 }
145 if(px_link_list_is_empty(list))
146 {
147 // Add first item
148 list->first = item;
149 list->last = item;
150 item->next = NULL;
151 item->prev = NULL;
152 }
153 else
154 {
155 // Append new item to last item
156 item->prev = list->last;
157 item->next = NULL;
158 list->last->next = item;
159 list->last = item;
160 }
161 // Increment item count
162 list->item_count++;
163
164 return true;
165}
166
168 px_link_list_item_t * item,
169 px_link_list_item_t * item_pos)
170{
171 if(px_link_list_is_full(list))
172 {
173 return false;
174 }
175 if(px_link_list_is_empty(list))
176 {
177 // Add first item
178 list->first = item;
179 list->last = item;
180 item->next = NULL;
181 item->prev = NULL;
182 }
183 else
184 {
185 // Is this the first item in the list
186 if(list->first == item_pos)
187 {
188 // Yes. Insert first
189 list->first = item;
190 item->prev = NULL;
191 item->next = item_pos;
192 item_pos->prev = item;
193 }
194 else
195 {
196 // No. Insert before
197 item->prev = item_pos->prev;
198 item->next = item_pos;
199 item_pos->prev->next = item;
200 item_pos->prev = item;
201 }
202 }
203 // Increment item count
204 list->item_count++;
205
206 return true;
207}
208
210 px_link_list_item_t * item,
211 px_link_list_item_t * item_pos)
212{
213 if(px_link_list_is_full(list))
214 {
215 return false;
216 }
217 if(px_link_list_is_empty(list))
218 {
219 // Add first item
220 list->first = item;
221 list->last = item;
222 item->next = NULL;
223 item->prev = NULL;
224 }
225 else
226 {
227 // Is this the last item in the list
228 if(list->last == item_pos)
229 {
230 // Yes. Insert last
231 list->last = item;
232 item->prev = item_pos;
233 item->next = NULL;
234 item_pos->next = item;
235 }
236 else
237 {
238 // No. Insert after
239 item->prev = item_pos;
240 item->next = item_pos->next;
241 item_pos->next->prev = item;
242 item_pos->next = item;
243 }
244 }
245 // Increment item count
246 list->item_count++;
247
248 return true;
249}
250
252{
253 px_link_list_item_t * item = list->first;
254
255 // See if list is empty
256 if(px_link_list_is_empty(list))
257 {
258 return NULL;
259 }
260 // See if there is only one item
261 if(list->first == list->last)
262 {
263 list->first = NULL;
264 list->last = NULL;
265 }
266 else
267 {
268 // The next item become the first one in the list
269 list->first = item->next;
270 item->next->prev = NULL;
271 }
272 // Clear links of removed item
273 item->prev = NULL;
274 item->next = NULL;
275 // Decrement item count
276 list->item_count--;
277
278 return item;
279}
280
282{
283 px_link_list_item_t * item = list->last;
284
285 // See if list is empty
286 if(px_link_list_is_empty(list))
287 {
288 return NULL;
289 }
290 // See if there is only one item
291 if(list->first == list->last)
292 {
293 list->first = NULL;
294 list->last = NULL;
295 }
296 else
297 {
298 // The previous item become the last one in the list
299 list->last = item->prev;
300 item->prev->next = NULL;
301 }
302 // Clear links of removed item
303 item->prev = NULL;
304 item->next = NULL;
305 // Decrement item count
306 list->item_count--;
307
308 return item;
309}
310
312 px_link_list_item_t * item)
313{
314 // Extra sanity check
315 if(px_link_list_is_empty(list))
316 {
317 return;
318 }
319 // See if this is the first item in the list
320 if(item == list->first)
321 {
323 return;
324 }
325
326 // See if this is the last item in the list
327 if(item == list->last)
328 {
330 return;
331 }
332 // Link previous and next item to each other
333 item->prev->next = item->next;
334 item->next->prev = item->prev;
335 // Clear links of item
336 item->prev = NULL;
337 item->next = NULL;
338 // Decrement item count
339 list->item_count--;
340}
341
343 px_link_list_item_t * item)
344{
345 // Start at first item in the list
346 px_link_list_item_t * item_in_list = list->first;
347
348 // Search all items in the list
349 while(item_in_list != NULL)
350 {
351 if(item_in_list == item)
352 {
353 // Item is in the list
354 return true;
355 }
356 // Next item
357 item_in_list = item_in_list->next;
358 }
359 // Item is not in the list
360 return false;
361}
362
#define NULL
NULL pointer.
Definition: px_defs.h:49
struct px_link_list_item_s * next
Pointer to next item in the list.
Definition: px_link_list.h:71
size_t item_count
Number of items in the list.
Definition: px_link_list.h:80
size_t items_max
Maximum number of items allowed in list; 0 means no limit.
Definition: px_link_list.h:81
struct px_link_list_item_s * first
Pointer to first item in the list.
Definition: px_link_list.h:78
struct px_link_list_item_s * last
Pointer to last item in the list.
Definition: px_link_list.h:79
struct px_link_list_item_s * prev
Pointer to previous item in the list.
Definition: px_link_list.h:72
bool px_link_list_is_empty(px_link_list_t *list)
See if the list is empty.
Definition: px_link_list.c:53
px_link_list_item_t * px_link_list_get_item_next(px_link_list_t *list, px_link_list_item_t *item)
Get a pointer to the next item in the list (after the specified item).
Definition: px_link_list.c:97
size_t px_link_list_get_item_count(px_link_list_t *list)
Get the number of items in the list.
Definition: px_link_list.c:82
px_link_list_item_t * px_link_list_remove_item_last(px_link_list_t *list)
Remove last item from the list.
Definition: px_link_list.c:281
bool px_link_list_insert_item_end(px_link_list_t *list, px_link_list_item_t *item)
Add item to the end of the list.
Definition: px_link_list.c:138
px_link_list_item_t * px_link_list_get_item_last(px_link_list_t *list)
Get a pointer to the last item in the list.
Definition: px_link_list.c:92
px_link_list_item_t * px_link_list_get_item_first(px_link_list_t *list)
Get a pointer to the first item in the list.
Definition: px_link_list.c:87
px_link_list_item_t * px_link_list_remove_item_first(px_link_list_t *list)
Remove first item from the list.
Definition: px_link_list.c:251
px_link_list_item_t * px_link_list_get_item_prev(px_link_list_t *list, px_link_list_item_t *item)
Get a pointer to the previous item in the list (before the specified item).
Definition: px_link_list.c:103
bool px_link_list_has_item(px_link_list_t *list, px_link_list_item_t *item)
See if item is in the list.
Definition: px_link_list.c:342
void px_link_list_init(px_link_list_t *list, size_t max_nr_of_items)
Initialises a linked list structure.
Definition: px_link_list.c:37
void px_link_list_remove_item(px_link_list_t *list, px_link_list_item_t *item)
Remove item from the list.
Definition: px_link_list.c:311
bool px_link_list_insert_item_after(px_link_list_t *list, px_link_list_item_t *item, px_link_list_item_t *item_pos)
Insert item after specified item.
Definition: px_link_list.c:209
bool px_link_list_is_full(px_link_list_t *list)
See if the list is full.
Definition: px_link_list.c:65
void px_link_list_item_init(px_link_list_t *list, px_link_list_item_t *item)
Initialises a list item.
Definition: px_link_list.c:46
bool px_link_list_insert_item_start(px_link_list_t *list, px_link_list_item_t *item)
Insert item to the start of the list.
Definition: px_link_list.c:109
bool px_link_list_insert_item_before(px_link_list_t *list, px_link_list_item_t *item, px_link_list_item_t *item_pos)
Insert item before specified item.
Definition: px_link_list.c:167