#include<Reader/Reader.hpp>
#include"variables.hpp"
#include"result/namespace.hpp"

#define X reader.values[0]
#define Y reader.values[1]
#define Z reader.values[2]
#define letter reader.letter
#define Cut()last::type=Null
#define Le(I)Ledge(lAdjacenc0[pin*4+I])
#define LE(I)LEdge(lAdjacenc0[Pin*4+I])
#define xy(I)SEdge(sAdjacenc0[pack*6+I])
#define xY(I)SEdge(sAdjacenc1[pack*6+I])
#define Xy(I)SEdge(sAdjacenc1[Pack*6+I])
#define XY(I)SEdge(sAdjacenc0[Pack*6+I])
#define LastL()pin=Pin,x=X,y=Y,type=LPin,lVertex=Vertex
#define LastC(T)pack=Pack,x=X,y=Y,type=T,cVertex=Vertex
#define Ledge(L){auto&l=L;if(!l)l=1,Vertex->lEdge(lVertex);}
#define LEdge(L){auto&l=L;if(!l)l=1,cVertex->lEdge(Vertex);}
#define SEdge(S){auto&s=S;if(!s)s=1,cVertex->sEdge(Vertex);}

namespace ncomputers{
  namespace fpga{
    namespace detailed{
      namespace reader{
        using namespace boolean;
        using namespace result;

        bool*lAdjacency,*lAdjacenc0;
        bool*sAdjacency,*sAdjacenc0,*sAdjacenc1;
        CVertex**cBlocks,**xcBlock,**ycBlock,**cBlocks_end;
        LVertex**lBlocks,**lBlock0,**lBlocks_end;
        CPartition**cPartitions,**xcPartition,**ycPartition;
        CPartition*cPartitions_start,*cPartitions_end;
        Reader<3,Sizes,Interval<'v','z'>,Equal<','>>reader;
        Sizes xMin=max(xMin),xMax=min(xMax);
        Sizes yMin=max(yMin),yMax=min(yMax);
        Sizes zMin=max(zMin),zMax=min(zMax);
        Sizes xSize,ySize,zSize;

        namespace last{
          static union{
            CVertex*cVertex;
            LVertex*lVertex;
          };
          static union{
            Packs pack;
            Pins pin;
          };
          Sizes x,y;
          Types type;
        }

        void inline xyLimits(){
          if(X<xMin)xMin=X;
          if(X>xMax)xMax=X;
          if(Y<yMin)yMin=Y;
          if(Y>yMax)yMax=Y;
        }

        void inline zLimits(){
          if(Z<zMin)zMin=Z;
          if(Z>zMax)zMax=Z;
        }

        void inline normalize(){
          using namespace cGraph;
          switch(letter){
          case'x':
          case'y':
            xyLimits();
            ++cVertices;
            break;
          case'w':
            xyLimits(),zLimits();
            cVertices+=6,++lVertices;
            break;
          case'z':
            ++nGraph::cVertices;
            xyLimits(),zLimits();
            cVertices+=6,++lVertices;
            break;
          }
        }

        void inline horizontal(){
          Packs const Pack=X*ySize+Y;
          auto&Vertex=xcBlock[Pack];

          if(!Vertex){
            auto&part=xcPartition[Pack];
            if(!part)
              (part=cPartitions_end++)->create(X,Y,XCBlock);
            (Vertex=cVertices_end++)->create(part);
          }

          using namespace last;
          if(type==XCBlock){
            if(y==Y){
              if(x==X+1)XY(0)
              else if(x==X-1)xy(0)
            }
          }else if(type==YCBlock){
            if(y==Y){
              if(x==X)xy(1)
              else if(x==X-1)xy(2)
            }else if(y==Y+1){
              if(x==X)XY(3)
              else if(x==X-1)xY(4)
            }
          }else if(type==LPin){
            if(x==X){
              if(y==Y)Le(0)
              else if(y==Y+1)Le(1)
            }
          }LastC(XCBlock);
        }

        void inline vertical(){
          Packs const Pack=X*ySize+Y;
          auto&Vertex=ycBlock[Pack];

          if(!Vertex){
            auto&part=ycPartition[Pack];
            if(!part)
              (part=cPartitions_end++)->create(X,Y,YCBlock);
            (Vertex=cVertices_end++)->create(part);
          }

          using namespace last;
          if(type==YCBlock){
            if(x==X){
              if(y==Y+1)XY(5)
              else if(y==Y-1)xy(5)
            }
          }else if(type==XCBlock){
            if(y==Y){
              if(x==X)XY(1)
              else if(x==X+1)XY(2)
            }else if(y==Y-1){
              if(x==X)xy(3)
              else if(x==X+1)Xy(4)
            }
          }else if(type==LPin){
            if(y==Y){
              if(x==X)Le(2)
              else if(x==X+1)Le(3)
            }
          }LastC(YCBlock);
        }

