00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CH_BASEFABIMPLEM_H
00029 #define CH_BASEFABIMPLEM_H
00030 #include "BaseFabMacros.H"
00031
00032 #include "MayDay.H"
00033
00034
00035
00036
00037 template <class T>
00038 Arena* BaseFab<T>::s_Arena = NULL;
00039
00040 template <class T>
00041 inline
00042 int
00043 BaseFab<T>::nComp () const
00044 {
00045 return nvar;
00046 }
00047
00048 template <class T>
00049 inline
00050 const Box&
00051 BaseFab<T>::box () const
00052 {
00053 return domain;
00054 }
00055
00056 template <class T>
00057 inline
00058 const int*
00059 BaseFab<T>::size () const
00060 {
00061 return domain.size().getVect();
00062 }
00063
00064 template <class T>
00065 inline
00066 const IntVect&
00067 BaseFab<T>::smallEnd () const
00068 {
00069 return domain.smallEnd();
00070 }
00071
00072 template <class T>
00073 inline
00074 const IntVect&
00075 BaseFab<T>::bigEnd () const
00076 {
00077 return domain.bigEnd();
00078 }
00079
00080 template <class T>
00081 inline
00082 const int*
00083 BaseFab<T>::loVect () const
00084 {
00085 return domain.loVect();
00086 }
00087
00088 template <class T>
00089 inline
00090 const int*
00091 BaseFab<T>::hiVect () const
00092 {
00093 return domain.hiVect();
00094 }
00095
00096 template <class T>
00097 inline
00098 bool
00099 BaseFab<T>::contains (const BaseFab<T>& fab) const
00100 {
00101 return box().contains(fab.box()) && nvar <= fab.nvar;
00102 }
00103
00104 template <class T>
00105 inline
00106 bool
00107 BaseFab<T>::contains (const Box& bx) const
00108 {
00109 return box().contains(bx);
00110 }
00111
00112 template <class T>
00113 inline
00114 T*
00115 BaseFab<T>::dataPtr (int n)
00116 {
00117 assert(!(dptr == 0));
00118 assert((n >= 0) && (n < nvar));
00119 return &dptr[n*numpts];
00120 }
00121
00122 template <class T>
00123 inline
00124 const int*
00125 BaseFab<T>::nCompPtr () const
00126 {
00127 assert(!(dptr == 0));
00128 return &nvar;
00129 }
00130
00131 template <class T>
00132 inline
00133 const T*
00134 BaseFab<T>::dataPtr (int n) const
00135 {
00136 assert(!(dptr == 0));
00137 assert((n >= 0) && (n < nvar));
00138 return &dptr[n*numpts];
00139 }
00140
00141 template <class T>
00142 inline
00143 T&
00144 BaseFab<T>::operator() (const IntVect& p,
00145 int n)
00146 {
00147 assert(n >= 0);
00148 assert(n < nvar);
00149 assert(!(dptr == 0));
00150 assert(domain.contains(p));
00151
00152 return dptr[domain.index(p)+n*numpts];
00153 }
00154
00155 template <class T>
00156 inline
00157 T&
00158 BaseFab<T>::operator() (const IntVect& p)
00159 {
00160 assert(!(dptr == 0));
00161 assert(domain.contains(p));
00162
00163 return dptr[domain.index(p)];
00164 }
00165
00166 template <class T>
00167 inline
00168 const T&
00169 BaseFab<T>::operator() (const IntVect& p,
00170 int n) const
00171 {
00172 assert(n >= 0);
00173 assert(n < nvar);
00174 assert(!(dptr == 0));
00175 assert(domain.contains(p));
00176
00177 return dptr[domain.index(p)+n*numpts];
00178 }
00179
00180 template <class T>
00181 inline
00182 const T&
00183 BaseFab<T>::operator() (const IntVect& p) const
00184 {
00185 assert(!(dptr == 0));
00186 assert(domain.contains(p));
00187
00188 return dptr[domain.index(p)];
00189 }
00190
00191 template <class T>
00192 inline
00193 void
00194 BaseFab<T>::getVal (T* data,
00195 const IntVect& pos,
00196 int n,
00197 int numcomp) const
00198 {
00199 const int loc = domain.index(pos);
00200 const long size = domain.numPts();
00201
00202 assert(!(dptr == 0));
00203 assert(n >= 0 && n + numcomp <= nvar);
00204
00205 for (int k = 0; k < numcomp; k++)
00206 data[k] = dptr[loc+(n+k)*size];
00207 }
00208
00209 template <class T>
00210 inline
00211 void
00212 BaseFab<T>::getVal (T* data,
00213 const IntVect& pos) const
00214 {
00215 getVal(data,pos,0,nvar);
00216 }
00217
00218 template <class T>
00219 inline
00220 BaseFab<T>&
00221 BaseFab<T>::shift (const IntVect& v)
00222 {
00223 domain += v;
00224 return *this;
00225 }
00226
00227 template <class T>
00228 inline
00229 BaseFab<T>&
00230 BaseFab<T>::shift (int idir,
00231 int n_cell)
00232 {
00233 domain.shift(idir,n_cell);
00234 return *this;
00235 }
00236
00237 template <class T>
00238 inline
00239 BaseFab<T> &
00240 BaseFab<T>::shiftHalf (const IntVect& v)
00241 {
00242 domain.shiftHalf(v);
00243 return *this;
00244 }
00245
00246 template <class T>
00247 inline
00248 BaseFab<T> &
00249 BaseFab<T>::shiftHalf (int idir,
00250 int n_cell)
00251 {
00252 domain.shiftHalf(idir,n_cell);
00253 return *this;
00254 }
00255
00256 template <class T>
00257 inline
00258 void
00259 BaseFab<T>::setVal (T val)
00260 {
00261 performSetVal(val,box(), 0, nvar);
00262 }
00263
00264 template <class T>
00265 inline
00266 void
00267 BaseFab<T>::setVal (T x,
00268 const Box& bx,
00269 int n)
00270 {
00271 performSetVal(x,bx,n,1);
00272 }
00273
00274 template <class T>
00275 inline
00276 void
00277 BaseFab<T>::setVal (T x,
00278 int n)
00279 {
00280 performSetVal(x,domain,n,1);
00281 }
00282
00283 template <class T>
00284 inline
00285 void
00286 BaseFab<T>::setVal (T x,
00287 const Box& b,
00288 int ns,
00289 int num)
00290 {
00291 performSetVal(x,b,ns,num);
00292 }
00293
00294 template <class T>
00295 inline
00296 BaseFab<T>&
00297 BaseFab<T>::copy (const BaseFab<T>& src,
00298 const Box& srcbox,
00299 int srccomp,
00300 const Box& destbox,
00301 int destcomp,
00302 int numcomp)
00303 {
00304 assert(srcbox.sameSize(destbox));
00305 assert(src.box().contains(srcbox));
00306 assert(domain.contains(destbox));
00307 assert(srccomp >= 0 && srccomp+numcomp <= src.nComp());
00308 assert(destcomp >= 0 && destcomp+numcomp <= nvar);
00309 performCopy(src,srcbox,srccomp,destbox,destcomp,numcomp);
00310 return *this;
00311 }
00312
00313 template <class T>
00314 inline
00315 BaseFab<T>&
00316 BaseFab<T>::copy (const BaseFab<T>& src)
00317 {
00318 assert(nvar <= src.nvar);
00319 assert(domain.sameType(src.domain));
00320 Box overlap(domain);
00321 overlap &= src.domain;
00322 if (!overlap.isEmpty())
00323 performCopy(src,overlap,0,overlap,0,nvar);
00324 return *this;
00325 }
00326
00327 template <class T>
00328 inline
00329 BaseFab<T>&
00330 BaseFab<T>::copy (const BaseFab<T>& src,
00331 const Box& destbox)
00332 {
00333 assert(nvar <= src.nvar);
00334 assert(domain.contains(destbox));
00335 Box overlap(destbox);
00336 overlap &= src.domain;
00337 if (!overlap.isEmpty())
00338 performCopy(src,overlap,0,overlap,0,nvar);
00339 return *this;
00340 }
00341
00342 template <class T>
00343 inline
00344 BaseFab<T>&
00345 BaseFab<T>::copy (const BaseFab<T>& src,
00346 int srccomp,
00347 int destcomp,
00348 int numcomp)
00349 {
00350 assert(srccomp >= 0 && srccomp + numcomp <= src.nvar);
00351 assert(destcomp >= 0 && destcomp + numcomp <= nvar);
00352 Box overlap(domain);
00353 overlap &= src.domain;
00354 if (!overlap.isEmpty())
00355 performCopy(src,overlap,srccomp,overlap,destcomp,numcomp);
00356 return *this;
00357 }
00358
00359 template <class T>
00360 inline
00361 void
00362 BaseFab<T>::define ()
00363 {
00364 assert(nvar > 0);
00365 assert(dptr == 0);
00366 assert(numpts > 0);
00367
00368 #ifdef ENABLE_MEMORY_TRACKING
00369 if(s_Arena == NULL) s_Arena = new BArena(name().c_str());
00370 #else
00371 if(s_Arena == NULL) s_Arena = new BArena("");
00372 #endif
00373 if(s_Arena == NULL)
00374 MayDay::Error("malloc in basefab failed");
00375 truesize = nvar*numpts;
00376 dptr = static_cast<T*>(s_Arena->alloc(truesize*sizeof(T)));
00377
00378 #ifdef ENABLE_MEMORY_TRACKING
00379 s_Arena->bytes+=truesize*sizeof(T)+sizeof(BaseFab<T>);
00380 if(s_Arena->bytes > s_Arena->peak)
00381 s_Arena->peak = s_Arena->bytes;
00382 #endif
00383
00384
00385
00386
00387 T* ptr = dptr;
00388
00389 for (int i = 0; i < truesize; i++, ptr++)
00390 new (ptr) T;
00391 }
00392
00393 template <class T>
00394 inline
00395 void
00396 BaseFab<T>::undefine ()
00397 {
00398
00399
00400
00401
00402 if(dptr == 0) return;
00403 T* ptr = dptr;
00404
00405 for (int i = 0; i < truesize; i++, ptr++)
00406 ptr->~T();
00407
00408 s_Arena->free(dptr);
00409
00410 #ifdef ENABLE_MEMORY_TRACKING
00411 s_Arena->bytes-= truesize*sizeof(T)+sizeof(BaseFab<T>);
00412 #endif
00413
00414 dptr = 0;
00415 }
00416
00417 template <class T>
00418 inline
00419 BaseFab<T>::BaseFab ()
00420 :
00421 domain(Box()),
00422 nvar(0),
00423 numpts(0),
00424 truesize(0),
00425 dptr(0)
00426 {}
00427
00428 template <class T>
00429 inline
00430 BaseFab<T>::BaseFab (const Box& bx,
00431 int n)
00432 :
00433 domain(bx),
00434 nvar(n),
00435 numpts(bx.numPts()),
00436 dptr(0)
00437 {
00438 define();
00439 }
00440
00441
00442 template <class T>
00443 void
00444 BaseFab<T>::resize (const Box& b,
00445 int n)
00446 {
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461 undefine();
00462 nvar = n;
00463 domain = b;
00464 numpts = domain.numPts();
00465 define();
00466 }
00467
00468 template <class T>
00469 inline
00470 BaseFab<T>::~BaseFab ()
00471 {
00472 undefine();
00473 }
00474
00475 template <class T>
00476 inline
00477 void
00478 BaseFab<T>::clear ()
00479 {
00480 undefine();
00481
00482 domain = Box();
00483 nvar = 0;
00484 numpts = 0;
00485 }
00486
00487 template <class T>
00488 inline
00489 void
00490 BaseFab<T>::performCopy (const BaseFab<T>& src,
00491 const Box& srcbox,
00492 int srccomp,
00493 const Box& destbox,
00494 int destcomp,
00495 int numcomp)
00496 {
00497 assert(src.box().contains(srcbox));
00498 assert(box().contains(destbox));
00499 assert(destbox.sameSize(srcbox));
00500 assert(srccomp >= 0 && srccomp+numcomp <= src.nComp());
00501 assert(destcomp >= 0 && destcomp+numcomp <= nComp());
00502
00503 ForAllThisBNNXCBN(T, destbox, destcomp, numcomp, src, srcbox, srccomp)
00504 {
00505 thisR = srcR;
00506 } EndForTX
00507 }
00508
00509 template <class T>
00510 inline
00511 void
00512 BaseFab<T>::performSetVal (T val,
00513 const Box& bx,
00514 int ns,
00515 int num)
00516 {
00517 assert(domain.contains(bx));
00518 assert(ns >= 0 && ns + num <= nvar);
00519
00520 if (bx == domain)
00521 {
00522 T* data = &dptr[ns*numpts];
00523 for (long i = 0, N = num*numpts; i < N; i++)
00524 {
00525 *data++ = val;
00526 }
00527 }
00528 else
00529 {
00530 ForAllThisBNN(T,bx,ns,num)
00531 {
00532 thisR = val;
00533 } EndFor
00534 }
00535 }
00536
00537
00538 template <class T>
00539 inline
00540 void
00541 BaseFab<T>::copy(const Box& RegionFrom,
00542 const Interval& Cdest,
00543 const Box& RegionTo,
00544 const BaseFab<T>& src,
00545 const Interval& Csrc)
00546 {
00547 if((this == &src) && (RegionFrom == RegionTo)) return;
00548 assert(Cdest.size() == Csrc.size());
00549 copy(src, RegionFrom, Csrc.begin(), RegionTo,
00550 Cdest.begin(), Cdest.size());
00551 }
00552
00553 template <class T>
00554 inline
00555 void
00556 BaseFab<T>::linearOut(void* buf, const Box& R, const Interval& comps) const
00557 {
00558 T* buffer = (T*)buf;
00559 ForAllThisBNN(T,R,comps.begin(), comps.size())
00560 {
00561 *buffer = thisR;
00562 ++buffer;
00563 } EndFor;
00564 }
00565
00566 template <class T>
00567 inline
00568 void
00569 BaseFab<T>::linearIn(void* buf, const Box& R, const Interval& comps)
00570 {
00571 T* buffer = (T*)buf;
00572 ForAllThisBNN(T,R,comps.begin(), comps.size())
00573 {
00574 thisR = *buffer;
00575 ++buffer;
00576 } EndFor;
00577
00578 }
00579
00580 template <class T>
00581 inline
00582 int
00583 BaseFab<T>::size(const Box& box, const Interval& comps) const
00584 {
00585 return box.numPts()*sizeof(T)*comps.size();
00586 }
00587
00588 template <class T>
00589 inline
00590 std::string
00591 BaseFab<T>::name()
00592 {
00593 std::string rtn = (typeid(T)).name();
00594 return rtn;
00595 }
00596
00597 #endif
00598