00001 /* 00002 ** (c) 1996-2000 The Regents of the University of California (through 00003 ** E.O. Lawrence Berkeley National Laboratory), subject to approval by 00004 ** the U.S. Department of Energy. Your use of this software is under 00005 ** license -- the license agreement is attached and included in the 00006 ** directory as license.txt or you may contact Berkeley Lab's Technology 00007 ** Transfer Department at TTD@lbl.gov. NOTICE OF U.S. GOVERNMENT RIGHTS. 00008 ** The Software was developed under funding from the U.S. Government 00009 ** which consequently retains certain rights as follows: the 00010 ** U.S. Government has been granted for itself and others acting on its 00011 ** behalf a paid-up, nonexclusive, irrevocable, worldwide license in the 00012 ** Software to reproduce, prepare derivative works, and perform publicly 00013 ** and display publicly. Beginning five (5) years after the date 00014 ** permission to assert copyright is obtained from the U.S. Department of 00015 ** Energy, and subject to any subsequent five (5) year renewals, the 00016 ** U.S. Government is granted for itself and others acting on its behalf 00017 ** a paid-up, nonexclusive, irrevocable, worldwide license in the 00018 ** Software to reproduce, prepare derivative works, distribute copies to 00019 ** the public, perform publicly and display publicly, and to permit 00020 ** others to do so. 00021 */ 00022 00023 #ifndef BL_PARRAY_H 00024 #define BL_PARRAY_H 00025 // 00026 // $Id: PArray.H,v 1.23 2001/07/31 22:43:19 lijewski Exp $ 00027 // 00028 #include <Array.H> 00029 00030 // 00031 // : 00033 00038 enum PArrayPolicy 00039 { 00040 PArrayNoManage = 0, 00041 PArrayManage = 1 00042 }; 00043 00044 // 00045 // : 00047 00067 template <class T> 00068 class PArray 00069 { 00070 public: 00071 00076 explicit PArray (PArrayPolicy _managed = PArrayNoManage); 00077 00082 explicit PArray (int len, 00083 PArrayPolicy _managed = PArrayNoManage); 00084 00090 ~PArray (); 00091 // 00093 // 00094 bool defined (int i) const; 00095 00100 const T& operator[] (int i) const; 00101 00106 T& operator[] (int i); 00107 00112 const T& get (int i) const; 00113 00118 T& get (int i); 00119 00124 void set (int i, 00125 T* elem); 00126 // 00128 // 00129 int size () const; 00130 00136 void clear (); 00137 00143 void clear (int i); 00144 00154 void resize (int newsize); 00155 00163 void resize (int newsize, 00164 PArrayPolicy newmanagedpolicy); 00165 00170 T* remove (int i); 00171 00172 protected: 00173 // 00174 // The underlying representation. 00175 // 00176 Array<void*> vp; 00177 // 00178 // The memory management policy. 00179 // 00180 PArrayPolicy managed; 00181 00182 public: 00183 // 00184 // Disallow these. FIXME 00185 // 00186 PArray (const PArray<T>&); 00187 // PArray<T> operator= (const PArray<T>&); 00188 }; 00189 00190 template <class T> 00191 PArray<T>::PArray (const PArray<T>& c) 00192 { 00193 BL_ASSERT(c.size()==0); 00194 } 00195 00196 template <class T> 00197 PArray<T>::PArray (PArrayPolicy _managed) 00198 : 00199 managed(_managed) 00200 {} 00201 00202 template <class T> 00203 inline 00204 bool 00205 PArray<T>::defined (int i) const 00206 { 00207 return vp[i] != 0; 00208 } 00209 00210 template <class T> 00211 inline 00212 const T& 00213 PArray<T>::operator[] (int i) const 00214 { 00215 BL_ASSERT(vp[i] != 0); 00216 return *((T*)(vp[i])); 00217 } 00218 00219 template <class T> 00220 inline 00221 T& 00222 PArray<T>::operator[] (int i) 00223 { 00224 BL_ASSERT(vp[i] != 0); 00225 return *((T*)(vp[i])); 00226 } 00227 00228 template <class T> 00229 inline 00230 const T& 00231 PArray<T>::get (int i) const 00232 { 00233 BL_ASSERT(vp[i] != 0); 00234 return *((T*)(vp[i])); 00235 } 00236 00237 template <class T> 00238 inline 00239 T& 00240 PArray<T>::get (int i) 00241 { 00242 BL_ASSERT(vp[i] != 0); 00243 return *((T*)(vp[i])); 00244 } 00245 00246 template <class T> 00247 inline 00248 void 00249 PArray<T>::set (int i, 00250 T* elem) 00251 { 00252 BL_ASSERT(vp[i] == 0); 00253 vp[i] = elem; 00254 } 00255 00256 template <class T> 00257 inline 00258 int 00259 PArray<T>::size () const 00260 { 00261 return vp.size(); 00262 } 00263 00264 template <class T> 00265 T* 00266 PArray<T>::remove (int i) 00267 { 00268 BL_ASSERT(vp[i] != 0); 00269 void* tmp = vp[i]; 00270 vp[i] = 0; 00271 return (T*) tmp; 00272 } 00273 00274 template <class T> 00275 void 00276 PArray<T>::clear (int n) 00277 { 00278 if (managed) 00279 delete ((T*)(vp[n])); 00280 vp[n] = 0; 00281 } 00282 00283 template <class T> 00284 PArray<T>::PArray (int len, 00285 PArrayPolicy _managed) 00286 : 00287 vp(len), 00288 managed(_managed) 00289 { 00290 for (int i = 0; i < size(); i++) 00291 vp[i] = 0; 00292 } 00293 00294 template <class T> 00295 void 00296 PArray<T>::clear () 00297 { 00298 if (managed) 00299 { 00300 for (int i = 0; i < size(); ++i) 00301 { 00302 delete ((T*)(vp[i])); 00303 } 00304 } 00305 for (int i = 0; i < size(); ++i) 00306 vp[i] = 0; 00307 } 00308 00309 template <class T> 00310 PArray<T>::~PArray () 00311 { 00312 clear(); 00313 } 00314 00315 template <class T> 00316 void 00317 PArray<T>::resize (int newlen) 00318 { 00319 void** ovp = vp.size() ? &vp[0] : 0; 00320 00321 int oldlen = vp.size(); 00322 00323 vp.resize(newlen); 00324 00325 for (int i = oldlen; i < newlen; ++i) 00326 vp[i] = 0; 00327 00328 if (managed) 00329 { 00330 for (int i = newlen; i < oldlen; i++) 00331 { 00332 delete ((T*)(ovp[i])); 00333 ovp[i] = 0; 00334 } 00335 } 00336 } 00337 00338 template <class T> 00339 void 00340 PArray<T>::resize (int newlen, 00341 PArrayPolicy newmanagedpolicy) 00342 { 00343 if (newmanagedpolicy == managed) 00344 { 00345 resize(newlen); 00346 } 00347 else 00348 { 00349 clear(); 00350 resize(newlen); 00351 managed = newmanagedpolicy; 00352 } 00353 } 00354 00355 #endif /*BL_PARRAY_H*/