4d3c71a38a1735225052df3afa295a1e8915ace6
[oweals/gnunet.git] / src / util / container_slist.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/container_slist.c
23  * @brief Implementation of a singly-linked list
24  * @author Nils Durner
25  */
26
27 #include "platform.h"
28 #include "gnunet_container_lib.h"
29
30 /**
31  * Element in our linked list.
32  */
33 struct GNUNET_CONTAINER_SList_Elem
34 {
35   /**
36    * This is a linked list.
37    */ 
38   struct GNUNET_CONTAINER_SList_Elem *next;
39
40   /**
41    * Application data stored at this element.
42    */
43   void *elem;
44
45   /**
46    * Number of bytes stored in elem.
47    */
48   size_t len;
49
50   /**
51    * Disposition of the element.
52    */
53   enum GNUNET_CONTAINER_SListDisposition disp;
54 };
55
56
57 /**
58  * Handle to a singly linked list  
59  */
60 struct GNUNET_CONTAINER_SList
61 {
62   /**
63    * Head of the linked list.
64    */
65   struct GNUNET_CONTAINER_SList_Elem *head;
66
67   /**
68    * Number of elements in the list.
69    */
70   unsigned int length;
71 };
72
73
74 /**
75  * Handle to a singly linked list iterator 
76  */
77 struct GNUNET_CONTAINER_SList_Iterator
78 {
79   /**
80    * Linked list that we are iterating over.
81    */
82   struct GNUNET_CONTAINER_SList *list;
83
84   /**
85    * Last element accessed.
86    */
87   struct GNUNET_CONTAINER_SList_Elem *last;
88
89   /**
90    * Current list element.
91    */
92   struct GNUNET_CONTAINER_SList_Elem *elem;
93 };
94
95 /**
96  * Create a new element that is to be inserted into the list
97  * @internal
98  * @param disp memory disposition
99  * @param buf payload buffer
100  * @param len length of the buffer
101  * @return a new element
102  */
103 static struct GNUNET_CONTAINER_SList_Elem *
104 create_elem (enum GNUNET_CONTAINER_SListDisposition disp, 
105              const void *buf, 
106              size_t len)
107 {
108   struct GNUNET_CONTAINER_SList_Elem *e;
109
110   if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
111     {
112       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
113       memcpy (&e[1], buf, len);
114       e->elem = (void*) &e[1];
115     }
116   else
117     {
118       e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
119       e->elem = (void*) buf;
120     }
121   e->disp = disp;
122   e->len = len;
123   return e;
124 }
125
126
127 /**
128  * Add a new element to the list
129  * @param l list
130  * @param disp memory disposition
131  * @param buf payload buffer
132  * @param len length of the buffer
133  */
134 void
135 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, 
136                             enum GNUNET_CONTAINER_SListDisposition disp, 
137                             const void *buf, size_t len)
138 {
139   struct GNUNET_CONTAINER_SList_Elem *e;
140
141   e = create_elem (disp, buf, len);
142   e->next = l->head;
143   l->head = e;
144   l->length++;
145 }
146
147
148 /**
149  * Create a new singly linked list
150  * @return the new list
151  */
152 struct GNUNET_CONTAINER_SList *
153 GNUNET_CONTAINER_slist_create ()
154 {
155   return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
156 }
157
158
159 /**
160  * Destroy a singly linked list
161  * @param l the list to be destroyed
162  */
163 void
164 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
165 {
166   GNUNET_CONTAINER_slist_clear (l);
167   GNUNET_free (l);
168 }
169
170
171 /**
172  * Return the beginning of a list
173  * @param l list
174  * @return iterator pointing to the beginning
175  */
176 struct GNUNET_CONTAINER_SList_Iterator *
177 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
178 {
179   struct GNUNET_CONTAINER_SList_Iterator *ret;
180
181   ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
182   ret->elem = l->head;
183   ret->list = l;
184   return ret;
185 }
186
187
188 /**
189  * Clear a list
190  * @param l list
191  */
192 void
193 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
194 {
195   struct GNUNET_CONTAINER_SList_Elem *e;
196   struct GNUNET_CONTAINER_SList_Elem *n;
197
198   e = l->head;
199   while (e != NULL)
200     {
201       n = e->next;
202       if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
203         GNUNET_free (e->elem);
204       GNUNET_free (e);
205       e = n;
206     }
207   l->head = NULL;
208   l->length = 0;
209 }
210
211
212 /**
213  * Check if a list contains a certain element
214  *
215  * @param l list
216  * @param buf payload buffer to find
217  * @param lenght of the payload
218  */
219 int
220 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
221                                  const void *buf, size_t len)
222 {
223   struct GNUNET_CONTAINER_SList_Elem *e;
224
225   for (e = l->head; e != NULL; e = e->next)
226     if ( (e->len == len) && 
227          (memcmp (buf, e->elem, len) == 0) )
228       return GNUNET_YES;
229   return GNUNET_NO;
230 }
231
232
233 /**
234  * Count the elements of a list
235  * @param l list
236  * @return number of elements in the list
237  */
238 int
239 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
240 {
241   return l->length;
242 }
243
244
245 /**
246  * Remove an element from the list
247  *
248  * @param i iterator that points to the element to be removed
249  */
250 void
251 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
252 {
253   struct GNUNET_CONTAINER_SList_Elem *next;
254
255   next = i->elem->next;
256   if (i->last != NULL)
257     i->last->next = next;
258   else
259     i->list->head = next;
260   if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
261     GNUNET_free (i->elem->elem);
262   GNUNET_free (i->elem);
263   i->list->length--;
264   i->elem = next;
265 }
266
267
268 /**
269  * Insert an element into a list at a specific position
270  * @param before where to insert the new element
271  * @param disp memory disposition
272  * @param buf payload buffer
273  * @param len length of the payload
274  */
275 void
276 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
277                                enum GNUNET_CONTAINER_SListDisposition disp, 
278                                const void *buf, size_t len)
279 {
280   struct GNUNET_CONTAINER_SList_Elem *e;
281
282   e = create_elem (disp, buf, len);
283   e->next = before->elem;
284   if (before->last != NULL)
285     before->last->next = e;
286   else
287     before->list->head = e;
288   before->list->length++;
289 }
290
291
292 /**
293  * Advance an iterator to the next element
294  * @param i iterator
295  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
296  */
297 int
298 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
299 {
300   i->last = i->elem;
301   i->elem = i->elem->next;
302
303   return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
304 }
305
306
307 /**
308  * Check if an iterator points beyond the end of a list
309  *
310  * @param i iterator
311  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
312  *         points to a valid element
313  */
314 int
315 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
316 {
317   return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
318 }
319
320
321 /**
322  * Retrieve the element at a specific position in a list
323  * @param i iterator
324  * @param len payload length
325  * @return payload
326  */
327 const void *
328 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
329                             size_t * len)
330 {
331   if (len)
332     *len = i->elem->len;
333   return i->elem->elem;
334 }
335
336 /* end of container_slist.c */