-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelation.cpp
208 lines (182 loc) · 4.28 KB
/
relation.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
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
#include "relation.h"
Relation::Relation(vector<string> col_names)
{
columns = col_names;
}
void Relation::unionWith(Relation& other, ostream& out)
{
for (auto t: other.tuples)
{
if (tuples.find(t) == tuples.end()) out << " " << t.namedToString(columns) << endl;
add(t);
}
}
void init_join_map(int * join_map, vector<string>& columns, vector<string>& ocols, vector<string>& new_names)
{
size_t n_othercols = ocols.size();
size_t ncols = columns.size();
for (size_t i = 0; i < n_othercols; i++)
{
join_map[i] = -1;
for (size_t j = 0; j < ncols; j++)
{
//If one column name is the same as another
if (columns[j] == ocols[i]) {
join_map[i] = j;
break; // don't need to look any further, as there are no duplicate column names in a single relation
}
}
// if the current column in table other is not in the first table, add it to the new columns
if (join_map[i] == -1)
{
new_names.push_back(ocols[i]);
}
}
}
//Join this relation with another relation, creating a new relation
//Step 1: Determine the column names
Relation Relation::join(Relation& other)
{
//initialize the columns of the new relation to be those of the first table
vector<string> new_names = columns;
vector<string> ocols = other.columns;
// if join_map[i] is -1, then column i from table other is not found in the first table
// if join_map[i] = j >=0, then column i from table other is the same as column j in the first table.
int join_map[other.columns.size()];
init_join_map(join_map, columns, ocols, new_names);
Relation res = Relation(new_names);
for (auto t1: tuples)
{
for (auto t2: other.tuples)
{
vector<string> newData = t1.data;
bool is_compat = true;
for (int k = 0; k < t2.size(); k++)
{
if (join_map[k] >= 0)
{
if (t2[k] != t1[join_map[k]])
{
is_compat = false;
break;
}
}
else newData.push_back(t2[k]);
}
if (is_compat) res.add(Tuple(newData));
}
}
return res;
}
Relation Relation::select(vector<Constraint>& constraints)
{
Relation res = Relation(columns);
int l = constraints.size();
for (auto t: tuples)
{
bool flag = true;
for (int i = 0; i < l; i++)
{
if (!constraints[i].isMet(t.data))
{
flag = false;
}
}
if(flag) res.add(t); // we know the entries will be traversed in sorted order
}
return res;
}
Relation Relation::project(vector<string> names)
{
vector<int> newCols;
for (size_t k = 0; k < names.size(); k++)
{
for (size_t j = 0; j < columns.size(); j++) {
if (names[k] == columns[j])
{
newCols.push_back(j);
break;
}
}
}
// cout << "Projecting onto cols ";
// for (size_t i=0; i < newCols.size(); i++) cout << newCols[i] << " ";
// cout << endl;
return project(newCols);
}
Relation Relation::project(vector<int>& newInds)
{
int l = newInds.size();
vector<string> newColumns;
for (int k = 0; k < l; k++) newColumns.push_back(columns[newInds[k]]);
Relation res = Relation(newColumns);
if (l <=0 ) return res;
for (auto f : tuples)
{
vector<string> newTuple;
for (int j =0; j < l; j++)
{
newTuple.push_back(f[newInds[j]]);
}
res.add(Tuple(newTuple));
}
return res;
}
Relation Relation::rename(vector<string> newNames)
{
Relation res = Relation(newNames);
for (auto t: tuples)
{
res.add(t);
}
return res;
}
void Relation::add(vector<string> v)
{
// cout << "called add " << tuple_to_string(v) << endl;
tuples.insert(Tuple(v));
}
string Relation::toString()
{
string res = "";
// res += tuple_to_string(columns);
// res += "\n";
for (auto t: tuples)
{
res += " ";
res += t.namedToString(columns);
res += "\n";
}
return res;
}
void Relation::add(Tuple t)
{
tuples.insert(t);
}
int Relation::size()
{
return tuples.size();
}
//utility function
string tuple_to_string(vector<string>& t)
{
string res = "";
for (size_t j = 0; j < t.size(); j++)
{
if (j) res += " ";
res += t[j];
}
return res;
}
string named_tuple_to_string(vector<string>& t, vector<string>& names)
{
string res = "";
for (size_t j = 0; j < t.size(); j++)
{
if (j) res += " ";
res += names[j];
res += "=";
res += t[j];
}
return res;
}