27d9db64b68f6698719e2e4b128ba0db6908c3eb
[strongswan.git] / Source / charon / utils / linked_list.c
1 /**
2 * @file linked_list.c
3 *
4 * @brief Implementation of linked_list_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdlib.h>
24
25 #include "linked_list.h"
26
27 #include <utils/allocator.h>
28
29
30 typedef struct linked_list_element_t linked_list_element_t;
31
32 /**
33 * @brief Element of the linked_list.
34 *
35 * This element holds a pointer to the value of the list item itself.
36 */
37 struct linked_list_element_t {
38 /**
39 * Value of a list item.
40 */
41 void *value;
42
43 /**
44 * previous list element
45 * NULL if first element in list
46 */
47 linked_list_element_t *previous;
48
49 /**
50 * next list element
51 * NULL if last element in list
52 */
53 linked_list_element_t *next;
54
55 /**
56 * Destroys a linked_list_element object.
57 *
58 * @param linked_list_element_t calling object
59 */
60 void (*destroy) (linked_list_element_t *this);
61 };
62
63 /**
64 * Implementation of linked_list_element_t.destroy.
65 */
66 static void linked_list_element_destroy(linked_list_element_t *this)
67 {
68 allocator_free(this);
69 }
70
71 /**
72 * @brief Creates an empty linked list object.
73 *
74 * @warning Only the pointer to the value is stored.
75 *
76 * @param[in] value value of item to be set
77 * @return linked_list_element_t object
78 */
79
80 linked_list_element_t *linked_list_element_create(void *value)
81 {
82 linked_list_element_t *this = allocator_alloc_thing(linked_list_element_t);
83
84 this->destroy = linked_list_element_destroy;
85
86 this->previous=NULL;
87 this->next=NULL;
88 this->value = value;
89
90 return (this);
91 }
92
93
94 typedef struct private_linked_list_t private_linked_list_t;
95 /**
96 * Private variables and functions of linked list.
97 *
98 */
99 struct private_linked_list_t {
100 /**
101 * Public part of linked list.
102 */
103 linked_list_t public;
104
105 /**
106 * Number of items in the list.
107 */
108 int count;
109
110 /**
111 * First element in list.
112 * NULL if no elements in list.
113 */
114 linked_list_element_t *first;
115
116 /**
117 * Last element in list.
118 * NULL if no elements in list.
119 */
120 linked_list_element_t *last;
121 };
122
123
124 typedef struct private_iterator_t private_iterator_t;
125
126 /**
127 * Private variables and functions of linked list iterator.
128 */
129 struct private_iterator_t {
130 /**
131 * Public part of linked list iterator.
132 */
133 iterator_t public;
134
135 /**
136 * Associated linked list.
137 */
138 private_linked_list_t * list;
139
140 /**
141 * Current element of the iterator.
142 */
143 linked_list_element_t *current;
144
145 /**
146 * Direction of iterator.
147 */
148 bool forward;
149 };
150
151 /**
152 * Implementation of iterator_t.has_next.
153 */
154 bool iterator_has_next(private_iterator_t *this)
155 {
156 if (this->list->count == 0)
157 {
158 return FALSE;
159 }
160 if (this->current == NULL)
161 {
162 this->current = (this->forward) ? this->list->first : this->list->last;
163 return TRUE;
164 }
165 if (this->forward)
166 {
167 if (this->current->next == NULL)
168 {
169 return FALSE;
170 }
171 this->current = this->current->next;
172 return TRUE;
173 }
174 /* backward */
175 if (this->current->previous == NULL)
176 {
177 return FALSE;
178 }
179 this->current = this->current->previous;
180 return TRUE;
181 }
182
183 /**
184 * Implementation of iterator_t.current.
185 */
186 static status_t iterator_current(private_iterator_t *this, void **value)
187 {
188 if (this->current == NULL)
189 {
190 return NOT_FOUND;
191 }
192 *value = this->current->value;
193 return SUCCESS;
194 }
195
196 /**
197 * Implementation of iterator_t.reset.
198 */
199 static void iterator_reset(private_iterator_t *this)
200 {
201 this->current = NULL;
202 }
203
204 /**
205 * Implementation of iterator_t.remove.
206 */
207 static status_t remove(private_iterator_t *this)
208 {
209 linked_list_element_t *new_current;
210
211 if (this->current == NULL)
212 {
213 return NOT_FOUND;
214 }
215
216 if (this->list->count == 0)
217 {
218 return NOT_FOUND;
219 }
220 /* find out the new iterator position */
221 if (this ->current->previous != NULL)
222 {
223 new_current = this->current->previous;
224 }
225 else if (this->current->next != NULL)
226 {
227 new_current = this->current->next;
228 }
229 else
230 {
231 new_current = NULL;
232 }
233
234 /* now delete the entry :-) */
235 if (this->current->previous == NULL)
236 {
237 if (this->current->next == NULL)
238 {
239 this->list->first = NULL;
240 this->list->last = NULL;
241 }
242 else
243 {
244 this->current->next->previous = NULL;
245 this->list->first = this->current->next;
246 }
247 }
248 else if (this->current->next == NULL)
249 {
250 this->current->previous->next = NULL;
251 this->list->last = this->current->previous;
252 }
253 else
254 {
255 this->current->previous->next = this->current->next;
256 this->current->next->previous = this->current->previous;
257 }
258
259 this->list->count--;
260 this->current->destroy(this->current);
261 /* set the new iterator position */
262 this->current = new_current;
263 return SUCCESS;
264 }
265
266 /**
267 * Implementation of iterator_t.insert_before.
268 */
269 static void insert_before(private_iterator_t * iterator, void *item)
270 {
271 if (iterator->current == NULL)
272 {
273 iterator->list->public.insert_first(&(iterator->list->public), item);
274 }
275
276 linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
277
278 if (iterator->current->previous == NULL)
279 {
280 iterator->current->previous = element;
281 element->next = iterator->current;
282 iterator->list->first = element;
283 }
284 else
285 {
286 iterator->current->previous->next = element;
287 element->previous = iterator->current->previous;
288 iterator->current->previous = element;
289 element->next = iterator->current;
290 }
291
292 iterator->list->count++;
293 }
294
295 /**
296 * Implementation of iterator_t.insert_after.
297 */
298 static void insert_after(private_iterator_t * iterator, void *item)
299 {
300 if (iterator->current == NULL)
301 {
302 iterator->list->public.insert_first(&(iterator->list->public),item);
303 }
304
305 linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
306
307 if (iterator->current->next == NULL)
308 {
309 iterator->current->next = element;
310 element->previous = iterator->current;
311 iterator->list->last = element;
312 }
313 else
314 {
315 iterator->current->next->previous = element;
316 element->next = iterator->current->next;
317 iterator->current->next = element;
318 element->previous = iterator->current;
319 }
320 iterator->list->count++;
321 }
322
323 /**
324 * Implementation of iterator_t.destroy.
325 */
326 static void iterator_destroy(private_iterator_t *this)
327 {
328 allocator_free(this);
329 }
330
331 /**
332 * Implementation of linked_list_t.get_count.
333 */
334 static int get_count(private_linked_list_t *this)
335 {
336 return this->count;
337 }
338
339
340 /**
341 * Implementation of linked_list_t.insert_first.
342 */
343 static void insert_first(private_linked_list_t *this, void *item)
344 {
345 linked_list_element_t *element;
346
347 element =(linked_list_element_t *) linked_list_element_create(item);
348
349 if (this->count == 0)
350 {
351 /* first entry in list */
352 this->first = element;
353 this->last = element;
354 element->previous = NULL;
355 element->next = NULL;
356 }
357 else
358 {
359 linked_list_element_t *old_first_element = this->first;
360 element->next = old_first_element;
361 element->previous = NULL;
362 old_first_element->previous = element;
363 this->first = element;
364 }
365
366 this->count++;
367 }
368
369 /**
370 * Implementation of linked_list_t.remove_first.
371 */
372 static status_t remove_first(private_linked_list_t *this, void **item)
373 {
374 if (this->count == 0)
375 {
376 return NOT_FOUND;
377 }
378
379 linked_list_element_t *element = this->first;
380
381 if (element->next != NULL)
382 {
383 element->next->previous = NULL;
384 }
385 this->first = element->next;
386
387 *item = element->value;
388
389 this->count--;
390
391 element->destroy(element);
392
393 return SUCCESS;
394 }
395
396 /**
397 * Implementation of linked_list_t.get_first.
398 */
399 static status_t get_first(private_linked_list_t *this, void **item)
400 {
401 if (this->count == 0)
402 {
403 return NOT_FOUND;
404 }
405
406 *item = this->first->value;
407
408 return SUCCESS;
409 }
410
411 /**
412 * Implementation of linked_list_t.insert_last.
413 */
414 static void insert_last(private_linked_list_t *this, void *item)
415 {
416 linked_list_element_t *element = (linked_list_element_t *) linked_list_element_create(item);
417
418 if (this->count == 0)
419 {
420 /* first entry in list */
421 this->first = element;
422 this->last = element;
423 element->previous = NULL;
424 element->next = NULL;
425 }
426 else
427 {
428
429 linked_list_element_t *old_last_element = this->last;
430 element->previous = old_last_element;
431 element->next = NULL;
432 old_last_element->next = element;
433 this->last = element;
434 }
435
436 this->count++;
437 }
438
439 /**
440 * Implementation of linked_list_t.remove_last.
441 */
442 static status_t remove_last(private_linked_list_t *this, void **item)
443 {
444 if (this->count == 0)
445 {
446 return NOT_FOUND;
447 }
448
449 linked_list_element_t *element = this->last;
450
451 if (element->previous != NULL)
452 {
453 element->previous->next = NULL;
454 }
455 this->last = element->previous;
456
457 *item = element->value;
458
459 this->count--;
460
461 element->destroy(element);
462
463 return SUCCESS;
464 }
465
466 /**
467 * Implementation of linked_list_t.get_last.
468 */
469 static status_t get_last(private_linked_list_t *this, void **item)
470 {
471 if (this->count == 0)
472 {
473 return NOT_FOUND;
474 }
475
476 *item = this->last->value;
477
478 return SUCCESS;
479 }
480
481 /**
482 * Implementation of linked_list_t.create_iterator.
483 */
484 static void create_iterator (private_linked_list_t *linked_list, iterator_t **iterator,bool forward)
485 {
486 private_iterator_t *this = allocator_alloc_thing(private_iterator_t);
487
488 this->public.has_next = (bool (*) (iterator_t *this)) iterator_has_next;
489 this->public.current = (status_t (*) (iterator_t *this, void **value)) iterator_current;
490 this->public.insert_before = (void (*) (iterator_t *this, void *item)) insert_before;
491 this->public.insert_after = (void (*) (iterator_t *this, void *item)) insert_after;
492 this->public.remove = (status_t (*) (iterator_t *this)) remove;
493 this->public.reset = (void (*) (iterator_t *this)) iterator_reset;
494 this->public.destroy = (void (*) (iterator_t *this)) iterator_destroy;
495
496 this->forward = forward;
497 this->current = NULL;
498 this->list = linked_list;
499
500 *iterator = &(this->public);
501 }
502
503 /**
504 * Implementation of linked_list_t.destroy.
505 */
506 static void linked_list_destroy(private_linked_list_t *this)
507 {
508 void * value;
509 /* Remove all list items before destroying list */
510
511 while (this->public.remove_first(&(this->public),&value) != NOT_FOUND)
512 {
513 /* values are not destroyed so memory leaks are possible
514 * if list is not empty when deleting */
515 }
516 allocator_free(this);
517 }
518
519 /*
520 * Described in header
521 */
522 linked_list_t *linked_list_create()
523 {
524 private_linked_list_t *this = allocator_alloc_thing(private_linked_list_t);
525
526 this->public.get_count = (int (*) (linked_list_t *linked_list)) get_count;
527 this->public.create_iterator = (void (*) (linked_list_t *linked_list, iterator_t **iterator,bool forward)) create_iterator;
528 this->public.get_first = (status_t (*) (linked_list_t *linked_list, void **item)) get_first;
529 this->public.get_last = (status_t (*) (linked_list_t *linked_list, void **item)) get_last;
530 this->public.insert_first = (void (*) (linked_list_t *linked_list, void *item)) insert_first;
531 this->public.insert_last = (void (*) (linked_list_t *linked_list, void *item)) insert_last;
532 this->public.remove_first = (status_t (*) (linked_list_t *linked_list, void **item)) remove_first;
533 this->public.remove_last = (status_t (*) (linked_list_t *linked_list, void **item)) remove_last;
534 this->public.destroy = (void (*) (linked_list_t *linked_list)) linked_list_destroy;
535
536 this->count = 0;
537 this->first = NULL;
538 this->last = NULL;
539
540 return (&(this->public));
541 }