

// SORTEDARRAY.CC


#include "sortedarray.h"


void SortedArray::InitializeFromFile()
     { 
       char tempchar;
       int i,j;

       ifstream infile("input");            //  Open file for reading.
       loIndex=0;
       hiIndex=-1;

       while(infile)           // Main loop here goes through file line by line,
          {                    //  until end of file.
           hiIndex++;
           for (i = 0; i < CHROMLENGTH; i++)  // Inner loop here goes down a  
             {                                //  chromosome bit by bit, reading in
               infile.get(tempchar);          //  a single character from file as
               if(tempchar == '0')            //  the next bit.
                 everyone[hiIndex].chromosome[i] = FALSE;    //  '0' is FALSE. 
                 else everyone[hiIndex].chromosome[i] = TRUE; // '1' is TRUE.
             };
               infile.get(tempchar);         // Get newline character, to skip it.
           };
        hiIndex--;                           // Decrement hiIndex just before returning, 
                                             // because it gets incremented one too many times
                                             // loop.
       };

void SortedArray::printEveryone()
     { 
       int j;
       for(j=loIndex; j <= hiIndex; j++)
          {
            everyone[j].printchrom();
            cout << "\n";
          };
     };


boolean SortedArray::Empty()
    {
      if(loIndex >  hiIndex)
         return TRUE;
         else return FALSE;;
    };

int SortedArray::totalnumber()
    {
     cout << "\n" << "Current number of chromosomes stored:  " << hiIndex+1 << ". \n";
    };

boolean SortedArray::Member(Individual indi)   //  Linear search of array.
    {
     int i=0; 
     while( i <= hiIndex)
        {
          if(everyone[i].equal(indi))
             return(TRUE);
             else i++;
        };
     return(FALSE);
};

int SortedArray::IndexOf(Individual indi)      // Linear search of array.
    {
     int i=0; count = 0;
     while( i <= hiIndex)
        {
          if(indi == everyone[i])
             {
                cout << "\n Number of comparisons:  "  << count << ".\n";
                return(i);
              }
             else i++;
        };
     cout << "\n Number of comparisons:  "  << count << ".\n";
     return(-1);
};

