cgma
|
00001 //- Class: ArrayBasedContainer 00002 //- Description: Base class for container classed implemented as arrays. 00003 //- Contains 'void*' entities. This class allows copying 00004 //- between container types. 00005 //- Assumptions: Containers derived from this are implemented as arrays. 00006 //- 00007 //- Owner: Paul Kinney 00008 //- Checked by: 00009 //- Version: $Id: 00010 00011 #ifndef ARRAYBASEDCONTAINER_HPP 00012 #define ARRAYBASEDCONTAINER_HPP 00013 00014 #include "CubitDefines.h" 00015 #include "MemoryManager.hpp" 00016 #include "CGMUtilConfigure.h" 00017 #include <cstring> 00018 00019 const int INCREMENT = 8; //- Default list size increment 00020 00021 class CUBIT_UTIL_EXPORT ArrayBasedContainer 00022 { 00023 00024 public: 00025 00026 ArrayBasedContainer (int size); 00027 //- Constructor: Create a list with initial size {size}. The list 00028 //- will be grown by {size} each time it is filled. Memory for the 00029 //- list is not allocated until the first element is inserted using 00030 //- {insertLink}. 00031 //- If {size} is zero, the default increment ({INCREMENT}) will be used 00032 //- From an efficiency standpoint, it is very important that the 00033 //- increment be set large enough to reduce the number of list 00034 //- growths, but small enough to not waste memory. 00035 //- It is more efficient to sligthly overestimate the size than 00036 //- to underestimate the size. 00037 00038 ArrayBasedContainer(const ArrayBasedContainer& copy_from); 00039 //- Copy Constructor 00040 00041 virtual ~ArrayBasedContainer(); 00042 //- Destructor: Free all resources used by this list. 00043 00044 int size() const; //- Return the number of items stored in the list. 00045 00046 void clean_out(); 00047 //- Delete all elements in the list, reset the pointer to zero. 00048 //- Does not release memory already allocated for the list. This 00049 //- call is more efficient creating a new list repeatadly within 00050 //- a loop. 00051 00052 void shrink(int k); 00053 //- itemCount -= k; Doesn't change array elements. 00054 00055 ArrayBasedContainer& operator=(const ArrayBasedContainer&); 00056 //- Create a copy of a list. 00057 00058 ArrayBasedContainer& operator+=(const ArrayBasedContainer&); 00059 //- Append one list to the end of the other list. 00060 00061 ArrayBasedContainer& operator-=(const ArrayBasedContainer&); 00062 //- Subtract one container from the other. Preserves order of first container. 00063 00064 virtual void compress(); 00065 //- remove all the NULLs, preserve order 00066 00067 static unsigned int current_allocated_memory(); 00068 static unsigned int maximum_allocated_memory(); 00069 00070 protected: 00071 00072 void **listArray; //- array of items in the list 00073 int itemCount; //- number of items in list 00074 int listLength; //- size of {listArray} 00075 Bit poolAllocated : 1; 00076 //- TRUE if allocated from special memory pool, FALSE if normal allocation 00077 Bit listIsSorted : 1; //- Used in SDLList 00078 Bit listIncrement : 30; //- amount by which to grow list 00079 //- This is limited to 1.07e9 00080 00081 void lengthen_list(); 00082 00083 private: 00084 static unsigned int currentAllocatedMemory; 00085 static unsigned int maxAllocatedMemory; 00086 }; 00087 00088 inline void ArrayBasedContainer::shrink(int k) 00089 { 00090 if (k > 0) 00091 { 00092 if (itemCount >= k) 00093 itemCount -= k; 00094 else 00095 itemCount = 0; 00096 } 00097 } 00098 00099 inline int ArrayBasedContainer::size() const { return itemCount; } 00100 00101 inline void ArrayBasedContainer::clean_out () 00102 { 00103 itemCount = 0; 00104 } 00105 00106 00107 // This class is used internally by ArrayBasedContainer to maintain 00108 // a large pool of memory of size {INCREMENT} (The default size for 00109 // ArrayBasedContainers). If a list of size {INCREMENT} is constructed, 00110 // the memory is allocated from this pool rather than allocating the memory 00111 // from the system global new routine. 00112 class ArrayMemory 00113 { 00114 public: 00115 ArrayMemory() { memset(mem, 0, sizeof(mem)); } 00116 00117 SetDynamicMemoryAllocation(memoryManager) 00118 //- overloaded new and delete operators 00119 00120 private: 00121 00122 static MemoryManager memoryManager; 00123 00124 void* mem[INCREMENT]; 00125 }; 00126 00127 #endif //- ARRAYBASEDCONTAINER_HPP 00128