#include "fronta.h"

      Front::Item::Item(Uzel *x, Item *y){
                 next=y;
                 node=x;     
      }
     
      Front::Front(){
            Zacatek=NULL;
      }
      
      bool Front::isIn(Uzel* x){
            Item *iterator=Zacatek;
            while (iterator!=NULL){
                  if (iterator->node->name==x->name) return 1;
                  iterator=iterator->next;
            }       
            return 0;
      }           
      
      void Front::push (Uzel* x){
           if (isIn(x)){

           }else{
                 Zacatek=new Item(x,Zacatek);
           }
      }
      
      void Front::flush(){
           Zacatek=NULL;
      }

      Uzel* Front::pop (int circ){
           if (Zacatek==NULL) return NULL;
           
           Item *max=Zacatek;
           Item *iterator=Zacatek;
           Item *pre_max=Zacatek;
           Item *temp_pre_max=Zacatek;
           double priority=0;
           while (iterator!=NULL){
                 int prio_g=0;
                 int prio_b=0;
                 for (int i=0;i<iterator->node->count_i;i++){
                      if (iterator->node->inputs[i].second->circ==circ) prio_g++; else prio_b++;
                 }
                 for (int i=0;i<iterator->node->count_o;i++){
                      if (iterator->node->outputs[i].second->circ==circ) prio_g++;
                 }                 
                 if ((prio_b==0)||(iterator->node->count_i<2)){
                        priority=200;                                                         
                        pre_max=temp_pre_max;
                        max=iterator;
                 }else if ((prio_g/prio_b)>priority){
                        priority=prio_g/prio_b;
                        pre_max=temp_pre_max;
                        max=iterator;
                 }
                 temp_pre_max=iterator;
                 iterator=iterator->next;
           }
           if (max!=Zacatek){
                 pre_max->next=max->next;
           }else{
                 Zacatek=max->next;
           }
           max->node->prio=(int)priority;
           return max->node;          
           
      }   

