-
Notifications
You must be signed in to change notification settings - Fork 7
/
set.c
108 lines (88 loc) · 2.45 KB
/
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
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
#include "ptoc.h"
void pascal_set_construct(SetArrayElemType* set_array, size_t set_card,
int param1, va_list params)
{
SetElemType elem = param1;
boolean interval = false;
int param = param1;
size_t i;
for (i = 0; i < set_card; i++) {
set_array[i] = 0;
}
while (param != eos) {
if (interval) {
size_t low = elem;
size_t high = (SetElemType)param;
size_t lowindex = low / BITS_PER_WORD;
size_t highindex = high / BITS_PER_WORD;
assert(highindex < set_card);
if (lowindex == highindex) {
set_array[lowindex] |=
((((SetArrayElemType)1 << (high-low)) << 1) - 1)
<< (low % BITS_PER_WORD);
} else {
set_array[lowindex] |= ALL_BITS_SET << (low % BITS_PER_WORD);
for(i = lowindex+1; i < highindex; i++)
set_array[i] = ALL_BITS_SET;
set_array[highindex] |=
(((SetArrayElemType)1 << (high % BITS_PER_WORD)) << 1) - 1;
}
interval = false;
} else if (param == INTERVAL_MARKER) {
interval = true;
} else { /* single value */
size_t index;
elem = param;
index = (size_t)elem / BITS_PER_WORD;
assert(index < set_card);
set_array[index] |=
(SetArrayElemType)1 << ((size_t)elem % BITS_PER_WORD);
}
param = va_arg(params, int);
}
}
set join(set a, set b) {
set c;
size_t i;
for(i = 0; i < SET_LENGTH; i++)
c.setarray[i] = a.setarray[i] | b.setarray[i];
return c;
}
set difference(set a, set b) {
set c;
size_t i;
for (i = 0; i < SET_LENGTH; i++)
c.setarray[i] = a.setarray[i] & ~b.setarray[i];
return c;
}
set intersect(set a, set b) {
set c;
size_t i;
for (i = 0; i < SET_LENGTH; i++)
c.setarray[i] = a.setarray[i] & b.setarray[i];
return c;
}
boolean equivalent(set a, set b) {
size_t i;
for (i = 0; i < SET_LENGTH; i++)
if (a.setarray[i] != b.setarray[i]) return false;
return true;
}
boolean subset(set a, set b) {
size_t i;
for (i = 0; i < SET_LENGTH; i++)
if (a.setarray[i] & ~b.setarray[i]) return false;
return true;
}
boolean inset(SetElemType elem, set s) {
return (s.setarray[(size_t)elem / BITS_PER_WORD] &
((SetArrayElemType)1 << ((size_t)elem % BITS_PER_WORD))) != 0;
}
set setof(int param1,...) {
set s = {{0}};
va_list ap;
va_start(ap, param1);
pascal_set_construct(s.setarray, items(s.setarray), param1, ap);
va_end(ap);
return s;
}