[Avida-SVN] r3248 - development/source/classification

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Wed May 20 06:53:19 PDT 2009


Author: brysonda
Date: 2009-05-20 09:53:19 -0400 (Wed, 20 May 2009)
New Revision: 3248

Modified:
   development/source/classification/cMutationSteps.cc
   development/source/classification/cMutationSteps.h
Log:
Revamp cMutationSteps to reduce memory allocation and working set size under high pressure replication (simple repro) experiments.

Modified: development/source/classification/cMutationSteps.cc
===================================================================
--- development/source/classification/cMutationSteps.cc	2009-05-20 13:09:38 UTC (rev 3247)
+++ development/source/classification/cMutationSteps.cc	2009-05-20 13:53:19 UTC (rev 3248)
@@ -24,72 +24,80 @@
 
 #include "cMutationSteps.h"
 
-cMutationSteps::cMutationSteps(const cMutationSteps& in_ms) : tList<cMutationStep>()
-{
-  tConstListIterator<cMutationStep> mutation_it(in_ms);
-  while (cMutationStep* mut = mutation_it.Next()) {
-    this->PushRear(mut->copy());
-  }
-}
 
-void cMutationSteps::operator=(const cMutationSteps& in_ms)
+void cMutationSteps::AddSubstitutionMutation(int _pos, char _from, char _to)
 {
-  tConstListIterator<cMutationStep> mutation_it(in_ms);
-  while (cMutationStep* mut = mutation_it.Next()) {
-    this->PushRear(mut->copy());
-  }
+  int idx = m_steps.GetSize();
+  m_steps.Resize(idx + 1);
+  m_steps[idx].step_type = sMutStep::SUB_STEP;
+  m_steps[idx].sub.pos = _pos;
+  m_steps[idx].sub.from = _from;
+  m_steps[idx].sub.to = _to;
 }
 
-cMutationSteps::~cMutationSteps()
+void cMutationSteps::AddDeletionMutation(int _pos, char _from)
 {
-  tListIterator<cMutationStep> mutation_it(*this);
-  while (cMutationStep* mut = mutation_it.Next()) {
-    delete mut;
-  }
+  int idx = m_steps.GetSize();
+  m_steps.Resize(idx + 1);
+  m_steps[idx].step_type = sMutStep::DEL_STEP;
+  m_steps[idx].indel.pos = _pos;
+  m_steps[idx].indel.inst = _from;
 }
 
-void cMutationSteps::Clear()
+void cMutationSteps::AddInsertionMutation(int _pos, int _to)
 {
-  tListIterator<cMutationStep> mutation_it(*this);
-  while (cMutationStep* mut = mutation_it.Next()) {
-    delete mut;
-  }
-  tList<cMutationStep>::Clear();
+  int idx = m_steps.GetSize();
+  m_steps.Resize(idx + 1);
+  m_steps[idx].step_type = sMutStep::IN_STEP;
+  m_steps[idx].indel.pos = _pos;
+  m_steps[idx].indel.inst = _to;
 }
 
-void cMutationSteps::AddSubstitutionMutation(int _pos, char _from, char _to)
+void cMutationSteps::AddSlipMutation(int _start, int _end)
 {
-  cMutationStep* mut = new cSubstitutionMutationStep(_pos, _from, _to);
-  this->PushRear(mut);
+  int idx = m_steps.GetSize();
+  m_steps.Resize(idx + 1);
+  m_steps[idx].step_type = sMutStep::SLIP_STEP;
+  m_steps[idx].slip.start = _start;
+  m_steps[idx].slip.end = _end;
 }
 
-void cMutationSteps::AddDeletionMutation(int _pos, char _from)
+cString cMutationSteps::sMutStep::AsString() const
 {
-  cMutationStep* mut = new cDeletionMutationStep(_pos, _from);
-  this->PushRear(mut);
+  cString s;
+  
+  switch (step_type) {
+    case SUB_STEP:
+      s.Set("M%c%i%c", sub.from, sub.pos, sub.to);
+      break;
+    case DEL_STEP:
+      s.Set("D%i%c", indel.pos, indel.inst);
+      break;
+    case IN_STEP:
+      s.Set("I%i%c", indel.pos, indel.inst);
+      break;
+    case SLIP_STEP:
+      s.Set("S%i-%i", slip.start, slip.end);
+      break;
+      
+    default:
+      s = "(unknown";
+      break;
+  }
+  return s;
 }
 
-void cMutationSteps::AddInsertionMutation(int _pos, int _to)
-{
-  cMutationStep* mut = new cInsertionMutationStep(_pos, _to);
-  this->PushRear(mut);
-}
 
