00001 #ifdef CH_LANG_CC 00002 /* 00003 * _______ __ 00004 * / ___/ / ___ __ _ / / ___ 00005 * / /__/ _ \/ _ \/ V \/ _ \/ _ \ 00006 * \___/_//_/\___/_/_/_/_.__/\___/ 00007 * Please refer to Copyright.txt, in Chombo's root directory. 00008 */ 00009 #endif 00010 00011 #ifndef _LAYOUTDATAI_H_ 00012 #define _LAYOUTDATAI_H_ 00013 00014 #include "MayDay.H" 00015 #include "DataIterator.H" 00016 #include "SPMD.H" 00017 #include "CH_Timer.H" 00018 #include "NamespaceHeader.H" 00019 00020 // template < > void LayoutData<Real>::allocate(); 00021 00022 // template < > void LayoutData<int>::allocate(); 00023 00024 template<class T> 00025 inline const T& LayoutData<T>::operator [] (const DataIndex& a_index) const 00026 { 00027 CH_assert(m_boxLayout.check(a_index)); 00028 00029 // using a DataIndex for data on another processor - 00030 // if you are reading this error in a debugger, then you haven't used 00031 // addBox(Box, int) correctly, or you are using a LayoutIterator to 00032 // access a data holder. 00033 // CH_assert(m_boxLayout.procID(a_index) == procID()); 00034 00035 return *(m_vector[a_index.datInd()]); 00036 } 00037 00038 template<class T> 00039 inline const T& LayoutData<T>::operator [] (const DataIterator& a_index) const 00040 { 00041 return (*this)[a_index()]; 00042 } 00043 00044 template<class T> 00045 inline T& LayoutData<T>::operator [] (const DataIndex& a_index) 00046 { 00047 CH_assert(m_boxLayout.check(a_index)); 00048 00049 // using a DataIndex for data on another processor - 00050 // if you are reading this error in a debugger, then you haven't used 00051 // addBox(Box, int) correctly, or you are using a LayoutIterator to 00052 // access a data holder. 00053 //CH_assert(m_boxLayout.procID(a_index) == procID()); 00054 00055 return *(m_vector[a_index.datInd()]); 00056 } 00057 00058 template<class T> 00059 inline T& LayoutData<T>::operator [] (const DataIterator& a_index) 00060 { 00061 00062 return (*this)[a_index()]; 00063 } 00064 00065 template<class T> 00066 inline Box LayoutData<T>::box(const DataIndex& a_index) const 00067 { 00068 return m_boxLayout.get(a_index); 00069 } 00070 00071 template<class T> 00072 inline Box LayoutData<T>::box(const DataIterator& a_index) const 00073 { 00074 return m_boxLayout.get(a_index()); 00075 } 00076 00077 template<class T> 00078 inline DataIterator LayoutData<T>::dataIterator() const 00079 { 00080 return m_boxLayout.dataIterator(); 00081 } 00082 00083 template<class T> 00084 inline TimedDataIterator LayoutData<T>::timedDataIterator() const 00085 { 00086 return m_boxLayout.timedDataIterator(); 00087 } 00088 00089 template<class T> 00090 inline LayoutData<T>::LayoutData() 00091 { 00092 m_boxLayout.close(); 00093 m_callDelete = true; 00094 } 00095 00096 template<class T> 00097 inline LayoutData<T>::LayoutData(const BoxLayout& a_dp) 00098 : 00099 m_boxLayout(a_dp) 00100 { 00101 CH_assert(a_dp.isClosed()); 00102 m_callDelete = true; 00103 allocate(); 00104 } 00105 00106 template<class T> 00107 inline void LayoutData<T>::define(const BoxLayout& a_dp) 00108 { 00109 CH_assert(a_dp.isClosed()); 00110 m_boxLayout = a_dp; 00111 allocate(); 00112 } 00113 00114 template<class T> 00115 inline LayoutData<T>::~LayoutData() 00116 { 00117 CH_TIME("~LayoutData"); 00118 if (m_callDelete == true) 00119 { 00120 DataIterator it(dataIterator()); 00121 for (; it.ok(); ++it) 00122 { 00123 delete m_vector[it().datInd()]; 00124 } 00125 } 00126 } 00127 00128 template<class T> 00129 inline void LayoutData<T>::allocate() 00130 { 00131 m_callDelete = true; 00132 00133 for (unsigned int i = 0; i < m_vector.size(); ++i) 00134 { 00135 delete m_vector[i]; 00136 m_vector[i] = NULL; 00137 } 00138 00139 DataIterator it(dataIterator()); 00140 m_vector.resize(it.size(), NULL); 00141 00142 for (; it.ok(); ++it) 00143 { 00144 unsigned int index = it().datInd(); 00145 if (m_vector[index] == NULL) 00146 { 00147 m_vector[index] = new T; 00148 if (m_vector[index] == NULL) 00149 { 00150 MayDay::Error("OutOfMemory in LayoutData::allocate"); 00151 } 00152 } 00153 } 00154 } 00155 00156 #include "NamespaceFooter.H" 00157 #endif