xref: /freebsd/contrib/pkgconf/libpkgconf/iter.h (revision a3cefe7f2b4df0f70ff92d4570ce18e517af43ec)
1 /*
2  * iter.h
3  * Linked lists and iterators.
4  *
5  * Copyright (c) 2013 pkgconf authors (see AUTHORS).
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * This software is provided 'as is' and without any warranty, express or
12  * implied.  In no event shall the authors be liable for any damages arising
13  * from the use of this software.
14  */
15 
16 #ifndef LIBPKGCONF_ITER_H
17 #define LIBPKGCONF_ITER_H
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 typedef struct pkgconf_node_ pkgconf_node_t;
24 
25 struct pkgconf_node_ {
26 	pkgconf_node_t *prev, *next;
27 	void *data;
28 };
29 
30 typedef struct {
31 	pkgconf_node_t *head, *tail;
32 	size_t length;
33 } pkgconf_list_t;
34 
35 #define PKGCONF_LIST_INITIALIZER		{ NULL, NULL, 0 }
36 
37 static inline void
pkgconf_list_zero(pkgconf_list_t * list)38 pkgconf_list_zero(pkgconf_list_t *list)
39 {
40 	list->head = NULL;
41 	list->tail = NULL;
42 	list->length = 0;
43 }
44 
45 static inline void
pkgconf_node_insert(pkgconf_node_t * node,void * data,pkgconf_list_t * list)46 pkgconf_node_insert(pkgconf_node_t *node, void *data, pkgconf_list_t *list)
47 {
48 	pkgconf_node_t *tnode;
49 
50 	node->data = data;
51 
52 	if (list->head == NULL)
53 	{
54 		list->head = node;
55 		list->tail = node;
56 		list->length = 1;
57 		return;
58 	}
59 
60 	tnode = list->head;
61 
62 	node->next = tnode;
63 	tnode->prev = node;
64 
65 	list->head = node;
66 	list->length++;
67 }
68 
69 static inline void
pkgconf_node_insert_tail(pkgconf_node_t * node,void * data,pkgconf_list_t * list)70 pkgconf_node_insert_tail(pkgconf_node_t *node, void *data, pkgconf_list_t *list)
71 {
72 	pkgconf_node_t *tnode;
73 
74 	node->data = data;
75 
76 	if (list->tail == NULL)
77 	{
78 		list->head = node;
79 		list->tail = node;
80 		list->length = 1;
81 		return;
82 	}
83 
84 	tnode = list->tail;
85 
86 	node->prev = tnode;
87 	tnode->next = node;
88 
89 	list->tail = node;
90 	list->length++;
91 }
92 
93 static inline void
pkgconf_node_delete(pkgconf_node_t * node,pkgconf_list_t * list)94 pkgconf_node_delete(pkgconf_node_t *node, pkgconf_list_t *list)
95 {
96 	list->length--;
97 
98 	if (node->prev == NULL)
99 		list->head = node->next;
100 	else
101 		node->prev->next = node->next;
102 
103 	if (node->next == NULL)
104 		list->tail = node->prev;
105 	else
106 		node->next->prev = node->prev;
107 }
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif
114