-void cMutationSteps::AddSlipMutation(int _start, int _end)
-{
-  cMutationStep* mut = new cSlipMutationStep(_start, _end);
-  this->PushRear(mut);
-}
-
 cString cMutationSteps::AsString() const
 {
   if (m_loaded_string.GetSize() > 0) return m_loaded_string;
 
   cString return_string;
-  tConstListIterator<cMutationStep> mutation_it(*this);
-  while (cMutationStep* mut = mutation_it.Next()) {
-    if (return_string.GetSize() > 0) { return_string += ","; }
-    return_string += mut->AsString();
+  for (int i = 0; i < m_steps.GetSize(); i++) {
+    if (return_string.GetSize() > 0) return_string += ",";
+    return_string += m_steps[i].AsString();
   }
   return return_string;
 }
+
+

Modified: development/source/classification/cMutationSteps.h
===================================================================
--- development/source/classification/cMutationSteps.h	2009-05-20 13:09:38 UTC (rev 3247)
+++ development/source/classification/cMutationSteps.h	2009-05-20 13:53:19 UTC (rev 3248)
@@ -29,85 +29,51 @@
 #include "cString.h"
 #endif
 
-#ifndef tList_h
-#include "tList.h"
+#ifndef tSmartArray_h
+#include "tSmartArray.h"
 #endif
 
 
-class cMutationStep {
-public:
-  virtual ~cMutationStep() {};
-  virtual cMutationStep* copy() const = 0;
-  virtual cString AsString() = 0;  
-};
-
-class cSubstitutionMutationStep : public cMutationStep {
-public:
-  int m_pos;
-  char m_from;
-  char m_to;
-  cSubstitutionMutationStep(const cSubstitutionMutationStep& _in) : cMutationStep(), m_pos(_in.m_pos), m_from(_in.m_from), m_to(_in.m_to) {}
-  void operator=(const cSubstitutionMutationStep& _in) { m_pos=_in.m_pos; m_from=_in.m_from; m_to=_in.m_to; };  
-  cSubstitutionMutationStep(int _pos, char _from, char _to) { m_pos = _pos; m_from = _from;  m_to = _to; }
-  ~cSubstitutionMutationStep() {};
-  cMutationStep* copy() const { return new cSubstitutionMutationStep(*this); }
-  cString AsString() { cString s; s.Set("M%c%i%c", m_from, m_pos, m_to); return s; }
-};
-
-class cDeletionMutationStep : public cMutationStep {
-public:
-  int m_pos;
-  char m_from;
-  cDeletionMutationStep(const cDeletionMutationStep& _in) : cMutationStep(), m_pos(_in.m_pos), m_from(_in.m_from) {}
-  void operator=(const cDeletionMutationStep& _in) { m_pos=_in.m_pos; m_from=_in.m_from; };  
-  cDeletionMutationStep(int _pos, char _from) { m_pos = _pos; m_from = _from; }
-  ~cDeletionMutationStep() {};
-  cMutationStep* copy() const { return new cDeletionMutationStep(*this); }
-  cString AsString() { cString s; s.Set("D%i%c", m_pos, m_from); return s; }
-};
-
-class cInsertionMutationStep : public cMutationStep{
-public:
-  int m_pos;
-  char m_to;
-  cInsertionMutationStep(const cInsertionMutationStep& _in) : cMutationStep(), m_pos(_in.m_pos), m_to(_in.m_to) {}
-  void operator=(const cInsertionMutationStep& _in) { m_pos=_in.m_pos; m_to=_in.m_to; };  
-  cInsertionMutationStep(int _pos, int _to) { m_pos = _pos; m_to = _to; };
-  ~cInsertionMutationStep() {};
-  cMutationStep* copy() const { return new cInsertionMutationStep(*this); }
-  cString AsString() { cString s; s.Set("I%i%c", m_pos, m_to); return s; }
-};
-
-class cSlipMutationStep : public cMutationStep {
-public:
-  int m_start;
-  int m_end;
-  cSlipMutationStep(const cSlipMutationStep& _in) : cMutationStep(), m_start(_in.m_start), m_end(_in.m_end) {}
-  void operator=(const cSlipMutationStep& _in) { m_start=_in.m_start; m_end=_in.m_end; };
-  cSlipMutationStep(int _start, int _end) { m_start = _start; m_end = _end; };
-  ~cSlipMutationStep() {};
-  cMutationStep* copy() const { return new cSlipMutationStep(*this); }
-  cString AsString() { cString s; s.Set("S%i-%i", m_start, m_end); return s; }
-};
-
-
-class cMutationSteps : public tList<cMutationStep> {
+class cMutationSteps
+{
 private:
+  struct sMutStep {
+    enum {SUB_STEP, IN_STEP, DEL_STEP, SLIP_STEP} step_type;
+    union {
+      struct {
+        int pos;
+        char from;
+        char to;
+      } sub;
+      struct {
+        int pos;
+        char inst;
+      } indel;
+      struct {
+        int start;
+        int end;
+      } slip;
+    };
+    cString AsString() const;
+  };
+  
+  tSmartArray<sMutStep> m_steps;
   cString m_loaded_string;
+  
 public:
-  cMutationSteps() {};
-  cMutationSteps(const cMutationSteps& in_ms);
-  void operator=(const cMutationSteps& in_ms);
-  ~cMutationSteps();
-  void Clear();  
+  cMutationSteps() : m_steps(0) { ; }
+  cMutationSteps(const cMutationSteps& in_ms) : m_steps(in_ms.m_steps) { ; }
+  void operator=(const cMutationSteps& in_ms) { m_steps = in_ms.m_steps; }
+  ~cMutationSteps() { ; }
+  void Clear() { m_steps.Resize(0); }  
 
   void AddSubstitutionMutation(int _pos, char _from, char _to);
   void AddDeletionMutation(int _pos, char _from);
   void AddInsertionMutation(int _pos, int _to);
   void AddSlipMutation(int _start, int _end);
 
-  void Set(const cString& in_mut_steps) { m_loaded_string = in_mut_steps; }  
-    // need to implement for reading, right now, we fake it.
+  void Set(const cString& in_mut_steps) { m_loaded_string = in_mut_steps; }
+  // need to implement for reading, right now, we fake it.
   cString AsString() const;
 };
 




More information about the Avida-cvs mailing list