[Avida-cvs] [avida-svn] r531 - in development: Avida.xcodeproj source/main source/tools

brysonda@myxo.css.msu.edu brysonda at myxo.css.msu.edu
Sun Mar 26 09:28:03 PST 2006


Author: brysonda
Date: 2006-03-26 12:28:02 -0500 (Sun, 26 Mar 2006)
New Revision: 531

Added:
   development/source/tools/tSmartArray.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/main/cOrganism.h
Log:
Add stub tSmartArray. Will encapsulate similar functionality to cCPUMemory, but in a generic form.

Also fix improperly committed cOrganism.h.

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2006-03-26 16:00:20 UTC (rev 530)
+++ development/Avida.xcodeproj/project.pbxproj	2006-03-26 17:28:02 UTC (rev 531)
@@ -116,6 +116,7 @@
 		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 */; };
@@ -515,6 +516,7 @@
 				7054A12409A7BEFC00038658 /* cThread.h in CopyFiles */,
 				7005A70709BA0FA90007E16E /* cTestCPUInterface.h in CopyFiles */,
 				701D51CD09C645F50009B4F8 /* cAvidaContext.h in CopyFiles */,
+				7093DB4109D6F50300DE7FEB /* tSmartArray.h in CopyFiles */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -637,6 +639,7 @@
 		70731668097C6E0C00815164 /* cASSymbol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cASSymbol.h; sourceTree = "<group>"; };
 		70731669097C6E0C00815164 /* cASSymbol.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cASSymbol.cc; sourceTree = "<group>"; };
 		707A687408E3750300576968 /* cHardwareTracer_SMT.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cHardwareTracer_SMT.h; sourceTree = "<group>"; };
+		7093DB4009D6F50300DE7FEB /* tSmartArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tSmartArray.h; sourceTree = "<group>"; };
 		70AA941909D486AE006A24C8 /* libtcmalloc.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libtcmalloc.a; sourceTree = BUILT_PRODUCTS_DIR; };
 		70B0864808F4972600FC65FE /* cLandscape.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cLandscape.h; sourceTree = "<group>"; };
 		70B0864B08F4972600FC65FE /* cLocalMutations.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cLocalMutations.h; sourceTree = "<group>"; };
@@ -1673,6 +1676,7 @@
 				70B3984E0947B29D0018F09D /* tManagedPointerArray.h */,
 				7054A11E09A7BEFC00038658 /* cThread.h */,
 				7054A11F09A7BEFC00038658 /* cThread.cc */,
+				7093DB4009D6F50300DE7FEB /* tSmartArray.h */,
 			);
 			path = tools;
 			sourceTree = "<group>";

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2006-03-26 16:00:20 UTC (rev 530)
+++ development/source/main/cOrganism.h	2006-03-26 17:28:02 UTC (rev 531)
@@ -91,8 +91,8 @@
   tBuffer<cOrgMessage> inbox;
   tBuffer<cOrgMessage> sent;
   
-  tBuffer<cOrgMessage*> m_net_pending;
-  t
+  //tBuffer<cOrgMessage*> m_net_pending;
+  
 
 #ifdef DEBUG
   bool initialized;      // Has this CPU been initialized yet, w/hardware.

Added: development/source/tools/tSmartArray.h
===================================================================
--- development/source/tools/tSmartArray.h	2006-03-26 16:00:20 UTC (rev 530)
+++ development/source/tools/tSmartArray.h	2006-03-26 17:28:02 UTC (rev 531)
@@ -0,0 +1,157 @@
+/*
+ *  tSmartArray.h
+ *  Avida
+ *
+ *  Created by David on 3/26/06.
+ *  Copyright 2006 Michigan State University. All rights reserved.
+ *
+ */
+
+#ifndef tSmartArray_h
+#define tSmartArray_h
+
+#include <assert.h>
+
+#ifndef tArray_h
+#include "tArray.h"
+#endif
+
+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() { delete [] data; }
+  
+  tSmartArray& operator=(const tSmartArray& rhs)
+  {
+    if (size != rhs.GetSize()) Resize(rhs.GetSize());
+    for(int i = 0; i < size; i++) 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];
+    return *this;
+  }
+  
+  bool Good() const { return (data != NULL); }
+  int GetSize() const { return size; }
+  
+  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;
+  }
+
+  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 new size is 0, clean up and go!
+    if (new_size == 0) {
+      delete [] data;
+      data = NULL;
+      size = 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];
+    }
+    if (data != NULL) delete [] data;  // remove old data if exists
+    data = new_data;
+    
+    size = new_size;
+  }
+  
+  
+  void Resize(int new_size, const T& empty_value)
+  {
+    assert(new_size >= 0);
+    int old_size = size;
+    Resize(new_size);
+    if( new_size > old_size ) {
+      for (int i = old_size; i < new_size; i++) {
+        data[i] = empty_value;
+      }
+    }
+  }
+  
+  
+  T& ElementAt(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 >= 0);    // Lower Bounds Error
+    assert(index < size);  // Upper Bounds Error
+    return data[index];    // in range, so return element
+  }
+  
+  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 < size; i++) data[i] = value;
+  }
+};
+
+#endif




More information about the Avida-cvs mailing list