[Avida-SVN] r3445 - in development/source: classification main tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Oct 5 11:39:27 PDT 2009


Author: brysonda
Date: 2009-10-05 14:39:26 -0400 (Mon, 05 Oct 2009)
New Revision: 3445

Modified:
   development/source/classification/cGenotypeControl.cc
   development/source/classification/cGenotypeControl.h
   development/source/main/cPopulation.cc
   development/source/tools/cFile.cc
   development/source/tools/cInitFile.cc
   development/source/tools/cString.cc
   development/source/tools/cString.h
Log:
Add support for LONG string loading and manipulation.
- Eliminated the use of fixed size buffers for file line load operations (cFile and cInitFile)
- Extended cCharProxy index to full int
- Altered cStringData initialization to index with an int

Altered cPopulation::SaveStructurePopulation to support large population dumps where string lengths may exceed MAX_STRING_LENGTH (currently 4k)
- Changed string concatenation to use append operation as a work-around to the fixed length buffer limitations of cString::Set

Reformat and cleanup some of the code in cGenotypeControl.

Modified: development/source/classification/cGenotypeControl.cc
===================================================================
--- development/source/classification/cGenotypeControl.cc	2009-10-04 18:52:02 UTC (rev 3444)
+++ development/source/classification/cGenotypeControl.cc	2009-10-05 18:39:26 UTC (rev 3445)
@@ -30,21 +30,12 @@
 #include "cGenotype.h"
 #include "cWorld.h"
 
-cGenotypeControl::cGenotypeControl(cWorld* world) : m_world(world)
+cGenotypeControl::cGenotypeControl(cWorld* world)
+  : m_world(world), size(0), best(NULL), coalescent(NULL), historic_list(NULL), historic_count(0)
 {
-  size = 0;
-  best = NULL;
-  coalescent = NULL;
   for (int i = 0; i < nGenotype::THREADS; i++) threads[i] = NULL;
-
-  historic_list = NULL;
-  historic_count = 0;
 }
 
