-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSet.c
96 lines (87 loc) · 1.81 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
/*
Group Number : 2
1 Dhruv Rawat 2019B3A70537P thedhruvrawat
2 Chirag Gupta 2019B3A70555P Chirag5128
3 Swastik Mantry 2019B1A71019P Swastik-Mantry
4 Shreyas Sheeranali 2019B3A70387P ShreyasSR
5 Vaibhav Prabhu 2019B3A70593P prabhuvaibhav
*/
#include <stdlib.h>
#include <string.h>
#include "Set.h"
/**
* @brief Allocates memory for a new set from the heap and initializes it
*
* @param sz
* @return Set*
*/
Set* initSet(int sz)
{
Set* s = malloc(sizeof(Set));
s->sz = sz;
s->contains = malloc(sz * sizeof(bool));
memset(s->contains, false, sz * sizeof(bool));
return s;
}
/**
* @brief Computes the union of the two input sets and stores it in the first set
*
* @param a
* @param b
* @return true
* @return false
*/
bool unionSet(Set* a, Set* b)
{
int sz = a->sz;
// Flag for checking LL(1)
bool flag = false;
for (int i = 0; i < sz; ++i) {
if (a->contains[i] && b->contains[i]) {
flag = true;
}
a->contains[i] = a->contains[i] || b->contains[i];
}
return flag;
}
/**
* @brief Computes the intersection of the two input sets and stores it in the first set
*
* @param a
* @param b
*/
void intersectionSet(Set* a, Set* b)
{
int sz = a->sz;
for (int i = 0; i < sz; ++i) {
a->contains[i] = a->contains[i] && b->contains[i];
}
return;
}
/**
* @brief Computes the difference of the two input sets and stores it in the first set
*
* @param a
* @param b
*/
void differenceSet(Set* a, Set* b)
{
int sz = a->sz;
for (int i = 0; i < sz; ++i) {
if (b->contains[i]) {
a->contains[i] = false;
}
}
return;
}
/**
* @brief Frees the memory allocated for the set
*
* @param s
*/
void destroySet(Set* s)
{
free(s->contains);
free(s);
return;
}