-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathset.c
222 lines (183 loc) · 4.98 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
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
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: set.c
* Author : Kyle Loudon/Dan Levin
* Date : Mon Apr 08 12:32:13 2013
* Version : 0.51
* ---
* Description: A pure, generic set ADT - written in ANSI C
*
* Date Revision message
* 130413 Created this file
* 150331 This code ready for version 0.51
*
*/
/**
* @file set.c
**/
#include <stdio.h>
#include <stdlib.h>
#include "set.h"
/* --- Function: Set SETinit(int (*match)(const void *key1, const void *key2), void (*destroy)(void *data)) --- */
Set SETinit(int (*match)(const void *key1, const void *key2), void (*destroy)(void *data))
{
Set set;
set = SLISTinit(destroy);
if (set)
SLISTsetmatch(set, match);
return set;
}
/* --- Furnction: void SETdestroy(Set set) --- */
void SETdestroy(Set set)
{
SLISTdestroy(set);
}
/* --- Function: int SETinsert(Set set, const void *data) --- */
int SETinsert(Set set, const void *data)
{
/* Do not allow the insertion of duplicates */
if (SETis_member(set, data))
return 1;
/* Insert the data */
return SLISTinsnext(set, SLISTtail(set), data);
}
/* --- Function: int SETremove(Set set, void **data) --- */
int SETremove(Set set, void **data)
{
return SLISTfind_remove(set, data);
}
/* --- Function: Set SETunion(Set set1, Set set2) --- */
Set SETunion(Set set1, Set set2)
{
Set setu;
SlistNode member;
void *data;
/* Initialize the set for the union */
setu = SETinit(SLISTgetmatch(set1), NULL);
/* Insert the members of the first set */
for (member = SLISThead(set1); member != NULL; member = SLISTnext(member))
{
data = SLISTdata(member);
if (SLISTinsnext(setu, SLISTtail(setu), data) != 0)
{
SETdestroy(setu);
return NULL;
}
}
/* Insert the members of the second set */
for (member = SLISThead(set2); member != NULL; member = SLISTnext(member))
{
if (SETis_member(set1, SLISTdata(member)))
{
/* Do not allow the insertion of duplicates */
continue;
}
else
{
data = SLISTdata(member);
if (SLISTinsnext(setu, SLISTtail(setu), data) != 0)
{
SETdestroy(setu);
return NULL;
}
}
}
return setu;
}
/* --- Function: Set SETintersection(Set set1, Set set2) --- */
Set SETintersection(Set set1, Set set2)
{
Set seti;
SlistNode member;
void *data;
/* Initialize the set for the intersection */
seti = SETinit(SLISTgetmatch(set1), NULL);
/* Insert the members present in both sets */
for (member = SLISThead(set1); member != NULL; member = SLISTnext(member))
{
if (SETis_member(set2, SLISTdata(member)))
{
data = SLISTdata(member);
if (SLISTinsnext(seti, SLISTtail(seti), data) != 0)
{
SETdestroy(seti);
return NULL;
}
}
}
return seti;
}
/* --- Function: Set SETdifference(Set set1, Set set2) --- */
Set SETdifference(Set set1, Set set2)
{
Set setd;
SlistNode member;
void *data;
/* Initialize the set for the difference */
setd = SETinit(SLISTgetmatch(set1), NULL);
/* Insert the members from set1 not in set2 */
for (member = SLISThead(set1); member != NULL; member = SLISTnext(member))
{
if (!SETis_member(set2, SLISTdata(member)))
{
data = SLISTdata(member);
if (SLISTinsnext(setd, SLISTtail(setd), data) != 0)
{
SETdestroy(setd);
return NULL;
}
}
}
return setd;
}
/* --- Function: int SETis_member(Set set, const void *data) --- */
int SETis_member(Set set, const void *data)
{
if (SLISTfindnode(set, data) != NULL)
return 1;
else
return 0;
}
/* --- Function: int SETis_subset(const Set set1, const Set set2) --- */
int SETis_subset(const Set set1, const Set set2)
{
SlistNode tmp;
/* Do a quick test to rule out some cases */
if (SETsize(set1) > SETsize(set2))
return 0;
/* Determine if set1 is a subset of set2 */
for (tmp = SLISThead(set1); tmp != NULL; tmp = SLISTnext(tmp))
{
if (!SETis_member(set2, SLISTdata(tmp)))
return 0;
}
return 1;
}
/* --- Function: int SETis_equal(Set set1, Set set2) --- */
int SETis_equal(Set set1, Set set2)
{
/* Do a quick test to rule out some cases */
if (SETsize(set1) != SETsize(set2))
return 0;
/* Sets of the same size are equal if they are subsets */
return SETis_subset(set1, set2);
}
/* --- Function: int SETsize(Set set) --- */
int SETsize(Set set)
{
return SLISTsize(set);
}
/* --- Function: void SETsort(Set set, int (*cmp)(const void *key1, const void *key2)) --- */
void SETsort(Set set, int (*cmp)(const void *key1, const void *key2))
{
SLISTsort(set, cmp);
}
/* --- Function: void SETtraverse(Set set, void (*callback)(const void *data), int direction) --- */
void SETtraverse(Set set, void (*callback)(const void *data), int direction)
{
SLISTtraverse(set, callback, direction);
}