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 #include <iostream>
00027
00028 #include <BLassert.H>
00029 #include <BoxArray.H>
00030
00031 void
00032 BoxArray::reserve (long _truesize)
00033 {
00034 if (!m_ref.unique())
00035 uniqify();
00036 m_ref->m_abox.reserve(_truesize);
00037 }
00038
00039 BoxArray::Ref::Ref () {}
00040
00041 BoxArray::BoxArray ()
00042 :
00043 m_ref(new BoxArray::Ref)
00044 {}
00045
00046 BoxArray::BoxArray (const Box& bx)
00047 :
00048 m_ref(new BoxArray::Ref(1))
00049 {
00050 m_ref->m_abox[0] = bx;
00051 }
00052
00053 BoxArray::Ref::Ref (const BoxList& bl)
00054 {
00055 define(bl);
00056 }
00057
00058 BoxArray::BoxArray (const BoxList& bl)
00059 :
00060 m_ref(new BoxArray::Ref(bl))
00061 {}
00062
00063 BoxArray::Ref::Ref (std::istream& is)
00064 {
00065 define(is);
00066 }
00067
00068 BoxArray::BoxArray (const BoxArray& rhs)
00069 :
00070 m_ref(rhs.m_ref)
00071 {}
00072
00073 BoxArray::Ref::Ref (size_t size)
00074 :
00075 m_abox(size)
00076 {}
00077
00078 BoxArray::BoxArray (size_t size)
00079 :
00080 m_ref(new BoxArray::Ref(size))
00081 {}
00082
00083 BoxArray::BoxArray (const Box* bxvec,
00084 int nbox)
00085 :
00086 m_ref(new BoxArray::Ref(nbox))
00087 {
00088 for (int i = 0; i < nbox; i++)
00089 m_ref->m_abox[i] = *bxvec++;
00090 }
00091
00092 BoxArray::Ref::Ref (const Ref& rhs)
00093 :
00094 m_abox(rhs.m_abox)
00095 {}
00096
00097 BoxArray&
00098 BoxArray::operator= (const BoxArray& rhs)
00099 {
00100 m_ref = rhs.m_ref;
00101 return *this;
00102 }
00103
00104 void
00105 BoxArray::uniqify ()
00106 {
00107 m_ref = new Ref(*m_ref);
00108 }
00109
00110 void
00111 BoxArray::clear ()
00112 {
00113 if (!m_ref.unique())
00114 uniqify();
00115 m_ref->m_abox.clear();
00116 }
00117
00118 void
00119 BoxArray::resize (int len)
00120 {
00121 if (!m_ref.unique())
00122 uniqify();
00123 m_ref->m_abox.resize(len);
00124 }
00125
00126 void
00127 BoxArray::set (int i,
00128 const Box& ibox)
00129 {
00130 if (!m_ref.unique())
00131 uniqify();
00132 m_ref->m_abox.set(i, ibox);
00133 }
00134
00135
00136
00137
00138 #define BL_IGNORE_MAX 100000
00139
00140 void
00141 BoxArray::readFrom (std::istream& is)
00142 {
00143 BL_ASSERT(size() == 0);
00144 if (!m_ref.unique())
00145 uniqify();
00146 m_ref->define(is);
00147 }
00148
00149 void
00150 BoxArray::Ref::define (std::istream& is)
00151 {
00152
00153
00154
00155 BL_ASSERT(m_abox.size() == 0);
00156 int maxbox;
00157 unsigned long hash;
00158 is.ignore(BL_IGNORE_MAX, '(') >> maxbox >> hash;
00159 m_abox.resize(maxbox);
00160 for (int i = 0; i < m_abox.size(); i++)
00161 is >> m_abox.get(i);
00162 is.ignore(BL_IGNORE_MAX, ')');
00163 if (is.fail())
00164 BoxLib::Error("BoxArray::define(istream&) failed");
00165 }
00166
00167 void
00168 BoxArray::define (const BoxList& bl)
00169 {
00170 BL_ASSERT(size() == 0);
00171 if (!m_ref.unique())
00172 uniqify();
00173 m_ref->define(bl);
00174 }
00175
00176 void
00177 BoxArray::Ref::define (const BoxList& bl)
00178 {
00179 BL_ASSERT(m_abox.size() == 0);
00180 m_abox.resize(bl.size());
00181 int count = 0;
00182 for (BoxList::const_iterator bli = bl.begin(); bli != bl.end(); ++bli)
00183 m_abox.get(count++) = *bli;
00184 }
00185
00186 void
00187 BoxArray::define (const BoxArray& bs)
00188 {
00189 BL_ASSERT(size() == 0);
00190 m_ref = bs.m_ref;
00191 }
00192
00193 BoxArray::~BoxArray () {}
00194
00195 bool
00196 BoxArray::operator== (const BoxArray& rhs) const
00197 {
00198 return m_ref == rhs.m_ref || m_ref->m_abox == rhs.m_ref->m_abox;
00199 }
00200
00201 bool
00202 BoxArray::operator!= (const BoxArray& rhs) const
00203 {
00204 return !operator==(rhs);
00205 }
00206
00207 BoxArray&
00208 BoxArray::refine (int refinement_ratio)
00209 {
00210 if (!m_ref.unique())
00211 uniqify();
00212 for (int i = 0; i < size(); i++)
00213 m_ref->m_abox.get(i).refine(refinement_ratio);
00214 return *this;
00215 }
00216
00217 BoxArray&
00218 BoxArray::refine (const IntVect& iv)
00219 {
00220 if (!m_ref.unique())
00221 uniqify();
00222 for (int i = 0; i <size(); i++)
00223 m_ref->m_abox.get(i).refine(iv);
00224 return *this;
00225 }
00226
00227 BoxArray&
00228 BoxArray::shift (int dir,
00229 int nzones)
00230 {
00231 if (!m_ref.unique())
00232 uniqify();
00233 for (int i = 0; i < size(); i++)
00234 m_ref->m_abox.get(i).shift(dir, nzones);
00235 return *this;
00236 }
00237
00238 BoxArray&
00239 BoxArray::shiftHalf (int dir,
00240 int num_halfs)
00241 {
00242 if (!m_ref.unique())
00243 uniqify();
00244 for (int i = 0; i < size(); i++)
00245 m_ref->m_abox.get(i).shiftHalf(dir, num_halfs);
00246 return *this;
00247 }
00248
00249 BoxArray&
00250 BoxArray::shiftHalf (const IntVect& iv)
00251 {
00252 if (!m_ref.unique())
00253 uniqify();
00254 for (int i = 0; i < size(); i++)
00255 m_ref->m_abox.get(i).shiftHalf(iv);
00256 return *this;
00257 }
00258
00259 BoxArray&
00260 BoxArray::coarsen (int refinement_ratio)
00261 {
00262 if (!m_ref.unique())
00263 uniqify();
00264 for (int i = 0; i < size(); i++)
00265 m_ref->m_abox.get(i).coarsen(refinement_ratio);
00266 return *this;
00267 }
00268
00269 BoxArray&
00270 BoxArray::coarsen (const IntVect& iv)
00271 {
00272 if (!m_ref.unique())
00273 uniqify();
00274 for (int i = 0; i < size(); i++)
00275 m_ref->m_abox.get(i).coarsen(iv);
00276 return *this;
00277 }
00278
00279 BoxArray&
00280 BoxArray::grow (int n)
00281 {
00282 if (!m_ref.unique())
00283 uniqify();
00284 for (int i = 0; i < size(); i++)
00285 m_ref->m_abox.get(i).grow(n);
00286 return *this;
00287 }
00288
00289 BoxArray&
00290 BoxArray::grow (const IntVect& iv)
00291 {
00292 if (!m_ref.unique())
00293 uniqify();
00294 for (int i = 0; i < size(); i++)
00295 m_ref->m_abox.get(i).grow(iv);
00296 return *this;
00297 }
00298
00299 BoxArray&
00300 BoxArray::grow (int dir,
00301 int n_cell)
00302 {
00303 if (!m_ref.unique())
00304 uniqify();
00305 for (int i = 0; i < size(); i++)
00306 m_ref->m_abox.get(i).grow(dir, n_cell);
00307 return *this;
00308 }
00309
00310 bool
00311 BoxArray::contains (const IntVect& v) const
00312 {
00313 for (int i = 0; i < size(); i++)
00314 if (m_ref->m_abox.get(i).contains(v))
00315 return true;
00316 return false;
00317 }
00318
00319 bool
00320 BoxArray::contains (const Box& b) const
00321 {
00322 BoxArray bnew = BoxLib::complementIn(b, *this);
00323
00324 return bnew.size() == 0;
00325 }
00326
00327 bool
00328 BoxArray::contains (const BoxArray& bl) const
00329 {
00330 for (int i = 0; i < bl.size(); i++)
00331 if (!contains(bl.m_ref->m_abox.get(i)))
00332 return false;
00333 return true;
00334 }
00335
00336 BoxArray&
00337 BoxArray::surroundingNodes ()
00338 {
00339 if (!m_ref.unique())
00340 uniqify();
00341 for (int i = 0; i < size(); i++)
00342 m_ref->m_abox.get(i).surroundingNodes();
00343 return *this;
00344 }
00345
00346 BoxArray&
00347 BoxArray::surroundingNodes (int dir)
00348 {
00349 if (!m_ref.unique())
00350 uniqify();
00351 for (int i = 0; i < size(); i++)
00352 m_ref->m_abox.get(i).surroundingNodes(dir);
00353 return *this;
00354 }
00355
00356 BoxArray&
00357 BoxArray::enclosedCells ()
00358 {
00359 if (!m_ref.unique())
00360 uniqify();
00361 for (int i = 0; i < size(); i++)
00362 m_ref->m_abox.get(i).enclosedCells();
00363 return *this;
00364 }
00365
00366 BoxArray&
00367 BoxArray::enclosedCells (int dir)
00368 {
00369 if (!m_ref.unique())
00370 uniqify();
00371 for (int i = 0; i < size(); i++)
00372 m_ref->m_abox.get(i).enclosedCells(dir);
00373 return *this;
00374 }
00375
00376 BoxArray&
00377 BoxArray::convert (IndexType typ)
00378 {
00379 if (!m_ref.unique())
00380 uniqify();
00381 for (int i = 0; i < size(); i++)
00382 m_ref->m_abox.get(i).convert(typ);
00383 return *this;
00384 }
00385
00386 BoxArray&
00387 BoxArray::convert (Box (*fp)(const Box&))
00388 {
00389 BL_ASSERT(!(fp == 0));
00390
00391 if (!m_ref.unique())
00392 uniqify();
00393 for (int i = 0; i < size(); i++)
00394 m_ref->m_abox[i] = (*fp)(m_ref->m_abox[i]);
00395 return *this;
00396 }
00397
00398 std::ostream&
00399 BoxArray::writeOn (std::ostream& os) const
00400 {
00401
00402
00403
00404 os << '(' << size() << ' ' << 0 << '\n';
00405
00406 for (int i = 0; i < size(); i++)
00407 os << get(i) << '\n';
00408
00409 os << ')';
00410
00411 if (os.fail())
00412 BoxLib::Error("BoxArray::writeOn(ostream&) failed");
00413
00414 return os;
00415 }
00416
00417 bool
00418 BoxArray::isDisjoint () const
00419 {
00420 for (int i = 0; i < size(); i++)
00421 for (int j = i + 1; j < size(); j++)
00422 if (get(i).intersects(get(j)))
00423 return false;
00424 return true;
00425 }
00426
00427 bool
00428 BoxArray::ok () const
00429 {
00430 bool isok = true;
00431
00432 if (size() > 0)
00433 {
00434 const Box& bx0 = m_ref->m_abox[0];
00435
00436 if (size() == 1)
00437 isok = bx0.ok();
00438
00439 for (int i = 1; i < size() && isok; i++)
00440 {
00441 const Box& bxi = m_ref->m_abox[i];
00442 isok = bxi.ok() && bxi.sameType(bx0);
00443 }
00444 }
00445 return isok;
00446 }
00447
00448 long
00449 BoxArray::numPts () const
00450 {
00451 long result = 0;
00452 for ( int i = 0; i < size(); ++i )
00453 {
00454 result += m_ref->m_abox.get(i).numPts();
00455 }
00456 return result;
00457 }
00458
00459 BoxArray&
00460 BoxArray::maxSize (int block_size)
00461 {
00462 BoxList blst(*this);
00463 blst.maxSize(block_size);
00464 clear();
00465 m_ref->m_abox.resize(blst.size());
00466 BoxList::iterator bli = blst.begin();
00467 for (int i = 0; bli != blst.end(); ++bli)
00468 set(i++, *bli);
00469 return *this;
00470 }
00471
00472 std::ostream&
00473 operator<< (std::ostream& os,
00474 const BoxArray& ba)
00475 {
00476
00477
00478
00479 os << "(BoxArray maxbox("
00480 << ba.size()
00481 << ")\n m_ref->m_hash_sig("
00482 << 0
00483 << ")\n ";
00484
00485 for (int i = 0; i < ba.size(); ++i)
00486 os << ba[i] << ' ';
00487
00488 os << ")\n";
00489
00490 if (os.fail())
00491 BoxLib::Error("operator<<(ostream& os,const BoxArray&) failed");
00492
00493 return os;
00494 }
00495
00496 BoxList
00497 BoxArray::boxList () const
00498 {
00499 if ( size() == 0 ) return BoxList();
00500 BoxList newb(get(0).ixType());
00501 for (int i = 0; i < size(); ++i)
00502 newb.push_back(get(i));
00503 return newb;
00504 }
00505
00506 Box
00507 BoxArray::minimalBox () const
00508 {
00509 Box minbox;
00510 if (size() > 0)
00511 {
00512 minbox = m_ref->m_abox.get(0);
00513 for (int i = 0; i < size(); i++)
00514 minbox.minBox(m_ref->m_abox.get(i));
00515 }
00516 return minbox;
00517 }
00518
00519 BoxArray
00520 BoxLib::boxComplement (const Box& b1in,
00521 const Box& b2)
00522 {
00523 return BoxArray(BoxLib::boxDiff(b1in, b2));
00524 }
00525
00526 BoxArray
00527 BoxLib::complementIn (const Box& b,
00528 const BoxArray& ba)
00529 {
00530 return BoxArray(BoxLib::complementIn(b, ba.boxList()));
00531 }
00532
00533 BoxArray
00534 BoxLib::intersect (const BoxArray& ba,
00535 const Box& b)
00536 {
00537 return BoxArray(BoxLib::intersect(ba.boxList(), b));
00538 }
00539
00540 BoxArray
00541 BoxLib::intersect (const BoxArray& lhs,
00542 const BoxArray& rhs)
00543 {
00544 return BoxArray(BoxLib::intersect(lhs.boxList(), rhs.boxList()));
00545 }