-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntSet.c
194 lines (176 loc) · 4.47 KB
/
IntSet.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <stdlib.h>
#include "IntSet.h"
/**
* Toplevel structure for an IntSet.
* This implementation simply uses a linked list.
*/
struct IntSet {
struct IntSetNode *first;
struct IntSetNode *last;
};
struct IntSetIterator {
struct IntSetNode *node;
};
/**
* Structure for each element in an IntSet, stored as a linked list.
*/
typedef struct IntSetNode {
int value;
struct IntSetNode *next;
} IntSetNode;
/**
* Allocate, initialize and return a new (empty) IntSet.
*/
IntSet *
IntSet_new() {
IntSet *set = (IntSet*)malloc(sizeof(IntSet));
set->first = set->last = NULL;
return set;
}
/**
* Free the memory used for the given IntSet and all its elements.
*/
void
IntSet_free(IntSet *set) {
// Free the elements
IntSetNode *elt = set->first;
while (elt != NULL) {
IntSetNode *next = elt->next;
free(elt);
elt = next;
}
// Free the set (list)
free(set);
}
/**
* Allocate and initialize a new IntSetNode storing the given int value.
*/
static IntSetNode *
IntSetNode_new(int value) {
IntSetNode *node = (IntSetNode*)malloc(sizeof(IntSetNode));
if (node == NULL) {
abort();
}
node->value = value;
node->next = NULL;
return node;
}
/**
* Return true if the given IntSet is empty.
*/
bool
IntSet_is_empty(const IntSet *set) {
return set->first == NULL;
}
/**
* Add given int to the given IntSet (if it's not already there).
*/
void
IntSet_add(IntSet *set, int value) {
if (!IntSet_contains(set, value)) {
// Add at front
IntSetNode *node = IntSetNode_new(value);
node->next = set->first;
set->first = node;
}
}
/**
* Return true if the given IntSet contains the given int value.
*/
bool
IntSet_contains(const IntSet *set, int value) {
for (IntSetNode *node=set->first; node != NULL; node=node->next) {
if (node->value == value) {
return true;
}
}
return false;
}
/**
* Add the contents of IntSet set2 to IntSet set1 (adding those elements
* that aren't already in set1). This will modify set1 unless set2 is empty
* (or all its elements are already in set1).
*/
void
IntSet_union(IntSet *set1, const IntSet *set2) {
for (IntSetNode *node=set2->first; node != NULL; node=node->next) {
IntSet_add(set1, node->value);
}
}
/**
* Return true if the first IntSet contains every member of the second
* IntSet.
*/
bool
IntSet_contains_all(IntSet *set1, IntSet *set2) {
for (IntSetNode *node2=set2->first; node2 != NULL; node2=node2->next) {
if (!IntSet_contains(set1, node2->value)) {
return false;
}
}
return true;
}
/**
* Return true if the two given IntSets contain exactly the same members,
* otherwise false.
*/
bool
IntSet_equals(IntSet *set1, IntSet *set2) {
return IntSet_contains_all(set1, set2) && IntSet_contains_all(set2, set1);
}
/**
* Call the given function on each element of given IntSet, passing the
* int value to the function.
*/
void
IntSet_iterate(const IntSet *set, void (*func)(int)) {
for (IntSetNode *node=set->first; node != NULL; node=node->next) {
func(node->value);
}
}
/**
* Return an IntSetIterator for the given IntSet.
* Don't forget to free() this when you're done iterating.
*/
IntSetIterator *
IntSet_iterator(const IntSet *set) {
IntSetIterator *iterator = (IntSetIterator*)malloc(sizeof(IntSetIterator));
iterator->node = set->first;
return iterator;
}
/**
* Return the next int from the given IntSetIterator and increment it
* to point to the next element.
* This will cause a crash if there is no such element.
* You could make a safe version with a pass-by-reference (int*) parameter
* for the int and boolean return value that indicates whether the operation
* succeeded or not. Ah, the goold old days...
*/
bool
IntSetIterator_has_next(const IntSetIterator *iterator) {
return iterator != NULL && iterator->node != NULL;
}
int
IntSetIterator_next(IntSetIterator *iterator) {
if (iterator == NULL || iterator->node == NULL) {
abort();
} else {
int value = iterator->node->value;
iterator->node = iterator->node->next;
return value;
}
}
/**
* Print the given IntSet to stdout.
*/
void
IntSet_print(IntSet *set) {
for (IntSetNode *node=set->first; node != NULL; node=node->next) {
printf("%d", node->value);
if (node->next != NULL) {
printf(" ");
}
}
printf("\n");
}