-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelation.cpp
More file actions
210 lines (190 loc) · 6.28 KB
/
Relation.cpp
File metadata and controls
210 lines (190 loc) · 6.28 KB
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
//
// Created by 18bma on 10/21/2021.
//
#include "Relation.h"
Relation::Relation(){}
Relation::Relation(string name, Header header){
this->name = name;
this->header = header;
}
Header Relation::getHeader(){
return header;
}
void Relation::setHeader(Header header){
this->header = header;
}
void Relation::toString(){
for (Tuple t : rows) {
if(t.getValues().size() > 0){
cout << " ";
}
for(unsigned int i = 0; i < t.getValues().size(); i++){
cout << header.getAttributes().at(i) << "=" << t.getValues().at(i);
if(i < t.getValues().size() - 1){
cout << ", ";
}
}
if(t.getValues().size() > 0){
cout << endl;
}
}
return;
}
void Relation::addTuple(Tuple t){
rows.insert(t);
return;
}
void Relation::setCounter(int num){
queryCounter = num;
}
void Relation::setVariableTracker(map<string, int> variableTracker){
this->variableTracker = variableTracker;
}
void Relation::setVariableList(vector<string> variableList){
this->variableList = variableList;
}
map<string, int> Relation::getVariableTracker(){
return variableTracker;
}
vector<string> Relation::getVariableList(){
return variableList;
}
set<Tuple> Relation::getRows(){
return rows;
}
int Relation::getQueryCounter(){
return queryCounter;
}
Relation* Relation::Select(int columnNumber, string rowID){
Relation* relation = new Relation();
for(const Tuple& t: rows) {
if(t.getValues().at(columnNumber) == rowID){
relation->addTuple(t);
}
}
return relation;
}
Relation* Relation::Select2(int columnNumber, map<string, int> variableTracker, string rowID){
Relation* relation = new Relation();
for(const Tuple& t: rows) {
if(t.getValues().at(variableTracker[t.getValues().at(columnNumber)]) == t.getValues().at(columnNumber)){
relation->addTuple(t);
}
}
return relation;
}
Relation* Relation::Project(vector<string> variableList, map<string, int> variableTracker){
Relation *newRelation = new Relation;
for(const Tuple& t: rows){
Tuple newTuple;
for(unsigned int i = 0; i < variableList.size(); i++) {
newTuple.addValue(t.getValues().at(variableTracker[variableList.at(i)]));
}
newRelation->addTuple(newTuple);
}
return newRelation;
}
Relation* Relation::Rename(Relation *relation, vector<string> variableList, map<string, int> variableTracker){
for (unsigned int i = 0; i < variableList.size(); i++) {
relation->header.addAttribute(this->header.getAttributes().at(variableTracker[variableList.at(i)]));
relation->header.setAttribute(variableList.at(i), i);
}
return relation;
}
Relation* Relation::Join(Relation *relation){
Relation *newRelation = new Relation;
newRelation->setHeader(this->getHeader());
vector<int> matchingIndicies;
vector<int> differentIndicies;
vector<pair<int, int>> pairList;
bool same;
bool combine;
for(unsigned int i = 0; i < relation->getHeader().getAttributes().size(); i++){
same = false;
for(unsigned int j = 0; j < this->getHeader().getAttributes().size(); j++){
if(relation->getHeader().getAttributes().at(i) == this->getHeader().getAttributes().at(j)){
same = true;
pair<int, int> whereDoTheyMatch;
whereDoTheyMatch.first = i;
whereDoTheyMatch.second = j;
pairList.push_back(whereDoTheyMatch);
}
}
if(same == true){
matchingIndicies.push_back(i);
}
else{
differentIndicies.push_back(i);
}
}
//Combine Headers
for(unsigned int i = 0; i < differentIndicies.size(); i++){
newRelation->header.addAttribute(relation->getHeader().getAttributes().at(differentIndicies.at(i)));
}
//check if the tuples are able to be combined
for(const Tuple& t: relation->getRows()){
for(const Tuple& t2: rows){
combine = false;
for(unsigned int i = 0; i < pairList.size(); i++){
if(t.getValues().at(pairList.at(i).first) == t2.getValues().at(pairList.at(i).second)){
combine = true;
}
else {
combine = false;
break;
}
}
if(combine == true && differentIndicies.size() > 0){
Tuple newTuple;
newTuple = t2;
for(unsigned int i = 0; i < differentIndicies.size(); i++) {
newTuple.addValue(t.getValues().at(differentIndicies.at(i)));
}
newRelation->addTuple(newTuple);
}
if(combine == true && differentIndicies.size() == 0){
Tuple newTuple;
newTuple = t2;
for(unsigned int i = 0; i < matchingIndicies.size(); i++) {
newTuple.addValue(t.getValues().at(matchingIndicies.at(i)));
}
newRelation->addTuple(newTuple);
}
if(matchingIndicies.size() == 0){
Tuple newTuple;
newTuple = t2;
for(unsigned int i = 0; i < t.getValues().size(); i++) {
newTuple.addValue(t.getValues().at(i));
}
newRelation->addTuple(newTuple);
}
}
}
return newRelation;
}
bool Relation::FusionHa(Relation *relation) {
bool newTuples = false;
for(const Tuple& t: relation->getRows()){
bool combine = true;
for(const Tuple& t2: rows){
if(t.getValues() == t2.getValues()){
combine = false;
}
}
if(combine == true) {
newTuples = true;
this->addTuple(t);
for(unsigned int i = 0; i < this->getHeader().getAttributes().size(); i++){
if(i == 0){
cout << " ";
}
cout << this->getHeader().getAttributes().at(i) << "=" << t.getValues().at(i);
if(i < this->getHeader().getAttributes().size() - 1){
cout << ", ";
}
}
cout << endl;
}
}
return newTuples;
}