-
Notifications
You must be signed in to change notification settings - Fork 2
/
setoper.c
285 lines (239 loc) · 5.66 KB
/
setoper.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
/* setoper.c:
* A set operation library
* created by Komei Fukuda, Nov.14, 1993
* modified on December 5, 1994
(set_card function replaced with a better code by David Bremner)
* last modified on August 18, 1996
*/
#include "setoper.h"
#include <limits.h>
#define SETBITS (sizeof(long) * CHAR_BIT)
/* (Number of chars in a long) * (number of bits in a char) */
/* Definitions for optimized set_card function
by David Bremner [email protected]
*/
/* Caution!!!
Bremner's technique depends on the assumption that CHAR_BIT == 8.
*/
#define LUTBLOCKS(set) (((set[0]-1)/SETBITS+1)*(sizeof(long)/sizeof(set_card_lut_t)))
static unsigned char set_card_lut[]={
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};
/* End of Definitions for optimized set_card */
unsigned long set_blocks(long len)
{
long blocks=1L;
if (len>0) blocks=((long)len-1)/SETBITS+2;
return blocks;
}
void set_initialize(set_type *setp, long len)
/* Make a set with a given bit lengths */
{
long i,forlim1;
forlim1=set_blocks(len);
*setp=(unsigned long *) calloc(forlim1, sizeof i);
(*setp)[0]=(unsigned long) len; /* size of the ground set */
for (i=1; i<forlim1; i++)
(*setp)[i]=0U;
}
void set_free(set_type set)
/* Free the space created with the set pointer set*/
{
free(set);
}
void set_emptyset(set_type set)
/* Set set to be the emptyset */
{
long i,forlim;
forlim=set_blocks(set[0])-1;
for (i=1; i<=forlim; i++)
set[i]=0U;
}
void set_copy(set_type setcopy,set_type set)
/* Copy the set set[] to setcopy[] with setcopy[] length */
{
long i,forlim;
forlim=set_blocks(setcopy[0])-1;
for (i=1; i<=forlim; i++)
setcopy[i]=set[i];
}
void set_addelem(set_type set, long elem)
/* add elem only if it is within the set[] range */
{
long i,j;
unsigned long change;
unsigned long one=1U;
if (elem<=set[0])
{
i=(elem-1)/SETBITS+1;
j=(elem-1)%SETBITS;
change= one << j; /* put 1 in jth position */
set[i]=set[i] | change;
}
}
void set_delelem(set_type set, long elem)
/* delete elem only if it is within the set[] range */
{
long i,j;
unsigned long change;
unsigned long one=1U;
if (elem<=set[0])
{
i=(elem-1)/SETBITS+1;
j=(elem-1)%SETBITS;
change=one << j; /* put 1 in jth position */
set[i]=(set[i] | change) ^ change;
}
}
void set_int(set_type set,set_type set1,set_type set2)
/* Set intersection, assuming set1 and set2 have the same length as set */
{
long i,forlim;
forlim=set_blocks(set[0])-1;
for (i=1;i<=forlim;i++)
set[i]=(set1[i] & set2[i]);
}
void set_uni(set_type set,set_type set1,set_type set2)
/* Set union,assuming set1 and set2 have the same length as set */
{
long i,forlim;
forlim=set_blocks(set[0])-1;
for (i=1;i<=forlim;i++)
set[i]=set1[i] | set2[i];
}
void set_diff(set_type set,set_type set1,set_type set2)
/* Set difference se1/set2, assuming set1 and set2 have the same length as set */
{
long i,forlim;
forlim=set_blocks(set[0])-1;
for (i=1;i<=forlim;i++)
set[i]=set1[i] & (~set2[i]);
}
void set_compl(set_type set,set_type set1)
/* set[] will be set to the complement of set1[] */
{
long i,forlim;
forlim=set_blocks(set[0])-1;
for (i=1;i<=forlim;i++)
set[i]= ~set1[i];
}
int set_subset(set_type set1,set_type set2)
/* Set containment check, set1 <= set2 */
{
int yes=1;
long i,forlim;
forlim=set_blocks(set2[0])-1;
for (i=1;i<=forlim && yes;i++)
if ((set1[i] | set2[i])!=set2[i])
yes=0;
return yes;
}
int set_member(long elem, set_type set)
/* Set membership check, elem in set */
{
int yes=0;
long i,j;
unsigned long testset;
unsigned long one=1U;
if (elem<=set[0])
{
i=(elem-1)/SETBITS+1;
j=(elem-1)%SETBITS;
testset=set[i] | (one<<j); /* add elem to set[i] */
if (testset==set[i])
yes=1;
}
return yes;
}
long set_card(set_type set)
/* set cardinality, modified by David Bremner [email protected]
to optimize for speed. */
{
unsigned long block;
long car=0;
set_card_lut_t *p;
p=(set_card_lut_t *)&set[1];
for (block=0; block< LUTBLOCKS(set);block++) {
car+=set_card_lut[p[block]];
}
return car;
}
/* old cardinality code
long set_card(set_type set)
{
long elem,car=0;
for (elem=1; elem<=set[0]; elem++) {
if (set_member(elem,set)) car++;
}
return car;
}
*/
void set_write(set_type set)
{
long elem;
for (elem=1;elem<=set[0];elem++)
{
if (set_member(elem,set))
printf("%ld ",elem);
}
printf("\n");
}
void set_fwrite(FILE *f,set_type set)
{
long elem;
for (elem=1;elem<=set[0];elem++)
{
if (set_member(elem,set))
fprintf(f,"%ld ",elem);
}
fprintf(f,"\n");
}
void set_binwrite(set_type set)
{
int i,j;
long forlim;
unsigned long e1,e2;
printf("max element = %ld,\n",set[0]);
forlim=set_blocks(set[0])-1;
for (i=forlim;i>=1;i--)
{
e1=e2=set[i];
for (j=SETBITS-1;j>=0;j--)
{
e1=(e1>>j);
printf("%1ld",e1);
e1=e2-(e1<<j);
e2=e1;
}
printf(" ");
}
printf("\n");
}
void set_fbinwrite(FILE *f,set_type set)
{
int i,j;
long forlim;
long e1,e2;
printf("max element = %ld,\n",set[0]);
forlim=set_blocks(set[0])-1;
for (i=forlim;i>=1;i--)
{
e1=e2=set[i];
for (j=SETBITS-1;j>=0;j--)
{
e1=(e1>>j);
fprintf(f,"%1ld",e1);
e1=e2-(e1<<j);
e2=e1;
}
fprintf(f," ");
}
fprintf(f,"\n");
}
/* End of the library: setoper.c */