-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSet.c
427 lines (401 loc) · 10.7 KB
/
CSet.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// On my honor:
//
// - I have not discussed the C language code in my program with
// anyone other than my instructor or the teaching assistants
// assigned to this course.
//
// - I have not used C language code obtained from another student,
// the Internet, or any other unauthorized source, either modified
// or unmodified.
//
// - If any C language code or documentation used in my program
// was obtained from an authorized source, such as a text book or
// course notes, that has been clearly noted with a proper citation
// in the comments of my program.
//
// - I have not designed this program in such a way as to defeat or
// interfere with the normal operation of the grading code.
//
// Dana Gurland
// dgurland
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "CSet.h"
#define FILLER INT32_MIN // sentinel value for unused array cells
int compare(const void * a, const void * b);
// CSet provides an implementation of a set type for storing a collection of
// signed 32-bit integer values (int32_t).
//
// The implementation imposes the following constraints:
// - storage is array-based
// - duplicate elements are not allowed in a CSet
// - logically empty cells are set to INT32_MIN
// - sets can contain up to UINT32_MAX elements
// - unless noted to the contrary, the worst-case cost of each operation
// is O(N), where N is the number of elements in the CSet object(s)
// that are involved
// - empty test is O( 1 )
// - Contains() is O( log N )
// - there are no memory leaks during any of the supported operations
//
// We say a CSet object A is proper if and only if it satisfies each of the
// following conditions:
//
// 1. If A.Capacity == 0 then A.Usage == 0 and A.Data == NULL.
// 2. If A.Capacity > 0 then A.Data points to an array of dimension A.Capacity.
// 3. A.Data[0 : A.Usage-1] are the values stored in the set
// (in unspecified order)
// 4. A.Data[A.Usage : A.Capacity-1] equal INT32_MIN (FILLER)
//
// CSet objects that have not been initialized are said to be raw.
//
// With the sole exception of CSet_Init(), all CSet objects supplied as
// parameters are expected to be proper.
//
/**
struct _CSet {
uint32_t Capacity; // dimension of the set's array
uint32_t Usage; // number of elements in the set
int32_t* Data; // pointer to the set's array
};
typedef struct _CSet CSet;
*/
/**
* Initializes a raw pSet object, with capacity Sz.
*
* Pre:
* pSet points to a CSet object, but *pSet may be proper or raw
* Sz has been initialized
* Post:
* If successful:
* pSet->Capacity == Sz
* pSet->Usage == 0
* pSet->Data points to an array of dimension Sz, or
* is NULL if Sz == 0
* if Sz != 0, pSet->Data[0 : Sz-1] == INT32_MIN
* else:
* pSet->Capacity == 0
* pSet->Usage == 0
* pSet->Data == NULL
* Returns:
* true if successful, false otherwise
*
* Complexity: O( Sz )
*/
bool CSet_Init(CSet* const pSet, uint32_t Sz){
if(Sz > 0) {
pSet->Capacity = Sz;
// free(pSet->Data);
pSet->Usage = 0;
// pSet->Data = calloc(pSet->Capacity, sizeof(int32_t));
int32_t* temp = (int32_t*)calloc(Sz, sizeof(int32_t));
pSet->Data = temp;
for(int i = 0; i < pSet->Capacity; i++) {
pSet->Data[i] = INT32_MIN;
}//for
return true;
}//if
else {
pSet->Data = NULL;
pSet->Usage = 0;
pSet->Capacity = 0;
return false;
}//else
}//init
/**
* Adds Value to a pSet object.
*
* Pre:
* *pSet is proper
* Value has been initialized
* Post:
* If successful:
* Value is a member of *pSet
* pSet->Capacity has been increased, if necessary
* *pSet is proper
* else:
* *pSet is unchanged
* Returns:
* true if successful, false otherwise
*
* Complexity: O( pSet->Usage )
*/
bool CSet_Insert(CSet* const pSet, int32_t Value){
if(CSet_Contains(pSet, Value)) {
return false;
}
if(pSet->Usage == pSet->Capacity){
int32_t* newData = (int32_t*)realloc(pSet->Data, 2*pSet->Capacity);
pSet->Capacity = pSet->Capacity * 2;
*pSet->Data = newData;
}//increase capacity
pSet->Data[pSet->Usage] = Value;
pSet->Usage = pSet->Usage + 1;
qsort(pSet->Data, pSet->Usage, sizeof(int32_t), compare);
return true;
}//insert
/**
* Removes Value from a pSet object.
*
* Pre:
* *pSet is proper
* Value has been initialized
* Post:
* If Value was a member of *pSet:
* Value is no longer a member of *pSet
* pSet->Capacity is unchanged
* pSet->Usage is decremented
* *pSet is proper
* else:
* *pSet is unchanged
* Returns:
* true if Value was removed, false otherwise
*
* Complexity: O( pSet->Usage )
*/
bool CSet_Remove(CSet* const pSet, int32_t Value){
if(!CSet_Contains(pSet, Value)){
return false;
}//if
int32_t counter = 0;
bool found = false;
while(!found && counter < pSet->Usage){
if(pSet->Data[counter] == Value) {
found = true;
pSet->Data[counter] = pSet->Data[pSet->Usage - 1];
pSet->Data[pSet->Usage - 1] = FILLER;
pSet->Usage = pSet->Usage - 1;
}//if
counter = counter + 1;
}//while
qsort(pSet->Data, pSet->Usage, sizeof(int32_t), compare);
return found;
}//remove
/**
* Determines if Value belongs to the given pSet object.
*
* Pre:
* *pSet is proper
* Value has been initialized
* Post:
* *pSet is unchanged
* Returns:
* true if Value belongs to *pSet, false otherwise
*
* Complexity: O( log(pSet->Usage) )
*/
bool CSet_Contains(const CSet* const pSet, int32_t Value){
int32_t *found;
found = (int32_t*) bsearch(&Value, pSet->Data, pSet->Usage, sizeof(int32_t), compare);
if (found != NULL) {
return true;
}
else {
return false;
}
}//contains
/**
* Determines if two CSet objects contain the same elements.
*
* Pre:
* *pA and *pB are proper
* Post:
* *pA is unchanged
* *pB is unchanged
* Returns:
* true if sets contain same elements, false otherwise
*
* Complexity: O( pA->Usage )
*/
bool CSet_Equals(const CSet* const pA, const CSet* const pB){
bool equal = true;
int32_t counter = 0;
if(pA->Usage != pB->Usage) {
return false;
}
while(equal && counter < pA->Usage) {
if(pA->Data[counter] != pB->Data[counter]){
equal = false;
}//if
counter = counter + 1;
}//
return equal;
}//equals
/**
* Determines if one CSet object is a subset of another.
*
* Pre:
* *pA and *pB are proper
* Post:
* *pA is unchanged
* *pB is unchanged
* Returns:
* true if *pB contains every element of *pA, false otherwise
* Complexity: O( pA->Usage )
*/
bool CSet_isSubsetOf(const CSet* const pA, const CSet* const pB){
if(pA->Usage > pB->Usage) {
return false;
}
bool equal = true;
int32_t counter = 0;
while(equal && counter < pA->Usage){
if(!CSet_Contains(pB, pA->Data[counter])){
equal = false;
}//if
counter = counter + 1;
}//while
return equal;
}//subset
/**
* Sets *pUnion to be the union of the sets *pA and *pB.
*
* Pre:
* *pUnion, *pA and *pB are proper
* Post:
* *pA and *pB are unchanged
* For every integer x, x is contained in *pUnion iff x is contained in
* *pA or in *pB (or both).
* pUnion->Capacity == pA->Capacity + pB->Capacity
* pUnion->Usage == pA->Usage + pB->Usage - number of elements that
* occur in both *pA and *pB
* *pUnion is proper
* Returns:
* true if the union is successfully created; false otherwise
*
* Complexity: O( max(pA->Usage, pB->Usage) )
*/
bool CSet_Union(CSet* const pUnion, const CSet* const pA, const CSet* const pB){
CSet* temp = malloc(sizeof(CSet));
CSet_Init(temp, pA->Capacity + pB->Capacity);
int max = pA->Usage;
if(pB->Usage > pA->Usage) {
max = pB->Usage;
}//max;
for(int i = 0; i < max; i++) {
if (i < pA->Usage) {
CSet_Insert(temp, pA->Data[i]);
}
if (i < pB->Usage) {
CSet_Insert(temp, pB->Data[i]);
}
}//for
CSet_Copy(pUnion, temp);
return true;
}//union
/**
* Sets *pSym to be the symmetric difference of the sets *pA and *pB.
*
* Pre:
* *pSym, *pA and *pB are proper
* Post:
* *pA and *pB are unchanged
* For every integer x, x is contained in *pSym iff x is contained in
* *pA but not in *pB, or x is contained i *pB but not in *pA.
* pDiff->Capacity == pA->Capacity + pB->Capacity
* pDiff->Usage == pA->Usage - number of elements that
* occur in exactly one of *pA and *pB
* *pSym is proper
* Returns:
* true if the difference is successfully created; false otherwise
*
* Complexity: O( max(pA->Usage, pB->Usage) )
*/
bool CSet_SymDifference(CSet* const pSym, const CSet* const pA, const CSet* const pB){
CSet* temp = malloc(sizeof(CSet));
CSet_Init(temp, pA->Capacity + pB->Capacity);
int max = pA->Usage;
if(pB->Usage > max) {
max = pB->Usage;
}//max
for(int i = 0; i < max; i++){
if (i < pA->Usage && !CSet_Contains(pB, pA->Data[i])) {
CSet_Insert(temp, pA->Data[i]);
}
if (i < pB->Usage && !CSet_Contains(pA, pB->Data[i])) {
CSet_Insert(temp, pB->Data[i]);
}
}//for
CSet_Copy(pSym, temp);
return true;
}
/**
* Makes a deep copy of a CSet object.
*
* Pre:
* *pTarget and *pSource are proper
* Post:
* *pSource is unchanged
* If successful:
* pTarget->Capacity == pSource->Capacity
* pTarget->Usage == pSource->Usage
* pTarget[0:pTarget->Capacity-1] == pSource[0:pSource->Capacity-1]
* pTarget->Data != pSource->Data, unless pTarget == pSource
* *pTarget is proper.
* else:
* *pTarget is unchanged
* Returns:
* true if successful, false otherwise
*
* Complexity: O( max(pSource->Usage) )
*/
bool CSet_Copy(CSet* const pTarget, const CSet* const pSource){
if (pTarget == pSource) {
return false;
}
CSet_Init(pTarget, pSource->Capacity);
for(int i = 0; i < pSource->Usage; i++) {
CSet_Insert(pTarget, pSource->Data[i]);
}//for
return true;
}
/**
* Reports the current capacity of a pSet object.
*
* Pre:
* *pSet is proper
* Post:
* *pSet is unchanged
* Returns:
* pSet->Capacity
*
* Complexity: O( 1 )
*/
uint32_t CSet_Capacity(const CSet* const pSet){
return(pSet->Capacity);
}//capacity
/**
* Reports the number of elements in a pSet object.
*
* Pre:
* *pSet is proper
* Post:
* *pSet is unchanged
* Returns:
* pSet->Usage
*
* Complexity: O( 1 )
*/
uint32_t CSet_Usage(const CSet* const pSet){
return(pSet->Usage);
}//usage
/**
* Determines whether a CSet object is empty.
*
* Pre:
* *pSet is proper
* Post:
* *pSet is unchanged
* Returns:
* true if pSet->Usage == 0, false otherwise
*
* Complexity: O( 1 )
*/
bool CSet_isEmpty(const CSet* const pSet){
return(pSet->Usage == 0);
}//empty
int compare(const void *a, const void * b) {
return (*(int*)a - *(int*)b );
}