-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_ops.c
350 lines (319 loc) · 7.64 KB
/
set_ops.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <stdlib.h>
#include "util.h"
#include "set_ops.h"
#include "log.h"
GList *g_list_union(GList *a, GList *b)
{
return g_list_concat(a, b);
}
GList *g_list_intersection (GList *a, GList *b, GCompareFunc cmp)
{
if (a == NULL || b == NULL)
{
return NULL;
}
if (cmp(a->data, b->data) < 0)
{
return g_list_intersection(a->next, b, cmp);
}
if (cmp(b->data, a->data) < 0)
{
return g_list_intersection(a, b->next, cmp);
}
if (cmp(a->data, b->data) == 0)
{
return g_list_prepend(g_list_intersection(b->next, a->next, cmp), b->data);
}
return NULL;
}
GList *g_list_difference (GList *a, GList *b, GCompareFunc cmp)
{
if (a == NULL)
{
return NULL;
}
if (b == NULL)
{
return a;
}
if (cmp(a->data, b->data) < 0)
{
return g_list_difference(a->next, b, cmp);
}
if (cmp(b->data, a->data) < 0)
{
return g_list_difference(a, b->next, cmp);
}
if (cmp(a->data, b->data) == 0)
{
return g_list_difference(b->next, a->next, cmp);
}
return NULL;
}
GList *g_list_filter (GList *l, set_predicate p, gpointer data)
{
GList *res = NULL;
LL (l, it)
{
if (p(NULL, it->data, data))
res = g_list_prepend(res, it->data);
LL_END;
}
return res;
}
// can't return 0, else same size hashses
// would get overwritten
gint hash_size_cmp (GHashTable *a, GHashTable *b)
{
int asize;
int bsize;
if (a == NULL)
asize = 0;
else
asize = g_hash_table_size((GHashTable*) a);
if (b == NULL)
bsize = 0;
else
bsize = g_hash_table_size((GHashTable*) b);
return asize - bsize;
}
// returns NULL if neither set is NULL, else returns
// a valid result based on arguments
// the correct result of any operation on two null sets
// is a null (empty) set
GHashTable *null_set_check (GHashTable *a, GHashTable *b,
gpointer if_a_null, gpointer if_b_null)
{
if (a == NULL && b == NULL)
{
return set_new(g_direct_hash, g_direct_equal, NULL);
}
if (a == NULL)
{
return if_a_null;
}
if (b == NULL)
{
return if_b_null;
}
return NULL;
}
// assumes a is the smaller
GHashTable *_intersect_s (GHashTable *a, GHashTable *b)
{
GHashTable *empty_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
GHashTable *checked = null_set_check(a, b, empty_hash, empty_hash);
if (checked != NULL) // Means one or both is NULL
{
return checked;
}
GHashTableIter it;
gpointer key;
gpointer value;
GHashTable *res = g_hash_table_new(g_direct_hash, g_direct_equal);
g_hash_table_iter_init(&it, a);
while (g_hash_table_iter_next(&it, &key, &value))
{
if (g_hash_table_lookup(b, key) != NULL)
{
g_hash_table_insert(res, key, value);
}
}
return res;
}
GHashTable *_union_s (GHashTable *a, GHashTable *b)
{
GHashTable *checked = null_set_check(a, b, b, a);
if (checked != NULL)
{
return checked;
}
GHashTableIter it;
gpointer key;
gpointer value;
GHashTable *res = g_hash_table_new(g_direct_hash, g_direct_equal);
g_hash_table_iter_init(&it, b);
while (g_hash_table_iter_next(&it, &key, &value))
{
g_hash_table_insert(res, key, value);
}
g_hash_table_iter_init(&it, a);
while (g_hash_table_iter_next(&it, &key, &value))
{
g_hash_table_insert(res, key, value);
}
return res;
}
// puts sets in order of size
GHashTable *set_intersect_s (GHashTable *a, GHashTable *b)
{
if (hash_size_cmp(a, b) < 0)
return _intersect_s(a, b);
else
return _intersect_s(b, a);
}
GHashTable *set_union_s (GHashTable *a, GHashTable *b)
{
if (hash_size_cmp(a, b) < 0)
return _union_s(a, b);
else
return _union_s(b, a);
}
GHashTable *set_union (GList *sets)
{
GHashTable *res = NULL;
GHashTable *tmp = NULL;
while (sets != NULL)
{
tmp = set_union_s(sets->data, res);
res = tmp;
sets = sets->next;
}
return res;
}
GHashTable *set_intersect (GList *tables)
{
if (tables == NULL)
{
return NULL;
}
if (g_list_next(tables) == NULL)
{
return tables->data;
}
GHashTable *res = tables->data;
GHashTable *tmp = NULL;
while (tables != NULL)
{
tmp = set_intersect_s(tables->data, res);
res = tmp;
tables = tables->next;
}
return res;
}
GHashTable *set_intersect_p (GHashTable *a, ...)
{
va_list args;
va_start(args, a);
GHashTable *arg = va_arg(args, GHashTable*);
GList *tables = g_list_prepend(NULL, a);
while (arg != NULL)
{
tables = g_list_prepend(tables, arg);
arg = va_arg(args, GHashTable*);
}
va_end(args);
// we sort ascending and reverse
// this makes it so that every intersect_s is comparing
// the smallest lists possible
tables = g_list_sort(tables, (GCompareFunc) hash_size_cmp);
return set_intersect(tables);
}
GHashTable *set_difference_s (GHashTable *a, GHashTable *b)
{
GHashTable *checked = null_set_check(a, b, a, a);
if (checked != NULL)
{
return checked;
}
GHashTableIter it;
gpointer key;
gpointer value;
GHashTable *res = g_hash_table_new(g_direct_hash, g_direct_equal);
g_hash_table_iter_init(&it, a);
while (g_hash_table_iter_next(&it, &key, &value))
{
if (g_hash_table_lookup(b, key) == NULL)
{
g_hash_table_insert(res, key, value);
}
}
return res;
}
// does first - second - third - ...
GHashTable *set_difference (GList *sets)
{
if (sets == NULL)
{
return NULL;
}
GHashTable *res = sets->data;
sets = sets->next;
GHashTable *tmp = NULL;
while (sets != NULL)
{
tmp = set_difference_s(res, sets->data);
res = tmp;
sets = sets->next;
}
return res;
}
GHashTable *set_subset (GHashTable *hash, set_predicate pred, gpointer user_data)
{
GHashTableIter it;
gpointer k, v;
GHashTable *res = g_hash_table_new(g_direct_hash, g_direct_equal);
g_hash_loop(hash, it, k, v)
{
if (pred(k, v, user_data))
{
g_hash_table_insert(res, k, v);
}
}
return res;
}
GHashTable *set_new (GHashFunc hash_func, GEqualFunc equal_func, GDestroyNotify destroy)
{
return g_hash_table_new_full (hash_func, equal_func, destroy, NULL);
}
void set_add (GHashTable *set, gpointer element)
{
g_hash_table_replace (set, element, element);
}
gboolean _equal_s (GHashTable *a, GHashTable *b)
{
if (a == NULL) // means a and b are null. null==null
{
return TRUE;
}
GHashTableIter it;
gpointer key;
gpointer value;
g_hash_table_iter_init(&it, a);
while (g_hash_table_iter_next(&it, &key, &value))
{
if (g_hash_table_lookup(b, key) == NULL)
return FALSE;
}
return TRUE;
}
// compares first on size, then on equality
// if the sets are the same size, but not equal
// we say a is "larger"
int set_cmp_s (GHashTable *a, GHashTable *b)
{
int res = hash_size_cmp(a, b);
if (res == 0)
{
if (_equal_s(a, b))
return 0;
else
return 1;
}
return res;
}
gboolean set_equal_s (GHashTable *a, GHashTable *b)
{
if (hash_size_cmp(a, b) == 0)
return _equal_s(a, b);
else
return FALSE;
}
gboolean set_contains (GHashTable *set, gpointer element)
{
return g_hash_table_lookup_extended (set, element, NULL, NULL);
}
gboolean set_remove (GHashTable *set, gpointer element)
{
return g_hash_table_remove (set, element);
}