indentation
[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    * Tail of the linked list.
69    */
70   struct GNUNET_CONTAINER_SList_Elem *tail;
71
72   /**
73    * Number of elements in the list.
74    */
75   unsigned int length;
76 };
77
78
79 /**
80  * Handle to a singly linked list iterator 
81  */
82 struct GNUNET_CONTAINER_SList_Iterator
83 {
84   /**
85    * Linked list that we are iterating over.
86    */
87   struct GNUNET_CONTAINER_SList *list;
88
89   /**
90    * Last element accessed.
91    */
92   struct GNUNET_CONTAINER_SList_Elem *last;
93
94   /**
95    * Current list element.
96    */
97   struct GNUNET_CONTAINER_SList_Elem *elem;
98 };
99
100
101 /**
102  * Create a new element that is to be inserted into the list
103  * @internal
104  * @param disp memory disposition
105  * @param buf payload buffer
106  * @param len length of the buffer
107  * @return a new element
108  */
109 static struct GNUNET_CONTAINER_SList_Elem *
110 create_elem (enum GNUNET_CONTAINER_SListDisposition disp,
111              const void *buf, size_t len)
112 {
113   struct GNUNET_CONTAINER_SList_Elem *e;
114
115   if (disp == GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT)
116   {
117     e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
118     memcpy (&e[1], buf, len);
119     e->elem = (void *) &e[1];
120   }
121   else
122   {
123     e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
124     e->elem = (void *) buf;
125   }
126   e->disp = disp;
127   e->len = len;
128   return e;
129 }
130
131
132 /**
133  * Add a new element to the list
134  * @param l list
135  * @param disp memory disposition
136  * @param buf payload buffer
137  * @param len length of the buffer
138  */
139 void
140 GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
141                             enum GNUNET_CONTAINER_SListDisposition disp,
142                             const void *buf, size_t len)
143 {
144   struct GNUNET_CONTAINER_SList_Elem *e;
145
146   e = create_elem (disp, buf, len);
147   e->next = l->head;
148   l->head = e;
149   if (l->tail == NULL)
150     l->tail = e;
151   l->length++;
152 }
153
154 /**
155  * Add a new element to the end of the list
156  * @param l list
157  * @param disp memory disposition
158  * @param buf payload buffer
159  * @param len length of the buffer
160  */
161 void
162 GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
163                                 enum GNUNET_CONTAINER_SListDisposition disp,
164                                 const void *buf, size_t len)
165 {
166   struct GNUNET_CONTAINER_SList_Elem *e;
167
168   e = create_elem (disp, buf, len);
169   if (l->tail != NULL)
170     l->tail->next = e;
171   if (l->head == NULL)
172     l->head = e;
173   l->tail = e;
174   l->length++;
175 }
176
177
178 /**
179  * Append a singly linked list to another
180  * @param dst list to append to
181  * @param src source
182  */
183 void
184 GNUNET_CONTAINER_slist_append (struct GNUNET_CONTAINER_SList *dst,
185                                struct GNUNET_CONTAINER_SList *src)
186 {
187   struct GNUNET_CONTAINER_SList_Iterator *i;
188
189   for (i = GNUNET_CONTAINER_slist_begin (src); GNUNET_CONTAINER_slist_end (i) !=
190        GNUNET_YES; GNUNET_CONTAINER_slist_next (i))
191
192   {
193     GNUNET_CONTAINER_slist_add (dst,
194                                 (i->elem->disp ==
195                                  GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC) ?
196                                 GNUNET_CONTAINER_SLIST_DISPOSITION_STATIC :
197                                 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
198                                 i->elem->elem, i->elem->len);
199   }
200   GNUNET_CONTAINER_slist_iter_destroy (i);
201 }
202
203
204 /**
205  * Create a new singly linked list
206  * @return the new list
207  */
208 struct GNUNET_CONTAINER_SList *
209 GNUNET_CONTAINER_slist_create ()
210 {
211   return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
212 }
213
214
215 /**
216  * Destroy a singly linked list
217  * @param l the list to be destroyed
218  */
219 void
220 GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l)
221 {
222   GNUNET_CONTAINER_slist_clear (l);
223   GNUNET_free (l);
224 }
225
226
227 /**
228  * Return the beginning of a list
229  * @param l list
230  * @return iterator pointing to the beginning
231  */
232 struct GNUNET_CONTAINER_SList_Iterator *
233 GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
234 {
235   struct GNUNET_CONTAINER_SList_Iterator *ret;
236
237   ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
238   ret->elem = l->head;
239   ret->list = l;
240   return ret;
241 }
242
243
244 /**
245  * Clear a list
246  * @param l list
247  */
248 void
249 GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
250 {
251   struct GNUNET_CONTAINER_SList_Elem *e;
252   struct GNUNET_CONTAINER_SList_Elem *n;
253
254   e = l->head;
255   while (e != NULL)
256   {
257     n = e->next;
258     if (e->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
259       GNUNET_free (e->elem);
260     GNUNET_free (e);
261     e = n;
262   }
263   l->head = NULL;
264   l->tail = NULL;
265   l->length = 0;
266 }
267
268
269 /**
270  * Check if a list contains a certain element
271  *
272  * @param l list
273  * @param buf payload buffer to find
274  * @param len length of the payload (number of bytes in buf)
275  */
276 int
277 GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l,
278                                  const void *buf, size_t len)
279 {
280   struct GNUNET_CONTAINER_SList_Elem *e;
281
282   for (e = l->head; e != NULL; e = e->next)
283     if ((e->len == len) && (memcmp (buf, e->elem, len) == 0))
284       return GNUNET_YES;
285   return GNUNET_NO;
286 }
287
288
289 /**
290  * Count the elements of a list
291  * @param l list
292  * @return number of elements in the list
293  */
294 int
295 GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
296 {
297   return l->length;
298 }
299
300
301 /**
302  * Remove an element from the list
303  *
304  * @param i iterator that points to the element to be removed
305  */
306 void
307 GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
308 {
309   struct GNUNET_CONTAINER_SList_Elem *next;
310
311   next = i->elem->next;
312   if (i->last != NULL)
313     i->last->next = next;
314   else
315     i->list->head = next;
316   if (next == NULL)
317     i->list->tail = i->last;
318   if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
319     GNUNET_free (i->elem->elem);
320   GNUNET_free (i->elem);
321   i->list->length--;
322   i->elem = next;
323 }
324
325
326 /**
327  * Insert an element into a list at a specific position
328  * @param before where to insert the new element
329  * @param disp memory disposition
330  * @param buf payload buffer
331  * @param len length of the payload
332  */
333 void
334 GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
335                                enum GNUNET_CONTAINER_SListDisposition disp,
336                                const void *buf, size_t len)
337 {
338   struct GNUNET_CONTAINER_SList_Elem *e;
339
340   e = create_elem (disp, buf, len);
341   e->next = before->elem;
342   if (before->last != NULL)
343     before->last->next = e;
344   else
345     before->list->head = e;
346   if (e->next == NULL)
347     before->list->tail = e;
348   before->list->length++;
349 }
350
351
352 /**
353  * Advance an iterator to the next element
354  * @param i iterator
355  * @return GNUNET_YES on success, GNUNET_NO if the end has been reached
356  */
357 int
358 GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i)
359 {
360   i->last = i->elem;
361   i->elem = i->elem->next;
362
363   return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
364 }
365
366
367 /**
368  * Check if an iterator points beyond the end of a list
369  *
370  * @param i iterator
371  * @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
372  *         points to a valid element
373  */
374 int
375 GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
376 {
377   return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
378 }
379
380
381 /**
382  * Retrieve the element at a specific position in a list
383  * @param i iterator
384  * @param len payload length
385  * @return payload
386  */
387 void *
388 GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
389                             size_t * len)
390 {
391   if (len)
392     *len = i->elem->len;
393   return i->elem->elem;
394 }
395
396 /**
397  * Release an iterator
398  * @param i iterator
399  */
400 void
401 GNUNET_CONTAINER_slist_iter_destroy (struct GNUNET_CONTAINER_SList_Iterator *i)
402 {
403   GNUNET_free (i);
404 }
405
406 /* end of container_slist.c */