gStore/Query/Varset.cpp

148 lines
2.7 KiB
C++
Raw Normal View History

2016-09-18 20:01:57 +08:00
/*=============================================================================
# Filename: Varset.cpp
# Author: Jiaqi, Chen
# Mail: chenjiaqi93@163.com
# Last Modified: 2017-03-10
2016-09-18 20:01:57 +08:00
# Description: implement functions in Varset.h
=============================================================================*/
#include "Varset.h"
using namespace std;
Varset::Varset(const string &_var)
2016-09-18 20:01:57 +08:00
{
this->addVar(_var);
2016-09-18 20:01:57 +08:00
}
Varset::Varset(const vector<string> &_vars)
2016-09-18 20:01:57 +08:00
{
for (int i = 0; i < (int)_vars.size(); i++)
this->addVar(_vars[i]);
2016-09-18 20:01:57 +08:00
}
bool Varset::empty() const
2016-09-18 20:01:57 +08:00
{
return this->vars.empty();
2016-09-18 20:01:57 +08:00
}
int Varset::getVarsetSize() const
{
return this->vars.size();
}
bool Varset::findVar(const string &_var) const
{
if (this->vars.empty())
return false;
for (int i = 0; i < (int)this->vars.size(); i++)
if (this->vars[i] == _var) return true;
return false;
}
void Varset::addVar(const string &_var)
2016-09-18 20:01:57 +08:00
{
if (!this->findVar(_var))
this->vars.push_back(_var);
2016-09-18 20:01:57 +08:00
}
Varset Varset::operator + (const Varset &_varset) const
2016-09-18 20:01:57 +08:00
{
Varset r(*this);
for (int i = 0; i < (int)_varset.vars.size(); i++)
r.addVar(_varset.vars[i]);
2016-09-18 20:01:57 +08:00
return r;
};
Varset& Varset::operator += (const Varset &_varset)
{
for (int i = 0; i < (int)_varset.vars.size(); i++)
this->addVar(_varset.vars[i]);
return *this;
}
Varset Varset::operator * (const Varset &_varset) const
2016-09-18 20:01:57 +08:00
{
Varset r;
for (int i = 0; i < (int)this->vars.size(); i++)
if (_varset.findVar(this->vars[i]))
r.addVar(this->vars[i]);
2016-09-18 20:01:57 +08:00
return r;
}
Varset Varset::operator - (const Varset &_varset) const
2016-09-18 20:01:57 +08:00
{
Varset r;
for (int i = 0; i < (int)this->vars.size(); i++)
if (!_varset.findVar(this->vars[i]))
r.addVar(this->vars[i]);
2016-09-18 20:01:57 +08:00
return r;
}
bool Varset::operator == (const Varset &_varset) const
2016-09-18 20:01:57 +08:00
{
if ((int)this->vars.size() != (int)_varset.vars.size())
return false;
for (int i = 0; i < (int)this->vars.size(); i++)
if (!_varset.findVar(this->vars[i]))
return false;
2016-09-18 20:01:57 +08:00
return true;
}
bool Varset::hasCommonVar(const Varset &_varset) const
2016-09-18 20:01:57 +08:00
{
for (int i = 0; i < (int)this->vars.size(); i++)
if (_varset.findVar(this->vars[i]))
return true;
2016-09-18 20:01:57 +08:00
return false;
}
bool Varset::belongTo(const Varset &_varset) const
2016-09-18 20:01:57 +08:00
{
for (int i = 0; i < (int)this->vars.size(); i++)
if (!_varset.findVar(this->vars[i]))
return false;
2016-09-18 20:01:57 +08:00
return true;
}
vector<int> Varset::mapTo(const Varset &_varset) const
2016-09-18 20:01:57 +08:00
{
vector<int> r;
for (int i = 0; i < (int)this->vars.size(); i++)
2016-09-18 20:01:57 +08:00
{
r.push_back(-1);
for (int j = 0; j < (int)_varset.vars.size(); j++)
if (this->vars[i] == _varset.vars[j])
2016-09-18 20:01:57 +08:00
r[i] = j;
}
2016-09-18 20:01:57 +08:00
return r;
}
void Varset::print() const
2016-09-18 20:01:57 +08:00
{
printf("Varset: ");
for (int i = 0; i < (int)this->vars.size(); i++)
2016-09-18 20:01:57 +08:00
{
printf("%s ", this->vars[i].c_str());
2016-09-18 20:01:57 +08:00
}
2016-09-18 20:01:57 +08:00
printf("\n");
}