00001 00007 // written by David Hurwitz, March to May 1998. 00008 00009 #include <memory.h> 00010 #include <iostream> 00011 #include <iomanip> 00012 using namespace std; 00013 #include "charm++.h" 00014 #include "FreeEnergyAssert.h" 00015 #include "FreeEnergyGroup.h" 00016 00017 00018 AGroup::AGroup() { 00019 //------------------------------------------------------------------------ 00020 // make room for some ints. 00021 //------------------------------------------------------------------------ 00022 m_NumInts = 0; 00023 m_pInts = new int[kGroupNumToStart]; 00024 m_MaxNum = kGroupNumToStart; 00025 } 00026 00027 00028 AGroup::~AGroup() { 00029 //------------------------------------------------------------------------ 00030 // return borrowed memory to the free store. 00031 //------------------------------------------------------------------------ 00032 delete []m_pInts; 00033 } 00034 00035 00036 void AGroup::Clear() { 00037 //------------------------------------------------------------------------ 00038 // leave memory allocation alone. 00039 //------------------------------------------------------------------------ 00040 m_NumInts = 0; 00041 } 00042 00043 00044 void AGroup::Add(int AnInt) { 00045 //------------------------------------------------------------------------ 00046 // add an int to the list. if there's not enough room, make room. 00047 //------------------------------------------------------------------------ 00048 int* pInts; 00049 00050 // if there's no room for a new int 00051 if (m_NumInts == m_MaxNum) { 00052 // create an array with more space 00053 m_MaxNum *= kGroupMultiplier; 00054 pInts = new int[m_MaxNum]; 00055 // fast copy from the full array to the new one (memcpy(dest, src, bytes)) 00056 memcpy(pInts, m_pInts, sizeof(int)*m_NumInts); 00057 // return the space used for the full array 00058 delete []m_pInts; 00059 // point to the bigger array 00060 m_pInts = pInts; 00061 } 00062 00063 // add the int to the int array 00064 m_pInts[m_NumInts] = AnInt; 00065 m_NumInts++; 00066 } 00067 00068 00069 int AGroup::operator[] (int Index) { 00070 //------------------------------------------------------------------------ 00071 // return an int from this group of ints. 00072 // note, by returning int, rather than int&, this function only allows 00073 // retrieval of an int, not setting an int. to set, use add. 00074 //------------------------------------------------------------------------ 00075 ASSERT((Index>=0) && (Index<m_NumInts)); 00076 return(m_pInts[Index]); 00077 } 00078 00079 00080 AGroup& AGroup::operator= (AGroup& Group) { 00081 //------------------------------------------------------------------------ 00082 // make this object identical to the passed one. 00083 //------------------------------------------------------------------------ 00084 // if there's not enough room here for Group's array 00085 if (m_MaxNum < Group.m_MaxNum) { 00086 // free this array and allocate space for the bigger one 00087 delete []m_pInts; 00088 m_MaxNum = Group.m_MaxNum; 00089 m_pInts = new int[m_MaxNum]; 00090 } 00091 // fast copy from the passed array to this one (memcpy(dest, src, bytes)) 00092 m_NumInts = Group.m_NumInts; 00093 memcpy(m_pInts, Group.m_pInts, sizeof(int)*m_NumInts); 00094 return(*this); 00095 } 00096 00097 void AGroup::List(int NumToList) { 00098 //------------------------------------------------------------------------ 00099 // list NumToList integers in the group to standard out. 00100 // if NumToList is negative, list them all. 00101 //------------------------------------------------------------------------ 00102 int i; 00103 00104 if (NumToList < 0) { 00105 NumToList = m_NumInts; 00106 } 00107 for (i=0; i<NumToList; i++) { 00108 // cout << setw(10) << i << " " << setw(10) << (*this)[i] << std::endl; 00109 CkPrintf("%10d %10d\n", i, (*this)[i]); 00110 } 00111 }
1.3.9.1