1 | /** |
---|
2 | ** abac_set.c |
---|
3 | **/ |
---|
4 | |
---|
5 | #include <stdlib.h> |
---|
6 | |
---|
7 | #include "abac_util.h" |
---|
8 | #include "abac_list.h" |
---|
9 | #include "uthash.h" |
---|
10 | |
---|
11 | #include "abac_set.h" |
---|
12 | |
---|
13 | typedef struct _abac_set_element_t { |
---|
14 | char *key; |
---|
15 | UT_hash_handle hh; |
---|
16 | } abac_set_element_t; |
---|
17 | |
---|
18 | struct _abac_set_t { |
---|
19 | abac_set_element_t *elts; |
---|
20 | int size; |
---|
21 | }; |
---|
22 | |
---|
23 | /****************************************************************/ |
---|
24 | /** |
---|
25 | * Create a new struct. |
---|
26 | */ |
---|
27 | abac_set_t *abac_set_new(void) { |
---|
28 | abac_set_t *ret = abac_xmalloc(sizeof(abac_set_t)); |
---|
29 | ret->elts = NULL; |
---|
30 | ret->size = 0; |
---|
31 | return ret; |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * Add an item to the set, returns true if it doesn't exist. |
---|
36 | */ |
---|
37 | int abac_set_add(abac_set_t *set, char *value) { |
---|
38 | abac_set_element_t *elt; |
---|
39 | |
---|
40 | HASH_FIND_STR(set->elts, value, elt); |
---|
41 | if (elt) return 0; // already exists |
---|
42 | |
---|
43 | elt = abac_xmalloc(sizeof(abac_set_element_t)); |
---|
44 | elt->key = abac_xstrdup(value); |
---|
45 | HASH_ADD_KEYPTR(hh, set->elts, elt->key, strlen(elt->key), elt); |
---|
46 | |
---|
47 | ++set->size; |
---|
48 | |
---|
49 | return 1; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Does the set contain the value. |
---|
54 | */ |
---|
55 | int abac_set_contains(abac_set_t *set, char *value) { |
---|
56 | abac_set_element_t *elt; |
---|
57 | |
---|
58 | HASH_FIND_STR(set->elts, value, elt); |
---|
59 | return elt != NULL; |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Return a list of the set's elements. |
---|
64 | */ |
---|
65 | abac_list_t *abac_set_elements(abac_set_t *set) { |
---|
66 | abac_set_element_t *elt; |
---|
67 | abac_list_t *ret = abac_list_new(); |
---|
68 | |
---|
69 | for (elt = set->elts; elt != NULL; elt = elt->hh.next) { |
---|
70 | char *next = elt->key; |
---|
71 | abac_list_add(ret, next); |
---|
72 | } |
---|
73 | |
---|
74 | return ret; |
---|
75 | } |
---|
76 | |
---|
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 | |
---|
103 | /** |
---|
104 | * Destroy a set. |
---|
105 | */ |
---|
106 | void abac_set_free(abac_set_t *set) { |
---|
107 | abac_set_element_t *elt; |
---|
108 | |
---|
109 | while ((elt = set->elts) != NULL) { |
---|
110 | HASH_DEL(set->elts, elt); |
---|
111 | free(elt->key); |
---|
112 | free(elt); |
---|
113 | } |
---|
114 | |
---|
115 | free(set); |
---|
116 | } |
---|