Eneboo - Documentación para desarrolladores
|
00001 /* 00002 00003 Heap Layers: An Extensible Memory Allocation Infrastructure 00004 00005 Copyright (C) 2000-2003 by Emery Berger 00006 http://www.cs.umass.edu/~emery 00007 emery@cs.umass.edu 00008 00009 This program is free software; you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation; either version 2 of the License, or 00012 (at your option) any later version. 00013 00014 This program is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with this program; if not, write to the Free Software 00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 00023 */ 00024 00025 #ifndef _DYNARRAY_H_ 00026 #define _DYNARRAY_H_ 00027 00028 #include <assert.h> 00029 #include <cstdlib> 00030 00040 namespace HL { 00041 00042 template <class ObjType> 00043 class DynamicArray { 00044 public: 00045 DynamicArray (void) 00046 : internalArray (NULL), 00047 internalArrayLength (0) 00048 {} 00049 00050 ~DynamicArray (void) 00051 { 00052 clear(); 00053 } 00054 00056 inline void clear (void) { 00057 if (internalArray != NULL) { 00058 delete internalArray; 00059 internalArray = NULL; 00060 internalArrayLength = 0; 00061 //printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength); 00062 } 00063 } 00064 00066 inline const ObjType& operator[] (int index) const { 00067 assert (index < internalArrayLength); 00068 assert (index >= 0); 00069 return internalArray[index]; 00070 } 00071 00073 inline ObjType& operator[] (int index) { 00074 assert (index >= 0); 00075 if (index >= internalArrayLength) { 00076 00077 // This index is beyond the current size of the array. 00078 // Grow the array by doubling and copying the old array into the new. 00079 00080 const int newSize = index * 2 + 1; 00081 ObjType * arr = new ObjType[newSize]; 00082 #if MALLOC_TRACE 00083 printf ("m %x %d\n", arr, newSize * sizeof(ObjType)); 00084 #endif 00085 if (internalArray != NULL) { 00086 memcpy (arr, internalArray, internalArrayLength * sizeof(ObjType)); 00087 delete internalArray; 00088 #if MALLOC_TRACE 00089 printf ("f %x\n", internalArray); 00090 #endif 00091 } 00092 internalArray = arr; 00093 internalArrayLength = newSize; 00094 //printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength); 00095 } 00096 return internalArray[index]; 00097 } 00098 00104 inline void trim (int nelts) { 00105 00106 // Halve the array if the number of elements 00107 // drops below one-fourth of the array size. 00108 00109 if (internalArray != NULL) { 00110 if (nelts * 4 < internalArrayLength) { 00111 const int newSize = nelts * 2; 00112 ObjType * arr = new ObjType[newSize]; 00113 #if MALLOC_TRACE 00114 printf ("m %x %d\n", arr, newSize * sizeof(ObjType)); 00115 #endif 00116 memcpy (arr, internalArray, sizeof(ObjType) * nelts); 00117 delete internalArray; 00118 #if MALLOC_TRACE 00119 printf ("f %x\n", internalArray); 00120 #endif 00121 internalArray = arr; 00122 internalArrayLength = newSize; 00123 } 00124 assert (nelts <= internalArrayLength); 00125 } 00126 } 00127 00128 00129 private: 00130 00132 ObjType * internalArray; 00133 00135 int internalArrayLength; 00136 }; 00137 00138 }; 00139 00140 #endif