[2e9455f] | 1 | /** |
---|
| 2 | ** abac_set.c |
---|
| 3 | **/ |
---|
| 4 | |
---|
[cb0f2b2] | 5 | #include <stdlib.h> |
---|
| 6 | |
---|
[3c251d0] | 7 | #include "abac_util.h" |
---|
[2e9455f] | 8 | #include "abac_list.h" |
---|
[cb0f2b2] | 9 | #include "uthash.h" |
---|
| 10 | |
---|
[2e9455f] | 11 | #include "abac_set.h" |
---|
| 12 | |
---|
[2fd24c7] | 13 | typedef struct _abac_set_element_t { |
---|
[fdb4f28] | 14 | char *key; |
---|
| 15 | UT_hash_handle hh; |
---|
[2fd24c7] | 16 | } abac_set_element_t; |
---|
[fdb4f28] | 17 | |
---|
[2fd24c7] | 18 | struct _abac_set_t { |
---|
| 19 | abac_set_element_t *elts; |
---|
[e2a0f26] | 20 | int size; |
---|
[fdb4f28] | 21 | }; |
---|
| 22 | |
---|
[2e9455f] | 23 | /****************************************************************/ |
---|
[48f3a37] | 24 | /** |
---|
| 25 | * Create a new struct. |
---|
| 26 | */ |
---|
[2fd24c7] | 27 | abac_set_t *abac_set_new(void) { |
---|
[3c251d0] | 28 | abac_set_t *ret = abac_xmalloc(sizeof(abac_set_t)); |
---|
[cb0f2b2] | 29 | ret->elts = NULL; |
---|
[e2a0f26] | 30 | ret->size = 0; |
---|
[cb0f2b2] | 31 | return ret; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Add an item to the set, returns true if it doesn't exist. |
---|
| 36 | */ |
---|
[2fd24c7] | 37 | int abac_set_add(abac_set_t *set, char *value) { |
---|
| 38 | abac_set_element_t *elt; |
---|
[cb0f2b2] | 39 | |
---|
| 40 | HASH_FIND_STR(set->elts, value, elt); |
---|
| 41 | if (elt) return 0; // already exists |
---|
| 42 | |
---|
[3c251d0] | 43 | elt = abac_xmalloc(sizeof(abac_set_element_t)); |
---|
| 44 | elt->key = abac_xstrdup(value); |
---|
[cb0f2b2] | 45 | HASH_ADD_KEYPTR(hh, set->elts, elt->key, strlen(elt->key), elt); |
---|
| 46 | |
---|
[e2a0f26] | 47 | ++set->size; |
---|
| 48 | |
---|
[cb0f2b2] | 49 | return 1; |
---|
| 50 | } |
---|
[ebde9dd] | 51 | |
---|
[1fa0621] | 52 | /** |
---|
| 53 | * Does the set contain the value. |
---|
| 54 | */ |
---|
[2fd24c7] | 55 | int abac_set_contains(abac_set_t *set, char *value) { |
---|
| 56 | abac_set_element_t *elt; |
---|
[1fa0621] | 57 | |
---|
| 58 | HASH_FIND_STR(set->elts, value, elt); |
---|
| 59 | return elt != NULL; |
---|
| 60 | } |
---|
| 61 | |
---|
[ebde9dd] | 62 | /** |
---|
| 63 | * Return a list of the set's elements. |
---|
| 64 | */ |
---|
[2fd24c7] | 65 | abac_list_t *abac_set_elements(abac_set_t *set) { |
---|
| 66 | abac_set_element_t *elt; |
---|
[6d5623e] | 67 | abac_list_t *ret = abac_list_new(); |
---|
[ebde9dd] | 68 | |
---|
| 69 | for (elt = set->elts; elt != NULL; elt = elt->hh.next) { |
---|
| 70 | char *next = elt->key; |
---|
[6d5623e] | 71 | abac_list_add(ret, next); |
---|
[ebde9dd] | 72 | } |
---|
| 73 | |
---|
| 74 | return ret; |
---|
| 75 | } |
---|
| 76 | |
---|
[e2a0f26] | 77 | /** |
---|
| 78 | * Returns the number of elements in the set. |
---|
| 79 | */ |
---|
| 80 | int abac_set_size(abac_set_t *set) { |
---|
| 81 | return set->size; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Takes the intersection of l and r. |
---|
| 86 | */ |
---|
| 87 | void abac_set_intersect(abac_set_t *l, abac_set_t *r) { |
---|
| 88 | abac_set_element_t *elt, *next; |
---|
| 89 | |
---|
| 90 | for (elt = l->elts; elt != NULL; elt = next) { |
---|
| 91 | next = elt->hh.next; |
---|
| 92 | |
---|
| 93 | // if the rhs doesn't contain this item, remove it |
---|
| 94 | if (!abac_set_contains(r, elt->key)) { |
---|
| 95 | HASH_DEL(l->elts, elt); |
---|
| 96 | free(elt->key); |
---|
| 97 | free(elt); |
---|
| 98 | --l->size; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
[ebde9dd] | 103 | /** |
---|
| 104 | * Destroy a set. |
---|
| 105 | */ |
---|
[2fd24c7] | 106 | void abac_set_free(abac_set_t *set) { |
---|
| 107 | abac_set_element_t *elt; |
---|
[ebde9dd] | 108 | |
---|
[be963dc] | 109 | while ((elt = set->elts) != NULL) { |
---|
| 110 | HASH_DEL(set->elts, elt); |
---|
[ebde9dd] | 111 | free(elt->key); |
---|
[be963dc] | 112 | free(elt); |
---|
| 113 | } |
---|
[ebde9dd] | 114 | |
---|
| 115 | free(set); |
---|
| 116 | } |
---|