#ifndef _REGION_H
#define _REGION_H


#include <queue>
#include <math.h>




class Region{

SG* sg;

queue<int> erp;         // ER- pomocna mnozina - fronta
queue<int> erm;         // ER+ pomocna mnozina - fronta
queue<int> qrm;         // QR+ pomocna mnozina - fronta
queue<int> qrp;         // QR- pomocna mnozina - fronta
           
queue<string> erps;     // ER- pomocna mnozina - fronta
queue<string> erms;     // ER+ pomocna mnozina - fronta
queue<string> qrms;     // QR+ pomocna mnozina - fronta
queue<string> qrps;     // QR- pomocna mnozina - fronta

RegStavBuf* rs;         // ukazatel na nosnou strukturu

public:      

Region(SG* s, RegStavBuf* r){
sg = s;                   // init ukazatele pro SG
rs = r;                   // init ukazatele pro strukturu pro regiony
}


//-----------------------------------------------------------------------------      
// odstrani duplicity z regionu
void duplicit(){

int d1 =erp.size();
int d2 =erm.size();
int d3 =qrm.size();
int d4 =qrm.size();

int tmp = 0;

for(int j = 0 ; j < d1 + d2 ; ++j ){
if(j < d1) tmp = erp.front();
else tmp = erm.front();

for(int i = 0;i < d3 ;++i)
if(qrm.front() == tmp) {qrm.pop(); qrms.pop();}
else{
qrm.push(qrm.front()); qrm.pop();
qrms.push(qrms.front()); qrms.pop();
}
d3 = qrm.size();

for(int i = 0;i < d4 ;++i)
if(qrp.front() == tmp) {qrp.pop(); qrps.pop();}
else{
qrp.push(qrp.front()); qrp.pop();
qrps.push(qrps.front()); qrps.pop();
}

d4 = qrm.size();

if(j < d1) { erp.push(erp.front()); erp.pop();}
else {erm.push(erm.front()); erm.pop();}

}
}


//-----------------------------------------------------------------------------      
// prevadi binarni zakodovani do desitkove soustavy
int binToint(char* s){
int bin = 0;
int i = int(strlen(s)) - 1;
int j = 0;
for( ; j < int(strlen(s)) ; ++j,--i)
if(s[i] == '1') {
bin += int(pow(double(2),double(j)));
}

return  bin;
}


//-----------------------------------------------------------------------------      
// prevede pomocne froty nesouci regiony do nosne strktury pro regiony
void makeReg(int tmp){


rs->rs[tmp].pqrm =qrm.size();
rs->rs[tmp].pqrp =qrp.size();
rs->rs[tmp].perp =erp.size();
rs->rs[tmp].perm =erm.size();

rs->rs[tmp].qrm = new int[qrm.size()];
rs->rs[tmp].qrp = new int[qrp.size()];
rs->rs[tmp].erp = new int[erp.size()];
rs->rs[tmp].erm = new int[erm.size()];

rs->rs[tmp].qrms = new string[qrm.size()];
rs->rs[tmp].qrps = new string[qrp.size()];
rs->rs[tmp].erps = new string[erp.size()];
rs->rs[tmp].erms = new string[erm.size()];


for(int i = 0;i < rs->rs[tmp].pqrm;++i)
{rs->rs[tmp].qrm[i] = qrm.front();qrm.pop();
 rs->rs[tmp].qrms[i] = qrms.front();qrms.pop();
}
for(int i = 0;i < rs->rs[tmp].pqrp;++i)
{rs->rs[tmp].qrp[i]= qrp.front();qrp.pop();
 rs->rs[tmp].qrps[i]= qrps.front();qrps.pop();
}
for(int i = 0;i < rs->rs[tmp].perp;++i)
{rs->rs[tmp].erp[i]= erp.front();erp.pop();
 rs->rs[tmp].erps[i]= erps.front();erps.pop();
}
for(int i = 0;i < rs->rs[tmp].perm;++i)
{rs->rs[tmp].erm[i] = erm.front();erm.pop();
 rs->rs[tmp].erms[i] = erms.front();erms.pop();
 
}


}


//-----------------------------------------------------------------------------      
// odstrani duplicity z regionu
int clearDupli(queue<int> &qu,int c){
int test = 1;

for(int i = 0;i < int(qu.size()); ++i){
if(qu.front() == c) test = 0;
qu.push(qu.front());
qu.pop();
}

if(test){ qu.push(c); return 1;}
return 0;
}


//-----------------------------------------------------------------------------      
// prochazi pro jednotlive signal SG z ucelem vytvorit regiony
void makeRegion(){
int z = rs->pocet;
for(int j = 0; j < z; ++j){
for(int i = 0; i < sg->pocet; ++i){
for(int x = 0; x < sg->sg[i]->num;++x){
char c1 = sg->sg[i]->code[j];
int h = sg->sg[i]->p[x];
char c2 = sg->sg[h]->code[j];
int c3 = binToint(sg->sg[i]->code);

if(c1 == '0'&&  c2 == '1') {if(clearDupli(erp,c3)) erps.push(sg->sg[i]->code);}
if(c1 == '1'&&  c2 == '0') {if(clearDupli(erm,c3)) erms.push(sg->sg[i]->code);}
if(c1 == '1'&&  c2 == '1') {if(clearDupli(qrp,c3)) qrps.push(sg->sg[i]->code);}
if(c1 == '0'&&  c2 == '0') {if(clearDupli(qrm,c3)) qrms.push(sg->sg[i]->code);}
}

}

duplicit();
makeReg(j);

}



     
}

      
};

#endif