-cGenotypeControl::~cGenotypeControl()
-{
-}
-
 bool cGenotypeControl::OK()
 {
   int ret_value = true;
@@ -52,7 +43,7 @@
   // Cycle through the list, making sure all connections are proper, size
   // is correct, and all genotypes are OK().
 
-  cGenotype * cur_pos = best;
+  cGenotype* cur_pos = best;
   for (int i = 0; i < size; i++) {
     if (!cur_pos->OK()) ret_value = false;
     assert (cur_pos->GetNext()->GetPrev() == cur_pos);
@@ -64,33 +55,8 @@
   return ret_value;
 }
 
-void cGenotypeControl::Insert(cGenotype & in_genotype, cGenotype * prev_genotype)
+void cGenotypeControl::Remove(cGenotype& in_genotype)
 {
-  if (prev_genotype == NULL) {
-    assert(size == 0); // Destroying a full genotype queue...
-
-    best = &in_genotype;
-    best->SetNext(best);
-    best->SetPrev(best);
-  }
-  else {
-    in_genotype.SetPrev(prev_genotype);
-    in_genotype.SetNext(prev_genotype->GetNext());
-    prev_genotype->SetNext(&in_genotype);
-    in_genotype.GetNext()->SetPrev(&in_genotype);
-  }
-
-  /*if (!CheckPos(in_genotype))
-    cerr << "Genotype insertion fail! @MRR -- \n"
-      << "Best Genotype:    " << best << " " << best->GetNumOrganisms() << " prev: " << best->GetPrev() << "  next: " << best->GetNext() << endl
-      << "In Genotype:      " << &in_genotype << " " << in_genotype.GetNumOrganisms() << " prev: " << in_genotype.GetPrev() << "  next: " << in_genotype.GetNext() << endl
-      << "Prev Genotype:    " << prev_genotype << " " << prev_genotype->GetNumOrganisms() << " prev: " << prev_genotype->GetPrev() << "  next: " << prev_genotype->GetNext() << endl;
-    */
-  size++;
-}
-
-void cGenotypeControl::Remove(cGenotype & in_genotype)
-{
   if (size == 1) best = NULL;
   if (&in_genotype == best) best = best->GetNext();
   if (&in_genotype == coalescent) coalescent = NULL;
@@ -103,7 +69,7 @@
   size--;
 }
 
-void cGenotypeControl::RemoveHistoric(cGenotype & in_genotype)
+void cGenotypeControl::RemoveHistoric(cGenotype& in_genotype)
 {
   if (historic_count == 1) {
     historic_list = NULL;
@@ -120,7 +86,7 @@
   historic_count--;
 }
 
-void cGenotypeControl::InsertHistoric(cGenotype & in_genotype)
+void cGenotypeControl::InsertHistoric(cGenotype& in_genotype)
 {
   if (historic_count == 0) {
     in_genotype.SetNext(&in_genotype);
@@ -172,43 +138,16 @@
 }
 
 
-bool cGenotypeControl::CheckPos(cGenotype & in_genotype)
+void cGenotypeControl::Insert(cGenotype& new_genotype)
 {
-  int next_OK = false;
-  int prev_OK = false;
-
-  if (in_genotype.GetNumOrganisms() >= in_genotype.GetNext()->GetNumOrganisms()) {
-    next_OK =true;
-  }
-  if (in_genotype.GetNumOrganisms() <= in_genotype.GetPrev()->GetNumOrganisms()) {
-    prev_OK =true;
-  }
-
-  if ( (next_OK && prev_OK) ||
-       (&in_genotype == best && next_OK) ||
-       (&in_genotype == best->GetPrev() && prev_OK)) {
-    return true;
-  }
-
-  return false;
-}
-
-void cGenotypeControl::Insert(cGenotype & new_genotype)
-{
   // If there is nothing in the list, add this.
+  if (size == 0) insert(new_genotype, NULL);
 
-  if (size == 0) {
-    Insert(new_genotype, NULL);
-  }
-
   // Otherwise tack it on the end.
-
-  else {
-    Insert(new_genotype, best->GetPrev());
-  }
+  else insert(new_genotype, best->GetPrev());
 }
 
-bool cGenotypeControl::Adjust(cGenotype & in_genotype)
+bool cGenotypeControl::Adjust(cGenotype& in_genotype)
 {
   cGenotype* cur_genotype = in_genotype.GetPrev();
 
@@ -220,14 +159,14 @@
 
   // Do not adjust the position of this genotype if it was and still is the
   // best genotype, or if it is otherwise in the proper spot...
-  if (CheckPos(in_genotype)) return true;
+  if (checkPos(in_genotype)) return true;
 
   // Otherwise, remove it from the queue (for just the moment).
   Remove(in_genotype);
 
   // If this genotype is the best, put it there.
   if (in_genotype.GetNumOrganisms() > best->GetNumOrganisms()) {
-    Insert(in_genotype, best->GetPrev());
+    insert(in_genotype, best->GetPrev());
     best = &in_genotype;
     return true;
   }
@@ -243,16 +182,16 @@
     cur_genotype = cur_genotype->GetPrev();
   }
 
-  Insert(in_genotype, cur_genotype);
+  insert(in_genotype, cur_genotype);
 
   return true;
 }
 
 
-cGenotype * cGenotypeControl::Find(const cGenome & in_genome) const
+cGenotype* cGenotypeControl::Find(const cGenome& in_genome) const
 {
   int i;
-  cGenotype * cur_genotype = best;
+  cGenotype* cur_genotype = best;
 
   for (i = 0; i < size; i++) {
     if (in_genome == cur_genotype->GetGenome()) {
@@ -264,10 +203,10 @@
   return NULL;
 }
 
-cGenotype * cGenotypeControl::Find(const int in_genotype_id) const
+cGenotype* cGenotypeControl::Find(const int in_genotype_id) const
 {
   int i;
-  cGenotype * cur_genotype = best;
+  cGenotype* cur_genotype = best;
 
   for (i = 0; i < size; i++) {
     if (in_genotype_id == cur_genotype->GetID()) {
@@ -279,9 +218,9 @@
   return NULL;
 }
 
-int cGenotypeControl::FindPos(cGenotype & in_genotype, int max_depth)
+int cGenotypeControl::FindPos(cGenotype& in_genotype, int max_depth)
 {
-  cGenotype * temp_genotype = best;
+  cGenotype* temp_genotype = best;
   if (max_depth < 0 || max_depth > size) max_depth = size;
 
   for (int i = 0; i < max_depth; i++) {
@@ -292,12 +231,54 @@
   return -1;
 }
 
-cGenotype * cGenotypeControl::Next(int thread)
+cGenotype* cGenotypeControl::Next(int thread)
 {
   return threads[thread] = threads[thread]->GetNext();
 }
 
-cGenotype * cGenotypeControl::Prev(int thread)
+cGenotype* cGenotypeControl::Prev(int thread)
 {
   return threads[thread] = threads[thread]->GetPrev();
 }
+
+
+
+void cGenotypeControl::insert(cGenotype& in_genotype, cGenotype* prev_genotype)
+{
+  if (prev_genotype == NULL) {
+    assert(size == 0); // Destroying a full genotype queue...
+    
+    best = &in_genotype;
+    best->SetNext(best);
+    best->SetPrev(best);
+  } else {
+    in_genotype.SetPrev(prev_genotype);
+    in_genotype.SetNext(prev_genotype->GetNext());
+    prev_genotype->SetNext(&in_genotype);
+    in_genotype.GetNext()->SetPrev(&in_genotype);
+  }
+  
+  size++;
+}
+
+
+bool cGenotypeControl::checkPos(cGenotype& in_genotype)
+{
+  int next_OK = false;
+  int prev_OK = false;
+  
+  if (in_genotype.GetNumOrganisms() >= in_genotype.GetNext()->GetNumOrganisms()) {
+    next_OK =true;
+  }
+  if (in_genotype.GetNumOrganisms() <= in_genotype.GetPrev()->GetNumOrganisms()) {
+    prev_OK =true;
+  }
+  
+  if ( (next_OK && prev_OK) ||
+      (&in_genotype == best && next_OK) ||
+      (&in_genotype == best->GetPrev() && prev_OK)) {
+    return true;
+  }
+  
+  return false;
+}

Modified: development/source/classification/cGenotypeControl.h
===================================================================
--- development/source/classification/cGenotypeControl.h	2009-10-04 18:52:02 UTC (rev 3444)
+++ development/source/classification/cGenotypeControl.h	2009-10-05 18:39:26 UTC (rev 3445)
@@ -42,11 +42,9 @@
   cGenotype* coalescent;
   cGenotype* threads[nGenotype::THREADS];
 
-  cGenotype * historic_list;
+  cGenotype* historic_list;
   int historic_count;
 
-  void Insert(cGenotype & in_genotype, cGenotype * prev_genotype);
-  bool CheckPos(cGenotype & in_genotype);
   
   cGenotypeControl(); // @not_implemented
   cGenotypeControl(const cGenotypeControl&); // @not_implemented
@@ -54,34 +52,36 @@
   
 public:
   cGenotypeControl(cWorld* world);
-  ~cGenotypeControl();
+  ~cGenotypeControl() { ; }
 
   bool OK();
-  void Remove(cGenotype & in_genotype);
-  void Insert(cGenotype & new_genotype);
-  bool Adjust(cGenotype & in_genotype);
+  void Remove(cGenotype& in_genotype);
+  void Insert(cGenotype& new_genotype);
+  bool Adjust(cGenotype& in_genotype);
 
-  void RemoveHistoric(cGenotype & in_genotype);
-  void InsertHistoric(cGenotype & in_genotype);
+  void RemoveHistoric(cGenotype& in_genotype);
+  void InsertHistoric(cGenotype& in_genotype);
   int GetHistoricCount() { return historic_count; }
 
   int UpdateCoalescent();
 
   inline int GetSize() const { return size; }
-  inline cGenotype * GetBest() const { return best; }
-  inline cGenotype * GetCoalescent() const { return coalescent; }
+  inline cGenotype* GetBest() const { return best; }
+  inline cGenotype* GetCoalescent() const { return coalescent; }
 
-  cGenotype * Find(const cGenome & in_genome) const;
-  cGenotype * Find(const int in_genotype_id) const;
-  int FindPos(cGenotype & in_genotype, int max_depth = -1);
+  cGenotype* Find(const cGenome & in_genome) const;
+  cGenotype* Find(const int in_genotype_id) const;
+  int FindPos(cGenotype& in_genotype, int max_depth = -1);
 
-  inline cGenotype * Get(int thread) const { return threads[thread]; }
-  inline cGenotype * Reset(int thread)
-    { return threads[thread] = best; }
-  inline cGenotype * ResetHistoric(int thread)
-    { return threads[thread] = historic_list; }
-  cGenotype * Next(int thread);
-  cGenotype * Prev(int thread);
+  inline cGenotype* Get(int thread) const { return threads[thread]; }
+  inline cGenotype* Reset(int thread) { return threads[thread] = best; }
+  inline cGenotype* ResetHistoric(int thread) { return threads[thread] = historic_list; }
+  cGenotype* Next(int thread);
+  cGenotype* Prev(int thread);
+  
+private:
+  void insert(cGenotype& in_genotype, cGenotype* prev_genotype);
+  bool checkPos(cGenotype& in_genotype);  
 };
 
 

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2009-10-04 18:52:02 UTC (rev 3444)
+++ development/source/main/cPopulation.cc	2009-10-05 18:39:26 UTC (rev 3445)
@@ -4687,8 +4687,8 @@
     cellstr.Set("%d", cells[0].cell_id);
     offsetstr.Set("%d", cells[0].offset);
     for (int cell_i = 1; cell_i < cells.GetSize(); cell_i++) {
-      cellstr.Set("%s,%d", (const char*)cellstr, cells[cell_i].cell_id);
-      offsetstr.Set("%s,%d", (const char*)offsetstr, cells[cell_i].offset);
+      cellstr += cStringUtil::Stringf(",%d", cells[cell_i].cell_id);
+      offsetstr += cStringUtil::Stringf(",%d", cells[cell_i].offset);
     }
     df.Write(cellstr, "Occupied Cell IDs");
     df.Write(offsetstr, "Gestation (CPU) Cycle Offsets");
@@ -4744,7 +4744,7 @@
     /* depth */       cur_line.PopWord();
     cString name = cStringUtil::Stringf("org-%d", tmp.id_num);
     cGenome genome(cur_line.PopWord());
-    
+        
     // Process resident cell ids
     cString cellstr(cur_line.PopWord());
     while (cellstr.GetSize()) tmp.cells.Push(cellstr.Pop(',').AsInt());

Modified: development/source/tools/cFile.cc
===================================================================
--- development/source/tools/cFile.cc	2009-10-04 18:52:02 UTC (rev 3444)
+++ development/source/tools/cFile.cc	2009-10-05 18:39:26 UTC (rev 3445)
@@ -80,12 +80,9 @@
 
 bool cFile::ReadLine(cString & in_string)
 {
-  char cur_line[MAX_STRING_LENGTH];
-  cur_line[0]='\0';
-  fp.getline(cur_line, MAX_STRING_LENGTH);
-  if( fp.bad() ){
-    return false;
-  }
-  in_string = cur_line;
+  std::string linebuf;
+  std::getline(fp, linebuf);
+  if (fp.bad()) return false;
+  in_string = linebuf.c_str();
   return true;
 }

Modified: development/source/tools/cInitFile.cc
===================================================================
--- development/source/tools/cInitFile.cc	2009-10-04 18:52:02 UTC (rev 3444)
+++ development/source/tools/cInitFile.cc	2009-10-05 18:39:26 UTC (rev 3445)
@@ -59,17 +59,12 @@
   
   tSmartArray<sLine*> lines;
   
-  char cur_line[MAX_STRING_LENGTH];
-  in_stream.getline(cur_line, MAX_STRING_LENGTH);
-  
-  if (!in_stream && !strlen(cur_line)) return;
-  
   int linenum = 1;
-  while (in_stream) {
+  std::string linebuf;
+  while (std::getline(in_stream, linebuf)) {
+    cString cur_line(linebuf.c_str());
     if (cur_line[0] == '#' && cur_line[1] == '!') ProcessCommand(cur_line, lines, m_filename, linenum);
-    else lines.Push(new sLine(cur_line, m_filename, linenum));
-    
-    in_stream.getline(cur_line, MAX_STRING_LENGTH);
+    else lines.Push(new sLine(cur_line, m_filename, linenum));    
     linenum++;
   }
   

Modified: development/source/tools/cString.cc
===================================================================
--- development/source/tools/cString.cc	2009-10-04 18:52:02 UTC (rev 3444)
+++ development/source/tools/cString.cc	2009-10-05 18:39:26 UTC (rev 3445)
@@ -44,7 +44,7 @@
 cString::cStringData::cStringData(int in_size, const char* in) : m_size(in_size), m_data(new char[m_size + 1])
 {
   assert(m_data != NULL); // Memory Allocation Error: Out of Memory
-  for (short i = 0; i < m_size; i++) m_data[i] = in[i];
+  for (int i = 0; i < m_size; i++) m_data[i] = in[i];
   m_data[m_size] = '\0';
 }
 
@@ -59,10 +59,7 @@
 
 // ** class cString **
 
-// -- Constants --
-const int cString::MAX_LENGTH = MAX_STRING_LENGTH;
 
-
 // -- Comparisons --
 
 bool cString::operator==(const cString& in) const {
@@ -328,7 +325,7 @@
 cString & cString::Set(const char * fmt, ...)
 {
   va_list argp;
-  char buf[MAX_LENGTH];
+  char buf[MAX_STRING_LENGTH];
   va_start(argp, fmt);
   vsprintf(buf, fmt, argp);
   va_end(argp);
@@ -711,7 +708,7 @@
 
 istream & operator >> (istream & in, cString & string)
 {
-  char buf[cString::MAX_LENGTH];
+  char buf[MAX_STRING_LENGTH];
   in>>buf;
   string=buf;
   return in;

Modified: development/source/tools/cString.h
===================================================================
--- development/source/tools/cString.h	2009-10-04 18:52:02 UTC (rev 3444)
+++ development/source/tools/cString.h	2009-10-05 18:39:26 UTC (rev 3445)
@@ -67,10 +67,10 @@
   {
   private:
     cString& string;
-    short index;
+    int index;
 
   public:
-    cCharProxy(cString& _string, short _index) : string(_string), index(_index) { ; }
+    cCharProxy(cString& _string, int _index) : string(_string), index(_index) { ; }
 
     inline cCharProxy& operator=(char c);     // lvalue
     inline cCharProxy& operator+=(char c);    // lvalue
@@ -134,8 +134,6 @@
   };
 
 public:
-  static const int MAX_LENGTH;
-
   cString(const char* in_str = "") : value(new cStringData(strlen(in_str), in_str))
   {
     assert( in_str != NULL );     // NULL input string




More information about the Avida-cvs mailing list