-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
156 lines (135 loc) · 3.64 KB
/
utils.cpp
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
// Author: Kristi Daigh
// Project: MLEM2 Rule Induction
// Date: 11/20/2019
/** Source file for utils.
@file utils.cpp
This class provides project-wide
utility functions for sets, etc. */
#include "utils.hpp"
#include <iostream>
using namespace std;
set<int> setDifference(set<int> setA, set<int> setB){
if(setA.empty() || setB.empty()){
return setA;
}
set<int> result;
set_difference(setA.begin(), setA.end(),
setB.begin(), setB.end(),
inserter(result, result.begin()));
return result;
}
set<int> setIntersection(set<int> setA, set<int> setB){
if(setB.empty()){
return setB;
} else if(setA.empty()){
return setA;
}
set<int> result;
set_intersection(setA.begin(), setA.end(),
setB.begin(), setB.end(),
inserter(result, result.begin()));
return result;
}
set<int> setsIntersection(vector<set<int> > sets){
set<int> result;
if(sets.empty()){
return result;
}
if(sets.size() == 1){
return sets[0];
}
set_intersection(sets[0].begin(), sets[0].end(),
sets[1].begin(), sets[1].end(),
inserter(result, result.begin()));
if(sets.size() == 2){
return result;
}
set<int> buffer;
for(unsigned i = 2; i < sets.size(); i++){
buffer.clear();
set_intersection(result.begin(), result.end(),
sets[i].begin(), sets[i].end(),
inserter(buffer, buffer.begin()));
swap(result, buffer);
}
return result;
}
set<int> setUnion(set<int> setA, set<int> setB){
if(setB.empty()){
return setA;
} else if(setA.empty()){
return setB;
}
set<int> result;
set_union(setA.begin(), setA.end(),
setB.begin(), setB.end(),
inserter(result, result.begin()));
return result;
}
set<int> setsUnion(vector<set<int> > sets){
set<int> result;
if(sets.empty()){
return result;
}
if(sets.size() == 1){
return sets[0];
}
set_union(sets[0].begin(), sets[0].end(),
sets[1].begin(), sets[1].end(),
inserter(result, result.begin()));
if(sets.size() == 2){
return result;
}
set<int> buffer;
for(unsigned i = 2; i < sets.size(); i++){
buffer.clear();
set_union(result.begin(), result.end(),
sets[i].begin(), sets[i].end(),
inserter(buffer, buffer.begin()));
swap(result, buffer);
}
return result;
}
bool subsetEq(set<int> setA, set<int> setB){
if(setA.empty()){
return true;
}
return includes(setB.begin(), setB.end(),
setA.begin(), setA.end());
}
bool commonElements(set<int> setA, set<int> setB){
return !(setIntersection(setA, setB).empty());
}
void printSet(string label, set<int> set){
if(set.empty()){
cout << label << "{}\n";
return;
}
cout << label << "{";
for(int i : set){
cout << i << ", ";
}
cout << "}\n";
}
void printList(string label, vector<set<int> > list){
if(list.empty()){
cout << label << "{}\n";
return;
}
cout << label << "{\t";
for(set<int> set : list){
cout << "{";
for(int i : set){
cout << i << ", ";
}
cout << "},\n\t";
}
cout << "}\n";
}
void printMap( map<int, set<int>> map ) {
// FOR: Each rule, print to file
for( auto const & [x, y] : map ){
cout << "T[" << x << "] = ";
printSet("", y);
}
}