[Avida-cvs] [Avida2-svn] r329 - in trunk: Avida2.xcodeproj source/cpu source/support source/tools
brysonda@myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Mon Oct 3 19:37:33 PDT 2005
Author: brysonda
Date: 2005-10-03 22:37:32 -0400 (Mon, 03 Oct 2005)
New Revision: 329
Modified:
trunk/Avida2.xcodeproj/project.pbxproj
trunk/source/cpu/cHardwareSMT.cc
trunk/source/cpu/cHardwareSMT.h
trunk/source/support/genesis.4stack
trunk/source/support/genesis.smt
trunk/source/tools/tDictionary.hh
trunk/source/tools/tHashTable.hh
trunk/source/tools/tList.hh
Log:
Fixes to SMT, fix memory leaks in tDictionary and tHashTable.
Modified: trunk/Avida2.xcodeproj/project.pbxproj
===================================================================
--- trunk/Avida2.xcodeproj/project.pbxproj 2005-09-28 23:37:58 UTC (rev 328)
+++ trunk/Avida2.xcodeproj/project.pbxproj 2005-10-04 02:37:32 UTC (rev 329)
@@ -178,7 +178,6 @@
70C1F03708C3C71300F50912 /* cHeadMultiMem.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70C1F02708C3C71300F50912 /* cHeadMultiMem.cc */; };
70C1F03808C3C71300F50912 /* cTestCPU.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70C1F02808C3C71300F50912 /* cTestCPU.cc */; };
70C1F03908C3C71300F50912 /* cTestUtil.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70C1F02908C3C71300F50912 /* cTestUtil.cc */; };
- 70C1F10108C40BEF00F50912 /* nHardware.pyste in CopyFiles */ = {isa = PBXBuildFile; fileRef = 70C1F10008C40BEF00F50912 /* nHardware.pyste */; };
70C1F19308C6A11100F50912 /* cEventFactoryManager.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70C1F18E08C6A11100F50912 /* cEventFactoryManager.cc */; };
70C1F19408C6A11100F50912 /* cEventList.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70C1F18F08C6A11100F50912 /* cEventList.cc */; };
70C1F19508C6A11100F50912 /* cEventListIterator.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70C1F19008C6A11100F50912 /* cEventListIterator.cc */; };
@@ -225,8 +224,6 @@
70D46934085F61DA004C8409 /* trio.c in Sources */ = {isa = PBXBuildFile; fileRef = DCC3146D076253A1008F7A48 /* trio.c */; };
70D46935085F61DD004C8409 /* trionan.c in Sources */ = {isa = PBXBuildFile; fileRef = DCC31471076253A1008F7A48 /* trionan.c */; };
70D46936085F61DF004C8409 /* triostr.c in Sources */ = {isa = PBXBuildFile; fileRef = DCC31474076253A1008F7A48 /* triostr.c */; };
- 70FB868708BFA02D00BDF589 /* tHashTable.hh in CopyFiles */ = {isa = PBXBuildFile; fileRef = 70FB868608BFA02D00BDF589 /* tHashTable.hh */; };
- 70FB868808BFA02D00BDF589 /* tHashTable.hh in CopyFiles */ = {isa = PBXBuildFile; fileRef = 70FB868608BFA02D00BDF589 /* tHashTable.hh */; };
DCC3166107628531008F7A48 /* avida.cc in Sources */ = {isa = PBXBuildFile; fileRef = DCC3109C0762539E008F7A48 /* avida.cc */; };
DCC3167A07628567008F7A48 /* landscape.cc in Sources */ = {isa = PBXBuildFile; fileRef = DCC310E80762539E008F7A48 /* landscape.cc */; };
DCC3167B07628568008F7A48 /* lineage.cc in Sources */ = {isa = PBXBuildFile; fileRef = DCC310EA0762539E008F7A48 /* lineage.cc */; };
@@ -344,8 +341,6 @@
700E2B7B085DE50C00CF158A /* organism.smt in CopyFiles */,
700E2B7C085DE50C00CF158A /* genesis.smt in CopyFiles */,
700E2B7D085DE50C00CF158A /* inst_set.smt in CopyFiles */,
- 70FB868808BFA02D00BDF589 /* tHashTable.hh in CopyFiles */,
- 70C1F10108C40BEF00F50912 /* nHardware.pyste in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -370,7 +365,6 @@
706D330F0854A7B900D7DC8F /* organism.smt in CopyFiles */,
706D33110854A7D700D7DC8F /* genesis.smt in CopyFiles */,
706D33280854A90D00D7DC8F /* inst_set.smt in CopyFiles */,
- 70FB868708BFA02D00BDF589 /* tHashTable.hh in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -680,16 +674,12 @@
70CD47BD089692210070D2DF /* UnitTest_pyEduMainCtrl.py */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.script.python; path = UnitTest_pyEduMainCtrl.py; sourceTree = "<group>"; };
70CD47BE089692210070D2DF /* UnitTest_pyTestCase.py */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.script.python; path = UnitTest_pyTestCase.py; sourceTree = "<group>"; };
70CD47BF089692210070D2DF /* UnitTest_pyUnitTestSuiteRecurser.py */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.script.python; path = UnitTest_pyUnitTestSuiteRecurser.py; sourceTree = "<group>"; };
- 70CD47C0089692210070D2DF /* untar_in_resource_directory.tar */ = {isa = PBXFileReference; lastKnownFileType = archive.tar; path = untar_in_resource_directory.tar; sourceTree = "<group>"; };
70CD47C1089692210070D2DF /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
- 70CD47C3089692210070D2DF /* average.dat */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = average.dat; sourceTree = "<group>"; };
- 70CD47C4089692210070D2DF /* count.dat */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = count.dat; sourceTree = "<group>"; };
70CD47C5089692210070D2DF /* environment.default */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = environment.default; sourceTree = "<group>"; };
70CD47C6089692210070D2DF /* events.default */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = events.default; sourceTree = "<group>"; };
70CD47C8089692210070D2DF /* default.empty */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = default.empty; sourceTree = "<group>"; };
70CD47C9089692210070D2DF /* default.organism */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = default.organism; sourceTree = "<group>"; };
70CD47CA089692210070D2DF /* no_mutations.empty */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = no_mutations.empty; sourceTree = "<group>"; };
- 70CD47CC089692210070D2DF /* petri_dish */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = petri_dish; sourceTree = "<group>"; };
70CD47CD089692210070D2DF /* genesis.default */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = genesis.default; sourceTree = "<group>"; };
70CD47CE089692210070D2DF /* inst_set.default */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = inst_set.default; sourceTree = "<group>"; };
70CD47CF089692210070D2DF /* pmock.py */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.script.python; path = pmock.py; sourceTree = "<group>"; };
@@ -1633,7 +1623,6 @@
70CD47BD089692210070D2DF /* UnitTest_pyEduMainCtrl.py */,
70CD47BE089692210070D2DF /* UnitTest_pyTestCase.py */,
70CD47BF089692210070D2DF /* UnitTest_pyUnitTestSuiteRecurser.py */,
- 70CD47C0089692210070D2DF /* untar_in_resource_directory.tar */,
);
path = AvidaGui2;
sourceTree = "<group>";
@@ -1641,8 +1630,6 @@
70CD47C2089692210070D2DF /* default.workspace */ = {
isa = PBXGroup;
children = (
- 70CD47C3089692210070D2DF /* average.dat */,
- 70CD47C4089692210070D2DF /* count.dat */,
70CD47C5089692210070D2DF /* environment.default */,
70CD47C6089692210070D2DF /* events.default */,
70CD47C7089692210070D2DF /* freezer */,
@@ -1666,7 +1653,6 @@
70CD47CB089692210070D2DF /* Update200.full */ = {
isa = PBXGroup;
children = (
- 70CD47CC089692210070D2DF /* petri_dish */,
);
path = Update200.full;
sourceTree = "<group>";
Modified: trunk/source/cpu/cHardwareSMT.cc
===================================================================
--- trunk/source/cpu/cHardwareSMT.cc 2005-09-28 23:37:58 UTC (rev 328)
+++ trunk/source/cpu/cHardwareSMT.cc 2005-10-04 02:37:32 UTC (rev 329)
@@ -143,36 +143,17 @@
m_functions = s_inst_slib->GetFunctions();
m_mem_array[0] = in_organism->GetGenome(); // Initialize memory...
- m_mem_array[0].Resize(GetMemory(0).GetSize() + 1);
+ m_mem_array[0].Resize(m_mem_array[0].GetSize() + 1);
m_mem_array[0][m_mem_array[0].GetSize() - 1] = cInstruction();
Reset(); // Setup the rest of the hardware...
}
-
-cHardwareSMT::cHardwareSMT(const cHardwareSMT& hardware)
-: cHardwareBase(hardware.organism, hardware.inst_set)
-, m_functions(hardware.m_functions)
-, m_mem_array(hardware.m_mem_array)
-, m_threads(hardware.m_threads)
-, thread_id_chart(hardware.thread_id_chart)
-, m_cur_thread(hardware.m_cur_thread)
-, inst_cost(hardware.inst_cost)
-#ifdef INSTRUCTION_COSTS
-, inst_ft_cost(hardware.inst_ft_cost)
-#endif
-{
- for(int i = 0; i < nHardwareSMT::NUM_GLOBAL_STACKS; i++) {
- m_global_stacks[i] = hardware.m_global_stacks[i];
- }
-}
-
-
void cHardwareSMT::Recycle(cOrganism * new_organism, cInstSet * in_inst_set)
{
cHardwareBase::Recycle(new_organism, in_inst_set);
m_mem_array[0] = new_organism->GetGenome();
- m_mem_array[0].Resize(GetMemory(0).GetSize() + 1);
- m_mem_array[0][m_mem_array[0].GetSize()-1] = cInstruction();
+ m_mem_array[0].Resize(m_mem_array[0].GetSize() + 1);
+ m_mem_array[0][m_mem_array[0].GetSize() - 1] = cInstruction();
Reset();
}
@@ -181,6 +162,7 @@
{
// Setup the memory...
m_mem_array.Resize(1);
+ m_mem_lbls.ClearAll();
// We want to reset to have a single thread.
m_threads.Resize(1);
@@ -356,19 +338,17 @@
bool cHardwareSMT::OK()
{
- bool result = true;
-
for(int i = 0 ; i < m_mem_array.GetSize(); i++) {
- if (!m_mem_array[i].OK()) result = false;
+ if (!m_mem_array[i].OK()) return false;
}
for (int i = 0; i < GetNumThreads(); i++) {
for(int j=0; j < nHardwareSMT::NUM_LOCAL_STACKS; j++)
- if (m_threads[i].local_stacks[j].OK() == false) result = false;
- if (m_threads[i].next_label.OK() == false) result = false;
+ if (m_threads[i].local_stacks[j].OK() == false) return false;
+ if (m_threads[i].next_label.OK() == false) return false;
}
- return result;
+ return true;
}
void cHardwareSMT::PrintStatus(ostream & fp)
@@ -400,7 +380,7 @@
<< endl;
for (int i = 0; i < m_mem_array.GetSize(); i++) {
- const cCPUMemory& mem = GetMemory(i);
+ const cCPUMemory& mem = m_mem_array[i];
fp << " Mem " << i << " (" << mem.GetSize() << "): " << mem.AsString() << endl;
}
@@ -432,7 +412,6 @@
// results. If direction is 0, search from the beginning of the genome.
//
/////////////////////////////////////////////////////////////////////////
-
cHeadMultiMem cHardwareSMT::FindLabel(int direction)
{
cHeadMultiMem& inst_ptr = IP();
@@ -476,9 +455,8 @@
// Search forwards for search_label from _after_ position pos in the
// memory. Return the first line _after_ the the found label. It is okay
// to find search label's match inside another label.
-
-int cHardwareSMT::FindLabel_Forward(const cCodeLabel & search_label,
- const cGenome & search_genome, int pos)
+int cHardwareSMT::FindLabel_Forward(const cCodeLabel& search_label,
+ const cGenome& search_genome, int pos)
{
assert (pos < search_genome.GetSize() && pos >= 0);
@@ -558,7 +536,6 @@
// Search backwards for search_label from _before_ position pos in the
// memory. Return the first line _after_ the the found label. It is okay
// to find search label's match inside another label.
-
int cHardwareSMT::FindLabel_Backward(const cCodeLabel & search_label,
const cGenome & search_genome, int pos)
{
@@ -634,7 +611,7 @@
}
// Search for 'in_label' anywhere in the hardware.
-cHeadMultiMem cHardwareSMT::FindLabel(const cCodeLabel & in_label, int direction)
+cHeadMultiMem cHardwareSMT::FindLabel(const cCodeLabel& in_label, int direction)
{
assert (in_label.GetSize() > 0);
@@ -674,9 +651,6 @@
// direction which the heads[nHardware::HEAD_IP] should progress through a creature.
cHeadMultiMem cHardwareSMT::FindFullLabel(const cCodeLabel & in_label)
{
- // cout << "Running FindFullLabel with " << in_label.AsString() <<
- // endl;
-
assert(in_label.GetSize() > 0); // Trying to find label of 0 size!
cHeadMultiMem temp_head(this);
@@ -689,13 +663,11 @@
}
// Otherwise, rewind to the begining of this label...
-
while (!(temp_head.AtFront()) && inst_set->IsNop(temp_head.GetInst(-1)))
temp_head.AbsJump(-1);
// Calculate the size of the label being checked, and make sure they
- // are equal.
-
+ // are equal.
int checked_size = 0;
while (inst_set->IsNop(temp_head.GetInst(checked_size))) {
checked_size++;
@@ -704,12 +676,8 @@
temp_head.AbsJump(checked_size + 1);
continue;
}
-
- // cout << "Testing label at line " << temp_head.GetPosition() <<
- // endl;
-
+
// ...and do the comparison...
-
int j;
bool label_match = true;
for (j = 0; j < in_label.GetSize(); j++) {
@@ -752,20 +720,20 @@
}
if (end_pos < MIN_INJECT_SIZE) {
- GetMemory(mem_space_used) = cGenome('a');
+ m_mem_array[mem_space_used] = cGenome("a");
Fault(FAULT_LOC_INJECT, FAULT_TYPE_ERROR, "inject: new size too small");
return false; // (inject fails)
}
- GetMemory(mem_space_used).Resize(end_pos);
+ m_mem_array[mem_space_used].Resize(end_pos);
- cCPUMemory injected_code = GetMemory(mem_space_used);
+ cCPUMemory injected_code = m_mem_array[mem_space_used];
Inject_DoMutations(mut_multiplier, injected_code);
int inject_signal = false;
- if(injected_code.GetSize()>0)
+ if(injected_code.GetSize() > 0)
inject_signal = organism->InjectParasite(injected_code);
//************* CALL GOES HERE ******************//
@@ -786,7 +754,7 @@
//************** CALL ENDS HERE ******************//
//reset the memory space which was injected
- GetMemory(mem_space_used) = cGenome('a');
+ m_mem_array[mem_space_used] = cGenome("a");
for(int x=0; x<nHardware::NUM_HEADS; x++) {
GetHead(x).Reset(IP().GetMemSpace(), this);
@@ -804,13 +772,14 @@
//This is the code run by the TARGET of an injection. This RECIEVES the infection.
bool cHardwareSMT::InjectHost(const cCodeLabel & in_label, const cGenome & inject_code)
{
- // Make sure the genome will be below max size after injection.
-
- // xxxTEMPORARYxxx - we should have this match injection templates. For now it simply
+ // DDD - Need to discuss how InjectHost should work with extensible memory spaces...
+ return false;
+ // Make sure the genome will be below max size after injection.
+ // xxxTEMPORARYxxx - we should have this match injection templates. For now it simply
// FIND THE FIRST EMPTY MEMORY SPACE
- int target_mem_space;
- for (target_mem_space = 0; target_mem_space < m_mem_array.GetSize(); target_mem_space++)
+ int target_mem_space = 0;
+ for (; target_mem_space < m_mem_array.GetSize(); target_mem_space++)
{
if(isEmpty(target_mem_space))
{
@@ -827,14 +796,14 @@
if(ForkThread()) {
// Inject the new code
- cCPUMemory oldcode = GetMemory(target_mem_space);
- GetMemory(target_mem_space) = inject_code;
- GetMemory(target_mem_space).Resize(inject_code.GetSize() + oldcode.GetSize());
+ cCPUMemory oldcode = m_mem_array[target_mem_space];
+ m_mem_array[target_mem_space] = inject_code;
+ m_mem_array[target_mem_space].Resize(inject_code.GetSize() + oldcode.GetSize());
// Copies previous instructions to the end of the injected code.
// Is there a faster way to do this?? -law
for(int x=0; x<oldcode.GetSize(); x++)
- GetMemory(target_mem_space)[inject_code.GetSize()+x] = oldcode[x];
+ m_mem_array[target_mem_space][inject_code.GetSize()+x] = oldcode[x];
// Set instruction flags on the injected code
for (int i = 0; i < inject_code.GetSize(); i++) {
@@ -867,22 +836,20 @@
void cHardwareSMT::Mutate(int mut_point)
{
// Test if trying to mutate outside of genome...
- assert(mut_point >= 0 && mut_point < GetMemory(0).GetSize());
+ assert(mut_point >= 0 && mut_point < m_mem_array[0].GetSize());
- GetMemory(0)[mut_point] = GetRandomInst();
- GetMemory(0).FlagMutated(mut_point) = true;
- GetMemory(0).FlagPointMut(mut_point) = true;
- //organism->GetPhenotype().IsMutated() = true;
+ m_mem_array[0][mut_point] = GetRandomInst();
+ m_mem_array[0].FlagMutated(mut_point) = true;
+ m_mem_array[0].FlagPointMut(mut_point) = true;
organism->CPUStats().mut_stats.point_mut_count++;
}
int cHardwareSMT::PointMutate(const double mut_rate)
{
- const int num_muts =
- g_random.GetRandBinomial(GetMemory(0).GetSize(), mut_rate);
+ const int num_muts = g_random.GetRandBinomial(m_mem_array[0].GetSize(), mut_rate);
for (int i = 0; i < num_muts; i++) {
- const int pos = g_random.GetUInt(GetMemory(0).GetSize());
+ const int pos = g_random.GetUInt(m_mem_array[0].GetSize());
Mutate(pos);
}
@@ -904,10 +871,10 @@
return TriggerMutations(trigger, IP());
}
-bool cHardwareSMT::TriggerMutations(int trigger, cHeadMultiMem & cur_head)
+bool cHardwareSMT::TriggerMutations(int trigger, cHeadMultiMem& cur_head)
{
// Collect information about mutations from the organism.
- cLocalMutations & mut_info = organism->GetLocalMutations();
+ cLocalMutations& mut_info = organism->GetLocalMutations();
const tList<cMutation> & mut_list =
mut_info.GetMutationLib().GetMutationList(trigger);
@@ -917,7 +884,7 @@
// Determine what memory this mutation will be affecting.
cCPUMemory & target_mem = (trigger == MUTATION_TRIGGER_DIVIDE)
- ? organism->ChildGenome() : GetMemory(0);
+ ? organism->ChildGenome() : m_mem_array[0];
// Loop through all mutations associated with this trigger and test them.
tConstListIterator<cMutation> mut_it(mut_list);
@@ -1224,7 +1191,7 @@
{
// Make sure the organism is okay with dividing now...
if (organism->Divide_CheckViable() == false) return false; // (divide fails)
-
+
// Make sure that neither parent nor child will be below the minimum size.
const int genome_size = organism->GetGenome().GetSize();
@@ -1243,7 +1210,7 @@
int executed_size = 0;
for (int i = 0; i < parent_size; i++) {
- if (GetMemory(0).FlagExecuted(i)) executed_size++;
+ if (m_mem_array[0].FlagExecuted(i)) executed_size++;
}
const int min_exe_lines = (int) (parent_size * cConfig::GetMinExeLines());
@@ -1258,11 +1225,11 @@
// sure the specified fraction has been reached.
int copied_size = 0;
- for (int i = 0; i < GetMemory(mem_space).GetSize(); i++) {
- if (GetMemory(mem_space).FlagCopied(i)) copied_size++;
+ for (int i = 0; i < m_mem_array[mem_space].GetSize(); i++) {
+ if (m_mem_array[mem_space].FlagCopied(i)) copied_size++;
}
- const int min_copied = (int) (child_size * cConfig::GetMinCopiedLines());
+ const int min_copied = static_cast<int>(child_size * cConfig::GetMinCopiedLines());
if (copied_size < min_copied) {
Fault(FAULT_LOC_DIVIDE, FAULT_TYPE_ERROR,
cStringUtil::Stringf("Too few copied commands (%d < %d)",
@@ -1279,8 +1246,8 @@
void cHardwareSMT::Divide_DoMutations(double mut_multiplier)
{
- sCPUStats & cpu_stats = organism->CPUStats();
- cCPUMemory & child_genome = organism->ChildGenome();
+ sCPUStats& cpu_stats = organism->CPUStats();
+ cCPUMemory& child_genome = organism->ChildGenome();
organism->GetPhenotype().SetDivType(mut_multiplier);
@@ -1367,9 +1334,9 @@
// Mutations in the parent's genome
if (organism->GetParentMutProb() > 0) {
- for (int i = 0; i < GetMemory(0).GetSize(); i++) {
+ for (int i = 0; i < m_mem_array[0].GetSize(); i++) {
if (organism->TestParentMut()) {
- GetMemory(0)[i] = GetRandomInst();
+ m_mem_array[0][i] = GetRandomInst();
cpu_stats.mut_stats.parent_mut_line_count++;
}
}
@@ -1377,8 +1344,8 @@
// Count up mutated lines
- for(int i = 0; i < GetMemory(0).GetSize(); i++){
- if (GetMemory(0).FlagPointMut(i) == true) {
+ for(int i = 0; i < m_mem_array[0].GetSize(); i++){
+ if (m_mem_array[0].FlagPointMut(i) == true) {
cpu_stats.mut_stats.point_mut_line_count++;
}
}
@@ -1391,31 +1358,24 @@
void cHardwareSMT::Inject_DoMutations(double mut_multiplier, cCPUMemory & injected_code)
{
- //sCPUStats & cpu_stats = organism->CPUStats();
- //cCPUMemory & child_genome = organism->ChildGenome();
-
organism->GetPhenotype().SetDivType(mut_multiplier);
// Divide Mutations
if (organism->TestDivideMut()) {
const UINT mut_line = g_random.GetUInt(injected_code.GetSize());
injected_code[mut_line] = GetRandomInst();
- //cpu_stats.mut_stats.divide_mut_count++;
}
// Divide Insertions
if (organism->TestDivideIns() && injected_code.GetSize() < MAX_CREATURE_SIZE){
const UINT mut_line = g_random.GetUInt(injected_code.GetSize() + 1);
injected_code.Insert(mut_line, GetRandomInst());
- //cpu_stats.mut_stats.divide_insert_mut_count++;
}
// Divide Deletions
if (organism->TestDivideDel() && injected_code.GetSize() > MIN_CREATURE_SIZE){
const UINT mut_line = g_random.GetUInt(injected_code.GetSize());
- // if( injected_code.FlagCopied(mut_line) == true) copied_size_change--;
injected_code.Remove(mut_line);
- //cpu_stats.mut_stats.divide_delete_mut_count++;
}
// Divide Mutations (per site)
@@ -1427,7 +1387,6 @@
for (int i = 0; i < num_mut; i++) {
int site = g_random.GetUInt(injected_code.GetSize());
injected_code[site]=GetRandomInst();
- //cpu_stats.mut_stats.div_mut_count++;
}
}
}
@@ -1453,7 +1412,6 @@
// Actually do the mutations (in reverse sort order)
for(int i = num_mut-1; i >= 0; i--) {
injected_code.Insert(mut_sites[i], GetRandomInst());
- //cpu_stats.mut_stats.insert_mut_count++;
}
}
}
@@ -1471,34 +1429,19 @@
// If we have lines to delete...
for (int i = 0; i < num_mut; i++) {
int site = g_random.GetUInt(injected_code.GetSize());
- // if (injected_code.FlagCopied(site) == true) copied_size_change--;
injected_code.Remove(site);
- //cpu_stats.mut_stats.delete_mut_count++;
}
}
// Mutations in the parent's genome
if (organism->GetParentMutProb() > 0) {
- for (int i = 0; i < GetMemory(0).GetSize(); i++) {
+ for (int i = 0; i < m_mem_array[0].GetSize(); i++) {
if (organism->TestParentMut()) {
- GetMemory(0)[i] = GetRandomInst();
- //cpu_stats.mut_stats.parent_mut_line_count++;
+ m_mem_array[0][i] = GetRandomInst();
}
}
}
- /*
- // Count up mutated lines
- for(int i = 0; i < GetMemory(0).GetSize(); i++){
- if (GetMemory(0).FlagPointMut(i) == true) {
- cpu_stats.mut_stats.point_mut_line_count++;
- }
- }
- for(int i = 0; i < injected_code.GetSize(); i++){
- if( injected_code.FlagCopyMut(i) == true) {
- cpu_stats.mut_stats.copy_mut_line_count++;
- }
- }*/
}
@@ -1567,19 +1510,19 @@
int write_head_pos = GetHead(nHardware::HEAD_WRITE).GetPosition();
// DDD - change to allow ???
- // We're going to disallow division calls from memory spaces other than zero
- // for right now -law
+ // We're going to disallow division calls from memory spaces other than zero for right now @law
if(IP().GetMemSpace() != 0) return false;
+
+ // Make sure the memory space we're using exists
+ if (m_mem_array.GetSize() <= mem_space_used) return false;
// Make sure this divide will produce a viable offspring.
- if(!Divide_CheckViable(GetMemory(IP().GetMemSpace()).GetSize(), write_head_pos, mem_space_used))
+ if(!Divide_CheckViable(m_mem_array[IP().GetMemSpace()].GetSize(), write_head_pos, mem_space_used))
return false;
- // Since the divide will now succeed, set up the information to be sent
- // to the new organism
- cGenome & child_genome = organism->ChildGenome();
- GetMemory(mem_space_used).Resize(write_head_pos);
- child_genome = GetMemory(mem_space_used);
+ // Since the divide will now succeed, set up the information to be sent to the new organism
+ m_mem_array[mem_space_used].Resize(write_head_pos);
+ organism->ChildGenome() = m_mem_array[mem_space_used];
// Handle Divide Mutations...
Divide_DoMutations(mut_multiplier);
@@ -1599,7 +1542,7 @@
bool parent_alive = organism->ActivateDivide();
//reset the memory of the memory space that has been divided off
- GetMemory(mem_space_used) = cGenome('a');
+ m_mem_array[mem_space_used] = cGenome("a");
// 3 Division Methods:
// 1) DIVIDE_METHOD_OFFSPRING - Create a child, leave parent state untouched.
@@ -1616,31 +1559,23 @@
}
else if(cConfig::GetDivideMethod() == DIVIDE_METHOD_BIRTH)
{
- //if this isn't the only thread, get rid of it!
- // ***this can cause a concurrency problem if we have
- // multiprocessor support for single organisms...don't
- // think that's happening anytime soon though -law ***
- if(!organism->GetPhenotype().IsModified() && GetNumThreads()>1 ||
- GetNumThreads()>2)
- {
+ if(!organism->GetPhenotype().IsModified() && GetNumThreads() > 1 || GetNumThreads() > 2) {
+ //if this isn't the only thread, get rid of it!
+ // *** this can cause a concurrency problem if we have multiprocessor support for single
+ // *** organisms...don't think that's happening anytime soon though @law
KillThread();
- }
-
- //this will reset the current thread's heads and stacks. It will
- //not touch any other m_threads or memory spaces (ie: parasites)
- else
- {
- for(int x = 0; x < nHardware::NUM_HEADS; x++)
- {
+ } else {
+ //this will reset the current thread's heads and stacks. It will
+ //not touch any other m_threads or memory spaces (ie: parasites)
+ for(int x = 0; x < nHardware::NUM_HEADS; x++) {
GetHead(x).Reset(0, this);
}
- for(int x = 0; x < nHardwareSMT::NUM_LOCAL_STACKS; x++)
- {
+ for(int x = 0; x < nHardwareSMT::NUM_LOCAL_STACKS; x++) {
Stack(x).Clear();
}
}
}
- AdvanceIP()=false;
+ AdvanceIP() = false;
}
return true;
@@ -1788,10 +1723,10 @@
cHeadMultiMem & active_head = GetHead(head_id);
int mem_space_used = active_head.GetMemSpace();
- if(active_head.GetPosition()>=GetMemory(mem_space_used).GetSize()-1)
+ if(active_head.GetPosition() >= m_mem_array[mem_space_used].GetSize() - 1)
{
- GetMemory(mem_space_used).Resize(GetMemory(mem_space_used).GetSize()+1);
- GetMemory(mem_space_used).Copy(GetMemory(mem_space_used).GetSize()-1, GetMemory(mem_space_used).GetSize()-2);
+ m_mem_array[mem_space_used].Resize(m_mem_array[mem_space_used].GetSize() + 1);
+ m_mem_array[mem_space_used].Copy(m_mem_array[mem_space_used].GetSize() - 1, m_mem_array[mem_space_used].GetSize() - 2);
}
active_head.Adjust();
@@ -1884,10 +1819,6 @@
{
const int head_used = FindModifiedHead(nHardware::HEAD_IP);
Stack(nHardwareSMT::STACK_BX).Push(GetHead(head_used).GetPosition());
- //if (head_used == nHardware::HEAD_IP) {
- // GetHead(head_used).Set(GetHead(nHardware::HEAD_FLOW));
- // AdvanceIP() = false;
- //}
return true;
}
@@ -1922,12 +1853,11 @@
ReadLabel();
GetLabel().Rotate(2, nHardwareSMT::NUM_NOPS);
cHeadMultiMem found_pos = FindLabel(0);
- if(found_pos.GetPosition()-IP().GetPosition()==0)
+ if(found_pos.GetPosition() - IP().GetPosition() == 0)
{
- GetHead(nHardware::HEAD_FLOW).Set(IP().GetPosition()+1, IP().GetMemSpace(), this);
+ GetHead(nHardware::HEAD_FLOW).Set(IP().GetPosition() + 1, IP().GetMemSpace(), this);
// pushing zero into STACK_AX on a missed search makes it difficult to create
- // a self-replicating organism. -law
- //Stack(STACK_AX).Push(0);
+ // a self-replicating organism. @law
Stack(nHardwareSMT::STACK_BX).Push(0);
}
else
@@ -1945,7 +1875,7 @@
bool cHardwareSMT::Inst_PushNext()
{
int stack_used = FindModifiedStack(nHardwareSMT::STACK_AX);
- int successor = (stack_used+1) % nHardwareSMT::NUM_STACKS;
+ int successor = (stack_used + 1) % nHardwareSMT::NUM_STACKS;
Stack(successor).Push(Stack(stack_used).Pop());
return true;
}
@@ -2063,39 +1993,43 @@
int cHardwareSMT::FindFirstEmpty()
{
- bool OK=true;
+ bool OK = true;
const int current_mem_space = IP().GetMemSpace();
for(int x = 1; x < m_mem_array.GetSize(); x++)
{
- OK=true;
+ OK = true;
int index = (current_mem_space + x) % m_mem_array.GetSize();
- for(int y=0; y<GetMemory(index).GetSize() && OK; y++)
- {
- if(GetMemory(index)[y].GetOp() >= nHardwareSMT::NUM_NOPS)
- OK=false;
+ for(int y = 0; y < m_mem_array[index].GetSize(); y++) {
+ if(m_mem_array[index][y].GetOp() >= nHardwareSMT::NUM_NOPS) {
+ OK = false;
+ break;
+ }
}
- for(int y=0; y<GetNumThreads() && OK; y++)
- {
- for(int z=0; z<nHardware::NUM_HEADS; z++)
- {
- if(m_threads[y].heads[z].GetMemSpace() == index)
- OK=false;
+ if (!OK) break;
+
+ for(int y = 0; y < GetNumThreads(); y++) {
+ for(int z=0; z<nHardware::NUM_HEADS; z++) {
+ if(m_threads[y].heads[z].GetMemSpace() == index) {
+ OK = false;
+ break;
+ }
}
}
- if(OK)
- return index;
+
+ if(OK) return index;
}
+
return -1;
}
bool cHardwareSMT::isEmpty(int mem_space_used)
{
- for(int x=0; x<GetMemory(mem_space_used).GetSize(); x++)
- {
- if(GetMemory(mem_space_used)[x].GetOp() >= nHardwareSMT::NUM_NOPS)
+ // DDD - shouldn't this just be return false if GetSize() != 1
+ for(int x = 0; x < m_mem_array[mem_space_used].GetSize(); x++) {
+ if(m_mem_array[mem_space_used][x].GetOp() >= nHardwareSMT::NUM_NOPS)
return false;
}
return true;
@@ -2115,17 +2049,3 @@
return InjectParasite(mut_multiplier);
}
-
-
-
-/*
- bool cHardwareSMT::Inst_InjectRand()
- {
- // Rotate to a random facing and then run the normal inject instruction
- const int num_neighbors = organism->GetNeighborhoodSize();
- organism->Rotate(g_random.GetUInt(num_neighbors));
- Inst_Inject();
- return true;
- }
-
- */
Modified: trunk/source/cpu/cHardwareSMT.h
===================================================================
--- trunk/source/cpu/cHardwareSMT.h 2005-09-28 23:37:58 UTC (rev 328)
+++ trunk/source/cpu/cHardwareSMT.h 2005-10-04 02:37:32 UTC (rev 329)
@@ -91,10 +91,11 @@
tArray<int> inst_cost;
tArray<int> inst_ft_cost;
#endif
-
+
+ cHardwareSMT(const cHardwareSMT &); // disabled... can't (easily) copy m_mem_lbls @dmb
+
public:
cHardwareSMT(cOrganism * in_organism, cInstSet * in_inst_set);
- explicit cHardwareSMT(const cHardwareSMT &);
~cHardwareSMT() { ; }
void Recycle(cOrganism* new_organism, cInstSet * in_inst_set);
static cInstLibBase* GetInstLib();
@@ -106,7 +107,7 @@
bool SingleProcess_PayCosts(const cInstruction & cur_inst);
bool SingleProcess_ExecuteInst(const cInstruction & cur_inst);
void ProcessBonusInst(const cInstruction & inst);
- void LoadGenome(const cGenome& new_genome) { GetMemory(0) = new_genome; }
+ void LoadGenome(const cGenome& new_genome) { m_mem_array[0] = new_genome; }
// -------- Helper methods --------
bool OK();
Modified: trunk/source/support/genesis.4stack
===================================================================
--- trunk/source/support/genesis.4stack 2005-09-28 23:37:58 UTC (rev 328)
+++ trunk/source/support/genesis.4stack 2005-10-04 02:37:32 UTC (rev 329)
@@ -36,7 +36,7 @@
DEATH_METHOD 0 # 0 = Never die of old age.
# 1 = Die when inst executed = AGE_LIMIT (with deviation)
# 2 = Die when inst executed = length * AGE_LIMIT (+ dev.)
-AGE_LIMIT 5000 # Modifies DEATH_METHOD
+AGE_LIMIT 20 # Modifies DEATH_METHOD
AGE_DEVIATION 0 # Modified DEATH_METHOD
ALLOC_METHOD 0 # 0 = Allocated space is set to default instruction.
# 1 = Set to section of dead genome (Necrophilia)
Modified: trunk/source/support/genesis.smt
===================================================================
--- trunk/source/support/genesis.smt 2005-09-28 23:37:58 UTC (rev 328)
+++ trunk/source/support/genesis.smt 2005-10-04 02:37:32 UTC (rev 329)
@@ -37,7 +37,7 @@
DEATH_METHOD 2 # 0 = Never die of old age.
# 1 = Die when inst executed = AGE_LIMIT (with deviation)
# 2 = Die when inst executed = length * AGE_LIMIT (+ dev.)
-AGE_LIMIT 5000 # Modifies DEATH_METHOD
+AGE_LIMIT 20 # Modifies DEATH_METHOD
AGE_DEVIATION 0 # Modified DEATH_METHOD
ALLOC_METHOD 0 # 0 = Allocated space is set to default instruction.
# 1 = Set to section of dead genome (Necrophilia)
Modified: trunk/source/tools/tDictionary.hh
===================================================================
--- trunk/source/tools/tDictionary.hh 2005-09-28 23:37:58 UTC (rev 328)
+++ trunk/source/tools/tDictionary.hh 2005-10-04 02:37:32 UTC (rev 329)
@@ -9,7 +9,7 @@
* This template is used to look up objects of the desired type by name.
* I is implemented through use of a linked list and a hash table. The linked
* list contains all of the individual entries stored in the dictionary (in an
- * arbitrary order). The hash table points to the first entry in the list
+ * arbitrary order). The hash table points to the first entry in the list
* that fits in its cell. If there are no entries that fit in the cell, the
* has table contains a NULL pointer at that location.
*
@@ -68,7 +68,7 @@
template <class T> class tListIterator; // aggregate
template <class T> class tDictionary {
-
+
// We create a structure with full information about each entry stored in
// this dictionary.
template <class U> struct tDictEntry {
@@ -76,11 +76,11 @@
int id;
U data;
};
-
+
private:
int dict_size; // How many entries are we storing?
int hash_size; // What size hash table are we using?
-
+
tList< tDictEntry<T> > entry_list; // A linked list of ALL entries
tArray< tListNode< tDictEntry<T> > * > cell_array; // Pointers to the entry list.
tListIterator< tDictEntry<T> > list_it; // Iterator for entry_list
@@ -95,43 +95,43 @@
out_hash += (unsigned int) key[i];
return out_hash % hash_size;
}
-
+
// Function to find the appropriate tDictEntry for a string that is passed
// in and return it.
tDictEntry<T> * FindEntry(const cString & name) {
const int bin = HashString(name);
if (cell_array[bin] == NULL) return NULL;
-
+
// Set the list iterator to the first entry of this bin.
list_it.Set(cell_array[bin]);
-
+
// Loop through all entries in this bin to see if any are a perfect match.
while (list_it.Get() != NULL && list_it.Get()->id == bin) {
if (list_it.Get()->name == name) return list_it.Get();
list_it.Next();
}
-
+
// No matches found.
return NULL;
}
private:
- // disabled copy constructor.
- tDictionary(const tDictionary &);
+ // disabled copy constructor.
+ tDictionary(const tDictionary &);
public:
- tDictionary(int in_hash_size=DICTIONARY_HASH_DEFAULT)
+ tDictionary(int in_hash_size=DICTIONARY_HASH_DEFAULT)
: dict_size(0)
, hash_size(in_hash_size)
, cell_array(in_hash_size)
, list_it(entry_list)
{
- cell_array.SetAll(NULL);
+ cell_array.SetAll(NULL);
}
-
+
~tDictionary() {
while (entry_list.GetSize()) delete entry_list.Pop();
}
-
-
+
+
bool OK() {
std::cout << "DICT_SIZE = " << dict_size << std::endl;
std::cout << "HASH_SIZE = " << hash_size << std::endl;
@@ -141,29 +141,29 @@
while (list_it.Next() != NULL) {
tDictEntry<T> * cur_entry = list_it.Get();
std::cout << " " << count << " : "
- << cur_entry->id << " "
- << cur_entry->name << " "
- << cur_entry->data << " "
- << std::endl;
+ << cur_entry->id << " "
+ << cur_entry->name << " "
+ << cur_entry->data << " "
+ << std::endl;
}
std::cout << std::endl;
std::cout << "ARRAY CELLS: "
- << cell_array.GetSize()
- << std::endl;
+ << cell_array.GetSize()
+ << std::endl;
for (int i = 0; i < hash_size; i++) {
tListNode< tDictEntry<T> > * cur_list_node = cell_array[i];
if (cur_list_node == NULL) {
- std::cout << " NULL" << std::endl;
+ std::cout << " NULL" << std::endl;
} else {
- std::cout << " " << cur_list_node->data->id
- << " " << cur_list_node->data->name
- << std::endl;
+ std::cout << " " << cur_list_node->data->id
+ << " " << cur_list_node->data->name
+ << std::endl;
}
}
-
+
return true;
}
-
+
int GetSize() { return dict_size; }
// This function is used to add a new entry...
@@ -174,23 +174,23 @@
new_entry->data = data;
const int bin = HashString(name);
new_entry->id = bin;
-
-
+
+
// Determine where this new entry should go; either at the end of
// the list (if there are no others in the bin) or following another
// entry in the bin.
if (cell_array[bin] == NULL) { list_it.Reset(); } // Reset to list start
else { list_it.Set(cell_array[bin]); } // Else find insert point
-
+
entry_list.Insert(list_it, new_entry); // Place new entry in the list
list_it.Prev(); // Back up to new entry
cell_array[bin] = list_it.GetPos(); // Record position
-
+
// Update our entry count...
dict_size++;
}
-
+
// This function will change the value of an entry that exists, or add it
// if it doesn't exist.
void SetValue(const cString & name, T data) {
@@ -201,12 +201,12 @@
}
cur_entry->data = data;
}
-
-
+
+
bool HasEntry(const cString & name) {
return FindEntry(name) != NULL;
}
-
+
bool Find(const cString & name, T & out_data) {
tDictEntry<T> * found_entry = FindEntry(name);
if (found_entry != NULL) {
@@ -215,44 +215,44 @@
}
return false;
}
-
+
T Remove(const cString & name) {
// Determine the bin that we are going to be using.
const int bin = HashString(name);
-
+
T out_data;
assert(cell_array[bin] != NULL);
list_it.Set(cell_array[bin]);
-
+
// If we are deleting the first entry in this bin we must clean up...
if (list_it.Get()->name == name) {
out_data = list_it.Get()->data;
- list_it.Remove();
+ delete list_it.Remove();
list_it.Next();
dict_size--;
// See if the next entry is still part of this cell.
if (list_it.AtRoot() == false && list_it.Get()->id == bin) {
- cell_array[bin] = list_it.GetPos();
+ cell_array[bin] = list_it.GetPos();
} else {
- cell_array[bin] = NULL;
+ cell_array[bin] = NULL;
}
}
-
+
// If it was not the first entry in this cell, keep looking!
else {
while (list_it.Next() != NULL && list_it.Get()->id == bin) {
- if (list_it.Get()->name == name) {
- out_data = list_it.Get()->data;
- list_it.Remove();
- dict_size--;
- break;
- }
+ if (list_it.Get()->name == name) {
+ out_data = list_it.Get()->data;
+ delete list_it.Remove();
+ dict_size--;
+ break;
+ }
}
}
-
+
return out_data;
}
-
+
cString NearMatch(const cString name) {
cString best_match("");
int best_dist = name.GetSize();
@@ -260,30 +260,30 @@
while (list_it.Next() != NULL) {
int dist = cStringUtil::EditDistance(name, list_it.Get()->name);
if (dist < best_dist) {
- best_dist = dist;
- best_match = list_it.Get()->name;
+ best_dist = dist;
+ best_match = list_it.Get()->name;
}
}
return best_match;
}
-
+
void SetHash(int _hash) {
// Create the new table...
hash_size = _hash;
cell_array.ResizeClear(hash_size);
cell_array.SetAll(NULL);
-
+
// Backup all of the entries in the list and re-insert them one-by-one.
tList< tDictEntry<T> > backup_list;
backup_list.Transfer(entry_list);
-
+
while (backup_list.GetSize() > 0) {
tDictEntry<T> * cur_entry = backup_list.Pop();
-
+
// determine the new bin for this entry.
int bin = HashString(cur_entry->name);
cur_entry->id = bin;
-
+
if (cell_array[bin] == NULL) { list_it.Reset(); } // Reset to list start
else { list_it.Set(cell_array[bin]); } // Else find insert point
@@ -292,7 +292,7 @@
cell_array[bin] = list_it.GetPos(); // Record position
}
}
-
+
// The following method allows the user to convert the dictionary contents
// into lists. Empty lists show be passed in as arguments and the method
// will fill in their contents.
@@ -302,7 +302,7 @@
assert(value_list.GetSize() == 0);
tListIterator<cString> name_it(name_list);
tListIterator<T> value_it(value_list);
-
+
// Loop through the current entries and included them into the output
// list one at a time.
list_it.Reset();
@@ -316,7 +316,7 @@
value_it.Reset();
value_it.Next();
while (name_it.Next() != NULL && cur_name > *(name_it.Get())) {
- value_it.Next();
+ value_it.Next();
}
name_list.Insert(name_it, &cur_name);
value_list.Insert(value_it, &cur_value);
Modified: trunk/source/tools/tHashTable.hh
===================================================================
--- trunk/source/tools/tHashTable.hh 2005-09-28 23:37:58 UTC (rev 328)
+++ trunk/source/tools/tHashTable.hh 2005-10-04 02:37:32 UTC (rev 329)
@@ -68,7 +68,7 @@
template <class DATA_TYPE> class tListIterator; // aggregate
template <class HASH_TYPE, class DATA_TYPE> class tHashTable {
-
+
// We create a structure with full information about each entry stored in
// this dictionary.
template <class E_HASH_TYPE, class E_DATA_TYPE> struct tHashEntry {
@@ -76,29 +76,29 @@
int id;
E_DATA_TYPE data;
};
-
+
private:
int entry_count; // How many entries are we storing?
int table_size; // What size hash table are we using?
-
+
// Create a linked list of all hash entries in the table, as well as a
// companion array with pointers into the list that will give the start of
// each hash entry.
tList< tHashEntry<HASH_TYPE, DATA_TYPE> > entry_list;
tArray< tListNode< tHashEntry<HASH_TYPE, DATA_TYPE> > * > cell_array;
-
+
// Create an iterator for entry_list
tListIterator< tHashEntry<HASH_TYPE, DATA_TYPE> > list_it;
// Create a set of HashKey methods for each of the basic data types that
// we allow:
-
+
// HASH_TYPE = int
// Simply mod the into by the size of the hash table and hope for the best
int HashKey(const int & key) const {
return key % table_size;
}
-
+
// HASH_TYPE = cString
// We hash a string simply by adding up the individual character values in
// that string and modding by the hash size. For most applications this
@@ -110,43 +110,52 @@
out_hash += (unsigned int) key[i];
return out_hash % table_size;
}
-
+
// Function to find the appropriate tHashEntry for a key that is passed
// in and return it.
tHashEntry<HASH_TYPE, DATA_TYPE> * FindEntry(const HASH_TYPE & key) {
const int bin = HashKey(key);
if (cell_array[bin] == NULL) return NULL;
-
+
// Set the list iterator to the first entry of this bin.
list_it.Set(cell_array[bin]);
-
+
// Loop through all entries in this bin to see if any are a perfect match.
while (list_it.Get() != NULL && list_it.Get()->id == bin) {
if (list_it.Get()->key == key) return list_it.Get();
list_it.Next();
}
-
+
// No matches found.
return NULL;
}
private:
- // disabled copy constructor.
- tHashTable(const tHashTable &);
+ // disabled copy constructor.
+ tHashTable(const tHashTable &);
public:
- tHashTable(int in_table_size=HASH_TABLE_SIZE_DEFAULT)
+ tHashTable(int in_table_size=HASH_TABLE_SIZE_DEFAULT)
: entry_count(0)
, table_size(in_table_size)
, cell_array(in_table_size)
, list_it(entry_list)
{
- cell_array.SetAll(NULL);
+ cell_array.SetAll(NULL);
}
-
+
~tHashTable() {
while (entry_list.GetSize()) delete entry_list.Pop();
}
-
-
+
+ void ClearAll() {
+ list_it.Reset();
+ while (list_it.Next() != NULL) {
+ delete list_it.Remove();
+ }
+ entry_count = 0;
+ cell_array.SetAll(NULL);
+ }
+
+
bool OK() {
std::cout << "ENTRY_COUNT = " << entry_count << std::endl;
std::cout << "TABLE_SIZE = " << table_size << std::endl;
@@ -156,58 +165,54 @@
while (list_it.Next() != NULL) {
tHashEntry<HASH_TYPE, DATA_TYPE> * cur_entry = list_it.Get();
std::cout << " " << count << " : "
- << cur_entry->id << " "
- << cur_entry->key << " "
- << cur_entry->data << " "
- << std::endl;
+ << cur_entry->id << " "
+ << cur_entry->key << " "
+ << cur_entry->data << " "
+ << std::endl;
}
std::cout << std::endl;
std::cout << "ARRAY CELLS: "
- << cell_array.GetSize()
- << std::endl;
+ << cell_array.GetSize()
+ << std::endl;
for (int i = 0; i < table_size; i++) {
- tListNode< tHashEntry<HASH_TYPE, DATA_TYPE> > * cur_list_node =
- cell_array[i];
+ tListNode< tHashEntry<HASH_TYPE, DATA_TYPE> > * cur_list_node = cell_array[i];
if (cur_list_node == NULL) {
- std::cout << " NULL" << std::endl;
+ std::cout << " NULL" << std::endl;
} else {
- std::cout << " " << cur_list_node->data->id
- << " " << cur_list_node->data->key
- << std::endl;
+ std::cout << " " << cur_list_node->data->id << " " << cur_list_node->data->key << std::endl;
}
}
-
+
return true;
}
-
+
int GetSize() { return entry_count; }
// This function is used to add a new entry...
void Add(const HASH_TYPE & key, DATA_TYPE data) {
// Build the new entry...
- tHashEntry<HASH_TYPE, DATA_TYPE> * new_entry =
- new tHashEntry<HASH_TYPE, DATA_TYPE>;
+ tHashEntry<HASH_TYPE, DATA_TYPE> * new_entry = new tHashEntry<HASH_TYPE, DATA_TYPE>;
new_entry->key = key;
new_entry->data = data;
const int bin = HashKey(key);
new_entry->id = bin;
-
-
+
+
// Determine where this new entry should go; either at the end of
// the list (if there are no others in the bin) or following another
// entry in the bin.
if (cell_array[bin] == NULL) { list_it.Reset(); } // Reset to list start
else { list_it.Set(cell_array[bin]); } // Else find insert point
-
+
entry_list.Insert(list_it, new_entry); // Place new entry in the list
list_it.Prev(); // Back up to new entry
cell_array[bin] = list_it.GetPos(); // Record position
-
+
// Update our entry count...
entry_count++;
}
-
+
// This function will change the value of an entry that exists, or add it
// if it doesn't exist.
void SetValue(const HASH_TYPE & key, DATA_TYPE data) {
@@ -218,12 +223,12 @@
}
cur_entry->data = data;
}
-
-
+
+
bool HasEntry(const HASH_TYPE & key) {
return FindEntry(key) != NULL;
}
-
+
bool Find(const HASH_TYPE & key, DATA_TYPE & out_data) {
tHashEntry<HASH_TYPE, DATA_TYPE> * found_entry = FindEntry(key);
if (found_entry != NULL) {
@@ -232,61 +237,61 @@
}
return false;
}
-
+
DATA_TYPE Remove(const HASH_TYPE & key) {
// Determine the bin that we are going to be using.
const int bin = HashKey(key);
-
+
DATA_TYPE out_data;
assert(cell_array[bin] != NULL);
list_it.Set(cell_array[bin]);
-
+
// If we are deleting the first entry in this bin we must clean up...
if (list_it.Get()->key == key) {
out_data = list_it.Get()->data;
- list_it.Remove();
+ delete list_it.Remove();
list_it.Next();
entry_count--;
// See if the next entry is still part of this cell.
if (list_it.AtRoot() == false && list_it.Get()->id == bin) {
- cell_array[bin] = list_it.GetPos();
+ cell_array[bin] = list_it.GetPos();
} else {
- cell_array[bin] = NULL;
+ cell_array[bin] = NULL;
}
}
-
+
// If it was not the first entry in this cell, keep looking!
else {
while (list_it.Next() != NULL && list_it.Get()->id == bin) {
- if (list_it.Get()->key == key) {
- out_data = list_it.Get()->data;
- list_it.Remove();
- entry_count--;
- break;
- }
+ if (list_it.Get()->key == key) {
+ out_data = list_it.Get()->data;
+ delete list_it.Remove();
+ entry_count--;
+ break;
+ }
}
}
-
+
return out_data;
}
-
+
void SetTableSize(int _hash) {
// Create the new table...
table_size = _hash;
cell_array.ResizeClear(table_size);
cell_array.SetAll(NULL);
-
+
// Backup all of the entries in the list and re-insert them one-by-one.
tList< tHashEntry<HASH_TYPE, DATA_TYPE> > backup_list;
backup_list.Transfer(entry_list);
-
+
while (backup_list.GetSize() > 0) {
tHashEntry<HASH_TYPE, DATA_TYPE> * cur_entry = backup_list.Pop();
-
+
// determine the new bin for this entry.
int bin = HashKey(cur_entry->key);
cur_entry->id = bin;
-
+
if (cell_array[bin] == NULL) { list_it.Reset(); } // Reset to list start
else { list_it.Set(cell_array[bin]); } // Else find insert point
@@ -295,7 +300,7 @@
cell_array[bin] = list_it.GetPos(); // Record position
}
}
-
+
// The following method allows the user to convert the dictionary contents
// into lists. Empty lists show be passed in as arguments and the method
// will fill in their contents.
@@ -305,7 +310,7 @@
assert(value_list.GetSize() == 0);
tListIterator<HASH_TYPE> key_it(key_list);
tListIterator<DATA_TYPE> value_it(value_list);
-
+
// Loop through the current entries and included them into the output
// list one at a time.
list_it.Reset();
@@ -319,7 +324,7 @@
value_it.Reset();
value_it.Next();
while (key_it.Next() != NULL && cur_key > *(key_it.Get())) {
- value_it.Next();
+ value_it.Next();
}
key_list.Insert(key_it, &cur_key);
value_list.Insert(value_it, &cur_value);
Modified: trunk/source/tools/tList.hh
===================================================================
--- trunk/source/tools/tList.hh 2005-09-28 23:37:58 UTC (rev 328)
+++ trunk/source/tools/tList.hh 2005-10-04 02:37:32 UTC (rev 329)
@@ -17,7 +17,7 @@
T * data;
tListNode<T> * next;
tListNode<T> * prev;
-
+
tListNode() : data(NULL), next(this), prev(this) { ; }
};
@@ -29,16 +29,16 @@
virtual const tList<T> & GetConstList() = 0;
virtual const tListNode<T> * GetConstNode() = 0;
public:
- tBaseIterator() { ; }
+ tBaseIterator() { ; }
virtual ~tBaseIterator() { ; }
-
+
virtual void Set(tListNode<T> * in_node) = 0;
virtual void Reset() = 0;
-
+
virtual const T * GetConst() = 0;
virtual const T * NextConst() = 0;
virtual const T * PrevConst() = 0;
-
+
virtual bool AtRoot() const = 0;
virtual bool AtEnd() const = 0;
};
@@ -48,30 +48,30 @@
private:
tList<T> & list;
tListNode<T> * node;
-
+
const tList<T> & GetConstList() { return list; }
const tListNode<T> * GetConstNode() { return node; }
public:
- explicit tListIterator(tList<T> & _list);
+ explicit tListIterator(tList<T> & _list);
explicit tListIterator(tList<T> & _list, tListNode<T> * start_node);
~tListIterator();
-
+
void Set(tListNode<T> * in_node) { node = in_node; }
void Reset();
tListNode<T> * GetPos() { return node; }
-
+
T * Get();
T * Next();
T * Prev();
const T * GetConst() { return Get(); }
const T * NextConst() { return Next(); }
const T * PrevConst() { return Prev(); }
-
+
bool Find(T * test_data);
-
+
bool AtRoot() const;
bool AtEnd() const;
-
+
// Unique methods...
T * Remove();
};
@@ -81,18 +81,18 @@
private:
const tList<T> & list;
const tListNode<T> * node;
-
+
const tList<T> & GetConstList() { return list; }
const tListNode<T> * GetConstNode() { return node; }
public:
- explicit tConstListIterator(const tList<T> & _list);
+ explicit tConstListIterator(const tList<T> & _list);
explicit tConstListIterator(const tList<T> & _list,
- const tListNode<T> * start_node);
+ const tListNode<T> * start_node);
~tConstListIterator();
-
+
void Set(tListNode<T> * in_node) { node = in_node; }
void Reset();
-
+
const T * Get();
const T * Next();
const T * Prev();
@@ -100,7 +100,7 @@
const T * NextConst() { return Next(); }
const T * PrevConst() { return Prev(); }
bool Find(const T * test_data);
-
+
bool AtRoot() const;
bool AtEnd() const;
};
@@ -110,40 +110,40 @@
friend class tListIterator<T>;
friend class tConstListIterator<T>;
protected:
- tListNode<T> root; // Data root
+ tListNode<T> root; // Data root
int size;
mutable tListNode< tBaseIterator<T> > it_root; // Iterator root
mutable int it_count;
-
+
T * RemoveNode(tListNode<T> * out_node) {
// Make sure we're not trying to delete the root node!
if (out_node == &root) return NULL;
-
+
// Adjust any iterators on the deleted node.
tListNode< tBaseIterator<T> > * test_it = it_root.next;
while (test_it != &it_root) {
// If this iterator is on this node, move it back one.
if (test_it->data->GetConstNode() == out_node) {
- test_it->data->PrevConst();
+ test_it->data->PrevConst();
}
test_it = test_it->next;
}
-
+
// Save the data and patch up the linked list.
T * out_data = out_node->data;
out_node->prev->next = out_node->next;
out_node->next->prev = out_node->prev;
-
+
// Cleanup and return
size--;
delete out_node;
return out_data;
}
-
+
// To be called from iterator constructor only!
void AddIterator(tBaseIterator<T> * new_it) const {
tListNode< tBaseIterator<T> > * new_node =
- new tListNode< tBaseIterator<T> >;
+ new tListNode< tBaseIterator<T> >;
new_node->data = new_it;
new_node->next = it_root.next;
new_node->prev = &it_root;
@@ -151,7 +151,7 @@
it_root.next = new_node;
it_count++;
}
-
+
// To be called from iterator destructor only!
void RemoveIterator(tBaseIterator<T> * old_it) const {
tListNode< tBaseIterator<T> > * test_it = it_root.next;
@@ -161,13 +161,13 @@
delete test_it;
it_count--;
}
-
+
public:
- T * Pop() { return RemoveNode(root.next); }
+ T * Pop() { return RemoveNode(root.next); }
T * PopRear() { return RemoveNode(root.prev); }
-
+
void Clear() { while (size > 0) Pop(); }
-
+
void Append(const tList<T> & in_list) {
tListNode<T> * cur_node = in_list.root.next;
while (cur_node != &(in_list.root)) {
@@ -175,12 +175,12 @@
cur_node = cur_node->next;
}
}
-
+
void Copy(const tList<T> & in_list) {
Clear();
Append(in_list);
}
-
+
void Push(T * _in) {
tListNode<T> * new_node = new tListNode<T>;
new_node->data = _in;
@@ -190,7 +190,7 @@
root.next = new_node;
size++;
}
-
+
void PushRear(T * _in) {
tListNode<T> * new_node = new tListNode<T>;
new_node->data = _in;
@@ -200,94 +200,94 @@
root.prev = new_node;
size++;
}
-
+
const T * GetFirst() const { return root.next->data; }
const T * GetLast() const { return root.prev->data; }
T * GetFirst() { return root.next->data; }
T * GetLast() { return root.prev->data; }
-
+
T * GetPos(int pos) {
if (pos >= GetSize()) return NULL;
tListNode<T> * test_node = root.next;
for (int i = 0; i < pos; i++) test_node = test_node->next;
return test_node->data;
}
-
+
const T * GetPos(int pos) const {
if (pos >= GetSize()) return NULL;
tListNode<T> * test_node = root.next;
for (int i = 0; i < pos; i++) test_node = test_node->next;
return test_node->data;
}
-
+
void CircNext() { if (size > 0) PushRear(Pop()); }
void CircPrev() { if (size > 0) Push(PopRear()); }
-
+
T * Remove(tListIterator<T> & other) {
if (&(other.list) != this) return NULL; // @CAO make this an assert?
return RemoveNode(other.node);
}
-
+
T * Insert(tListIterator<T> & list_it, T * in_data) {
tListNode<T> * cur_node = list_it.node;
-
+
// Build the new node for the list...
tListNode<T> * new_node = new tListNode<T>;
new_node->data = in_data;
-
+
// Insert the new node before the iterator...
new_node->next = cur_node;
new_node->prev = cur_node->prev;
cur_node->prev->next = new_node;
cur_node->prev = new_node;
size++;
-
+
return in_data;
}
-
-
+
+
//bool Remove(T * other) {
T * Remove(T * other) {
tListNode<T> * test = root.next;
while (test != &root) {
if (test->data == other) {
- RemoveNode(test);
- //return true;
- return other;
+ RemoveNode(test);
+ //return true;
+ return other;
}
test = test->next;
}
//return false;
return NULL;
}
-
+
int GetSize() const { return size; }
-
+
// Copy another list onto the end of this one.
void Append(tList<T> & other_list) {
tListIterator<T> other_it(other_list);
while (other_it.Next() != NULL) PushRear(other_it.Get());
}
-
+
// Empty out another list, transferring its contents to the end of this one.
void Transfer(tList<T> & other_list) {
// If the other list is empty, stop here.
if (other_list.GetSize() == 0) return;
-
+
// Hook this list into the other one.
other_list.root.next->prev = root.prev;
other_list.root.prev->next = &root;
root.prev->next = other_list.root.next;
root.prev = other_list.root.prev;
-
+
// Clean up the other list so it has no entries.
other_list.root.next = &(other_list.root);
other_list.root.prev = &(other_list.root);
-
+
// Update the size
size += other_list.size;
other_list.size = 0;
-
+
// Update all iterators in the other list to point at the root.
tListNode< tBaseIterator<T> > * test_it = other_list.it_root.next;
while (test_it != &other_list.it_root) {
@@ -295,7 +295,7 @@
test_it = test_it->next;
}
}
-
+
// Find by value
T * Find(T * _in) const {
tListNode<T> * test = root.next;
@@ -305,7 +305,7 @@
}
return NULL;
}
-
+
// Find by Pointer
T * FindPtr(T * _in) const {
tListNode<T> * test = root.next;
@@ -315,7 +315,7 @@
}
return NULL;
}
-
+
// Find the position of the node by its pointer
int FindPosPtr(T * _in) const {
tListNode<T> * test = root.next;
@@ -327,8 +327,8 @@
}
return 0;
}
-
-
+
+
// Remove by position
T * PopPos(int pos) {
if (pos >= GetSize()) return NULL;
@@ -336,14 +336,14 @@
for (int i = 0; i < pos; i++) test_node = test_node->next;
return RemoveNode(test_node);
}
-
-
+
+
public:
- tList() : size(0), it_count(0) { }
+ tList() : size(0), it_count(0) { }
~tList() { Clear(); }
private:
- tList(tList & _list) { ; } // Never should be used...
-};
+ tList(tList & _list) { ; } // Never should be used...
+ };
// This is an extended version of tList that contains extra functions to
@@ -362,7 +362,7 @@
}
return test->data;
}
-
+
T * PopIntValue(int (T::*fun)() const, int value) {
tListNode<T> * test = this->root.next;
while (test != &(this->root)) {
@@ -371,7 +371,7 @@
}
return NULL;
}
-
+
T * PopIntMax(int (T::*fun)() const) {
if (this->size == 0) return NULL;
tListNode<T> * test = this->root.next;
@@ -380,14 +380,14 @@
while (test != &(this->root)) {
const int cur_val = (test->data->*fun)();
if ( cur_val > max_val ) {
- max_val = cur_val;
- best = test;
+ max_val = cur_val;
+ best = test;
}
test = test->next;
}
return RemoveNode(best);
}
-
+
T * PopDoubleMax(double (T::*fun)() const) {
if (this->size == 0) return NULL;
tListNode<T> * test = this->root.next;
@@ -396,14 +396,14 @@
while (test != &(this->root)) {
const double cur_val = (test->data->*fun)();
if ( cur_val > max_val ) {
- max_val = cur_val;
- best = test;
+ max_val = cur_val;
+ best = test;
}
test = test->next;
}
return RemoveNode(best);
}
-
+
int Count(int (T::*fun)() const) {
int total = 0;
tListNode<T> * test = this->root.next;
@@ -413,7 +413,7 @@
}
return total;
}
-
+
};
@@ -421,14 +421,14 @@
// tListIterator
template <class T> tListIterator<T>::tListIterator(tList<T> & _list)
- : list(_list), node(&(_list.root))
+: list(_list), node(&(_list.root))
{
list.AddIterator(this);
}
template <class T> tListIterator<T>::tListIterator(tList<T> & _list,
- tListNode<T> * start_node)
- : list(_list), node(start_node)
+ tListNode<T> * start_node)
+: list(_list), node(start_node)
{
list.AddIterator(this);
}
@@ -489,13 +489,13 @@
// tConstListIterator
template <class T> tConstListIterator<T>::tConstListIterator(const tList<T> & _list)
- : list(_list), node(&(_list.root))
+: list(_list), node(&(_list.root))
{
list.AddIterator(this);
}
template <class T> tConstListIterator<T>::tConstListIterator(const tList<T> & _list, const tListNode<T> * start_node)
- : list(_list), node(start_node)
+: list(_list), node(start_node)
{
list.AddIterator(this);
}
More information about the Avida-cvs
mailing list