1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/curriculum-design-express_station_system_gui

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
index.h 7 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
怎样 Отправлено 06.01.2021 15:16 af2f1d3
#pragma once
#include <functional>
#include <vector>
#include "rule.h"
#include "col.h"
#include "BPlusTree.h"
using namespace std;
//返回的下标组必须排序
//如果操作的是视图,就把查询结果和视图的allSub取交集(返回的还是原表的sub而不是视图表的)
class index
{
protected:
bool supportMod=false;
public:
virtual vector<int> find(ruleExp* rule)=0;
shared_ptr<col> c; //会直接跟着列变
index(shared_ptr<col> c) : c(c) {}
bool isSupportMod() { return this->supportMod; }
//都是在操作col前调用
virtual void add(shared_ptr<Basic> v) { throw string("This index does not support updating"); }
virtual void mod(int opSub, shared_ptr<Basic> v) { throw string("This index does not support updating"); }
virtual void del(int opSub) { throw string("This index does not support updating"); }
virtual ~index() {}
};
class traversalIndex : public index
{
public:
traversalIndex(shared_ptr<col> _c) : index(_c) {}
virtual vector<int> find(ruleExp* rule)
{
vector<int> result;
auto data=c->getAllData();
for(int i=0;i<data.size();i++)
{
shared_ptr<Basic> v=data[i];
if(rule->eval(v))
result.push_back(i);
}
return result; //这个本来就是从前到后遍历的,就不用排序了
}
};
class binarySearchIndex : public index
{
private:
vector<pair<float,int>> sortVec;
void colToVec()
{
sortVec.clear();
auto allData=this->c->getAllData();
for(int i=0;i<allData.size();i++)
{
shared_ptr<Basic> v=allData[i];
sortVec.push_back(make_pair(binarySearchIndex::getVal(v),i));
}
}
static int binFind(const vector<pair<float,int>> &invec, float value) //返回的是sub和opSub(表中的)
{
int low = 0, high = invec.size()-1;
//assert(!invec.empty() && pos>=0);
while(low <=high)
{
int mid = (low+high)/2;
if(invec[mid].first == value)
return mid;
else if(invec[mid].first < value)
low = mid+1;
else
high = mid-1;
}
return -1;
}
void resort()
{
sort(sortVec.begin(),sortVec.end(),
[](pair<int,int> i1,pair<int,int> i2) {
return i1.first<i2.first;
}
);
}
public:
static float getVal(shared_ptr<Basic> v)
{
if(v->getType()==INT)
{
shared_ptr<Int> rv=dynamic_pointer_cast<Int>(v);
return rv->val;
}
else if(v->getType()==FLOAT)
{
shared_ptr<Float> rv=dynamic_pointer_cast<Float>(v);
return rv->val;
}
else
throw string("type mismatch");
}
binarySearchIndex(shared_ptr<col> _c) : index(_c)
{
this->colToVec();
this->resort();
}
virtual void add(shared_ptr<Basic> v) override
{
int sub=sortVec.size();
this->sortVec.push_back(make_pair(binarySearchIndex::getVal(v),sub));
this->resort();
}
virtual void mod(int opSub, shared_ptr<Basic> v) override
{
int sub=-1;
for(auto i : this->sortVec)
{
if(i.second==opSub)
sub=i.second;
}
this->del(sub);
this->add(v);
}
virtual void del(int opSub) override
{
this->sortVec.erase(this->sortVec.begin()+opSub);
}
virtual vector<int> find(ruleExp *rule) override
{
if(rule->operandIsBasic())
{
int value=binarySearchIndex::getVal(rule->getOperand2B());
int sub=binFind(this->sortVec,value); //返回的是sub和opSub
int leftSub,rightSub;
const float& target=sortVec[sub].first;
for(int i=sub-1;i>=0;--i){
if(sortVec[i].first!=target){
leftSub=i+1;
break;
}
if(i==0){
leftSub=0;
}
}
for(int i=sub+1;i<sortVec.size();++i){
if(sortVec[i].first!=target){
rightSub=i-1;
break;
}
if(i==sortVec.size()-1){
rightSub=sortVec.size()-1;
}
}
//fix:找到以sub为中心,最左和最右等于value的,把它们赋值给leftSub和rightSub
vector<int> result;
if(rule->getOp()==EQU)
{
for(int i=leftSub;i<=rightSub;i++)
result.push_back(this->sortVec[i].second);
return result;
}
else if(rule->getOp()==GRAT)
{
for(int i=rightSub+1;i<this->sortVec.size();i++)
result.push_back(this->sortVec[i].second);
return result;
}
else if(rule->getOp()==SMAL)
{
for(int i=0;i<leftSub;i++)
result.push_back(this->sortVec[i].second);
return result;
}
}
throw string("this index does not support this exp");
}
};
class BPlusTreeIndex : public index //使用该索引默认为unique约束列
{
private:
BPlusTree* pTree;
public:
BPlusTreeIndex(shared_ptr<col> _c) : index(_c)
{
supportMod=true;
pTree = new BPlusTree;
bool isInt;
if(c->getType()==INT)
isInt=true;
else if(c->getType()==FLOAT)
isInt=false;
else
throw string("type mismatch");
int sub=0;
for(shared_ptr<Basic> v : c->getAllData())
{
float val;
if(isInt)
{
shared_ptr<Int> rv=dynamic_pointer_cast<Int>(v);
val=rv->val;
}
else
{
shared_ptr<Float> rv=dynamic_pointer_cast<Float>(v);
val=rv->val;
}
pTree->Insert(make_pair(val,sub));
sub++;
}
}
virtual void add(shared_ptr<Basic> v)
{
float val=binarySearchIndex::getVal(v);
int sub=c->getAllData().size();
pTree->Insert(make_pair(val,sub));
}
virtual void del(int opSub)
{
shared_ptr<Basic> v=c->getAllData()[opSub];
pTree->Delete(binarySearchIndex::getVal(v));
}
virtual void mod(int opSub, shared_ptr<Basic> v)
{
this->del(opSub);
float val=binarySearchIndex::getVal(v);
pTree->Insert(make_pair(val,opSub));
}
virtual vector<int> find(ruleExp* rule)
{
if(rule->getOp()==EQU)
{
shared_ptr<Basic> v=rule->getOperand2B();
int aresult=pTree->Search(binarySearchIndex::getVal(v));
vector<int> result;
result.push_back(aresult);
return result;
}
else
throw string("this index does not support this exp");
}
virtual ~BPlusTreeIndex() { delete pTree; }
};

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/curriculum-design-express_station_system_gui.git
git@api.gitlife.ru:oschina-mirror/curriculum-design-express_station_system_gui.git
oschina-mirror
curriculum-design-express_station_system_gui
curriculum-design-express_station_system_gui
master