[Avida-SVN] r3570 - in branches/biounit: Avida.xcodeproj source/classification source/cpu source/main source/tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Dec 21 11:55:41 PST 2009


Author: brysonda
Date: 2009-12-21 14:55:40 -0500 (Mon, 21 Dec 2009)
New Revision: 3570

Added:
   branches/biounit/source/main/cMetaGenome.cc
Modified:
   branches/biounit/Avida.xcodeproj/project.pbxproj
   branches/biounit/source/classification/cBGGenotype.cc
   branches/biounit/source/cpu/cHardwareManager.cc
   branches/biounit/source/cpu/cTestCPU.cc
   branches/biounit/source/main/cMetaGenome.h
   branches/biounit/source/main/cOrganism.h
   branches/biounit/source/main/cParasite.cc
   branches/biounit/source/main/cPopulation.cc
   branches/biounit/source/tools/cDataFile.cc
   branches/biounit/source/tools/cDataFile.h
   branches/biounit/source/tools/tArray.h
   branches/biounit/source/tools/tManagedPointerArray.h
Log:
Parasite genotypes are now output by SaveStructuredPopulationBG.

Enhance cDataFile to support setting filetype and tracking column format.  This allows for data file output hand off to sub objects.   Not quite ready for structured data save, but it is a step in that direction.  For example, cBGGenotype handles outputting its relevant information and defers outputting cMetaGenome information to the cMetaGenome class now.

Modified: branches/biounit/Avida.xcodeproj/project.pbxproj
===================================================================
--- branches/biounit/Avida.xcodeproj/project.pbxproj	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/Avida.xcodeproj/project.pbxproj	2009-12-21 19:55:40 UTC (rev 3570)
@@ -34,6 +34,7 @@
 		5629D81C0C3EE26A00C5F152 /* cCoreView_Info.cc in Sources */ = {isa = PBXBuildFile; fileRef = 565121C60C3B055F009CBD3F /* cCoreView_Info.cc */; };
 		56F555E50C3B402A00E2E929 /* cDriver_TextViewer.cc in Sources */ = {isa = PBXBuildFile; fileRef = 56F555DF0C3B402A00E2E929 /* cDriver_TextViewer.cc */; };
 		56F555E70C3B402A00E2E929 /* viewer-text.cc in Sources */ = {isa = PBXBuildFile; fileRef = 56F555E40C3B402A00E2E929 /* viewer-text.cc */; };
+		700B6D4110DFFDD000A1F8F1 /* cMetaGenome.cc in Sources */ = {isa = PBXBuildFile; fileRef = 700B6D4010DFFDD000A1F8F1 /* cMetaGenome.cc */; };
 		700D9C460F1A8F34002CC711 /* cModularityAnalysis.h in Headers */ = {isa = PBXBuildFile; fileRef = 700D9C440F1A8F34002CC711 /* cModularityAnalysis.h */; };
 		700D9C470F1A8F34002CC711 /* cModularityAnalysis.cc in Sources */ = {isa = PBXBuildFile; fileRef = 700D9C450F1A8F34002CC711 /* cModularityAnalysis.cc */; };
 		700E2996085A1F6000CF158A /* avida in CopyFiles */ = {isa = PBXBuildFile; fileRef = DCC3164D07626CF3008F7A48 /* avida */; };
@@ -439,6 +440,7 @@
 		7005A70209BA0FA90007E16E /* cTestCPUInterface.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cTestCPUInterface.cc; sourceTree = "<group>"; };
 		7005A70909BA0FBE0007E16E /* cOrgInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cOrgInterface.h; sourceTree = "<group>"; };
 		700AE91B09DB65F200A073FD /* cTaskContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cTaskContext.h; sourceTree = "<group>"; };
+		700B6D4010DFFDD000A1F8F1 /* cMetaGenome.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cMetaGenome.cc; sourceTree = "<group>"; };
 		700D9BD90F1A5D33002CC711 /* tAnalyzeJobBatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tAnalyzeJobBatch.h; sourceTree = "<group>"; };
 		700D9C170F1A7436002CC711 /* tArrayMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tArrayMap.h; sourceTree = "<group>"; };
 		700D9C1A0F1A77EC002CC711 /* tKVPair.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tKVPair.h; sourceTree = "<group>"; };
@@ -1718,6 +1720,7 @@
 				BBDE4FF80FC1B06600CC6170 /* cDemePredicate.h */,
 				7090F57310D956A400ECFBA1 /* cParasite.h */,
 				7090F57410D956A400ECFBA1 /* cParasite.cc */,
