-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.c
55 lines (48 loc) · 850 Bytes
/
set.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
#include "error.h"
#include <assert.h>
#include <stdlib.h>
static int size = 0;
/* Set the set size */
void
SetSize(int n) {
size = n + 1;
}
/* Allocate a new set */
char *
SetNew() {
char *s;
s = (char *)calloc((size_t)size, 1);
MemoryCheck(s);
return s;
}
/* Deallocate a set */
void
SetFree(char *s) {
free(s);
}
/* Add a new element to the set. Return TRUE if the element was added
* and FALSE if it was already there.
*/
int
SetAdd(char *s, int e) {
int rv;
assert(e >= 0 && e < size);
rv = s[e];
s[e] = 1;
return !rv;
}
/* Add every element of s2 to s1. Return TRUE if s1 changes. */
int
SetUnion(char *s1, char *s2) {
int i, progress;
progress = 0;
for (i = 0; i < size; i++) {
if (s2[i] == 0)
continue;
if (s1[i] == 0) {
progress = 1;
s1[i] = 1;
}
}
return progress;
}