[Avida-cvs] [avida-svn] r533 - in development: Avida.xcodeproj source/main source/tools
brysonda@myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Sun Mar 26 18:46:08 PST 2006
Author: brysonda
Date: 2006-03-26 21:46:07 -0500 (Sun, 26 Mar 2006)
New Revision: 533
Added:
development/source/main/cOrgSinkMessage.h
development/source/main/cOrgSourceMessage.h
Modified:
development/Avida.xcodeproj/project.pbxproj
development/source/main/cOrganism.cc
development/source/main/cOrganism.h
development/source/main/cPopulation.cc
development/source/tools/tArray.h
development/source/tools/tManagedPointerArray.h
development/source/tools/tSmartArray.h
Log:
Finish up tSmartArray implementation. Add some basic pieces for organism 'network' communication.
Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj 2006-03-26 18:47:29 UTC (rev 532)
+++ development/Avida.xcodeproj/project.pbxproj 2006-03-27 02:46:07 UTC (rev 533)
@@ -13,8 +13,6 @@
7005A70609BA0FA90007E16E /* cTestCPUInterface.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7005A70209BA0FA90007E16E /* cTestCPUInterface.cc */; };
7005A70709BA0FA90007E16E /* cTestCPUInterface.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7005A70109BA0FA90007E16E /* cTestCPUInterface.h */; };
7005A70809BA0FA90007E16E /* cTestCPUInterface.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7005A70209BA0FA90007E16E /* cTestCPUInterface.cc */; };
- 7005A70A09BA0FBE0007E16E /* cOrgInterface.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7005A70909BA0FBE0007E16E /* cOrgInterface.h */; };
- 7005A70B09BA0FBE0007E16E /* cOrgInterface.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7005A70909BA0FBE0007E16E /* cOrgInterface.h */; };
7005A83209BA621F0007E16E /* cOrgMessage.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70B0868808F49EA800FC65FE /* cOrgMessage.cc */; };
700E2996085A1F6000CF158A /* avida in CopyFiles */ = {isa = PBXBuildFile; fileRef = DCC3164D07626CF3008F7A48 /* avida */; };
700E2AF8085DE50C00CF158A /* avida.cc in Sources */ = {isa = PBXBuildFile; fileRef = DCC3109C0762539E008F7A48 /* avida.cc */; };
@@ -116,7 +114,6 @@
7073165D097C6C8F00815164 /* cParser.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7073165B097C6C8F00815164 /* cParser.cc */; };
70731665097C6DF500815164 /* cASLibrary.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70731663097C6DF500815164 /* cASLibrary.cc */; };
7073166B097C6E0C00815164 /* cASSymbol.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70731669097C6E0C00815164 /* cASSymbol.cc */; };
- 7093DB4109D6F50300DE7FEB /* tSmartArray.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7093DB4009D6F50300DE7FEB /* tSmartArray.h */; };
70AA941A09D486CA006A24C8 /* system-alloc.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70DCF50209CFB0B400924128 /* system-alloc.cc */; };
70AA941B09D486CB006A24C8 /* tcmalloc-logging.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70DCF57F09CFBD3D00924128 /* tcmalloc-logging.cc */; };
70AA941C09D486CD006A24C8 /* tcmalloc.cc in Sources */ = {isa = PBXBuildFile; fileRef = 70DCF56309CFBB9500924128 /* tcmalloc.cc */; };
@@ -488,7 +485,6 @@
7054A17B09A802BD00038658 /* cAnalyzeJob.h in CopyFiles */,
7054A1B709A810CB00038658 /* cAnalyzeJobWorker.h in CopyFiles */,
7005A70509BA0FA90007E16E /* cTestCPUInterface.h in CopyFiles */,
- 7005A70B09BA0FBE0007E16E /* cOrgInterface.h in CopyFiles */,
701D51D109C645F50009B4F8 /* cAvidaContext.h in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -516,7 +512,6 @@
7054A12409A7BEFC00038658 /* cThread.h in CopyFiles */,
7005A70709BA0FA90007E16E /* cTestCPUInterface.h in CopyFiles */,
701D51CD09C645F50009B4F8 /* cAvidaContext.h in CopyFiles */,
- 7093DB4109D6F50300DE7FEB /* tSmartArray.h in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -545,7 +540,6 @@
7054A17A09A802BD00038658 /* cAnalyzeJob.h in CopyFiles */,
7054A1B509A810CB00038658 /* cAnalyzeJobWorker.h in CopyFiles */,
7005A70309BA0FA90007E16E /* cTestCPUInterface.h in CopyFiles */,
- 7005A70A09BA0FBE0007E16E /* cOrgInterface.h in CopyFiles */,
701D51CF09C645F50009B4F8 /* cAvidaContext.h in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -575,6 +569,8 @@
701D93E6094CBF71008B845F /* cDefaultAnalyzeDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cDefaultAnalyzeDriver.h; sourceTree = "<group>"; };
701D93E7094CBF71008B845F /* cDefaultAnalyzeDriver.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cDefaultAnalyzeDriver.cc; sourceTree = "<group>"; };
701D941C094CC5F1008B845F /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
+ 7027621909D73E5900741717 /* cOrgSourceMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cOrgSourceMessage.h; sourceTree = "<group>"; };
+ 7027621A09D73E7700741717 /* cOrgSinkMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cOrgSinkMessage.h; sourceTree = "<group>"; };
702D4EF508DA5328007BA469 /* cEnvironment.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cEnvironment.h; sourceTree = "<group>"; };
702D4EF608DA5328007BA469 /* cPopulationInterface.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cPopulationInterface.h; sourceTree = "<group>"; };
702D4EFC08DA5341007BA469 /* cEnvironment.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cEnvironment.cc; sourceTree = "<group>"; };
@@ -1430,6 +1426,8 @@
DCC310A90762539E008F7A48 /* CMakeLists.txt */,
7005A70909BA0FBE0007E16E /* cOrgInterface.h */,
701D51CB09C645F50009B4F8 /* cAvidaContext.h */,
+ 7027621909D73E5900741717 /* cOrgSourceMessage.h */,
+ 7027621A09D73E7700741717 /* cOrgSinkMessage.h */,
);
path = main;
sourceTree = "<group>";
Added: development/source/main/cOrgSinkMessage.h
===================================================================
--- development/source/main/cOrgSinkMessage.h 2006-03-26 18:47:29 UTC (rev 532)
+++ development/source/main/cOrgSinkMessage.h 2006-03-27 02:46:07 UTC (rev 533)
@@ -0,0 +1,30 @@
+/*
+ * cOrgSinkMessage.h
+ * Avida
+ *
+ * Created by David on 3/26/06.
+ * Copyright 2006 Michigan State University. All rights reserved.
+ *
+ */
+
+#ifndef cOrgSinkMessage_h
+#define cOrgSinkMessage_h
+
+class cOrgSinkMessage
+{
+private:
+ int m_source_id;
+ int m_orig_val;
+ int m_actual_val;
+
+public:
+ cOrgSinkMessage() : m_source_id(-1), m_orig_val(0), m_actual_val(0) { ; }
+ cOrgSinkMessage(int source, int original, int actual)
+ : m_source_id(source), m_orig_val(original), m_actual_val(actual) { ; }
+
+ int GetSourceID() { return m_source_id; }
+ int GetOrignalValue() { return m_orig_val; }
+ int GetActualValue() { return m_actual_val; }
+};
+
+#endif
Property changes on: development/source/main/cOrgSinkMessage.h
___________________________________________________________________
Name: svn:eol-style
+ native
Added: development/source/main/cOrgSourceMessage.h
===================================================================
--- development/source/main/cOrgSourceMessage.h 2006-03-26 18:47:29 UTC (rev 532)
+++ development/source/main/cOrgSourceMessage.h 2006-03-27 02:46:07 UTC (rev 533)
@@ -0,0 +1,32 @@
+/*
+ * cOrgSourceMessage.h
+ * Avida
+ *
+ * Created by David on 3/26/06.
+ * Copyright 2006 Michigan State University. All rights reserved.
+ *
+ */
+
+#ifndef cOrgSourceMessage_h
+#define cOrgSourceMessage_h
+
+class cOrgSourceMessage
+{
+private:
+ int m_value;
+ int m_sent;
+ int m_dropped;
+
+public:
+ cOrgSourceMessage() : m_value(0), m_sent(0), m_dropped(0) { ; }
+ cOrgSourceMessage(int value) : m_value(value), m_sent(1), m_dropped(0) { ; }
+
+ int GetValue() { return m_value; }
+
+ int GetSent() { return m_sent; }
+ void SetSent() { m_sent++; }
+ int GetDropped() { return m_dropped; }
+ void SetDropped() { m_dropped++; }
+};
+
+#endif
Property changes on: development/source/main/cOrgSourceMessage.h
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc 2006-03-26 18:47:29 UTC (rev 532)
+++ development/source/main/cOrganism.cc 2006-03-27 02:46:07 UTC (rev 533)
@@ -22,11 +22,10 @@
#include "cInstSet.h"
#include "cInstUtil.h"
#include "cOrgMessage.h"
+#include "cOrgSinkMessage.h"
#include "cStringUtil.h"
-#include "tArray.h"
#include "cTools.h"
#include "cWorld.h"
-#include "tList.h"
#include <iomanip>
@@ -79,6 +78,8 @@
assert(is_running == false);
delete hardware;
delete m_interface;
+ while (m_net_pending.GetSize()) delete m_net_pending.Pop();
+ for (int i = 0; i < m_net_received.GetSize(); i++) delete m_net_received[i];
}
void cOrganism::SetOrgInterface(cOrgInterface* interface)
@@ -173,6 +174,19 @@
return true;
}
+void cOrganism::NetSend(int value)
+{
+ // @DMB - process message send
+}
+
+bool NetValidate(int value)
+{
+ // @DMB - check value against internal received buffer
+
+ return false;
+}
+
+
bool cOrganism::InjectParasite(const cGenome & injected_code)
{
assert(m_interface);
Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h 2006-03-26 18:47:29 UTC (rev 532)
+++ development/source/main/cOrganism.h 2006-03-27 02:46:07 UTC (rev 533)
@@ -35,9 +35,18 @@
#ifndef cOrgInterface_h
#include "cOrgInterface.h"
#endif
+#ifndef cOrgSourceMessage_h
+#include "cOrgSourceMessage.h"
+#endif
#ifndef tBuffer_h
#include "tBuffer.h"
#endif
+#ifndef tList_h
+#include "tList.h"
+#endif
+#ifndef tSmartArray_h
+#include "tSmartArray.h"
+#endif
/**
* The cOrganism class controls the running and manages all the statistics
@@ -50,6 +59,7 @@
class cInjectGenotype;
class cLineage;
class cOrgMessage;
+class cOrgSinkMessage;
class cEnvironment;
class cCodeLabel;
@@ -91,8 +101,9 @@
tBuffer<cOrgMessage> inbox;
tBuffer<cOrgMessage> sent;
- //tBuffer<cOrgMessage*> m_net_pending;
-
+ tList<cOrgSinkMessage> m_net_pending;
+ tSmartArray<cOrgSinkMessage*> m_net_received;
+ tSmartArray<cOrgSourceMessage> m_net_sent;
#ifdef DEBUG
bool initialized; // Has this CPU been initialized yet, w/hardware.
@@ -133,6 +144,11 @@
// Message stuff
void SendMessage(cOrgMessage & mess);
bool ReceiveMessage(cOrgMessage & mess);
+
+ // Network Stuff
+ void NetSend(int value);
+ cOrgSinkMessage* NetReceive() { return m_net_pending.PopRear(); }
+ bool NetValidate(int value);
bool InjectParasite(const cGenome & genome);
bool InjectHost(const cCodeLabel & in_label, const cGenome & genome);
@@ -146,8 +162,8 @@
double GetTestFitness();
double CalcMeritRatio();
- cCPUMemory & ChildGenome() { return child_genome; }
- sCPUStats & CPUStats() { return cpu_stats; }
+ cCPUMemory& ChildGenome() { return child_genome; }
+ sCPUStats& CPUStats() { return cpu_stats; }
bool TestCopyMut(cAvidaContext& ctx) const { return MutationRates().TestCopyMut(ctx); }
bool TestDivideMut(cAvidaContext& ctx) const { return MutationRates().TestDivideMut(ctx); }
Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc 2006-03-26 18:47:29 UTC (rev 532)
+++ development/source/main/cPopulation.cc 2006-03-27 02:46:07 UTC (rev 533)
@@ -894,7 +894,7 @@
for (int j = 0; j < num_task; j++) {
// only interested in tasks is done once!
- if (phenotype.GetLastTaskCount().ElementAt(j) > 0) {
+ if (phenotype.GetLastTaskCount()[j] > 0) {
single_deme_task[j].Add(1);
}
}
@@ -1987,7 +1987,7 @@
int id = 0;
for (int j = 0; j < phenotype.GetLastTaskCount().GetSize(); j++)
{
- if (phenotype.GetLastTaskCount().ElementAt(j) > 0)
+ if (phenotype.GetLastTaskCount()[j] > 0)
id += (1 << j);
}
ids.insert(id);
Modified: development/source/tools/tArray.h
===================================================================
--- development/source/tools/tArray.h 2006-03-26 18:47:29 UTC (rev 532)
+++ development/source/tools/tArray.h 2006-03-27 02:46:07 UTC (rev 533)
@@ -11,151 +11,102 @@
#ifndef tArray_h
#define tArray_h
-/*
- Array Templates
-
- Constructor:
- tArray( int size )
- tArray( const tArray & rhs )
-
- Interface:
- tArray & operator= ( const tArray & rhs )
-
- bool Good () const
- array is defined and ready to take data
-
- unsigned int GetSize () const
- returns the size of the array
-
- const T& ElementAt ( const int index ) const
- T& ElementAt ( const int index )
- const T& operator[] ( const int index ) const
- T& operator[] ( const int index )
- return the element at index in the array
-*/
-
#include <assert.h>
-#ifndef NULL
-#define NULL 0
-#endif
-/**
- * This class provides an array template.
- **/
+template <class T> class tArray
+{
+private:
+ T* m_data; // Data Elements
+ int m_size; // Number of Elements
-template <class T> class tArray {
-
-protected:
- // Internal Variables
- T * data; // Data Elements
- int size; // Number of Elements
-
public:
- void ResizeClear(const int in_size){
- size = in_size;
- assert(size >= 0); // Invalid size specified for array intialization
- if (data != NULL) delete [] data; // remove old data if exists
- if (size > 0) {
- data = new T[size]; // Allocate block for data
- assert(data != NULL); // Memory allocation error: Out of Memory?
- }
- else data = NULL;
- }
+ explicit tArray(const int size = 0) : m_data(NULL), m_size(0) { ResizeClear(size); }
+ tArray(const tArray& rhs) : m_data(NULL), m_size(0) { this->operator=(rhs); }
-public:
- // Constructor
- explicit tArray(const int _size=0) : data(NULL) { ResizeClear(_size); }
+ ~tArray() { delete [] m_data; }
- // Assingment Operator
- tArray & operator= (const tArray & rhs) {
- if (size != rhs.GetSize()) ResizeClear(rhs.GetSize());
- for(int i = 0; i < size; i++) data[i] = rhs[i];
+ tArray& operator=(const tArray& rhs) {
+ if (m_size != rhs.GetSize()) ResizeClear(rhs.GetSize());
+ for(int i = 0; i < m_size; i++) m_data[i] = rhs[i];
return *this;
}
- // Copy constructor
- tArray(const tArray& rhs) : data(NULL), size(0) { this->operator=(rhs); }
+ bool Good() const { return (m_data != NULL); }
+ int GetSize() const { return m_size; }
- // Destructor
- virtual ~tArray() { if (data != NULL) delete [] data; }
+ void ResizeClear(const int in_size)
+ {
+ m_size = in_size;
+ assert(m_size >= 0); // Invalid size specified for array intialization
+ if (m_data != NULL) delete [] m_data; // remove old data if exists
+ if (m_size > 0) {
+ m_data = new T[m_size]; // Allocate block for data
+ assert(m_data != NULL); // Memory allocation error: Out of Memory?
+ }
+ else m_data = NULL;
+ }
-
- // Interface Methods ///////////////////////////////////////////////////////
-
- bool Good() const { return (data != NULL); }
- int GetSize() const { return size; }
-
- void Resize(int new_size) {
+ void Resize(int new_size)
+ {
assert(new_size >= 0);
- // If we're already at the size we want, don't bother doing anything.
- if (size == new_size) return;
+ // If we're already at the size we want, don't bother doing anything.
+ if (m_size == new_size) return;
// If new size is 0, clean up and go!
if (new_size == 0) {
- delete [] data;
- data = NULL;
- size = 0;
+ if (m_data != NULL) delete [] m_data;
+ m_data = NULL;
+ m_size = 0;
return;
}
// If new size > 0
- T * new_data = new T[new_size];
+ T* new_data = new T[new_size];
assert(new_data != NULL); // Memory Allocation Error: Out of Memory?
// Copy over old data...
- for (int i = 0; i < size && i < new_size; i++) {
- new_data[i] = data[i];
+ for (int i = 0; i < m_size && i < new_size; i++) {
+ new_data[i] = m_data[i];
}
- if (data != NULL) delete [] data; // remove old data if exists
- data = new_data;
+ if (m_data != NULL) delete [] m_data; // remove old data if exists
+ m_data = new_data;
- size = new_size;
+ m_size = new_size;
}
- void Resize(int new_size, const T & empty_value) {
- assert(new_size >= 0);
- int old_size = size;
+ void Resize(int new_size, const T & empty_value)
+ {
+ int old_size = m_size;
Resize(new_size);
- if( new_size > old_size ){
- for (int i = old_size; i < new_size; i++) {
- data[i] = empty_value;
- }
- }
+ for (int i = old_size; i < new_size; i++) m_data[i] = empty_value;
}
-
- T & ElementAt(const int index){
- // check range
- assert(index >= 0); // Lower Bounds Error
- assert(index < size); // Upper Bounds Error
- return data[index]; // in range, so return element
+ T& operator[](const int index)
+ {
+ assert(index >= 0); // Lower Bounds Error
+ assert(index < m_size); // Upper Bounds Error
+ return m_data[index];
}
+ const T& operator[](const int index) const
+ {
+ assert(index >= 0); // Lower Bounds Error
+ assert(index < m_size); // Upper Bounds Error
+ return m_data[index];
+ }
- const T & ElementAt(const int index) const {
- // check range
- assert(index >= 0); // Lower Bounds Error
- assert(index < size); // Upper Bounds Error
- return data[index]; // in range, so return element
+ void Push(const T& value)
+ {
+ Resize(m_size + 1);
+ m_data[m_size - 1] = value;
}
- T & operator[](const int index) { return ElementAt(index); }
- const T & operator[](const int index) const { return ElementAt(index); }
-
-
- void Push(const T & value) {
- Resize(size+1);
- data[size-1] = value;
+ void SetAll(const T& value)
+ {
+ for (int i = 0; i < m_size; i++) m_data[i] = value;
}
-
- void SetAll(const T & value){
- for( int i=0; i < size; ++i ){
- data[i] = value;
- }
- }
-
};
#endif
Modified: development/source/tools/tManagedPointerArray.h
===================================================================
--- development/source/tools/tManagedPointerArray.h 2006-03-26 18:47:29 UTC (rev 532)
+++ development/source/tools/tManagedPointerArray.h 2006-03-27 02:46:07 UTC (rev 533)
@@ -16,52 +16,53 @@
#include "tArray.h"
#endif
-template <class T> class tManagedPointerArray {
-protected:
- T** data; // Data Elements
- int size; // Number of Elements
+template <class T> class tManagedPointerArray
+{
+private:
+ T** m_data; // Data Elements
+ int m_size; // Number of Elements
public:
- explicit tManagedPointerArray(const int _size = 0) : data(NULL), size(0) { ResizeClear(_size); }
- tManagedPointerArray(const tManagedPointerArray& rhs) : data(NULL), size(0) { this->operator=(rhs); }
- tManagedPointerArray(const tArray<T>& rhs) : data(NULL), size(0) { this->operator=(rhs); }
+ explicit tManagedPointerArray(const int _size = 0) : m_data(NULL), m_size(0) { ResizeClear(_size); }
+ tManagedPointerArray(const tManagedPointerArray& rhs) : m_data(NULL), m_size(0) { this->operator=(rhs); }
+ tManagedPointerArray(const tArray<T>& rhs) : m_data(NULL), m_size(0) { this->operator=(rhs); }
~tManagedPointerArray()
{
- for (int i = 0; i < size; i++) delete data[i];
- delete [] data;
+ for (int i = 0; i < m_size; i++) delete m_data[i];
+ delete [] m_data;
}
tManagedPointerArray& operator=(const tManagedPointerArray& rhs)
{
- if (size != rhs.GetSize()) Resize(rhs.GetSize());
- for(int i = 0; i < size; i++) *data[i] = rhs[i];
+ if (m_size != rhs.GetSize()) Resize(rhs.GetSize());
+ for(int i = 0; i < m_size; i++) *m_data[i] = rhs[i];
return *this;
}
tManagedPointerArray& operator=(const tArray<T>& rhs)
{
- if (size != rhs.GetSize()) Resize(rhs.GetSize());
- for(int i = 0; i < size; i++) *data[i] = rhs[i];
+ if (m_size != rhs.GetSize()) Resize(rhs.GetSize());
+ for(int i = 0; i < m_size; i++) *m_data[i] = rhs[i];
return *this;
}
- bool Good() const { return (data != NULL); }
- int GetSize() const { return size; }
+ bool Good() const { return (m_data != NULL); }
+ int GetSize() const { return m_size; }
void ResizeClear(const int in_size)
{
- for (int i = 0; i < size; i++) delete data[i];
- delete [] data; // remove old data if exists
+ for (int i = 0; i < m_size; i++) delete m_data[i];
+ if (m_data != NULL) delete [] m_data; // remove old data if exists
- size = in_size;
- assert(size >= 0); // Invalid size specified for array intialization
+ m_size = in_size;
+ assert(m_size >= 0); // Invalid size specified for array intialization
- if (size > 0) {
- data = new T*[size]; // Allocate block for data
- assert(data != NULL); // Memory allocation error: Out of Memory?
- for (int i = 0; i < size; i++) data[i] = new T;
- } else
- data = NULL;
+ if (m_size > 0) {
+ m_data = new T*[m_size]; // Allocate block for data
+ assert(m_data != NULL); // Memory allocation error: Out of Memory?
+ for (int i = 0; i < m_size; i++) m_data[i] = new T;
+ }
+ else m_data = NULL;
}
void Resize(int new_size)
@@ -69,72 +70,67 @@
assert(new_size >= 0);
// If we're already at the size we want, don't bother doing anything.
- if (size == new_size) return;
+ if (m_size == new_size) return;
// If new size is 0, clean up and go!
if (new_size == 0) {
- for (int i = 0; i < size; i++) delete data[i];
- delete [] data;
- data = NULL;
- size = 0;
+ for (int i = 0; i < m_size; i++) delete m_data[i];
+ if (m_data != NULL) delete [] m_data;
+ m_data = NULL;
+ m_size = 0;
return;
}
T** new_data = new T*[new_size];
assert(new_data != NULL); // Memory Allocation Error: Out of Memory?
- if (size < new_size) {
+ if (m_size < new_size) {
// Fill out the new portion of the array, if needed
- for (int i = size; i < new_size; i++) new_data[i] = new T;
- } else if (new_size < size) {
+ for (int i = m_size; i < new_size; i++) new_data[i] = new T;
+ } else if (new_size < m_size) {
// Clean up old portion of the array, if needed
- for (int i = new_size; i < size; i++) delete data[i];
+ for (int i = new_size; i < m_size; i++) delete m_data[i];
}
// Copy over old data...
- for (int i = 0; i < size && i < new_size; i++) {
- new_data[i] = data[i];
+ for (int i = 0; i < m_size && i < new_size; i++) {
+ new_data[i] = m_data[i];
}
- delete [] data; // remove old data if exists
- data = new_data;
+ if (m_data != NULL) delete [] m_data; // remove old data if exists
+ m_data = new_data;
- size = new_size;
+ m_size = new_size;
}
void Resize(int new_size, const T& empty_value)
{
- assert(new_size >= 0);
- int old_size = size;
+ int old_size = m_size;
Resize(new_size);
- if (new_size > old_size)
- for (int i = old_size; i < new_size; i++) *data[i] = empty_value;
+ for (int i = old_size; i < new_size; i++) *m_data[i] = empty_value;
}
- T& ElementAt(const int index)
+ T& operator[](const int index)
{
assert(index >= 0); // Lower Bounds Error
- assert(index < size); // Upper Bounds Error
- return *data[index]; // in range, so return element
- }
- const T& ElementAt(const int index) const
+ assert(index < m_size); // Upper Bounds Error
+ return *m_data[index];
+ }
+ const T& operator[](const int index) const
{
assert(index >= 0); // Lower Bounds Error
- assert(index < size); // Upper Bounds Error
- return *data[index]; // in range, so return element
+ assert(index < m_size); // Upper Bounds Error
+ return *m_data[index];
}
- T& operator[](const int index) { return ElementAt(index); }
- const T& operator[](const int index) const { return ElementAt(index); }
-
void Push(const T& value)
{
- Resize(size + 1);
- *data[size - 1] = value;
+ Resize(m_size + 1);
+ *m_data[m_size - 1] = value;
}
void SetAll(const T& value)
{
- for (int i = 0; i < size; i++) *data[i] = value;
+ for (int i = 0; i < m_size; i++) *m_data[i] = value;
}
};
Modified: development/source/tools/tSmartArray.h
===================================================================
--- development/source/tools/tSmartArray.h 2006-03-26 18:47:29 UTC (rev 532)
+++ development/source/tools/tSmartArray.h 2006-03-27 02:46:07 UTC (rev 533)
@@ -16,71 +16,56 @@
#include "tArray.h"
#endif
+// "I am so smart..."
+static const int SMRT_INCREASE_MINIMUM = 10;
+static const double SMRT_INCREASE_FACTOR = 1.5;
+static const double SMRT_SHRINK_TEST_FACTOR = 4.0;
+
template <class T> class tSmartArray
{
private:
- const int INCREASE_MIN = 10;
- const double INCREASE_FACTOR = 1.5;
- const double SHRINK_TEST_FACTOR = 4.0;
T* m_data; // Data Array
int m_size; // Raw Array Size
int m_active; // Active Size
-
- void SmartResize(int new_size)
- {
- assert(new_size > 0);
-
- // Make sure we're really changing the size...
- if (new_size == m_active) return;
-
- // Determine if we need to adjust the allocated array sizes...
- if (new_size > m_size || new_size * SHRINK_TEST_FACTOR < m_size) {
- int new_array_size = static_cast<int>(new_size * INCREASE_FACTOR);
- const int new_array_min = new_size + INCREASE_MINIMUM;
- if (new_array_min > new_array_size) new_array_size = new_array_min;
-
- // Resize
- }
-
- // And just change the active_size once we're sure it will be in range.
- active_size = new_size;
- }
-
public:
explicit tSmartArray(const int size = 0) : m_data(NULL), m_size(0), m_active(0) { ResizeClear(size); }
- tSmartArray(const tSmartArray& rhs) : data(NULL), m_size(0), m_active(0) { this->operator=(rhs); }
- tSmartArray(const tArray<T>& rhs) : data(NULL), m_size(0), m_active(0) { this->operator=(rhs); }
+ tSmartArray(const tSmartArray& rhs) : m_data(NULL), m_size(0), m_active(0) { this->operator=(rhs); }
+ tSmartArray(const tArray<T>& rhs) : m_data(NULL), m_size(0), m_active(0) { this->operator=(rhs); }
- ~tSmartArray() { delete [] data; }
+ ~tSmartArray() { delete [] m_data; }
tSmartArray& operator=(const tSmartArray& rhs)
{
- if (size != rhs.GetSize()) Resize(rhs.GetSize());
- for(int i = 0; i < size; i++) data[i] = rhs[i];
+ if (m_active != rhs.GetSize()) Resize(rhs.GetSize());
+ for(int i = 0; i < m_active; i++) m_data[i] = rhs[i];
return *this;
}
tSmartArray& operator=(const tArray<T>& rhs)
{
- if (size != rhs.GetSize()) Resize(rhs.GetSize());
- for(int i = 0; i < size; i++) data[i] = rhs[i];
+ if (m_active != rhs.GetSize()) Resize(rhs.GetSize());
+ for(int i = 0; i < m_active; i++) m_data[i] = rhs[i];
return *this;
}
- bool Good() const { return (data != NULL); }
- int GetSize() const { return size; }
+ bool Good() const { return (m_data != NULL); }
+ int GetSize() const { return m_active; }
void ResizeClear(const int in_size)
{
- size = in_size;
- assert(size >= 0); // Invalid size specified for array intialization
- if (data != NULL) delete [] data; // remove old data if exists
- if (size > 0) {
- data = new T[size]; // Allocate block for data
- assert(data != NULL); // Memory allocation error: Out of Memory?
+ assert(m_size >= 0);
+
+ m_active = in_size;
+ m_size = in_size;
+
+ if (m_data != NULL) delete [] m_data;
+
+ if (in_size > 0) {
+ m_data = new T[in_size]; // Allocate block for data
+ assert(m_data != NULL); // Memory allocation error: Out of Memory?
}
- else data = NULL;
+ else m_data = NULL;
}
void Resize(int new_size)
@@ -88,69 +73,69 @@
assert(new_size >= 0);
// If we're already at the size we want, don't bother doing anything.
- if (size == new_size) return;
+ if (new_size == m_active) return;
// If new size is 0, clean up and go!
if (new_size == 0) {
- delete [] data;
- data = NULL;
- size = 0;
+ if (m_data != NULL) delete [] m_data;
+ m_data = NULL;
+ m_size = 0;
+ m_active = 0;
return;
}
- T* new_data = new T[new_size];
- assert(new_data != NULL); // Memory Allocation Error: Out of Memory?
-
- // Copy over old data...
- for (int i = 0; i < size && i < new_size; i++) {
- new_data[i] = data[i];
+ // Determine if we need to adjust the allocated array sizes...
+ if (new_size > m_size || new_size * SMRT_SHRINK_TEST_FACTOR < m_size) {
+ int new_array_size = static_cast<int>(new_size * SMRT_INCREASE_FACTOR);
+ const int new_array_min = new_size + SMRT_INCREASE_MINIMUM;
+ if (new_array_min > new_array_size) new_array_size = new_array_min;
+
+
+ T* new_data = new T[new_array_size];
+ assert(new_data != NULL); // Memory Allocation Error: Out of Memory?
+
+ // Copy over old data...
+ for (int i = 0; i < m_active && i < new_size; i++) {
+ new_data[i] = m_data[i];
+ }
+ if (m_data != NULL) delete [] m_data; // remove old data if exists
+ m_data = new_data;
+
+ m_size = new_array_size;
}
- if (data != NULL) delete [] data; // remove old data if exists
- data = new_data;
- size = new_size;
+ m_active = new_size;
}
-
void Resize(int new_size, const T& empty_value)
{
- assert(new_size >= 0);
- int old_size = size;
+ int old_size = m_active;
Resize(new_size);
- if( new_size > old_size ) {
- for (int i = old_size; i < new_size; i++) {
- data[i] = empty_value;
- }
- }
+ for (int i = old_size; i < new_size; i++) m_data[i] = empty_value;
}
-
- T& ElementAt(const int index)
+ T& operator[](const int index)
{
- assert(index >= 0); // Lower Bounds Error
- assert(index < size); // Upper Bounds Error
- return data[index]; // in range, so return element
+ assert(index >= 0); // Lower Bounds Error
+ assert(index < m_active); // Upper Bounds Error
+ return m_data[index];
}
-
- const T& ElementAt(const int index) const
+ const T& operator[](const int index) const
{
- assert(index >= 0); // Lower Bounds Error
- assert(index < size); // Upper Bounds Error
- return data[index]; // in range, so return element
+ assert(index >= 0); // Lower Bounds Error
+ assert(index < m_active); // Upper Bounds Error
+ return m_data[index];
}
- T& operator[](const int index) { return ElementAt(index); }
- const T& operator[](const int index) const { return ElementAt(index); }
-
void Push(const T& value)
{
- Resize(size + 1);
- data[size - 1] = value;
+ Resize(m_active + 1);
+ m_data[m_active - 1] = value;
}
void SetAll(const T& value)
{
- for (int i = 0; i < size; i++) data[i] = value;
+ for (int i = 0; i < m_active; i++) m_data[i] = value;
}
};
More information about the Avida-cvs
mailing list