[379a53f] | 1 | #include <stdlib.h> |
---|
| 2 | |
---|
[3c251d0] | 3 | #include "abac_util.h" |
---|
[2e9455f] | 4 | #include "abac_list.h" |
---|
[379a53f] | 5 | |
---|
[2e9455f] | 6 | /****************************************************************/ |
---|
[6d5623e] | 7 | abac_list_t *abac_list_new(void) { |
---|
[3c251d0] | 8 | abac_list_t *ret = abac_xmalloc(sizeof(abac_list_t)); |
---|
[379a53f] | 9 | ret->elts = NULL; |
---|
[90d20f0] | 10 | ret->size = 0; |
---|
[379a53f] | 11 | return ret; |
---|
| 12 | } |
---|
| 13 | |
---|
[6d5623e] | 14 | void abac_list_add(abac_list_t *list, void *elt) { |
---|
[3c251d0] | 15 | abac_list_element_t *new_element = abac_xmalloc(sizeof(abac_list_element_t)); |
---|
[379a53f] | 16 | |
---|
| 17 | new_element->ptr = elt; |
---|
| 18 | DL_APPEND(list->elts, new_element); |
---|
[90d20f0] | 19 | ++list->size; |
---|
[379a53f] | 20 | } |
---|
[d8990b3] | 21 | |
---|
[abf8d5d] | 22 | /* add only if it is not in the list already */ |
---|
| 23 | int abac_list_unique_add(abac_list_t *list, void *elt) { |
---|
| 24 | void *cur; |
---|
| 25 | abac_list_foreach(list, cur, |
---|
| 26 | if(cur == elt) |
---|
| 27 | return 0; |
---|
| 28 | ); |
---|
| 29 | |
---|
| 30 | abac_list_element_t *new_element = abac_xmalloc(sizeof(abac_list_element_t)); |
---|
| 31 | new_element->ptr = elt; |
---|
| 32 | DL_APPEND(list->elts, new_element); |
---|
| 33 | ++list->size; |
---|
| 34 | return 1; |
---|
| 35 | } |
---|
| 36 | |
---|
[202a7f9] | 37 | void abac_list_prepend(abac_list_t *list, void *elt) { |
---|
| 38 | abac_list_element_t *new_element = abac_xmalloc(sizeof(abac_list_element_t)); |
---|
| 39 | |
---|
| 40 | new_element->ptr = elt; |
---|
| 41 | DL_PREPEND(list->elts, new_element); |
---|
| 42 | ++list->size; |
---|
| 43 | } |
---|
| 44 | |
---|
[6d5623e] | 45 | int abac_list_remove(abac_list_t *list, void *elt) { |
---|
| 46 | abac_list_element_t *cur; |
---|
[d526b7a] | 47 | |
---|
| 48 | // iterate the list, remove the item if we find it |
---|
| 49 | DL_FOREACH(list->elts, cur) { |
---|
| 50 | if (cur->ptr == elt) { |
---|
| 51 | DL_DELETE(list->elts, cur); |
---|
[be963dc] | 52 | free(cur); |
---|
[90d20f0] | 53 | --list->size; |
---|
[d526b7a] | 54 | return 1; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
[202a7f9] | 58 | // return false if we don't |
---|
[d526b7a] | 59 | return 0; |
---|
| 60 | } |
---|
| 61 | |
---|
[6d5623e] | 62 | int abac_list_size(abac_list_t *list) { |
---|
[53e540d] | 63 | if (list) |
---|
| 64 | return list->size; |
---|
| 65 | return 0; |
---|
[90d20f0] | 66 | } |
---|
| 67 | |
---|
[6d5623e] | 68 | void abac_list_free(abac_list_t *list) { |
---|
| 69 | abac_list_element_t *elt, *tmp; |
---|
[d8990b3] | 70 | |
---|
[53e540d] | 71 | if (list) { |
---|
| 72 | // free everthing in the list |
---|
| 73 | DL_FOREACH_SAFE(list->elts, elt, tmp) { |
---|
| 74 | DL_DELETE(list->elts, elt); |
---|
| 75 | free(elt); |
---|
| 76 | } |
---|
[d8990b3] | 77 | |
---|
[53e540d] | 78 | // free the list |
---|
| 79 | free(list); |
---|
| 80 | } |
---|
[d8990b3] | 81 | } |
---|