+				700B6D4010DFFDD000A1F8F1 /* cMetaGenome.cc */,
 			);
 			path = main;
 			sourceTree = "<group>";
@@ -2309,6 +2312,7 @@
 				701E748510A37DB2006B6193 /* cBioGroupManager.cc in Sources */,
 				70B547FD10AB22920013E999 /* cBGGenotypeManager.cc in Sources */,
 				7090F57610D956A400ECFBA1 /* cParasite.cc in Sources */,
+				700B6D4110DFFDD000A1F8F1 /* cMetaGenome.cc in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

Modified: branches/biounit/source/classification/cBGGenotype.cc
===================================================================
--- branches/biounit/source/classification/cBGGenotype.cc	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/classification/cBGGenotype.cc	2009-12-21 19:55:40 UTC (rev 3570)
@@ -115,28 +115,31 @@
 
 void cBGGenotype::Save(cDataFile& df)
 {
-  df.Write(m_id, "ID");
-  df.Write(Avida::BioUnitSourceMap[m_src], "Source");
-  cString parent_str("");
+  
+  df.Write(m_id, "ID", "id");
+  df.Write(Avida::BioUnitSourceMap[m_src], "Source", "src");
+  df.Write(m_src_args, "Source Args", "src_args");
+
+  cString str("");
   if (m_parents.GetSize()) {
-    parent_str += cStringUtil::Stringf("%d", m_parents[0]->GetID());
+    str += cStringUtil::Stringf("%d", m_parents[0]->GetID());
     for (int i = 1; i < m_parents.GetSize(); i++) {
-      parent_str += cStringUtil::Stringf(",%d", m_parents[i]->GetID());
+      str += cStringUtil::Stringf(",%d", m_parents[i]->GetID());
     }
   }
-  df.Write(parent_str, "Parent ID(s)");
-  df.Write(m_num_organisms, "Number of currently living organisms");
-  df.Write(m_total_organisms, "Total number of organisms that ever existed");
-  df.Write(m_genome.GetGenome().GetSize(), "Genome Length");
-  df.Write(m_merit.Average(), "Average Merit");
-  df.Write(m_gestation_time.Average(), "Average Gestation Time");
-  df.Write(m_fitness.Average(), "Average Fitness");
-  df.Write(m_generation_born, "Generation Born");
-  df.Write(m_update_born, "Update Born");
-  df.Write(m_update_deactivated, "Update Deactivated");
-  df.Write(m_depth, "Phylogenetic Depth");
-  df.Write(m_genome.GetGenome().AsString(), "Genome Sequence");
+  df.Write(str, "Parent ID(s)", "parents");
   
+  df.Write(m_num_organisms, "Number of currently living organisms", "num_units");
+  df.Write(m_total_organisms, "Total number of organisms that ever existed", "total_units");
+  df.Write(m_genome.GetGenome().GetSize(), "Genome Length", "length");
+  df.Write(m_merit.Average(), "Average Merit", "merit");
+  df.Write(m_gestation_time.Average(), "Average Gestation Time", "gest_time");
+  df.Write(m_fitness.Average(), "Average Fitness", "fitness");
+  df.Write(m_generation_born, "Generation Born", "gen_born");
+  df.Write(m_update_born, "Update Born", "update_born");
+  df.Write(m_update_deactivated, "Update Deactivated", "update_deactivated");
+  df.Write(m_depth, "Phylogenetic Depth", "depth");
+  m_genome.Save(df);
 }
 
 

Modified: branches/biounit/source/cpu/cHardwareManager.cc
===================================================================
--- branches/biounit/source/cpu/cHardwareManager.cc	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/cpu/cHardwareManager.cc	2009-12-21 19:55:40 UTC (rev 3570)
@@ -89,7 +89,7 @@
 {
   assert(org != NULL);
 	
-  int inst_set_id = (is == NULL) ? 1 : -1;
+  int inst_set_id = (is == NULL) ? 0 : -1;
   cInstSet* inst_set = (is == NULL) ? m_inst_set : is;
   
   cHardwareBase* hw = 0;

Modified: branches/biounit/source/cpu/cTestCPU.cc
===================================================================
--- branches/biounit/source/cpu/cTestCPU.cc	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/cpu/cTestCPU.cc	2009-12-21 19:55:40 UTC (rev 3570)
@@ -213,7 +213,7 @@
 {
   ctx.SetTestMode();
   test_info.Clear();
-  cMetaGenome mg(m_world->GetConfig().HARDWARE_TYPE.Get(), 1, genome); // @TODO - fix test cpu metagenome handling
+  cMetaGenome mg(m_world->GetConfig().HARDWARE_TYPE.Get(), 0, genome); // @TODO - fix test cpu metagenome handling
   TestGenome_Body(ctx, test_info, mg, 0);
   ctx.ClearTestMode();
   
@@ -225,7 +225,7 @@
 {
   ctx.SetTestMode();
   test_info.Clear();
-  cMetaGenome mg(m_world->GetConfig().HARDWARE_TYPE.Get(), 1, genome); // @TODO - fix test cpu metagenome handling
+  cMetaGenome mg(m_world->GetConfig().HARDWARE_TYPE.Get(), 0, genome); // @TODO - fix test cpu metagenome handling
   TestGenome_Body(ctx, test_info, mg, 0);
 
   ////////////////////////////////////////////////////////////////

Added: branches/biounit/source/main/cMetaGenome.cc
===================================================================
--- branches/biounit/source/main/cMetaGenome.cc	                        (rev 0)
+++ branches/biounit/source/main/cMetaGenome.cc	2009-12-21 19:55:40 UTC (rev 3570)
@@ -0,0 +1,35 @@
+/*
+ *  cMetaGenome.cc
+ *  Avida
+ *
+ *  Created by David on 12/21/09.
+ *  Copyright 2009 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#include "cMetaGenome.h"
+
+#include "cDataFile.h"
+
+
+void cMetaGenome::Save(cDataFile& df)
+{
+  df.Write(m_hw_type, "Hardware Type ID", "hw_type");
+  df.Write(m_inst_set_id, "Inst Set ID" , "inst_set");
+  df.Write(m_genome.AsString(), "Genome Sequence", "sequence");
+}

Modified: branches/biounit/source/main/cMetaGenome.h
===================================================================
--- branches/biounit/source/main/cMetaGenome.h	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/main/cMetaGenome.h	2009-12-21 19:55:40 UTC (rev 3570)
@@ -27,7 +27,9 @@
 
 #include "cGenome.h"
 
+class cDataFile;
 
+
 class cMetaGenome
 {
 private:
@@ -56,6 +58,8 @@
     { return (m_hw_type == mg.m_hw_type && m_inst_set_id == mg.m_inst_set_id && m_genome == mg.m_genome); }
   cMetaGenome& operator=(const cMetaGenome& mg)
     { m_hw_type = mg.m_hw_type; m_inst_set_id = mg.m_inst_set_id; m_genome = mg.m_genome; return *this; }
+  
+  void Save(cDataFile& df);
 };
 
 

Modified: branches/biounit/source/main/cOrganism.h
===================================================================
--- branches/biounit/source/main/cOrganism.h	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/main/cOrganism.h	2009-12-21 19:55:40 UTC (rev 3570)
@@ -331,6 +331,7 @@
   bool InjectParasite(cBioUnit* parent, const cString& label, const cGenome& genome);
   bool ParasiteInfectHost(cBioUnit* parasite);
   int GetNumParasites() const { return m_parasites.GetSize(); }
+  const tArray<cBioUnit*>& GetParasites() const { return m_parasites; }
   void ClearParasites();
 
   // --------  Mutation Rate Convenience Methods  --------

Modified: branches/biounit/source/main/cParasite.cc
===================================================================
--- branches/biounit/source/main/cParasite.cc	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/main/cParasite.cc	2009-12-21 19:55:40 UTC (rev 3570)
@@ -27,6 +27,5 @@
 cParasite::cParasite(const cMetaGenome& genome, int parent_generation, eBioUnitSource src, const cString& src_args)
   : m_src(src), m_src_args(src_args), m_initial_genome(genome)
 {
-  assert(false);
   // @TODO - properly construct cPhenotype
 }

Modified: branches/biounit/source/main/cPopulation.cc
===================================================================
--- branches/biounit/source/main/cPopulation.cc	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/main/cPopulation.cc	2009-12-21 19:55:40 UTC (rev 3570)
@@ -552,8 +552,8 @@
   
   // Pre-check target hardware
   const cHardwareBase& hw = target_organism->GetHardware();
-  if (hw.GetType() != parent.GetMetaGenome().GetHardwareType() ||
-      hw.GetInstSetID() != parent.GetMetaGenome().GetInstSetID() ||
+  if (hw.GetType() != parent->GetMetaGenome().GetHardwareType() ||
+      hw.GetInstSetID() != parent->GetMetaGenome().GetInstSetID() ||
       hw.GetNumThreads() == m_world->GetConfig().MAX_CPU_THREADS.Get()) return false;
   
 
@@ -4778,45 +4778,72 @@
   return true;
 }
 
+
+struct sGroupInfo {
+  cBioGroup* bg;
+  tArray<sOrgInfo> orgs;
+  bool parasite;
+  
+  sGroupInfo(cBioGroup* in_bg, bool is_para = false) : bg(in_bg), parasite(is_para) { ; }
+};
+
 bool cPopulation::SaveStructuredPopulationBG(const cString& filename)
 {
   cDataFile& df = m_world->GetDataFile(filename);
-  df.WriteRawComment("#filetype genotype_data");
-  df.WriteRawComment("#format id parents num_cpus total_cpus length merit gest_time fitness update_born update_dead depth sequence cells gest_offset");
-  df.WriteComment("");
-  df.WriteComment("Structured Population Save (BioGroup)");
+  df.SetFileType("genotype_data");
+  df.WriteComment("Structured Population Save");
   df.WriteTimeStamp();
   
   // Build up hash table of all current genotypes and the cells in which the organisms reside
-  tHashTable<int, tKVPair<cBioGroup*, tArray<sOrgInfo> >* > genotype_map;
+  tHashTable<int, sGroupInfo*> genotype_map;
   
-  for (int i = 0; i < cell_array.GetSize(); i++) {
-    if (cell_array[i].IsOccupied()) {
-      cOrganism* org = cell_array[i].GetOrganism();
+  for (int cell = 0; cell < cell_array.GetSize(); cell++) {
+    if (cell_array[cell].IsOccupied()) {
+      cOrganism* org = cell_array[cell].GetOrganism();
+      
+      // Handle any parasites
+      const tArray<cBioUnit*>& parasites = org->GetParasites();
+      for (int p = 0; p < parasites.GetSize(); p++) {
+        cBioGroup* pg = parasites[p]->GetBioGroup("genotype");
+        if (pg == NULL) continue;
+
+        sGroupInfo* map_entry = NULL;
+        if (genotype_map.Find(pg->GetID(), map_entry)) {
+          map_entry->orgs.Push(sOrgInfo(cell, 0));
+        } else {
+          map_entry = new sGroupInfo(pg, true);
+          map_entry->orgs.Push(sOrgInfo(cell, 0));
+          genotype_map.Add(pg->GetID(), map_entry);
+        }        
+      }
+      
+      
+      // Handle the organism itself
       cBioGroup* genotype = org->GetBioGroup("genotype");
+      if (genotype == NULL) continue;
+
       int offset = org->GetPhenotype().GetCPUCyclesUsed();
-      
-      tKVPair<cBioGroup*, tArray<sOrgInfo> >* map_entry = NULL;
+
+      sGroupInfo* map_entry = NULL;
       if (genotype_map.Find(genotype->GetID(), map_entry)) {
-        map_entry->Value().Push(sOrgInfo(i, offset));
+        map_entry->orgs.Push(sOrgInfo(cell, offset));
       } else {
-        map_entry = new tKVPair<cBioGroup*, tArray<sOrgInfo> >(genotype, tArray<sOrgInfo>(0));
-        map_entry->Value().Push(sOrgInfo(i, offset));
+        map_entry = new sGroupInfo(genotype);
+        map_entry->orgs.Push(sOrgInfo(cell, offset));
         genotype_map.Add(genotype->GetID(), map_entry);
       }
     }
   }
   
   // Output all current genotypes
-  
-  tArray<tKVPair<cBioGroup*, tArray<sOrgInfo> >* > genotype_entries;
+  tArray<sGroupInfo*> genotype_entries;
   genotype_map.GetValues(genotype_entries);
   for (int i = 0; i < genotype_entries.GetSize(); i++) {
-    cBioGroup* genotype = genotype_entries[i]->Key();
+    cBioGroup* genotype = genotype_entries[i]->bg;
     
     genotype->Save(df);
     
-    tArray<sOrgInfo>& cells = genotype_entries[i]->Value();
+    tArray<sOrgInfo>& cells = genotype_entries[i]->orgs;
     cString cellstr;
     cString offsetstr;
     cellstr.Set("%d", cells[0].cell_id);
@@ -4825,8 +4852,9 @@
       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");
+    df.Write(cellstr, "Occupied Cell IDs", "cells");
+    if (genotype_entries[i]->parasite) df.Write("", "Gestation (CPU) Cycle Offsets", "gest_offset");
+    else df.Write(offsetstr, "Gestation (CPU) Cycle Offsets", "gest_offset");
     df.Endl();
     
     delete genotype_entries[i];

Modified: branches/biounit/source/tools/cDataFile.cc
===================================================================
--- branches/biounit/source/tools/cDataFile.cc	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/tools/cDataFile.cc	2009-12-21 19:55:40 UTC (rev 3570)
@@ -33,7 +33,7 @@
 using namespace std;
 
 
-cDataFile::cDataFile(cString& name) : m_name(name), num_cols(0), m_descr_written(false)
+cDataFile::cDataFile(cString& name) : m_name(name), m_descr_written(false), m_num_cols(0)
 {
   m_fp.open(name);
   assert(m_fp.good());
@@ -41,93 +41,96 @@
 }
 
 
-void cDataFile::Write( double x, const char* descr )
+void cDataFile::Write(double x, const char* descr, const char* format)
 {
   if (!m_descr_written) {
     m_data << x << " ";
-    WriteColumnDesc(descr);
-  } else
+    WriteColumnDesc(descr, format);
+  } else {
     m_fp << x << " ";
+  }
 }
 
 
-void cDataFile::Write(int i, const char* descr)
+void cDataFile::Write(int i, const char* descr, const char* format)
 {
   if (!m_descr_written) {
     m_data << i << " ";
-    WriteColumnDesc(descr);
-  } else
+    WriteColumnDesc(descr, format);
+  } else {
     m_fp << i << " ";
+  }
 }
 
 
-void cDataFile::Write(long i, const char* descr)
+void cDataFile::Write(long i, const char* descr, const char* format)
 {
   if (!m_descr_written) {
     m_data << i << " ";
-    WriteColumnDesc(descr);
-  } else
+    WriteColumnDesc(descr, format);
+  } else {
     m_fp << i << " ";
+  }
 }
 
-void cDataFile::Write(unsigned int i, const char* descr)
+void cDataFile::Write(unsigned int i, const char* descr, const char* format)
 {
   if (!m_descr_written) {
     m_data << i << " ";
     WriteColumnDesc(descr);
-  } else
+  } else {
     m_fp << i << " ";
+  }
 }
 
 
-void cDataFile::Write(const char* data_str, const char* descr)
+void cDataFile::Write(const char* data_str, const char* descr, const char* format)
 {
   if (!m_descr_written) {
     m_data << data_str << " ";
-    WriteColumnDesc(descr);
-  } else
+    WriteColumnDesc(descr, format);
+  } else {
     m_fp << data_str << " ";
+  }
 }
 
 
 void cDataFile::WriteBlockElement(double x, int element, int x_size)
 {
   m_fp << x << " ";
-  if (((element + 1) % x_size) == 0) {
-    m_fp << "\n";
-  }
+  if (((element + 1) % x_size) == 0) m_fp << "\n";
 }
 
 void cDataFile::WriteBlockElement(int i, int element, int x_size)
 {
   m_fp << i << " ";
-  if (((element + 1) % x_size) == 0) {
-    m_fp << "\n";
-  }
+  if (((element + 1) % x_size) == 0) m_fp << "\n";
 }
 
-void cDataFile::WriteColumnDesc( const char * descr )
+void cDataFile::WriteColumnDesc(const char* descr, const char* format)
 {
-  if ( !m_descr_written ){
-    num_cols++;
-    m_descr += cStringUtil::Stringf( "# %2d: %s\n", num_cols, descr );
+  if (!m_descr_written) {
+    m_num_cols++;
+    m_descr += cStringUtil::Stringf("# %2d: %s\n", m_num_cols, descr);
+    cString formatstr(format);
+    if (formatstr != "") m_format += formatstr + " ";
   }
 }
 
-void cDataFile::WriteComment( const char * descr )
+void cDataFile::WriteComment(const char* comment)
 {
-  if ( !m_descr_written ) m_descr += cStringUtil::Stringf( "# %s\n", descr );
+  if (!m_descr_written) m_descr += cStringUtil::Stringf("# %s\n", comment);
 }
 
 
-void cDataFile::WriteRawComment( const char * descr )
+void cDataFile::WriteRawComment(const char* comment)
 {
-  if ( !m_descr_written ) m_descr += cStringUtil::Stringf( "%s\n", descr );
+  if (!m_descr_written) m_descr += cStringUtil::Stringf("%s\n", comment);
 }
 
-void cDataFile::WriteRaw( const char * descr )
+void cDataFile::WriteRaw(const char* str)
 {
-  m_fp << cStringUtil::Stringf( "%s\n", descr );
+  m_fp << cStringUtil::Stringf( "%s\n", str);
 }
 
 
@@ -135,15 +138,15 @@
 
 void cDataFile::WriteTimeStamp()
 {
-  if ( !m_descr_written ){
-    time_t time_p = time( 0 );
-    m_descr += cStringUtil::Stringf( "# %s", ctime( &time_p ) );
+  if (!m_descr_written) {
+    time_t time_p = time(0);
+    m_descr += cStringUtil::Stringf("# %s", ctime(&time_p));
   }
 }
 
 void cDataFile::FlushComments()
 {
-  if ( !m_descr_written ){
+  if (!m_descr_written) {
     m_fp << m_descr;
     m_descr = "";
     
@@ -155,15 +158,22 @@
 
 void cDataFile::Endl()
 {
-  if ( !m_descr_written ){
+  if (!m_descr_written) {
+    // Handle filetype and format first
+    if (m_filetype != "") m_fp << "#filetype " << m_filetype << endl;
+    if (m_format != "") m_fp << "#format " << m_format << endl;
+
+    // Output column descriptions and comments
     m_fp << m_descr << endl;
     m_descr = "";
     
+    // Print the first row of data
     m_fp << m_data.str() << endl;
     m_data.clear();
     m_data.str("");
     
     m_descr_written = true;
+  } else {
+    m_fp << endl;
   }
-  else m_fp << endl;
 }

Modified: branches/biounit/source/tools/cDataFile.h
===================================================================
--- branches/biounit/source/tools/cDataFile.h	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/tools/cDataFile.h	2009-12-21 19:55:40 UTC (rev 3570)
@@ -32,203 +32,108 @@
 #ifndef cString_h
 #include "cString.h"
 #endif
-#if USE_tMemTrack
-# ifndef tMemTrack_h
-#  include "tMemTrack.h"
-# endif
-#endif
 
-/**
- * This class encapsulates file handling for the class @ref cDataFileManager.
- * The files it creates are write only. Reading of files is handled by
- * @ref cFile.
- */
+// This class encapsulates output file handling for the class cDataFileManager.
 
 class cDataFile
 {
-#if USE_tMemTrack
-  tMemTrack<cDataFile> mt;
-#endif
 private:
   cString m_name;
-  std::ostringstream m_data;
-  cString m_descr;
-  int num_cols;
   
+  cString m_descr;
+  cString m_filetype;
+  cString m_format;
   bool m_descr_written;
+  std::ostringstream m_data;
+
+  int m_num_cols;
   
   std::ofstream m_fp;
 
+  
+  cDataFile();
   cDataFile(const cDataFile&); // @not_implemented.
   cDataFile& operator=(const cDataFile&); // @not_implemented
 
+  
 public:
-  cDataFile() : num_cols(0), m_descr_written(false) { ; }
-
-  /**
-   * This constructor opens a file of the given name, and makes sure
-   * the file is usable.
-   *
-   * @param _name The name of the file to open.
-   **/
   cDataFile(cString& name);
-
-  /**
-   * The desctructor automatically closes the file.
-   **/
   ~cDataFile() { m_fp.close(); }
 
-  /**
-   * @return The file name used
-   **/
-  const cString& GetName() const { return m_name; }
+  inline const cString& GetName() const { return m_name; }
+  inline const cString& GetFileType() const { return m_filetype; }
 
-  /**
-   * @return A bool that indicates whether the file is actually usable.
-   **/
-  bool Good() const { return m_fp.good(); }
+  inline bool Good() const { return m_fp.good(); }
+  inline bool HeaderDone() { return m_descr_written; }
+  
+  inline bool SetFileType(const cString& ft);
+  
 
-  /**
-   * This function allows low-level write access to the file. Although
-   * sometimes usefull, it provides the possibility to circumvent the
-   * automatic documentation. Use with care.
-   *
-   * @return The output stream corresponding to the file.
-   **/
   std::ofstream& GetOFStream() { return m_fp; }
 
-  /**
-   * Outputs a value into the data file.
-   *
-   * @param x The value to write (as double, int, or char *)
-   *
-   * @param descr A string that describes the meaning of the value. The string
-   * will be written only once, before the first data line has been finished.
-   **/
-
-  void Write(double x, const char* descr);
-  void Write(int i, const char* descr);
-  void Write(long i, const char* descr);
-	void Write(unsigned int i, const char* descr);
-  void Write(const char* data_str, const char* descr);
+  
+  // The following methods output a value into the data file.
+  //  first argument (x, i, data_str, etc.) - the value to write (as double, int, const char *, etc.)
+  //  descr - descriptive string detailing the meaning of the value
+  //  format (optional) - formatting identifier for the column
+  void Write(double x, const char* descr, const char* format = "");
+  void Write(int i, const char* descr, const char* format = "");
+  void Write(long i, const char* descr, const char* format = "");
+	void Write(unsigned int i, const char* descr, const char* format = "");
+  void Write(const char* data_str, const char* descr, const char* format = "");
+  
+  
+  // The following methods output a value into the data file anonymously (no column descriptor).
+  //  first argument (x, i, data_str, etc.) - the value to write (as double, int, const char *, etc.)
   inline void WriteAnonymous(double x) { m_fp << x << " "; }
   inline void WriteAnonymous(int i) { m_fp << i << " "; }
   inline void WriteAnonymous(long i) { m_fp << i << " "; }
   inline void WriteAnonymous(const char* data_str) { m_fp << data_str << " "; }
+  
+  
+  // The following methods are useful for outputting tables of values with row size x
   void WriteBlockElement(double x, int element, int x_size);
   void WriteBlockElement(int i, int element, int x_size);
 
-  /**
-   * Writes a descriptive string into a data file. The string is only
-   * written if the first data line hasn't been completed (Endl() hasn't
-   * been called. This allows to output initial comments into a file.
-   **/
-  void WriteComment( const char* descr );
+  
+  // Writes the description for a single column; keeps track of column numbers and formatting idendifier (optional).
+  void WriteColumnDesc(const char* descr, const char* format = ""); 
 
-  /**
-   * Same as WriteComment, but doesn't automatically include the # in the
-   * front of the line.  This should only be used in special circumstances
-   * where something outside of a typical comment needs to be at the top.
-   **/
-  void WriteRawComment( const char* descr );
+  // Writes a descriptive/comment string into a data file.
+  // The string is only written if the first data line hasn't been completed (i.e. Endl() hasn't been called).
+  void WriteComment(const char* comment);
 
-  /**
-   * Writes text string any where in the data file. This should only be used 
-   * in special circumstances where something outside of a typical comment 
-   * needs to be placed in the file.
-   **/
-  void WriteRaw( const char* descr );
+  // Same as WriteComment, but doesn't automatically include the # in the front of the line.
+  // This should only be used in special circumstances where something outside of a typical comment needs to be at the top.
+  void WriteRawComment(const char* comment);
 
-  /**
-   * Writes the description for a single column; keeps track of column numbers.
-   **/
-  void WriteColumnDesc(const char* descr );
-
-  /**
-   * Writes the current time into the data file. The time string is only
-   * written if the first data line hasn't been completed (Endl() hasn't
-   * been called.
-   **/
+  // Writes the current time into the data file comments.
   void WriteTimeStamp();
 
-  /**
-   * This function writes the comments that have accumulated. There should
-   * normally be no reason to call this function. Endl() does the same thing
-   * in a safer way.
-   **/
+  // Writes text string any where in the data file. This should only be used in special circumstances where something
+  // outside of a typical comment needs to be placed in the file.
+  void WriteRaw(const char* str);
+  
+
+  // This function writes the comments that have accumulated. There should normally be no reason to call this function,
+  // Endl() performs the same operation automatically after all columns are known.
   void FlushComments();
+
   
-  /**
-   * Write all data to disk and start a new line.
-   **/
+  // Write all data to disk and start a new line.
   void Endl();
 
-  /**
-   * Has the header been written to the file yet?
-   **/
-  bool HeaderDone() { return m_descr_written; }
   
-  /**
-   * This function makes sure that all cached data is written to the disk.
-   **/
   void Flush() { m_fp.flush(); }
+};
 