        void inline logical(){
          Pins const Pin=(X*ySize+Y)*zSize+Z;
          auto&Vertex=lBlock0[Pin];

          if(!Vertex)
            (Vertex=lVertices_end++)->create(X,Y,Z);

          using namespace last;
          if(type==XCBlock){
            if(x==X){
              if(y==Y)LE(0)
              else if(y==Y-1)LE(1)
            }
          }else if(type==YCBlock){
            if(y==Y){
              if(x==X)LE(2)
              else if(x==X-1)LE(3)
            }
          }LastL();
        }

        void inline net(){
          for(static LVertex const*l=lVertices_start;l<lVertices_end;++l)
            for(auto const*i=l->lEdges.z;i;i=i->z){
              auto const*const v=i->value->cPartition;
              sOut<<"cout<<\"w"<<l->x<<','<<l->y<<','<<l->z<<"\"<<endl;"<<endl;
              sOut<<"cout<<\""<<char(v->type+119)<<v->x<<','<<v->y<<'v'<<"\"<<endl;"<<endl;
            }
          for(static LVertex const*l=lVertices_start;l<lVertices_end;++l)
            for(auto const*i=l->lEdges.z;i;i=i->z){
              auto*const v=i->value;
              if(v->sCount>1)
                v->split();
            }
          static CVertex*c=cVertices_start;
          if(c<cVertices_end){
            for(auto*i=nets_start;i<nets_end;++i)
              i->adyacency=false;
            nets_end->create();
            do if(!c->subNet){
              for(auto*i=subNets_start;i<subNets_end;++i)
                i->adyacency=false;
              subNets_end->create();
              c->travel();
              ++subNets_end;
            }while(++c<cVertices_end);
            ++nets_end;
            sOut<<"cout<<\"vz\"<<endl;"<<endl;
          }
        }

        void inline reset(){
          auto*l=lAdjacency;
          auto*s=sAdjacency;
          auto*c=cBlocks;
          auto*p=lBlocks;
          do{
            l[0]=l[1]=l[2]=l[3]=0,l+=4;
            s[0]=s[1]=s[2]=s[3]=s[4]=s[5]=0,s+=6;
            p[0]=0,p+=1;
            c[0]=c[1]=0,c+=2;
          }while(c<cBlocks_end);
          while(p<lBlocks_end){
            l[0]=l[1]=l[2]=l[3]=0,l+=4;
            p[0]=0,p+=1;
          }Cut();
        }

        void inline interpret(){
          switch(letter){
          case'x':
            horizontal();
            break;
          case'y':
            vertical();
            break;
          case'w':
            logical();
            break;
          case'z':
            logical(),net(),reset();
            break;
          case'v':
            Cut();
            break;
          }
        }

        void inline create(){
          xSize=xMax-xMin+1;
          ySize=yMax-yMin+1;
          zSize=zMax-zMin+1;

          Packs const offset2=xMin*ySize+yMin;
          Packs const packs=xSize*ySize;
          Pins const offset3=offset2*zSize+zMin;
          Pins const pins=packs*zSize;
          Blocks const blocks=packs*2;

          lAdjacency=new bool[pins*4]();
          lAdjacenc0=lAdjacency-offset3*4;

          sAdjacency=new bool[packs*6]();
          sAdjacenc0=sAdjacency-offset2*6;
          sAdjacenc1=sAdjacenc0-6;

          cBlocks=new CVertex*[blocks]();
          cBlocks_end=cBlocks+blocks;
          xcBlock=cBlocks-offset2;
          ycBlock=xcBlock+packs;

          lBlocks=new LVertex*[pins]();
          lBlocks_end=lBlocks+pins;
          lBlock0=lBlocks-offset3;

          cPartitions=new CPartition*[blocks]();
          xcPartition=cPartitions-offset2;
          ycPartition=xcPartition+packs;

          cPartitions_start=cPartitions_end=new CPartition[blocks];
        }

        void inline read(FILE*const f){
          reader.read<normalize>(f);
          reader::create(),result::create();
          reader.read<interpret>((rewind(f),f));
          net(),shrink();
          delete[]cPartitions_start,delete[]cPartitions,
          delete[]lBlocks,delete[]cBlocks,
          delete[]sAdjacency,delete[]lAdjacency;
        }
      }
    }
  }
}
