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 00021 // template < > void LayoutData<Real>::allocate(); 00022 00023 // template < > void LayoutData<int>::allocate(); 00024 00025 template<class T> 00026 inline const T& LayoutData<T>::operator [] (const DataIndex& a_index) const 00027 { 00028 CH_assert(m_boxLayout.check(a_index)); 00029 00030 // using a DataIndex for data on another processor - 00031 // if you are reading this error in a debugger, then you haven't used 00032 // addBox(Box, int) correctly, or you are using a LayoutIterator to 00033 // access a data holder. 00034 // CH_assert(m_boxLayout.procID(a_index) == procID()); 00035 00036 return *(m_vector[m_boxLayout.lindex(a_index)]); 00037 } 00038 00039 template<class T> 00040 inline const T& LayoutData<T>::operator [] (const DataIterator& a_index) const 00041 { 00042 return (*this)[a_index()]; 00043 } 00044 00045 template<class T> 00046 inline T& LayoutData<T>::operator [] (const DataIndex& a_index) 00047 { 00048 CH_assert(m_boxLayout.check(a_index)); 00049 00050 // using a DataIndex for data on another processor - 00051 // if you are reading this error in a debugger, then you haven't used 00052 // addBox(Box, int) correctly, or you are using a LayoutIterator to 00053 // access a data holder. 00054 //CH_assert(m_boxLayout.procID(a_index) == procID()); 00055 00056 return *(m_vector[m_boxLayout.lindex(a_index)]); 00057 } 00058 00059 template<class T> 00060 inline T& LayoutData<T>::operator [] (const DataIterator& a_index) 00061 { 00062 00063 return (*this)[a_index()]; 00064 } 00065 00066 template<class T> 00067 inline Box LayoutData<T>::box(const DataIndex& a_index) const 00068 { 00069 return m_boxLayout.get(a_index); 00070 } 00071 00072 template<class T> 00073 inline Box LayoutData<T>::box(const DataIterator& a_index) const 00074 { 00075 return m_boxLayout.get(a_index()); 00076 } 00077 00078 template<class T> 00079 inline DataIterator LayoutData<T>::dataIterator() const 00080 { 00081 return m_boxLayout.dataIterator(); 00082 } 00083 00084 template<class T> 00085 inline TimedDataIterator LayoutData<T>::timedDataIterator() const 00086 { 00087 return m_boxLayout.timedDataIterator(); 00088 } 00089 00090 template<class T> 00091 inline LayoutData<T>::LayoutData() 00092 { 00093 m_boxLayout.close(); 00094 m_callDelete = true; 00095 } 00096 00097 template<class T> 00098 inline LayoutData<T>::LayoutData(const BoxLayout& a_dp) 00099 : 00100 m_boxLayout(a_dp) 00101 { 00102 CH_assert(a_dp.isClosed()); 00103 m_callDelete = true; 00104 allocate(); 00105 } 00106 00107 template<class T> 00108 inline void LayoutData<T>::define(const BoxLayout& a_dp) 00109 { 00110 CH_assert(a_dp.isClosed()); 00111 m_boxLayout = a_dp; 00112 allocate(); 00113 } 00114 00115 template<class T> 00116 inline LayoutData<T>::~LayoutData() 00117 { 00118 CH_TIME("~LayoutData"); 00119 if (m_callDelete == true) 00120 { 00121 for (DataIterator it(dataIterator()); it.ok(); ++it) 00122 { 00123 delete m_vector[m_boxLayout.lindex(it())]; 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 = m_boxLayout.lindex(it()); 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