#include<shrinkable.hpp>
#include"Stack.hpp"
#include"CPartition.hpp"
#include"LVertex.hpp"
#include"Net.hpp"
#include"SubNet.hpp"
#include"CVertex.hpp"

namespace ncomputers{
  namespace fpga{
    namespace detailed{
      namespace reader{
        namespace result{
          ofstream sOut("sGraph.out");

          CVertex*cVertices_start,*cVertices_end;
          LVertex*lVertices_start,*lVertices_end;
          SubNet*subNets_start,*subNets_end;
          Net*nets_start,*nets_end;

          void inline create(){
            {
              using namespace cGraph;
              cVertices_start=cVertices_end=new CVertex[cVertices];
              lVertices_start=lVertices_end=new LVertex[lVertices];
              subNets_start=subNets_end=new SubNet[cVertices];
            }{
              using namespace nGraph;
              nets_start=nets_end=new Net[cVertices];
            }
          }

          void inline shrink(){
            {
              using namespace cGraph;
              cVertices=Vertices(cVertices_end-cVertices_start);
              lVertices=Vertices(lVertices_end-lVertices_start);
              operator delete[](cVertices_start,sizeof(CVertex)*cVertices);
              operator delete[](lVertices_start,sizeof(LVertex)*lVertices);
            }{
              using namespace nGraph;
              cVertices=Vertices(nets_end-nets_start);
              operator delete[](nets_start,sizeof(Net)*cVertices);
            }{
              using namespace sGraph;
              cVertices=Vertices(subNets_end-subNets_start);
              operator delete[](subNets_start,sizeof(SubNet)*cVertices);
            }
          }

          void inline CPartition::vertex(CVertex*const vertex){
            if(++count>cGraph::tracks)
              cGraph::tracks=count;
            for(auto const*i=vertices.z;i;i=i->z)
              i->value->cEdge(vertex);
            vertices.push(vertex);
          }

          void inline CVertex::travel(){
            sOut<<"cout<<\""<<char(cPartition->type+119)<<cPartition->x<<','<<cPartition->y<<','<<"\"<<Solution["<<subNets_end-subNets_start<<"]<<endl;"<<endl;
            subNet=subNets_end;
            if(sCount>cGraph::sDegree)
              cGraph::sDegree=sCount;
            for(auto const*i=cEdges.z;i;i=i->z){
              auto const*const v=i->value;
              {
                auto*const s=v->subNet;
                if(!s->adyacency)
                  s->cEdge(subNet);
              }{
                auto*const n=v->net;
                if(!n->adyacency)
                  n->cEdge(net);
              }
            }for(auto const*i=sEdges.z;i;i=i->z){
              auto*const v=i->value;
              if(!v->subNet)
                v->travel();
            }
          }

          void inline CVertex::split(){
            auto*s=sEdges.z->z;
            sEdges.z->z=0,sCount=1;
            do{
              cPartition->vertices.push(cVertices_end);
              cVertices_end->sCount=1,cVertices_end->sEdges.z=s;
              cVertices_end->cPartition=cPartition;
              cVertices_end->subNet=nullptr;
              cVertices_end->net=nets_end;

              cVertices_end->cEdges.create();
              for(auto const*i=cEdges.z;i;i=i->z){
                auto*const v=i->value;
                v->cPush(cVertices_end),cVertices_end->cEdges.push(v);
              }cVertices_end->cCount=cCount,cGraph::cEdges+=cCount;

              cVertices_end->lEdges.create();
              for(auto const*i=lEdges.z;i;i=i->z){
                auto*const v=i->value;
                v->lPush(cVertices_end),cVertices_end->lEdges.push(v);
              }cVertices_end->lCount=lCount,cGraph::lEdges+=lCount;

              auto*i=s->value->sEdges.z;
              do if(i->value==this){
                i->value=cVertices_end++;
                break;
              }while(i=i->z,true);
              i=s->z,s->z=0,s=i;
            }while(s);
          }

          void inline CVertex::create(CPartition*const partition){
            cCount=lCount=sCount=0,cEdges.create(),lEdges.create(),sEdges.create();
            (cPartition=partition)->vertex(this);
            subNet=nullptr;
            net=nets_end;
          }
        }
      }
    }
  }
}
