[Avida-SVN] r2926 - in development/source: analyze tools
brysonda at myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Fri Nov 7 11:40:26 PST 2008
Author: brysonda
Date: 2008-11-07 14:40:25 -0500 (Fri, 07 Nov 2008)
New Revision: 2926
Modified:
development/source/analyze/cAnalyze.cc
development/source/analyze/cGenotypeBatch.cc
development/source/analyze/cGenotypeBatch.h
development/source/tools/tList.h
Log:
Reorganize and generalize tListPlus extended methods as embedded templates, add "get" variant.
Expand cGenotypeBatch functionality to support more operations available in analyze mode. Still more to implement.
Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc 2008-11-07 19:26:27 UTC (rev 2925)
+++ development/source/analyze/cAnalyze.cc 2008-11-07 19:40:25 UTC (rev 2926)
@@ -8769,16 +8769,15 @@
cAnalyzeGenotype * found_gen = NULL;
if (gen_desc == "num_cpus")
- found_gen = gen_list.PopIntMax(&cAnalyzeGenotype::GetNumCPUs);
+ found_gen = gen_list.PopMax(&cAnalyzeGenotype::GetNumCPUs);
else if (gen_desc == "total_cpus")
- found_gen = gen_list.PopIntMax(&cAnalyzeGenotype::GetTotalCPUs);
+ found_gen = gen_list.PopMax(&cAnalyzeGenotype::GetTotalCPUs);
else if (gen_desc == "merit")
- found_gen = gen_list.PopDoubleMax(&cAnalyzeGenotype::GetMerit);
+ found_gen = gen_list.PopMax(&cAnalyzeGenotype::GetMerit);
else if (gen_desc == "fitness")
- found_gen = gen_list.PopDoubleMax(&cAnalyzeGenotype::GetFitness);
+ found_gen = gen_list.PopMax(&cAnalyzeGenotype::GetFitness);
else if (gen_desc.IsNumeric(0))
- found_gen = gen_list.PopIntValue(&cAnalyzeGenotype::GetID,
- gen_desc.AsInt());
+ found_gen = gen_list.PopValue(&cAnalyzeGenotype::GetID, gen_desc.AsInt());
else if (gen_desc == "random") {
int gen_pos = random.GetUInt(gen_list.GetSize());
found_gen = gen_list.PopPos(gen_pos);
Modified: development/source/analyze/cGenotypeBatch.cc
===================================================================
--- development/source/analyze/cGenotypeBatch.cc 2008-11-07 19:26:27 UTC (rev 2925)
+++ development/source/analyze/cGenotypeBatch.cc 2008-11-07 19:40:25 UTC (rev 2926)
@@ -27,33 +27,153 @@
#include "cAnalyzeGenotype.h"
#include "cDriverManager.h"
#include "cDriverStatusConduit.h"
+#include "cRandom.h"
+#include "tSmartArray.h"
-cAnalyzeGenotype* cGenotypeBatch::PopGenotype(const cString& desc)
+cAnalyzeGenotype* cGenotypeBatch::FindGenotypeNumCPUs()
{
- cString gen_desc(desc);
- gen_desc.ToLower();
+ return m_list.FindMax(&cAnalyzeGenotype::GetNumCPUs);
+}
+
+cAnalyzeGenotype* cGenotypeBatch::PopGenotypeNumCPUs()
+{
+ clearFlags();
+ return m_list.PopMax(&cAnalyzeGenotype::GetNumCPUs);
+}
+
+
+cAnalyzeGenotype* cGenotypeBatch::FindGenotypeTotalCPUs()
+{
+ return m_list.FindMax(&cAnalyzeGenotype::GetTotalCPUs);
+}
+
+cAnalyzeGenotype* cGenotypeBatch::PopGenotypeTotalCPUs()
+{
+ clearFlags();
+ return m_list.PopMax(&cAnalyzeGenotype::GetTotalCPUs);
+}
+
+
+cAnalyzeGenotype* cGenotypeBatch::FindGenotypeMetabolicRate()
+{
+ return m_list.FindMax(&cAnalyzeGenotype::GetMerit);
+}
+
+cAnalyzeGenotype* cGenotypeBatch::PopGenotypeMetabolicRate()
+{
+ clearFlags();
+ return m_list.PopMax(&cAnalyzeGenotype::GetMerit);
+}
+
+
+cAnalyzeGenotype* cGenotypeBatch::FindGenotypeFitness()
+{
+ return m_list.FindMax(&cAnalyzeGenotype::GetFitness);
+}
+
+cAnalyzeGenotype* cGenotypeBatch::PopGenotypeFitness()
+{
+ clearFlags();
+ return m_list.PopMax(&cAnalyzeGenotype::GetFitness);
+}
+
+
+cAnalyzeGenotype* cGenotypeBatch::FindGenotypeID(int gid)
+{
+ return m_list.FindValue(&cAnalyzeGenotype::GetID, gid);
+}
+
+cAnalyzeGenotype* cGenotypeBatch::PopGenotypeID(int gid)
+{
+ clearFlags();
+ return m_list.PopValue(&cAnalyzeGenotype::GetID, gid);
+}
+
+
+cAnalyzeGenotype* cGenotypeBatch::FindGenotypeRandom(cRandom& rng)
+{
+ if (m_list.GetSize() == 0) return NULL;
- cAnalyzeGenotype* found_gen = NULL;
+ return m_list.GetPos(rng.GetUInt(m_list.GetSize()));
+}
+
+cAnalyzeGenotype* cGenotypeBatch::PopGenotypeRandom(cRandom& rng)
+{
+ if (m_list.GetSize() == 0) return NULL;
+
+ clearFlags();
+ return m_list.PopPos(rng.GetUInt(m_list.GetSize()));
+}
+
+
+cGenotypeBatch* cGenotypeBatch::FindLineage(cAnalyzeGenotype* end_genotype)
+{
+ if ((end_genotype)) return FindLineage(end_genotype->GetID());
- if (gen_desc == "num_cpus")
- found_gen = m_list.PopIntMax(&cAnalyzeGenotype::GetNumCPUs);
- else if (gen_desc == "total_cpus")
- found_gen = m_list.PopIntMax(&cAnalyzeGenotype::GetTotalCPUs);
- else if (gen_desc == "merit")
- found_gen = m_list.PopDoubleMax(&cAnalyzeGenotype::GetMerit);
- else if (gen_desc == "fitness")
- found_gen = m_list.PopDoubleMax(&cAnalyzeGenotype::GetFitness);
- else if (gen_desc.IsNumeric(0))
- found_gen = m_list.PopIntValue(&cAnalyzeGenotype::GetID, gen_desc.AsInt());
-// @TODO - should add support for random, but cGenotypeBatch currently does not have access to the world object
-// else if (gen_desc == "random") {
-// int gen_pos = random.GetUInt(m_list.GetSize());
-// found_gen = m_list.PopPos(gen_pos);
-// }
- else {
- cDriverManager::Status().SignalError(cString("unknown type ") + gen_desc);
+ return new cGenotypeBatch;
+}
+
+cGenotypeBatch* cGenotypeBatch::FindLineage(int end_genotype_id)
+{
+ cGenotypeBatch* batch = new cGenotypeBatch;
+ cAnalyzeGenotype* found_gen = FindGenotypeID(end_genotype_id);
+
+ while ((found_gen)) {
+ batch->m_list.Push(found_gen);
+ found_gen = FindGenotypeID(found_gen->GetParentID());
}
- return found_gen;
+ batch->m_is_lineage = true;
+
+ return batch;
}
+
+
+cGenotypeBatch* cGenotypeBatch::FindClade(cAnalyzeGenotype* start_genotype)
+{
+ if ((start_genotype)) return FindClade(start_genotype->GetID());
+
+ return new cGenotypeBatch;
+}
+
+cGenotypeBatch* cGenotypeBatch::FindClade(int start_genotype_id)
+{
+ cGenotypeBatch* batch = new cGenotypeBatch;
+ tList<cAnalyzeGenotype> list(m_list);
+ tSmartArray<int> scan_list;
+ cAnalyzeGenotype* found_gen = FindGenotypeID(start_genotype_id);
+
+ if ((found_gen)) {
+ batch->m_list.Push(found_gen);
+ scan_list.Push(found_gen->GetID());
+ }
+
+ while (scan_list.GetSize()) {
+ int parent_id = scan_list.Pop();
+
+ // Seach for all of the offspring of this genotype...
+ tListIterator<cAnalyzeGenotype> it(list);
+ while ((found_gen = it.Next()) != NULL) {
+ if (found_gen->GetParentID() == parent_id) {
+ it.Remove();
+ scan_list.Push(found_gen->GetID());
+ batch->m_list.Push(found_gen);
+ }
+ }
+ }
+
+ return batch;
+}
+
+
+void cGenotypeBatch::RemoveClade(cAnalyzeGenotype* start_genotype)
+{
+ if ((start_genotype)) RemoveClade(start_genotype->GetID());
+}
+
+void cGenotypeBatch::RemoveClade(int start_genotype_id)
+{
+ // @TODO - implement remove clade
+}
+
Modified: development/source/analyze/cGenotypeBatch.h
===================================================================
--- development/source/analyze/cGenotypeBatch.h 2008-11-07 19:26:27 UTC (rev 2925)
+++ development/source/analyze/cGenotypeBatch.h 2008-11-07 19:40:25 UTC (rev 2926)
@@ -36,11 +36,14 @@
// cGenotypeBatch : Collection of cAnalyzeGenotypes
class cAnalyzeGenotype;
+class cRandom;
class cGenotypeBatch {
private:
tListPlus<cAnalyzeGenotype> m_list;
cString m_name;
+ cAnalyzeGenotype* m_lineage_head;
+ cAnalyzeGenotype* m_clade_head;
bool m_is_lineage;
bool m_is_aligned;
@@ -48,19 +51,51 @@
cGenotypeBatch& operator=(const cGenotypeBatch&); // @not_implemented
public:
- cGenotypeBatch() : m_name(""), m_is_lineage(false), m_is_aligned(false) { ; }
+ cGenotypeBatch() : m_name(""), m_lineage_head(NULL), m_clade_head(NULL), m_is_lineage(false), m_is_aligned(false) { ; }
~cGenotypeBatch() { ; }
tListPlus<cAnalyzeGenotype>& List() { return m_list; }
cString& Name() { return m_name; }
const cString& GetName() { return m_name; }
- bool IsLineage() { return m_is_lineage; }
+
+ int GetSize() { return m_list.GetSize(); }
+
+ bool IsLineage() { return m_is_lineage || (m_lineage_head); }
+ bool IsClade() { return (m_clade_head); }
bool IsAligned() { return m_is_aligned; }
void SetLineage(bool _val = true) { m_is_lineage = _val; }
void SetAligned(bool _val = true) { m_is_aligned = _val; }
- cAnalyzeGenotype* PopGenotype(const cString& desc);
+ void MergeWith(cGenotypeBatch* batch) { m_list.Append(batch->m_list); }
+
+ cAnalyzeGenotype* FindGenotypeNumCPUs();
+ cAnalyzeGenotype* PopGenotypeNumCPUs();
+ cAnalyzeGenotype* FindGenotypeTotalCPUs();
+ cAnalyzeGenotype* PopGenotypeTotalCPUs();
+ cAnalyzeGenotype* FindGenotypeMetabolicRate();
+ cAnalyzeGenotype* PopGenotypeMetabolicRate();
+ cAnalyzeGenotype* FindGenotypeFitness();
+ cAnalyzeGenotype* PopGenotypeFitness();
+ cAnalyzeGenotype* FindGenotypeID(int gid);
+ cAnalyzeGenotype* PopGenotypeID(int gid);
+ cAnalyzeGenotype* FindGenotypeRandom(cRandom& rng);
+ cAnalyzeGenotype* PopGenotypeRandom(cRandom& rng);
+ inline cAnalyzeGenotype* FindGenotypeRandom(cRandom* rng) { return FindGenotypeRandom(*rng); }
+ inline cAnalyzeGenotype* PopGenotypeRandom(cRandom* rng) { return PopGenotypeRandom(*rng); }
+
+ cGenotypeBatch* FindLineage(cAnalyzeGenotype* end_genotype);
+ cGenotypeBatch* FindLineage(int end_genotype_id);
+
+ cGenotypeBatch* FindClade(cAnalyzeGenotype* start_genotype);
+ cGenotypeBatch* FindClade(int start_genotype_id);
+
+ void RemoveClade(cAnalyzeGenotype* start_genotype);
+ void RemoveClade(int start_genotype_id);
+
+
+private:
+ inline void clearFlags() { m_is_lineage = false; m_is_aligned = false; }
};
Modified: development/source/tools/tList.h
===================================================================
--- development/source/tools/tList.h 2008-11-07 19:26:27 UTC (rev 2925)
+++ development/source/tools/tList.h 2008-11-07 19:40:25 UTC (rev 2926)
@@ -360,9 +360,42 @@
explicit tListPlus(const tList<T>& in_list) : tList<T>(in_list) { ; }
explicit tListPlus(const tListPlus& in_list) : tList<T>(in_list) { ; }
+
+
+ template<typename V> T* FindValue(V (T::*fun)() const, V value)
+ {
+ tListNode<T>* node;
+ if (FindNode(fun, value, node)) return node->data;
+ return NULL;
+ }
+
+ template<typename V> T* PopValue(V (T::*fun)() const, V value)
+ {
+ tListNode<T>* node;
+ if (FindNode(fun, value, node)) return RemoveNode(node);
+ return NULL;
+ }
+
+ template<typename V> T* FindMax(V (T::*fun)() const)
+ {
+ tListNode<T>* node;
+ if (FindMax(fun, node)) return node->data;
+ return NULL;
+ }
+
+ template<typename V> T* PopMax(V (T::*fun)() const)
+ {
+ tListNode<T>* node;
+ if (FindMax(fun, node)) return RemoveNode(node);
+ return NULL;
+ }
+
+
// Find by summing values until a specified total is reached.
T* FindSummedValue(int sum, int (T::*fun)() const)
{
+ if (this->size == 0) return NULL;
+
int total = 0;
tListNode<T>* test = this->root.next;
while (test != &(this->root) && total < sum) {
@@ -372,60 +405,50 @@
return test->data;
}
- T* PopIntValue(int (T::*fun)() const, int value)
+
+ int Count(int (T::*fun)() const)
{
- tListNode<T>* test = this->root.next;
+ int total = 0;
+ tListNode<T> * test = this->root.next;
while (test != &(this->root)) {
- if ( (test->data->*fun)() == value) return RemoveNode(test);
+ total += (test->data->*fun)();
test = test->next;
}
- return NULL;
+ return total;
}
- T* PopIntMax(int (T::*fun)() const)
- {
- if (this->size == 0) return NULL;
- tListNode<T>* test = this->root.next;
- tListNode<T>* best = test;
- int max_val = (test->data->*fun)();
- while (test != &(this->root)) {
- const int cur_val = (test->data->*fun)();
- if ( cur_val > max_val ) {
- max_val = cur_val;
- best = test;
- }
- test = test->next;
+
+private:
+ template<typename V> bool FindNode(V (T::*fun)() const, V value, tListNode<T>*& node)
+ {
+ node = this->root.next;
+ while (node != &(this->root)) {
+ if ((node->data->*fun)() == value) return true;
+ node = node->next;
}
- return RemoveNode(best);
+
+ return false;
}
- T* PopDoubleMax(double (T::*fun)() const)
+
+ template<typename V> bool FindMax(V (T::*fun)() const, tListNode<T>*& best)
{
- if (this->size == 0) return NULL;
+ if (this->size == 0) return false;
+
tListNode<T>* test = this->root.next;
- tListNode<T>* best = test;
- double max_val = (test->data->*fun)();
+ best = test;
+ V max_val = (test->data->*fun)();
while (test != &(this->root)) {
- const double cur_val = (test->data->*fun)();
- if ( cur_val > max_val ) {
+ const V cur_val = (test->data->*fun)();
+ if (cur_val > max_val) {
max_val = cur_val;
best = test;
}
test = test->next;
}
- return RemoveNode(best);
+
+ return true;
}
-
- int Count(int (T::*fun)() const)
- {
- int total = 0;
- tListNode<T> * test = this->root.next;
- while (test != &(this->root)) {
- total += (test->data->*fun)();
- test = test->next;
- }
- return total;
- }
};
More information about the Avida-cvs
mailing list