-
Notifications
You must be signed in to change notification settings - Fork 3
/
strset.c
73 lines (55 loc) · 1.97 KB
/
strset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdlib.h>
#include <string.h>
#include "wcc.h"
StrSet *new_strset(void) {
StrSet *ss = wmalloc(sizeof(StrSet));
ss->strmap = new_strmap();
return ss;
}
void free_strset(StrSet *ss) {
free_strmap(ss->strmap);
wfree(ss);
}
void strset_add(StrSet *ss, char *element) {
strmap_put(ss->strmap, element, (void *) 1);
}
int strset_in(StrSet *ss, char *element) {
return !!strmap_get(ss->strmap, element);
}
StrSet *dup_strset(StrSet *ss) {
StrSet *result = new_strset();
for (StrMapIterator it = strmap_iterator(ss->strmap); !strmap_iterator_finished(&it); strmap_iterator_next(&it))
strmap_put(result->strmap, strmap_iterator_key(&it), (void *) 1);
return result;
}
StrSet *strset_union(StrSet *ss1, StrSet *ss2) {
StrSet *result = new_strset();
for (StrMapIterator it = strmap_iterator(ss1->strmap); !strmap_iterator_finished(&it); strmap_iterator_next(&it))
strmap_put(result->strmap, strmap_iterator_key(&it), (void *) 1);
for (StrMapIterator it = strmap_iterator(ss2->strmap); !strmap_iterator_finished(&it); strmap_iterator_next(&it))
strmap_put(result->strmap, strmap_iterator_key(&it), (void *) 1);
return result;
}
StrSet *strset_intersection(StrSet *ss1, StrSet *ss2) {
StrSet *result = new_strset();
for (StrMapIterator it = strmap_iterator(ss1->strmap); !strmap_iterator_finished(&it); strmap_iterator_next(&it)) {
char *key = strmap_iterator_key(&it);
if (strmap_get(ss2->strmap, key)) strmap_put(result->strmap, strmap_iterator_key(&it), (void *) 1);
}
return result;
}
void print_strset(StrSet *s) {
if (!s) {
printf("{}");
return;
}
int first = 1;
printf("{");
for (StrMapIterator it = strmap_iterator(s->strmap); !strmap_iterator_finished(&it); strmap_iterator_next(&it)) {
char *key = strmap_iterator_key(&it);
if (!first) { printf(", "); }
printf("%s", key);
first = 0;
}
printf("}");
}