s/sliepen.warande.net/sliepen.eu.org/g
[oweals/tinc.git] / lib / list.c
1 /*
2     list.c -- functions to deal with double linked lists
3     Copyright (C) 2000,2001 Ivo Timmermans <ivo@o2w.nl>
4                   2000,2001 Guus Sliepen <guus@sliepen.eu.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: list.c,v 1.1.2.11 2002/06/21 10:11:11 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26
27 #include <xalloc.h>
28 #include <system.h>
29
30 #include "list.h"
31
32 /* (De)constructors */
33
34 list_t *list_alloc(list_action_t delete)
35 {
36   list_t *list;
37
38   list = xmalloc_and_zero(sizeof(list_t));
39   list->delete = delete;
40
41   return list;
42 }
43
44 void list_free(list_t *list)
45 {
46   free(list);
47 }
48
49 list_node_t *list_alloc_node(void)
50 {
51   list_node_t *node;
52   
53   node = xmalloc_and_zero(sizeof(list_node_t));
54   
55   return node;
56 }
57
58 void list_free_node(list_t *list, list_node_t *node)
59 {
60   if(node->data && list->delete)
61     list->delete(node->data);
62   
63   free(node);
64 }
65
66 /* Insertion and deletion */
67
68 list_node_t *list_insert_head(list_t *list, void *data)
69 {
70   list_node_t *node;
71   
72   node = list_alloc_node();
73   
74   node->data = data;
75   node->prev = NULL;
76   node->next = list->head;
77   list->head = node;
78   
79   if(node->next)
80     node->next->prev = node;
81   else
82     list->tail = node;
83
84   list->count++;
85
86   return node;
87 }
88
89 list_node_t *list_insert_tail(list_t *list, void *data)
90 {
91   list_node_t *node;
92   
93   node = list_alloc_node();
94   
95   node->data = data;
96   node->next = NULL;
97   node->prev = list->tail;
98   list->tail = node;
99   
100   if(node->prev)
101     node->prev->next = node;
102   else
103     list->head = node;
104
105   list->count++;
106   
107   return node;
108 }
109
110 void list_unlink_node(list_t *list, list_node_t *node)
111 {
112   if(node->prev)
113     node->prev->next = node->next;
114   else
115     list->head = node->next;
116     
117   if(node->next)
118     node->next->prev = node->prev;
119   else
120     list->tail = node->prev;
121
122   list->count--;
123 }
124
125 void list_delete_node(list_t *list, list_node_t *node)
126 {
127   list_unlink_node(list, node);
128   list_free_node(list, node);
129 }
130
131 void list_delete_head(list_t *list)
132 {
133   list_delete_node(list, list->head);
134 }
135
136 void list_delete_tail(list_t *list)
137 {
138   list_delete_node(list, list->tail);
139 }
140
141 /* Head/tail lookup */
142
143 void *list_get_head(list_t *list)
144 {
145   if(list->head)
146     return list->head->data;
147   else
148     return NULL;
149 }
150
151 void *list_get_tail(list_t *list)
152 {
153   if(list->tail)
154     return list->tail->data;
155   else
156     return NULL;
157 }
158
159 /* Fast list deletion */
160
161 void list_delete_list(list_t *list)
162 {
163   list_node_t *node, *next;
164   
165   for(node = list->head; node; node = next)
166     {
167       next = node->next;
168       list_free_node(list, node);
169     }
170
171   list_free(list);
172 }
173
174 /* Traversing */
175
176 void list_foreach_node(list_t *list, list_action_node_t action)
177 {
178   list_node_t *node, *next;
179
180   for(node = list->head; node; node = next)
181     {
182       next = node->next;
183       action(node);
184     }
185 }
186
187 void list_foreach(list_t *list, list_action_t action)
188 {
189   list_node_t *node, *next;
190
191   for(node = list->head; node; node = next)
192     {
193       next = node->next;
194       if(node->data)
195         action(node->data);
196     }
197 }