2016-09-18 20:01:57 +08:00
|
|
|
/*=============================================================================
|
|
|
|
# Filename: Varset.cpp
|
|
|
|
# Author: Jiaqi, Chen
|
|
|
|
# Mail: chenjiaqi93@163.com
|
2017-07-14 16:42:47 +08:00
|
|
|
# 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;
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
Varset::Varset(const string &_var)
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
2017-07-14 16:42:47 +08:00
|
|
|
this->addVar(_var);
|
2016-09-18 20:01:57 +08:00
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
Varset::Varset(const vector<string> &_vars)
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
2017-07-14 16:42:47 +08:00
|
|
|
for (int i = 0; i < (int)_vars.size(); i++)
|
|
|
|
this->addVar(_vars[i]);
|
2016-09-18 20:01:57 +08:00
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
bool Varset::empty() const
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
2017-07-14 16:42:47 +08:00
|
|
|
return this->vars.empty();
|
2016-09-18 20:01:57 +08:00
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +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))
|
2017-07-14 16:42:47 +08:00
|
|
|
this->vars.push_back(_var);
|
2016-09-18 20:01:57 +08:00
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
Varset Varset::operator + (const Varset &_varset) const
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
2017-07-14 16:42:47 +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;
|
|
|
|
};
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
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;
|
2017-07-14 16:42:47 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
Varset Varset::operator - (const Varset &_varset) const
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
|
|
|
Varset r;
|
2017-07-14 16:42:47 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
bool Varset::operator == (const Varset &_varset) const
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
2017-07-14 16:42:47 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
bool Varset::hasCommonVar(const Varset &_varset) const
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
2017-07-14 16:42:47 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
bool Varset::belongTo(const Varset &_varset) const
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
2017-07-14 16:42:47 +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;
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
vector<int> Varset::mapTo(const Varset &_varset) const
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
|
|
|
vector<int> r;
|
2017-07-14 16:42:47 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < (int)this->vars.size(); i++)
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
|
|
|
r.push_back(-1);
|
2017-07-14 16:42:47 +08:00
|
|
|
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;
|
|
|
|
}
|
2017-07-14 16:42:47 +08:00
|
|
|
|
2016-09-18 20:01:57 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:42:47 +08:00
|
|
|
void Varset::print() const
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
|
|
|
printf("Varset: ");
|
2017-07-14 16:42:47 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < (int)this->vars.size(); i++)
|
2016-09-18 20:01:57 +08:00
|
|
|
{
|
2017-07-14 16:42:47 +08:00
|
|
|
printf("%s ", this->vars[i].c_str());
|
2016-09-18 20:01:57 +08:00
|
|
|
}
|
2017-07-14 16:42:47 +08:00
|
|
|
|
2016-09-18 20:01:57 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|