tommylist.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2// Copyright (C) 2010 Andrea Mazzoleni
3
135#ifndef TOMMY_LIST_H
136#define TOMMY_LIST_H
137
138#include "tommytypes.h"
139
140/******************************************************************************/
141/* list */
142
147
154typedef struct tommy_builder_struct {
157
164{
165 builder->dummy.next = 0;
166 return &builder->dummy;
167}
168
179{
180 head->prev = tail;
181 tail->next = head;
182 return last;
183}
184
190tommy_inline void tommy_list_init(tommy_list* list)
191{
192 *list = 0;
193}
194
204tommy_inline void tommy_list_clear(tommy_list* list)
205{
206 *list = 0;
207}
208
216{
217 return *list;
218}
219
227{
228 tommy_node* head = tommy_list_head(list);
229
230 if (!head)
231 return 0;
232
233 return head->prev;
234}
235
247{
248 if (node == tommy_list_head(list))
249 return 0;
250
251 return node->prev;
252}
253
259tommy_inline void tommy_list_insert_first(tommy_list* list, tommy_node* node)
260{
261 /* one element "circular" prev list */
262 node->prev = node;
263
264 /* one element "0 terminated" next list */
265 node->next = 0;
266
267 *list = node;
268}
269
276tommy_inline void tommy_list_insert_head_not_empty(tommy_list* list, tommy_node* node)
277{
278 tommy_node* head = tommy_list_head(list);
279
280 /* insert in the "circular" prev list */
281 node->prev = head->prev;
282 head->prev = node;
283
284 /* insert in the "0 terminated" next list */
285 node->next = head;
286
287 *list = node;
288}
289
296tommy_inline void tommy_list_insert_tail_not_empty(tommy_node* head, tommy_node* node)
297{
298 /* insert in the "circular" prev list */
299 node->prev = head->prev;
300 head->prev = node;
301
302 /* insert in the "0 terminated" next list */
303 node->next = 0;
304 node->prev->next = node;
305}
306
313tommy_inline void tommy_list_insert_head(tommy_list* list, tommy_node* node, void* data)
314{
315 tommy_node* head = tommy_list_head(list);
316
317 if (head)
318 tommy_list_insert_head_not_empty(list, node);
319 else
320 tommy_list_insert_first(list, node);
321
322 node->data = data;
323}
324
331tommy_inline void tommy_list_insert_tail(tommy_list* list, tommy_node* node, void* data)
332{
333 tommy_node* head = tommy_list_head(list);
334
335 if (head)
336 tommy_list_insert_tail_not_empty(head, node);
337 else
338 tommy_list_insert_first(list, node);
339
340 node->data = data;
341}
342
350tommy_inline void tommy_list_insert_before(tommy_list* list, tommy_node* reference, tommy_node* node, void* data)
351{
352 tommy_node* head = tommy_list_head(list);
353
354 /* if inserting before the head, update the list pointer */
355 if (head == reference) {
356 tommy_list_insert_head_not_empty(list, node);
357 } else {
358 /* insert in the "circular" prev list */
359 node->prev = reference->prev;
360 reference->prev = node;
361
362 /* insert in the "0 terminated" next list */
363 node->next = reference;
364 node->prev->next = node;
365 }
366
367 node->data = data;
368}
369
377tommy_inline void tommy_list_insert_after(tommy_list* list, tommy_node* reference, tommy_node* node, void* data)
378{
379 tommy_node* head = tommy_list_head(list);
380
381 /* if inserting after the tail (reference->next == 0), handle specially */
382 if (reference->next == 0) {
383 tommy_list_insert_tail_not_empty(head, node);
384 } else {
385 /* insert in the "circular" prev list */
386 node->prev = reference;
387 reference->next->prev = node;
388
389 /* insert in the "0 terminated" next list */
390 node->next = reference->next;
391 reference->next = node;
392 }
393
394 node->data = data;
395}
396
406tommy_inline void* tommy_list_remove_existing(tommy_list* list, tommy_node* node)
407{
408 tommy_node* head = tommy_list_head(list);
409
410 /* remove from the "circular" prev list */
411 if (node->next)
412 node->next->prev = node->prev;
413 else
414 head->prev = node->prev; /* the last */
415
416 /* remove from the "0 terminated" next list */
417 if (head == node)
418 *list = node->next; /* the new head, in case 0 */
419 else
420 node->prev->next = node->next;
421
422 return node->data;
423}
424
430tommy_inline void* tommy_list_remove_head(tommy_list* list)
431{
432 tommy_node* node = tommy_list_head(list);
433
434 if (!node)
435 return 0;
436
437 /* the new head must retain the tail pointer in the "circular" prev list */
438 if (node->next)
439 node->next->prev = node->prev;
440
441 *list = node->next;
442
443 return node->data;
444}
445
451tommy_inline void* tommy_list_remove_tail(tommy_list* list)
452{
453 tommy_node* head = tommy_list_head(list);
454
455 if (!head)
456 return 0;
457
458 tommy_node* node = head->prev;
459
460 /* a single node is also the head, and its links must remain unchanged */
461 if (node == head) {
462 *list = 0;
463 } else {
464 head->prev = node->prev;
465 node->prev->next = 0;
466 }
467
468 return node->data;
469}
470
478tommy_inline void tommy_list_move_head(tommy_list* list, tommy_node* node)
479{
480 if (node == tommy_list_head(list))
481 return;
482
483 tommy_list_remove_existing(list, node);
484 tommy_list_insert_head_not_empty(list, node);
485}
486
494tommy_inline void tommy_list_move_tail(tommy_list* list, tommy_node* node)
495{
496 if (node->next == 0)
497 return;
498
499 tommy_list_remove_existing(list, node);
500 tommy_list_insert_tail_not_empty(tommy_list_head(list), node);
501}
502
518tommy_inline void tommy_list_split(tommy_list* list, tommy_node* node, tommy_list* second)
519{
520 tommy_node* head = tommy_list_head(list);
521 tommy_node* tail;
522 tommy_node* before;
523
524 if (!node) {
525 *second = 0;
526 return;
527 }
528
529 if (node == head) {
530 *second = *list;
531 *list = 0;
532 return;
533 }
534
535 tail = head->prev;
536 before = node->prev;
537
538 /* terminate the retained list */
539 head->prev = before;
540 before->next = 0;
541
542 /* create the list of transferred nodes */
543 node->prev = tail;
544 *second = node;
545}
546
556tommy_inline void tommy_list_swap(tommy_list* first, tommy_list* second)
557{
558 tommy_list tmp = *first;
559 *first = *second;
560 *second = tmp;
561}
562
571tommy_inline void tommy_list_concat(tommy_list* first, tommy_list* second)
572{
573 /* if the second is empty, nothing to do */
574 tommy_node* second_head = tommy_list_head(second);
575 if (second_head == 0)
576 return;
577
578 /* if the first is empty, copy the second */
579 tommy_node* first_head = tommy_list_head(first);
580 if (first_head == 0) {
581 *first = *second;
582 return;
583 }
584
585 /* tail of the first list */
586 tommy_node* first_tail = first_head->prev;
587
588 /* set the "circular" prev list */
589 first_head->prev = second_head->prev;
590 second_head->prev = first_tail;
591
592 /* set the "0 terminated" next list */
593 first_tail->next = second_head;
594}
595
604tommy_inline void tommy_list_concat_builder(tommy_list* list, const tommy_builder* builder, tommy_node* tail)
605{
606 tommy_node* head = builder->dummy.next;
607
608 if (!head)
609 return;
610
611 tommy_node* first = *list;
612
613 /* replace the sentinel link so no list node refers to the temporary builder */
614 if (first) {
615 tommy_node* first_tail = first->prev;
616 first_tail->next = head;
617 head->prev = first_tail;
618 first->prev = tail;
619 } else {
620 *list = head;
621 head->prev = tail;
622 }
623
624 tail->next = 0;
625}
626
635tommy_inline void tommy_list_prepend(tommy_list* first, tommy_list* second)
636{
637 tommy_list_concat(second, first);
638 *first = *second;
639}
640
652TOMMY_API void tommy_list_splice_before(tommy_list* list, tommy_node* reference, tommy_list* second);
653
665TOMMY_API void tommy_list_splice_after(tommy_list* list, tommy_node* reference, tommy_list* second);
666
683TOMMY_API void tommy_list_merge(tommy_list* first, tommy_list* second, tommy_compare_func* cmp);
684
692TOMMY_API void tommy_list_reverse(tommy_list* list);
693
704
711tommy_inline tommy_bool_t tommy_list_empty(const tommy_list* list)
712{
713 return *list == 0;
714}
715
722tommy_inline tommy_size_t tommy_list_count(const tommy_list* list)
723{
724 tommy_size_t count = 0;
725 const tommy_node* i = *list;
726
727 while (i) {
728 ++count;
729 i = i->next;
730 }
731
732 return count;
733}
734
770tommy_inline void tommy_list_foreach(tommy_list* list, tommy_foreach_func* func)
771{
772 tommy_node* node = tommy_list_head(list);
773
774 while (node) {
775 void* data = node->data;
776 node = node->next;
777 func(data);
778 }
779}
780
790tommy_inline void tommy_list_foreach_arg(tommy_list* list, tommy_foreach_arg_func* func, void* arg)
791{
792 tommy_node* node = tommy_list_head(list);
793
794 while (node) {
795 void* data = node->data;
796 node = node->next;
797 func(arg, data);
798 }
799}
800
801#endif
802
Temporary list builder with an embedded sentinel.
Definition: tommylist.h:154
tommy_node dummy
Sentinel excluded from the resulting list.
Definition: tommylist.h:155
Data structure node.
Definition: tommytypes.h:242
struct tommy_node_struct * next
Next node.
Definition: tommytypes.h:247
struct tommy_node_struct * prev
Previous node.
Definition: tommytypes.h:253
void * data
Pointer to the element containing the node.
Definition: tommytypes.h:260
void * tommy_list_remove_tail(tommy_list *list)
Removes the element at the tail of the list.
Definition: tommylist.h:451
void tommy_list_insert_after(tommy_list *list, tommy_node *reference, tommy_node *node, void *data)
Inserts an element after the specified reference node.
Definition: tommylist.h:377
TOMMY_API void tommy_list_reverse(tommy_list *list)
Reverses the order of the elements in a list.
tommy_node * tommy_builder_concat(tommy_node *tail, tommy_node *head, tommy_node *last)
Appends a non-empty chain to a builder in O(1), preserving element order.
Definition: tommylist.h:178
void tommy_list_move_head(tommy_list *list, tommy_node *node)
Moves an existing node to the head of the list.
Definition: tommylist.h:478
void tommy_list_foreach_arg(tommy_list *list, tommy_foreach_arg_func *func, void *arg)
Calls the specified function with an argument for each element in the list.
Definition: tommylist.h:790
void tommy_list_split(tommy_list *list, tommy_node *node, tommy_list *second)
Splits a list before the specified node.
Definition: tommylist.h:518
tommy_size_t tommy_list_count(const tommy_list *list)
Gets the number of elements.
Definition: tommylist.h:722
TOMMY_API void tommy_list_merge(tommy_list *first, tommy_list *second, tommy_compare_func *cmp)
Merges two sorted lists.
void tommy_list_init(tommy_list *list)
Initializes the list.
Definition: tommylist.h:190
void tommy_list_swap(tommy_list *first, tommy_list *second)
Exchanges the contents of two lists.
Definition: tommylist.h:556
void tommy_list_insert_head(tommy_list *list, tommy_node *node, void *data)
Inserts an element at the head of a list.
Definition: tommylist.h:313
void tommy_list_prepend(tommy_list *first, tommy_list *second)
Prepends a list to another list.
Definition: tommylist.h:635
tommy_node * tommy_list
Doubly linked list type.
Definition: tommylist.h:146
tommy_node * tommy_list_prev(tommy_list *list, tommy_node *node)
Gets the previous node in the list.
Definition: tommylist.h:246
void * tommy_list_remove_existing(tommy_list *list, tommy_node *node)
Removes an element from the list.
Definition: tommylist.h:406
TOMMY_API void tommy_list_splice_after(tommy_list *list, tommy_node *reference, tommy_list *second)
Transfers all elements of a list after the specified reference node.
void tommy_list_clear(tommy_list *list)
Removes all elements.
Definition: tommylist.h:204
struct tommy_builder_struct tommy_builder
Temporary list builder with an embedded sentinel.
tommy_node * tommy_list_tail(tommy_list *list)
Gets the tail of the list.
Definition: tommylist.h:226
void tommy_list_insert_tail(tommy_list *list, tommy_node *node, void *data)
Inserts an element at the tail of a list.
Definition: tommylist.h:331
void tommy_list_concat(tommy_list *first, tommy_list *second)
Concatenates two lists.
Definition: tommylist.h:571
void tommy_list_insert_before(tommy_list *list, tommy_node *reference, tommy_node *node, void *data)
Inserts an element before the specified reference node.
Definition: tommylist.h:350
TOMMY_API void tommy_list_sort(tommy_list *list, tommy_compare_func *cmp)
Sorts a list.
void tommy_list_move_tail(tommy_list *list, tommy_node *node)
Moves an existing node to the tail of the list.
Definition: tommylist.h:494
tommy_node * tommy_builder_init(tommy_builder *builder)
Initializes an empty builder in O(1).
Definition: tommylist.h:163
void * tommy_list_remove_head(tommy_list *list)
Removes the element at the head of the list.
Definition: tommylist.h:430
tommy_node * tommy_list_head(tommy_list *list)
Gets the head of the list.
Definition: tommylist.h:215
TOMMY_API void tommy_list_splice_before(tommy_list *list, tommy_node *reference, tommy_list *second)
Transfers all elements of a list before the specified reference node.
tommy_bool_t tommy_list_empty(const tommy_list *list)
Checks whether the list is empty.
Definition: tommylist.h:711
void tommy_list_concat_builder(tommy_list *list, const tommy_builder *builder, tommy_node *tail)
Appends a builder's chain to a list, preserving element order and restoring list links.
Definition: tommylist.h:604
void tommy_list_foreach(tommy_list *list, tommy_foreach_func *func)
Calls the specified function for each element in the list.
Definition: tommylist.h:770
Generic types.
void tommy_foreach_func(void *elem)
Foreach function.
Definition: tommytypes.h:346
int tommy_compare_func(const void *elem_a, const void *elem_b)
Compare function for elements.
Definition: tommytypes.h:302
uint64_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:40
void tommy_foreach_arg_func(void *arg, void *elem)
Foreach function with an argument.
Definition: tommytypes.h:353
int tommy_bool_t
Generic boolean type.
Definition: tommytypes.h:53