working on crossprodut
This commit is contained in:
parent
27f63bd452
commit
d1e610a8c6
6 changed files with 86 additions and 43 deletions
99
DBEngine.cpp
99
DBEngine.cpp
|
@ -101,6 +101,7 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){
|
|||
else {
|
||||
//currently all returned relations are called TEMP
|
||||
Relation new_r = r1;
|
||||
new_r.setTableName("TEMP");
|
||||
vector<string> temp;
|
||||
bool duplicate = false;
|
||||
|
||||
|
@ -122,32 +123,76 @@ Relation DBEngine::setUnion(Relation r1, Relation r2){
|
|||
}
|
||||
|
||||
return new_r;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*vector<Attribute> r1_atts = r1.getAttributes();
|
||||
vector<Attribute> r2_atts = r2.getAttributes();
|
||||
vector<Attribute> new_atts = r1_atts;
|
||||
|
||||
for (int i = 0; i < r2_atts.size(); ++i) {
|
||||
for (int j = 0; j < r2_atts[i].getSize(); ++j){
|
||||
new_atts[i].addCell(r2_atts[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < new_atts.size(); ++i) {
|
||||
for (int j = 0; j < new_atts.size(); ++j){
|
||||
if (new_atts[i] == new_atts[j]){
|
||||
new_atts.erase(new_atts.begin() + i);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//currently all returned relations are called TEMP
|
||||
Relation new_r("TEMP", new_atts);
|
||||
return new_r;*/
|
||||
}
|
||||
}
|
||||
|
||||
Relation DBEngine::setDiff(Relation r1, Relation r2){
|
||||
if (r1.getAttributeNames() != r2.getAttributeNames()){
|
||||
cout << "Failure to diff: the relations are not union-compatible.\nreturning the first relation.\n";
|
||||
return r1;
|
||||
}
|
||||
|
||||
else {
|
||||
//currently all returned relations are called TEMP
|
||||
Relation new_r = r1;
|
||||
new_r.setTableName("TEMP");
|
||||
vector<string> temp;
|
||||
bool duplicate = false;
|
||||
|
||||
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i){
|
||||
new_r.removeTuple(i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < r1.getAttributes()[0].getSize(); ++i) {
|
||||
temp = r1.getTuple(i);
|
||||
|
||||
for (int j = 0; j < r2.getAttributes()[0].getSize(); ++j){
|
||||
if (temp == r2.getTuple(j)){
|
||||
duplicate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!duplicate){
|
||||
new_r.insertTuple(temp);
|
||||
}
|
||||
|
||||
duplicate = false;
|
||||
}
|
||||
|
||||
//make this one for loop later!
|
||||
|
||||
for (int i = 0; i < r2.getAttributes()[0].getSize(); ++i) {
|
||||
temp = r2.getTuple(i);
|
||||
|
||||
for (int j = 0; j < r1.getAttributes()[0].getSize(); ++j){
|
||||
if (temp == r1.getTuple(j)){
|
||||
duplicate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!duplicate){
|
||||
new_r.insertTuple(temp);
|
||||
}
|
||||
|
||||
duplicate = false;
|
||||
}
|
||||
|
||||
return new_r;
|
||||
}
|
||||
}
|
||||
|
||||
Relation DBEngine::crossProduct(Relation r1, Relation r2){
|
||||
vector<Attribute> new_atts = r1.getAttributes();
|
||||
|
||||
for (int i = 0; i < r2.getAttributes().size(); ++i) {
|
||||
new_atts.push_back(r2.getAttributes()[i]);
|
||||
}
|
||||
|
||||
//currently all returned relations are called TEMP
|
||||
Relation new_r("TEMP", new_atts);
|
||||
|
||||
|
||||
return new_r;
|
||||
}
|
|
@ -22,6 +22,6 @@ public:
|
|||
Relation projection(vector<string> input, Relation r);
|
||||
void rename(Relation& r, vector<string> oldnames, vector<string> newnames);
|
||||
Relation setUnion(Relation r1, Relation r2);
|
||||
//void setDiff();
|
||||
//void crossProduct();
|
||||
Relation setDiff(Relation r1, Relation r2);
|
||||
Relation crossProduct(Relation r1, Relation r2);
|
||||
};
|
||||
|
|
|
@ -23,6 +23,10 @@ string Relation::getTableName(){
|
|||
return name;
|
||||
}
|
||||
|
||||
void Relation::setTableName(string s){
|
||||
name = s;
|
||||
}
|
||||
|
||||
Attribute Relation::operator[](int i){
|
||||
return att[i];
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ public:
|
|||
Relation(string n, vector<Attribute> a);
|
||||
void insertAttributes(vector<Attribute> a);
|
||||
string getTableName();
|
||||
void setTableName(string s);
|
||||
Attribute operator[](int i);
|
||||
vector<string> getTuple(int index);
|
||||
vector<Attribute> getAttributes();
|
||||
|
|
BIN
a.out
BIN
a.out
Binary file not shown.
21
test.cpp
21
test.cpp
|
@ -29,26 +29,19 @@ int main() {
|
|||
|
||||
engine.createTable("Food", v);
|
||||
|
||||
Attribute att4("Breakfast", "VARCHAR(20)", true);
|
||||
Attribute att5("Lunch", "VARCHAR(20)", false);
|
||||
Attribute att6("Dinner", "VARCHAR(20)", false);
|
||||
Attribute att4("Dessert", "VARCHAR(20)", true);
|
||||
Attribute att5("Midnight Snack", "VARCHAR(20)", false);
|
||||
|
||||
att4.addCell("Pancakes");
|
||||
att4.addCell("Bacon");
|
||||
att4.addCell("Eggs");
|
||||
att5.addCell("Turkey Sandwich");
|
||||
att5.addCell("Pasta Salad");
|
||||
att5.addCell("Taco");
|
||||
att6.addCell("Steak");
|
||||
att6.addCell("Fajitas");
|
||||
att6.addCell("Spaghetti");
|
||||
att4.addCell("Ice Cream Sundae");
|
||||
att4.addCell("Chocolate Bar");
|
||||
att5.addCell("Hummus and Carrots");
|
||||
att5.addCell("Potato Chips");
|
||||
|
||||
vector<Attribute> v2;
|
||||
v2.push_back(att4);
|
||||
v2.push_back(att5);
|
||||
v2.push_back(att6);
|
||||
|
||||
engine.createTable("MoarFood", v2);
|
||||
|
||||
engine.setUnion(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display();
|
||||
engine.crossProduct(engine.getTableFromName("Food"), engine.getTableFromName("MoarFood")).display();
|
||||
}
|
||||
|
|
Reference in a new issue