-
-  /**
-   * Save to archive
-   **/
-  template<class Archive>
-  void save(Archive & a, const unsigned int version) const {
-    a.ArkvObj("m_name", m_name);
-    a.ArkvObj("m_data", m_data);
-    a.ArkvObj("m_descr", m_descr);
-    a.ArkvObj("num_cols", num_cols);
-
-    int __m_descr_written = (m_descr_written == false)?(0):(1);
-    a.ArkvObj("m_descr_written", __m_descr_written);
-
-    ///*
-    //Record current write-position.
-    //*/
-    //int position = m_fp.rdbuf()->pubseekoff(0,std::ios::cur);
-    //a.ArkvObj("position", position);
-  }
-
-  /**
-   * Load from archive
-   **/
-  template<class Archive>
-  void load(Archive & a, const unsigned int version){
-    a.ArkvObj("m_name", m_name);
-    a.ArkvObj("m_data", m_data);
-    a.ArkvObj("m_descr", m_descr);
-    a.ArkvObj("num_cols", num_cols);
-
-    int __m_descr_written;
-    a.ArkvObj("m_descr_written", __m_descr_written);
-    m_descr_written = (__m_descr_written == 0)?(false):(true);
-
-    /*
-    open file in write/append mode.
-    */
-    m_fp.open(m_name, std::ios::out|std::ios::app);
-
-    ///*
-    //Restore write-position.
-    //*/
-    //int position;
-    //a.ArkvObj("position", position);
-    //m_fp.rdbuf()->pubseekpos(position);
-  }
+inline bool cDataFile::SetFileType(const cString& ft)
+{
+  if (m_descr_written) return false;
   
-  /**
-   * Ask archive to handle loads and saves separately
-   **/
-  template<class Archive>
-  void serialize(Archive & a, const unsigned int version){
-    a.SplitLoadSave(*this, version);
-  }
+  m_filetype = ft;
+  return true;
+}
 
-};
 
 #endif

Modified: branches/biounit/source/tools/tArray.h
===================================================================
--- branches/biounit/source/tools/tArray.h	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/tools/tArray.h	2009-12-21 19:55:40 UTC (rev 3570)
@@ -87,7 +87,6 @@
     return tmp;
   }
   
-  bool Good() const { return (m_data != NULL); }
   int GetSize() const { return m_size; }
 
   void ResizeClear(const int in_size)

Modified: branches/biounit/source/tools/tManagedPointerArray.h
===================================================================
--- branches/biounit/source/tools/tManagedPointerArray.h	2009-12-20 00:16:56 UTC (rev 3569)
+++ branches/biounit/source/tools/tManagedPointerArray.h	2009-12-21 19:55:40 UTC (rev 3570)
@@ -70,7 +70,6 @@
     return *this;
   }
   
-  bool Good() const { return (m_data != NULL); }
   int GetSize() const { return m_size; }
   
   void ResizeClear(const int in_size)




More information about the Avida-cvs mailing list