home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / BEAGLE / FRAGMENT.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  12KB  |  434 lines

  1. /* fragment.c  27-3-92  makes fragment of tierra.run */
  2. /*** Beagle Explorer: Version 3.1  Copyright (c) 1990, 1991, 1992  Tom Ray ***/
  3.  
  4. #include "stdio.h"
  5. #include "stdlib.h"
  6. #include "string.h"
  7. #include "dir.h"
  8. #include "alloc.h"
  9. #include "ctype.h"
  10.  
  11. typedef unsigned int   Uint;
  12. typedef unsigned long  Ulong;
  13.  
  14. struct last_out {
  15.     Ulong  time;
  16.     Ulong  ctime;
  17.     char   bd;
  18.     Uint   size;
  19.     char   label[4];
  20.     } ;
  21.  
  22. struct pop_dat {
  23.     long  max; /* maximum population of this genotype */
  24.     long  min; /* minimum population of this genotype */
  25.     long  beg; /* beginning population of this genotype */
  26.     long  end; /* current population of this genotype */
  27.     } ;
  28.  
  29. struct gene_dat {
  30.     struct tnode  *t;
  31.     long          index;
  32.     } ;
  33.  
  34. struct snode {
  35.     long             max;  /* max pop value upon which tree is sorted */
  36.     long             num;  /* number of genotypes of this max value */
  37.     long             gsiz; /* allocated size of *gd array */
  38.     struct gene_dat  *g;
  39.     struct snode     *l;  /* left sub-tree */
  40.     struct snode     *r;  /* right sub-tree */
  41.     } ;
  42.  
  43. struct tnode {
  44.     long            size;  /* genome size */
  45.     struct pop_dat  sd; /* pop_dat for this size class */
  46.     int             gsize; /* allocated size of *g array */
  47.     struct pop_dat  *g; /* array of pop_dat structures */
  48.     struct tnode    *l;  /* left sub-tree */
  49.     struct tnode    *r;  /* right sub-tree */
  50.     } ;
  51.  
  52. int   genotypes;
  53. char  directory[80];
  54.  
  55. int  main(void);
  56. int  temp_info_chk_f(char  *infile, char  *directory);
  57. int  t_read(char  data[], struct last_out  *lo, int  *first, int  *genotypes);
  58. void OutFragTree(struct tnode  *t, FILE  *ouf);
  59. void CountTTree(struct tnode  *t, int  *num);
  60. void FreeTTree(struct tnode  *p);
  61. struct tnode * AddTree(struct tnode  *p, struct last_out  *lo);
  62. void fragmen(char  *infile, char  *oufile, char  *directory,
  63.     int  start, int  stop);
  64.  
  65. int Lbl2Int(char  s[])
  66. {   if(s[0] == '-') return 0;
  67.     return 1 + (s[2]- 'a') + (26 * (s[1] - 'a')) + (676 * (s[0] - 'a'));
  68. }
  69.  
  70. void Int2Lbl(int  i, char  s[])
  71. {   if(!i)
  72.     {   strcpy(s,"---");
  73.         return;
  74.     }
  75.     i--;
  76.     s[0] = 'a' + i / 676;
  77.     i %= 676;
  78.     s[1] = 'a' + i / 26;
  79.     i %= 26;
  80.     s[2] = 'a' + i;
  81.     s[3] = 0;
  82. }
  83.  
  84. int main(void)
  85. {   char  buf[80];
  86.     int   start = 0, stop = 1;
  87.     char  infile[13], oufile[13];
  88.  
  89.     sprintf(infile,"break.1"); sprintf(oufile,"fragment.run");
  90.     sprintf(directory,"c:\\tierra\\td");
  91.  
  92.     printf("directory (default: %s) = ", directory);
  93.     gets(buf);
  94.     if (strlen(buf))
  95.         sprintf(directory, "%s", buf);
  96.  
  97.     printf("input file (default: %s) = ", infile);
  98.     gets(buf);
  99.     if (strlen(buf))
  100.         sprintf(infile, "%s", buf);
  101.  
  102.     printf("output file (default: %s) = ", oufile);
  103.     gets(buf);
  104.     if (strlen(buf))
  105.         sprintf(oufile, "%s", buf);
  106.  
  107.     printf("start time (default: %d) = ", start);
  108.     gets(buf);
  109.     if (strlen(buf))
  110.         sscanf(buf, "%d", &start);
  111.  
  112.     printf("stop time (default: %d) = ", stop);
  113.     gets(buf);
  114.     if (strlen(buf))
  115.         sscanf(buf, "%d", &stop);
  116.  
  117.     if (temp_info_chk_f(infile, directory))
  118.         fragmen(infile,oufile,directory,start,stop);
  119.     return 1;
  120. }
  121.  
  122. int temp_info_chk_f(char  *infile, char  *directory)
  123. {   FILE  *inf;
  124.     char  path[80], cpath[80];
  125.     int  success;
  126.  
  127.     getcwd(cpath,79);
  128.     success = chdir(directory);
  129.     chdir(cpath);
  130.     if(success)
  131.     {   printf("directory %s not found\n", directory);
  132.         return 0;
  133.     }
  134.  
  135.     sprintf(path,"%s/%s", directory, infile);
  136.     inf = fopen(path,"r");
  137.     if(inf == NULL)
  138.     {   printf("file %s not found\n", path);
  139.         return 0;
  140.     }
  141.     fclose(inf);
  142.  
  143.     return 1;
  144. }
  145.  
  146. void fragmen(char  *infile, char  *oufile, char  *directory,
  147.     int  start, int  stop)
  148. {   int  first = 1, not_yet = 1, species = 0;
  149.     struct tnode  *troot = NULL;
  150.     struct last_out  lo;
  151.     long  time;
  152.     Ulong c = 0;
  153.     char  data[81], *obufr;
  154.     FILE  *inf, *ouf;
  155.     char   bifile[13];
  156.     int    binum = 1;
  157.  
  158.     sscanf(infile,"%[^.]", bifile);
  159.     sprintf(data,"%s/%s", directory, infile);
  160.     inf = fopen(data,"r");
  161.     if(inf == NULL)
  162.     {   printf("input file %s not found\n", data);
  163.         return ;
  164.     }
  165.     sprintf(data,"%s/%s", directory, oufile);
  166.     ouf = fopen(data,"w");
  167.     lo.ctime = lo.time = 0;
  168.     genotypes = 1;
  169.     obufr = (char  *) calloc(8192, sizeof(char));
  170.     if(obufr == NULL)
  171.     {   printf("calloc failure, exiting\n");
  172.         exit(0);
  173.     }
  174.     setvbuf(ouf,obufr,_IOFBF,8192);
  175.     for(;;)
  176.     {   if(fgets(data,80,inf) == NULL)
  177.         {   fclose(inf);
  178.             binum++;
  179.             sprintf(data,"%s/%s.%d", directory, bifile, binum);
  180.             inf = fopen(data,"r");
  181.             if(inf == NULL)
  182.                 break ;
  183.             if(fgets(data,80,inf) == NULL)
  184.                 break ;
  185.         }
  186.         t_read(data, &lo, &first, &genotypes);
  187.         if(!(c%1000LU))
  188.             printf("Time = %5lu,%06lu  Coreleft = %6lu\r",
  189.                 lo.ctime, lo.time, coreleft());
  190.         c++;
  191.         if(lo.ctime < start) troot = AddTree(troot,&lo);
  192.         if(lo.ctime >= start && lo.ctime <= stop)
  193.         {   if(not_yet && start > 0)
  194.             {   not_yet = 0;
  195.                 CountTTree(troot,&species);
  196.                 fprintf(ouf,"num_sp = %d  ctime = %lu  time = %lu\n",
  197.                     species, lo.ctime, lo.time );
  198.                 OutFragTree(troot,ouf);
  199.                 sscanf(data,"%lx", &time);
  200.                 fprintf(ouf,"%lx %c %d", time, lo.bd, lo.size);
  201.                 if(genotypes) fprintf(ouf," %s", lo.label);
  202.                 fprintf(ouf,"\n");
  203.                 FreeTTree(troot);
  204.             }
  205.             else
  206.             fprintf(ouf,"%s",data);
  207.         }
  208.         if(lo.ctime > stop) break;
  209.     }
  210.     fclose(inf);
  211.     fclose(ouf);
  212.     free(obufr);
  213. }
  214.  
  215. void CountTTree(struct tnode  *t, int  *num)
  216. {   int  i;
  217.  
  218.     if(t != NULL)
  219.     {   if(genotypes)
  220.         {   for(i = 0; i < t->gsize; i++)
  221.                 if((t->g + i)->end > 0)
  222.                     (*num)++;
  223.         }
  224.         else
  225.             if(t->sd.end > 0)
  226.                 (*num)++;
  227.         CountTTree(t->l,num);
  228.         CountTTree(t->r,num);
  229.     }
  230. }
  231.  
  232. void OutFragTree(struct tnode  *t, FILE  *ouf)
  233. {   int  i;
  234.     char  lbl[4];
  235.  
  236.     if(t != NULL)
  237.     {   if(genotypes) for(i = 0; i < t->gsize; i++)
  238.         {   if((t->g + i)->end <= 0)
  239.                 continue ;
  240.             Int2Lbl(i,lbl);
  241.             fprintf(ouf,"%04ld%s %ld\n", t->size, lbl, (t->g + i)->end);
  242.         }
  243.         else if(t->sd.end > 0)
  244.         {   strcpy(lbl,"---");
  245.             fprintf(ouf,"%04ld%s %ld\n", t->size, lbl, t->sd.end);
  246.         }
  247.         OutFragTree(t->r, ouf);
  248.         OutFragTree(t->l, ouf);
  249.     }
  250. }
  251.  
  252. int t_read(char  data[], struct last_out  *lo, int  *first, int  *genotypes)
  253. {   struct last_out  ti;
  254.     int    nargs;
  255.     char   v2[9], v3[9], v4[9];
  256.  
  257.     sscanf(data,"%s", v2);
  258.     if(!strcmp(v2,"num_sp")) return 0;
  259.     nargs = sscanf(data,"%lx%s%s%s", &ti.time, v2, v3, v4);
  260.     if(*first)
  261.     {   *first = 0;
  262.         if(nargs == 4) *genotypes = 1;
  263.         else *genotypes = 0;
  264.         lo->time += ti.time;     /* assumes lo structure initialized to zero */
  265.         if(lo->time >= 1000000L)
  266.         {   lo->time %= 1000000L;
  267.             lo->ctime++;
  268.         }
  269.         lo->bd = v2[0];
  270.         sscanf(v3,"%u", &lo->size);
  271.         if(*genotypes) strcpy(lo->label,v4);
  272.         else strcpy(lo->label,"");
  273.     }
  274.     else
  275.     {   lo->time += ti.time;
  276.         if(lo->time >= 1000000L)
  277.         {   lo->time %= 1000000L;
  278.             lo->ctime++;
  279.         }
  280.         if(*genotypes) switch(nargs)
  281.         {   case 1: break;
  282.             case 2:
  283.             {   if(isdigit(v2[0]))
  284.                 {   sscanf(v2,"%u", &lo->size); break; }
  285.                 else
  286.                 {   if(strlen(v2) == 1)
  287.                     {   lo->bd = v2[0]; break; }
  288.                     else
  289.                     {   strcpy(lo->label,v2); break; }
  290.                 }
  291.             }
  292.             case 3:
  293.             {   if(isdigit(v2[0]))
  294.                 {   sscanf(v2,"%u", &lo->size);
  295.                     strcpy(lo->label,v3);
  296.                 }
  297.                 else
  298.                 {   lo->bd = v2[0];
  299.                     if(isdigit(v3[0]))
  300.                         sscanf(v3,"%u", &lo->size);
  301.                     else
  302.                         strcpy(lo->label,v3);
  303.                 }
  304.                 break;
  305.             }
  306.             case 4:
  307.             {   lo->bd = v2[0];
  308.                 sscanf(v3,"%u", &lo->size);
  309.                 strcpy(lo->label,v4);
  310.                 break;
  311.             }
  312.         }
  313.         else switch(nargs)
  314.         {   case 1: break;
  315.             case 2:
  316.             {   if(isdigit(v2[0]))
  317.                     sscanf(v2,"%u", &lo->size);
  318.                 else
  319.                     lo->bd = v2[0];
  320.                 break;
  321.             }
  322.             case 3:
  323.             {   lo->bd = v2[0];
  324.                 sscanf(v3,"%u", &lo->size);
  325.                 break;
  326.             }
  327.         }
  328.     }
  329.     return 1;
  330. }
  331.  
  332. void FreeTTree(struct tnode  *p)
  333. {   if(p != NULL)
  334.     {   FreeTTree(p->l);
  335.         FreeTTree(p->r);
  336.         free(p->g);
  337.         free(p);
  338.     }
  339. }
  340.  
  341. struct tnode * AddTree(struct tnode  *p, struct last_out  *lo)
  342. {   int  i, j, osize;
  343.     struct pop_dat  *pd;
  344.  
  345.     if(p == NULL)
  346.     {   if(lo->bd == 'd')
  347.         {   printf("new node is a death, exiting\n");
  348.             exit(0);
  349.         }
  350.         p = (struct tnode  *) calloc(1,sizeof(struct tnode));
  351.         if(p == NULL)
  352.         {   printf("calloc failure, exiting\n");
  353.             exit(0);
  354.         }
  355. /*
  356.         else
  357.             printf("coreleft = %6lu\n", coreleft());
  358. */
  359.         p->size = lo->size;
  360.         p->sd.max = p->sd.end = 1;
  361.         p->sd.min = p->sd.beg = 0;
  362.         if(genotypes)
  363.         {   i = Lbl2Int(lo->label);
  364.             p->gsize = i + 1;
  365.             p->g = (struct pop_dat  *)
  366.                 calloc(p->gsize,sizeof(struct pop_dat));
  367.             if(p->g == NULL)
  368.             {   printf("calloc failure, exiting\n");
  369.                 exit(0);
  370.             }
  371. /*
  372.             else
  373.                 printf("coreleft = %6lu\n", coreleft());
  374. */
  375.             (p->g + i)->end = (p->g + i)->max = 1;
  376.         }
  377.         p->l = p->r = NULL;
  378.     }
  379.     else if(lo->size < p->size)
  380.         p->l = AddTree(p->l,lo);
  381.     else if(lo->size > p->size)
  382.         p->r = AddTree(p->r,lo);
  383.     else
  384.     {   if(genotypes)
  385.         {   i = Lbl2Int(lo->label);
  386.             if(i >= p->gsize)
  387.             {   osize = p->gsize;
  388.                 p->gsize = i + 5;
  389.                 if(p->g == NULL)
  390.                     p->g = (struct pop_dat  *)
  391.                         calloc(p->gsize, sizeof(struct pop_dat));
  392.                 else
  393.                     p->g = (struct pop_dat  *)
  394.                         realloc(p->g, p->gsize * sizeof(struct pop_dat));
  395.                 if(p->g == NULL)
  396.                 {   printf("realloc failure, exiting\n");
  397.                     exit(0);
  398.                 }
  399. /*
  400.                 else
  401.                     printf("coreleft = %6lu\n", coreleft());
  402. */
  403.                 for(j = osize; j < p->gsize; j++)
  404.                 {   pd = p->g + j;
  405.                     pd->max = pd->min = pd->beg = pd->end = 0;
  406.                 }
  407.             }
  408.         }
  409.         if(lo->bd == 'b')
  410.         {   p->sd.end++;
  411.             if(p->sd.end > p->sd.max)
  412.                 p->sd.max = p->sd.end;
  413.             if(genotypes)
  414.             {   pd = p->g + i;
  415.                 pd->end++;
  416.                 if(pd->end > pd->max)
  417.                     pd->max = pd->end;
  418.             }
  419.         }
  420.         else
  421.         {   p->sd.end--;
  422.             if(p->sd.end < p->sd.min)
  423.                 p->sd.min = p->sd.end;
  424.             if(genotypes)
  425.             {   pd = p->g + i;
  426.                 pd->end--;
  427.                 if(pd->end < pd->min)
  428.                     pd->min = pd->end;
  429.             }
  430.         }
  431.     }
  432.     return p;
  433. }
  434.