[Avida-cvs] [avida-svn] r417 - in trunk: Avida2.xcodeproj source/cpu source/tools

brysonda@myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Dec 8 13:21:40 PST 2005


Author: brysonda
Date: 2005-12-08 16:21:40 -0500 (Thu, 08 Dec 2005)
New Revision: 417

Added:
   trunk/source/tools/tManagedPointerArray.h
Modified:
   trunk/Avida2.xcodeproj/project.pbxproj
   trunk/source/cpu/cHardwareSMT.h
Log:
Backport tManagedPointerArray into Avida 2.4 (trunk).

Modified: trunk/Avida2.xcodeproj/project.pbxproj
===================================================================
--- trunk/Avida2.xcodeproj/project.pbxproj	2005-12-08 21:06:26 UTC (rev 416)
+++ trunk/Avida2.xcodeproj/project.pbxproj	2005-12-08 21:21:40 UTC (rev 417)
@@ -574,6 +574,7 @@
 		70B08B8F08FB2E5500FC65FE /* tVector.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = tVector.h; sourceTree = "<group>"; };
 		70B08B9008FB2E6B00FC65FE /* cTools.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cTools.cc; sourceTree = "<group>"; };
 		70B08B9108FB2E6B00FC65FE /* cWeightedIndex.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cWeightedIndex.cc; sourceTree = "<group>"; };
+		70BBB26C0948DB7200796167 /* tManagedPointerArray.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = tManagedPointerArray.h; sourceTree = "<group>"; };
 		70C1EF4608C393BA00F50912 /* cCodeLabel.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cCodeLabel.cc; sourceTree = "<group>"; };
 		70C1EF4708C393BA00F50912 /* cCodeLabel.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cCodeLabel.h; sourceTree = "<group>"; };
 		70C1EF5808C3948C00F50912 /* cCPUMemory.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cCPUMemory.cc; sourceTree = "<group>"; };
@@ -2727,6 +2728,7 @@
 		DCC314D8076253A2008F7A48 /* tools */ = {
 			isa = PBXGroup;
 			children = (
+				70BBB26C0948DB7200796167 /* tManagedPointerArray.h */,
 				70B08B9008FB2E6B00FC65FE /* cTools.cc */,
 				70B08B9108FB2E6B00FC65FE /* cWeightedIndex.cc */,
 				70B08B8008FB2E5500FC65FE /* cTools.h */,

Modified: trunk/source/cpu/cHardwareSMT.h
===================================================================
--- trunk/source/cpu/cHardwareSMT.h	2005-12-08 21:06:26 UTC (rev 416)
+++ trunk/source/cpu/cHardwareSMT.h	2005-12-08 21:21:40 UTC (rev 417)
@@ -36,8 +36,8 @@
 #ifndef STRING_HH
 #include "cString.h"
 #endif
-#ifndef TARRAY_HH
-#include "tArray.h"
+#ifndef tManagePointerArray_h
+#include "tManagedPointerArray.h"
 #endif
 #ifndef TINSTLIB_H
 #include "tInstLib.h"
@@ -78,7 +78,7 @@
   cCPUStack m_global_stacks[nHardwareSMT::NUM_GLOBAL_STACKS];
 	
   // Memory
-  tArray<cCPUMemory> m_mem_array;
+  tManagedPointerArray<cCPUMemory> m_mem_array;
   tHashTable<int, int> m_mem_lbls;
 
   // Threads

Added: trunk/source/tools/tManagedPointerArray.h
===================================================================
--- trunk/source/tools/tManagedPointerArray.h	2005-12-08 21:06:26 UTC (rev 416)
+++ trunk/source/tools/tManagedPointerArray.h	2005-12-08 21:21:40 UTC (rev 417)
@@ -0,0 +1,141 @@
+/*
+ *  tManagedPointerArray.h
+ *  Avida
+ *
+ *  Created by David on 12/7/05.
+ *  Copyright 2005 Michigan State University. All rights reserved.
+ *
+ */
+
+#ifndef tManagedPointerArray_h
+#define tManagedPointerArray_h
+
+#include <assert.h>
+
+#ifndef tArray_h
+#include "tArray.h"
+#endif
+
+template <class T> class tManagedPointerArray {  
+protected:
+  T** data;  // Data Elements
+  int 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); }
+
+  ~tManagedPointerArray()
+  { 
+    for (int i = 0; i < size; i++) delete data[i];
+    delete [] data;
+  }
+  
+  tManagedPointerArray& operator=(const tManagedPointerArray& rhs)
+  {
+    if (size != rhs.GetSize()) Resize(rhs.GetSize());
+    for(int i = 0; i < size; i++) *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];
+    return *this;
+  }
+  
+  bool Good() const { return (data != NULL); }
+  int GetSize() const { return size; }
+  
+  void ResizeClear(const int in_size)
+  {
+    for (int i = 0; i < size; i++) delete data[i];
+    delete [] data;  // remove old data if exists
+    
+    size = in_size;
+    assert(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;
+  }
+  
+  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) {
+      for (int i = 0; i < size; i++) delete data[i];
+      delete [] data;
+      data = NULL;
+      size = 0;
+      return;
+    }
+    
+    T** new_data = new T*[new_size];
+    assert(new_data != NULL); // Memory Allocation Error: Out of Memory?
+    
+    if (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) {
+      // Clean up old portion of the array, if needed
+      for (int i = new_size; i < size; i++) delete data[i];
+    }
+    
+    // Copy over old data...
+    for (int i = 0; i < size && i < new_size; i++) {
+      new_data[i] = data[i];
+    }
+    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


Property changes on: trunk/source/tools/tManagedPointerArray.h
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the Avida-cvs mailing list