#include "head.h"

/* This refreshes XplusG.min when a new point x is born 
   into the minimum process */
void minbirth(
	      long i,            /* Label of the point x */
	      long length,       /* Number of points in the process */
	      location points[]  /* All the info for all the points */
	      )
{
  long temp,size,j,nextcount,counter;
  long lastcount,llastcount,rightdad,lchild;

  temp=length;
/* Find out length of a row */
  for(size=0;temp>0;++size)
    temp -= pow(2,(double) size);
  size -= 1;

/* Deal with bottom row first */
  lastcount=-1;
  counter = pow(2,(double) size)-1;
  nextcount=counter+pow(2,(double) size-1);

  if(i==0){
      points[0].XplusG.min = points[1].XplusG.min = 
	points[counter].XplusG.min = 
	points[counter+1].XplusG.min = 
	points[nextcount].XplusG.min = 1;
  }

/* Bottom row minus end points. */
  else if(1<=i && i<counter){
    rightdad=counter+1+(i+(i%2))/2;
    points[i].XplusG.min = points[i-1].XplusG.min = 
      points[i+1].XplusG.min = 
      points[rightdad-1].XplusG.min = 
      points[rightdad].XplusG.min = 1;
  }

  else if(i==counter){
    points[0].XplusG.min = points[counter-1].XplusG.min = 
      points[counter].XplusG.min = 
      points[counter+1].XplusG.min = 
      points[nextcount].XplusG.min = 1;
  }

/* The middle bit. */
  else{
    for(j=1;j<size-1;++j){
      llastcount=lastcount;
      lastcount=counter;
      counter=nextcount;
      nextcount+=pow(2,(double) (size-j-1));

      if(i==lastcount+1){
	points[lastcount+1].XplusG.min = 
	  points[lastcount+2].XplusG.min = 
	  points[lastcount].XplusG.min = 
	  points[counter].XplusG.min = 
	  points[llastcount+1].XplusG.min = 
	  points[llastcount+2].XplusG.min = 
	  points[llastcount+3].XplusG.min = 
	  points[counter+1].XplusG.min = 
	  points[nextcount].XplusG.min = 1;
      }

/* The middle bit minus end points. */
      else if(i>(lastcount+1) && i<counter){

	rightdad=counter+1+(i-lastcount-1+(i%2))/2;
	lchild=llastcount+(i-lastcount)*2-1;
	points[i-1].XplusG.min = points[i+1].XplusG.min = 
	  points[rightdad-1].XplusG.min = 
	  points[rightdad].XplusG.min = 
	  points[i].XplusG.min = 
	  points[lchild].XplusG.min = 
	  points[lchild+1].XplusG.min = 
	  points[lchild+2].XplusG.min = 
	  points[lchild-1].XplusG.min = 1;
      }

      else if(i==counter){

	points[lastcount+1].XplusG.min = 
	  points[counter-1].XplusG.min = 
	  points[lastcount].XplusG.min = 
	  points[counter].XplusG.min = 
	  points[llastcount+1].XplusG.min = 
	  points[lastcount-1].XplusG.min = 
	  points[lastcount-2].XplusG.min = 
	  points[counter+1].XplusG.min = 
	  points[nextcount].XplusG.min = 1;
      }
    }

/* Finally, deal with the last three points. */
    llastcount=lastcount;
    lastcount=counter;
    counter=nextcount;
    nextcount+=pow(2,(double) (size-j-1));

    if(i==lastcount+1||i==counter){
      points[lastcount+1].XplusG.min = 
	points[lastcount].XplusG.min = 
	points[counter].XplusG.min = 
	points[llastcount+1].XplusG.min = 
	points[llastcount+2].XplusG.min = 
	points[llastcount+3].XplusG.min = 
	points[nextcount].XplusG.min = 1;
    }

    else if(i==nextcount){
      points[nextcount].XplusG.min = 
	points[counter].XplusG.min = 
	points[lastcount+1].XplusG.min = 1;
    }
  }
}
