Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

IntVect.H

Go to the documentation of this file.
00001 /* _______              __
00002   / ___/ /  ___  __ _  / /  ___
00003  / /__/ _ \/ _ \/  ' \/ _ \/ _ \
00004  \___/_//_/\___/_/_/_/_.__/\___/ 
00005 */
00006 //
00007 // This software is copyright (C) by the Lawrence Berkeley
00008 // National Laboratory.  Permission is granted to reproduce
00009 // this software for non-commercial purposes provided that
00010 // this notice is left intact.
00011 // 
00012 // It is acknowledged that the U.S. Government has rights to
00013 // this software under Contract DE-AC03-765F00098 between
00014 // the U.S.  Department of Energy and the University of
00015 // California.
00016 //
00017 // This software is provided as a professional and academic
00018 // contribution for joint exchange. Thus it is experimental,
00019 // is provided ``as is'', with no warranties of any kind
00020 // whatsoever, no support, no promise of updates, or printed
00021 // documentation. By using this software, you acknowledge
00022 // that the Lawrence Berkeley National Laboratory and
00023 // Regents of the University of California shall have no
00024 // liability with respect to the infringement of other
00025 // copyrights by any part of this software.
00026 //
00027 
00028 #ifndef CH_INTVECT_H
00029 #define CH_INTVECT_H
00030 
00031 //
00032 // $Id: IntVect.H,v 1.5 2002/02/25 19:19:37 bvs Exp $
00033 //
00034 
00035 #ifndef WRAPPER
00036 #include <cstddef>
00037 #include <cstdlib>
00038 #include <cstring>
00039 #include <iostream>
00040 
00041 
00042 class HDF5Handle;
00043 
00044 #include "Misc.H"
00045 #include <cassert>
00046 
00047 #endif
00048 
00049 #include "SPACE.H"
00050 //
00051 //@Man:
00052 //@Memo: An Integer Vector in SpaceDim-Dimensional Space
00053 /*@Doc:
00054 
00055   The class IntVect is an implementation of an integer vector in a
00056   SpaceDim-dimensional space.  It represents a point in a discrete space.
00057   IntVect values are accessed using the operator[] function, as for a normal
00058   C++ array.  In addition, the basic arithmetic operators have been overloaded
00059   to implement scaling and translation operations.
00060 */
00061 
00062 class IntVect
00063 {
00064 public:
00065 
00067 
00069 
00073   IntVect (){;}
00074 
00076 
00080   ~IntVect() {;}
00081 
00083 
00089   IntVect (D_DECL(int i, int j, int k));
00090 
00092 
00097   explicit IntVect (const int* a);
00098 
00100 
00103   const int*  dataPtr() const;
00105 
00109   IntVect (const IntVect& rhs);
00110 
00111   IntVect copy() const {return *this;}
00113 
00117   IntVect& operator= (const IntVect& rhs);
00118 
00120 
00125   inline
00126   int& operator[] (int i);
00127 
00129 
00133   inline
00134   int operator[] (int i) const;
00135 
00137 
00141   void setVal (int i,
00142                            int val);
00143   
00145 
00150   const int* getVect () const;
00151 
00153 
00155 
00160   bool operator== (const IntVect& p) const;
00161 
00163 
00168   bool operator!= (const IntVect& p) const;
00169 
00171 
00179   bool operator< (const IntVect& p) const;
00180 
00182 
00190   bool operator<= (const IntVect& p) const;
00191 
00193 
00201   bool operator> (const IntVect& p) const;
00202 
00204 
00213   bool operator>= (const IntVect& p) const;
00214 
00216 
00230   bool lexLT (const IntVect& s) const;
00231 
00233 
00247   bool lexGT (const IntVect& s) const;
00248 
00250 
00252 
00256   IntVect operator+ () const;
00257 
00259 
00263   IntVect operator- () const;
00264 
00266 
00268 
00272   IntVect& operator+= (int s);
00273 
00275 
00279   IntVect& operator+= (const IntVect& p);
00280 
00282 
00286   IntVect operator+ (const IntVect& p) const;
00287 
00289 
00294   IntVect operator+ (int s) const;
00295 
00297 
00302   friend inline IntVect operator+ (int            s,
00303                                    const IntVect& p);
00304 
00306 
00308 
00313   IntVect& operator-= (int s);
00314 
00316 
00320   IntVect& operator-= (const IntVect& p);
00321 
00323 
00327   IntVect operator- (const IntVect& p) const;
00328 
00330 
00335   IntVect operator- (int s) const;
00336 
00338 
00342   friend inline IntVect operator- (int            s,
00343                                    const IntVect& p);
00344 
00346 
00348 
00353   IntVect& operator*= (int s);
00354 
00356 
00360   IntVect& operator*= (const IntVect& p);
00361 
00363 
00367   IntVect operator* (const IntVect& p) const;
00368 
00370 
00375   IntVect operator* (int s) const;
00376 
00378 
00383   friend inline IntVect operator* (int            s,
00384                                    const IntVect& p);
00385 
00387 
00389 
00393   IntVect& operator/= (int s);
00394 
00396 
00401   IntVect& operator/= (const IntVect& p);
00402 
00404 
00408   IntVect operator/ (const IntVect& p) const;
00409 
00411 
00415   IntVect operator/ (int s) const;
00416 
00418 
00420 
00425   IntVect& min (const IntVect& p);
00426 
00428 
00433   friend inline IntVect min (const IntVect& p1,
00434                              const IntVect& p2);
00435 
00437 
00442   IntVect& max (const IntVect& p);
00443 
00445 
00450   friend inline IntVect max (const IntVect& p1,
00451                              const IntVect& p2);
00452 
00454 
00458   IntVect& scale (int s);
00459 
00461 
00466   friend inline IntVect scale (const IntVect& p,
00467                                int            s);
00468 
00470 
00476   IntVect& reflect (int ref_ix,
00477                     int idir);
00478 
00480 
00486   friend inline IntVect reflect(const IntVect& a,
00487                                 int            ref_ix,
00488                                 int            idir);
00489 
00491 
00496   IntVect& shift (int coord,
00497                   int s);
00498 
00500 
00505   IntVect& shift (const IntVect& iv);
00506 
00508 
00512   IntVect& diagShift (int s);
00513 
00515 
00520   friend inline IntVect diagShift (const IntVect& p,
00521                                    int            s);
00522 
00524 
00528   IntVect& coarsen (const IntVect& p);
00529 
00531 
00535   IntVect& coarsen (int p);
00536 
00538 
00543   friend inline IntVect coarsen (const IntVect& p,
00544                                  int            s);
00545 
00547 
00552   friend inline IntVect coarsen (const IntVect& p1,
00553                                  const IntVect& p2);
00554 
00555 
00557 
00559 
00563   void printOn (std::ostream& os) const;
00564 
00566 
00570   void p() const;
00571 
00573 
00577   void dumpOn (std::ostream& os) const;
00578 
00580 
00584   friend std::ostream& operator<< (std::ostream&       os,
00585                               const IntVect& iv);
00586 
00588 
00592   friend std::istream& operator>> (std::istream& os,
00593                               IntVect& iv);
00594 
00596 
00598 
00604   friend inline IntVect BASISV (int dir);
00605 
00607 
00611   static const IntVect Zero;
00612 
00614 
00618   static const IntVect Unit;
00619 
00620 #ifndef WRAPPER
00621 // TheZeroVector is out of favor.  use IntVect::Zero.
00622   static const IntVect TheZeroVector () {return Zero;}
00623 
00624 // TheUnitVector is out of favor.  use IntVect::Unit.
00625   static const IntVect TheUnitVector () {return Unit;}
00626 #endif
00627 
00628 protected:
00629   //
00630   // Box is a friend of ours.
00631   //
00632   friend class Box;
00633 
00634   friend class HDF5Handle;
00635   friend class VolIndex;
00636   friend class FaceIndex;
00637 
00638   int vect[SpaceDim];
00639 
00640   static const size_t IntVectSize;
00641 };
00642 
00643 IntVect UnitVector();
00644 IntVect ZeroVector();
00645 
00646 #ifndef WRAPPER
00647 //
00648 // Inlines.
00649 //
00650 
00651 // try uninitialized IntVect null construction for now.....
00652 
00653 // inline
00654 // IntVect::IntVect ()
00655 // {
00656 //     D_EXPR(vect[0] = 0, vect[1] = 0, vect[2] = 0);
00657 // }
00658 
00659 
00660 
00661 inline
00662 IntVect::IntVect (D_DECL(int i, int j, int k))
00663 {
00664   D_EXPR(vect[0] = i, vect[1] = j, vect[2] = k);
00665 }
00666 
00667 inline
00668 IntVect::IntVect (const int *a)
00669 {
00670   D_EXPR(vect[0] = a[0], vect[1] = a[1], vect[2] = a[2]);
00671 }
00672 
00673 inline
00674 IntVect::IntVect (const IntVect &iv)
00675 {
00676   //D_EXPR(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2]);
00677   memcpy(vect, iv.vect, IntVectSize);
00678 }
00679 
00680 inline
00681 IntVect&
00682 IntVect::operator= (const IntVect &iv)
00683 {
00684   D_EXPR(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2]);
00685   return *this;
00686 }
00687 
00688 inline
00689 int&
00690 IntVect::operator[] (int i)
00691 {
00692   assert(i>=0 && i < SpaceDim);
00693   return vect[i];
00694 }
00695 
00696 inline
00697 int
00698 IntVect::operator[] (int i) const
00699 {
00700   assert(i>=0 && i < SpaceDim);
00701   return vect[i];
00702 }
00703 
00704 inline
00705 void
00706 IntVect::setVal (int i,
00707                  int val)
00708 {
00709   assert(i >=0 && i < SpaceDim);
00710   vect[i] = val;
00711   //  return *this;
00712 }
00713 
00714 inline
00715 const int* 
00716 IntVect::dataPtr() const
00717 {
00718   return vect;
00719 }
00720 
00721 inline
00722 const int*
00723 IntVect::getVect () const
00724 {
00725   return vect;
00726 }
00727 
00728 inline
00729 bool
00730 IntVect::operator== (const IntVect& p) const
00731 {
00732   return D_TERM(vect[0] == p[0], && vect[1] == p[1], && vect[2] == p[2]);
00733 }
00734 
00735 inline
00736 bool
00737 IntVect::operator!= (const IntVect& p) const
00738 {
00739   return D_TERM(vect[0] != p[0], || vect[1] != p[1], || vect[2] != p[2]);
00740 }
00741 
00742 inline
00743 bool
00744 IntVect::operator< (const IntVect& p) const
00745 {
00746   return D_TERM(vect[0] < p[0], && vect[1] < p[1], && vect[2] < p[2]);
00747 }
00748 
00749 inline
00750 bool
00751 IntVect::operator<= (const IntVect& p) const
00752 {
00753   return D_TERM(vect[0] <= p[0], && vect[1] <= p[1], && vect[2] <= p[2]);
00754 }
00755 
00756 inline
00757 bool
00758 IntVect::operator> (const IntVect& p) const
00759 {
00760   return D_TERM(vect[0] > p[0], && vect[1] > p[1], && vect[2] > p[2]);
00761 }
00762 
00763 inline
00764 bool
00765 IntVect::operator>= (const IntVect& p) const
00766 {
00767   return D_TERM(vect[0] >= p[0], && vect[1] >= p[1], && vect[2] >= p[2]);
00768 }
00769 
00770 inline
00771 bool
00772 IntVect::lexLT (const IntVect &s) const
00773 {
00774 #define LLT0 (vect[0] < s[0])
00775 #define LLT1 ((vect[0] == s[0]) && (vect[1] < s[1]))
00776 #define LLT2 ((vect[1] == s[1]) && (vect[2] < s[2]))
00777 #if   CH_SPACEDIM == 1
00778   return LLT0;
00779 #elif CH_SPACEDIM == 2
00780   return LLT0 || LLT1;
00781 #elif CH_SPACEDIM == 3
00782   return LLT0 || (vect[0]==s[0] && ((vect[1] < s[1] || LLT2)));
00783 #endif
00784 #undef LLT0
00785 #undef LLT1
00786 #undef LLT2
00787 }
00788 
00789 inline
00790 bool
00791 IntVect::lexGT (const IntVect& s) const
00792 {
00793 #define LGT0 (vect[0] > s[0])
00794 #define LGT1 ((vect[0] == s[0]) && (vect[1] > s[1]))
00795 #define LGT2 ((vect[1] == s[1]) && (vect[2] > s[2]))
00796 #if   CH_SPACEDIM == 1
00797   return LGT0;
00798 #elif CH_SPACEDIM == 2
00799   return LGT0 || LGT1;
00800 #elif CH_SPACEDIM == 3
00801   return LGT0 || (vect[0] == s[0] && ((vect[1] > s[1] || LGT2)));
00802 #endif
00803 #undef LGT0
00804 #undef LGT1
00805 #undef LGT2
00806 }
00807 
00808 inline
00809 IntVect
00810 IntVect::operator+ () const
00811 {
00812   return IntVect(*this);
00813 }
00814 
00815 inline
00816 IntVect
00817 IntVect::operator- () const
00818 {
00819   return IntVect(D_DECL(-vect[0], -vect[1], -vect[2] ));
00820 }
00821 
00822 inline
00823 IntVect&
00824 IntVect::operator+= (int s)
00825 {
00826   D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
00827   return *this;
00828 }
00829 
00830 inline
00831 IntVect&
00832 IntVect::operator+= (const IntVect& p)
00833 {
00834   D_EXPR(vect[0] += p[0], vect[1] += p[1], vect[2] += p[2]);
00835   return *this;
00836 }
00837 
00838 inline
00839 IntVect&
00840 IntVect::operator*= (int s)
00841 {
00842   D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
00843   return *this;
00844 }
00845 
00846 inline
00847 IntVect&
00848 IntVect::operator*= (const IntVect &p)
00849 {
00850   D_EXPR(vect[0] *= p[0], vect[1] *= p[1], vect[2] *= p[2]);
00851   return *this;
00852 }
00853 
00854 inline
00855 IntVect&
00856 IntVect::operator/= (int s)
00857 {
00858   D_EXPR(vect[0] /= s, vect[1] /= s, vect[2] /= s);
00859   return *this;
00860 }
00861 
00862 inline
00863 IntVect&
00864 IntVect::operator/= (const IntVect& p)
00865 {
00866   D_EXPR(vect[0] /= p[0], vect[1] /= p[1], vect[2] /= p[2]);
00867   return *this;
00868 }
00869 
00870 inline
00871 IntVect&
00872 IntVect::operator-= (int s)
00873 {
00874   D_EXPR(vect[0] -= s, vect[1] -= s, vect[2] -= s);
00875   return *this;
00876 }
00877 
00878 inline
00879 IntVect&
00880 IntVect::operator-= (const IntVect& p)
00881 {
00882   D_EXPR(vect[0] -= p[0], vect[1] -= p[1], vect[2] -= p[2]);
00883   return *this;
00884 }
00885 
00886 inline
00887 IntVect
00888 IntVect::operator+ (const IntVect& p) const
00889 {
00890   return IntVect(D_DECL(vect[0] + p[0], vect[1] + p[1], vect[2] + p[2]));
00891 }
00892 
00893 inline
00894 IntVect
00895 IntVect::operator+ (int s) const
00896 {
00897   return IntVect(D_DECL(vect[0] + s, vect[1] + s, vect[2] + s));
00898 }
00899 
00900 inline
00901 IntVect
00902 IntVect::operator- (const IntVect& p) const
00903 {
00904   return IntVect(D_DECL(vect[0] - p[0], vect[1] - p[1], vect[2] - p[2]));
00905 }
00906 
00907 inline
00908 IntVect
00909 IntVect::operator- (int s) const
00910 {
00911   return IntVect(D_DECL(vect[0] - s, vect[1] - s, vect[2] - s));
00912 }
00913 
00914 inline
00915 IntVect
00916 IntVect::operator* (const IntVect& p) const
00917 {
00918   return IntVect(D_DECL(vect[0] * p[0], vect[1] * p[1], vect[2] * p[2]));
00919 }
00920 
00921 inline
00922 IntVect
00923 IntVect::operator* (int s) const
00924 {
00925   return IntVect(D_DECL(vect[0] * s, vect[1] * s, vect[2] * s));
00926 }
00927 
00928 inline
00929 IntVect
00930 IntVect::operator/ (const IntVect& p) const
00931 {
00932   return IntVect(D_DECL(vect[0] / p[0], vect[1] / p[1], vect[2] / p[2]));
00933 }
00934 
00935 inline
00936 IntVect
00937 IntVect::operator/ (int s) const
00938 {
00939   return IntVect(D_DECL(vect[0] / s, vect[1] / s, vect[2] / s));
00940 }
00941 
00942 inline
00943 IntVect&
00944 IntVect::min (const IntVect& p)
00945 {
00946   D_EXPR(vect[0] = Min(vect[0], p.vect[0]),
00947          vect[1] = Min(vect[1], p.vect[1]),
00948          vect[2] = Min(vect[2], p.vect[2]));
00949   return *this;
00950 }
00951 
00952 inline
00953 IntVect&
00954 IntVect::max (const IntVect& p)
00955 {
00956   D_EXPR(vect[0] = Max(vect[0], p.vect[0]),
00957          vect[1] = Max(vect[1], p.vect[1]),
00958          vect[2] = Max(vect[2], p.vect[2]));
00959   return *this;
00960 }
00961 
00962 inline
00963 IntVect&
00964 IntVect::scale (int s)
00965 {
00966   D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
00967   return *this;
00968 }
00969 
00970 inline
00971 IntVect&
00972 IntVect::reflect (int ref_ix,
00973                   int idir)
00974 {
00975   assert(idir >= 0 && idir < SpaceDim);
00976   vect[idir] = -vect[idir] + 2*ref_ix;
00977   return *this;
00978 }
00979 
00980 inline
00981 IntVect&
00982 IntVect::shift (int coord,
00983                 int s)
00984 {
00985   assert(coord >= 0 && coord < SpaceDim);
00986   vect[coord] += s;
00987   return *this;
00988 }
00989 
00990 inline
00991 IntVect&
00992 IntVect::shift (const IntVect& iv)
00993 {
00994   *this += iv;
00995   return *this;
00996 }
00997 
00998 inline
00999 IntVect&
01000 IntVect::diagShift (int s)
01001 {
01002   D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
01003   return *this;
01004 }
01005 
01006 inline
01007 IntVect
01008 operator+ (int            s,
01009            const IntVect& p)
01010 {
01011   return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
01012 }
01013 
01014 inline
01015 IntVect
01016 operator- (int            s,
01017            const IntVect& p)
01018 {
01019   return IntVect(D_DECL(s - p[0], s - p[1], s - p[2]));
01020 }
01021 
01022 inline
01023 IntVect
01024 operator* (int            s,
01025            const IntVect& p)
01026 {
01027   return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
01028 }
01029 
01030 inline
01031 IntVect
01032 scale (const IntVect& p,
01033        int            s)
01034 {
01035   return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
01036 }
01037 
01038 inline
01039 IntVect
01040 diagShift (const IntVect &p, int s)
01041 {
01042   return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
01043 }
01044 
01045 inline
01046 IntVect
01047 min (const IntVect& p1,
01048      const IntVect& p2)
01049 {
01050   IntVect p(p1);
01051   return p.min(p2);
01052 }
01053 
01054 inline
01055 IntVect
01056 max (const IntVect& p1,
01057      const IntVect& p2)
01058 {
01059   IntVect p(p1);
01060   return p.max(p2);
01061 }
01062 
01063 inline
01064 IntVect
01065 BASISV (int dir)
01066 {
01067   assert(dir >= 0 && dir < SpaceDim);
01068   IntVect tmp = IntVect::TheZeroVector() ;
01069   tmp.vect[dir] = 1;
01070   return tmp;
01071 }
01072 
01073 inline
01074 IntVect
01075 reflect (const IntVect& a,
01076          int            ref_ix,
01077          int            idir)
01078 {
01079   assert(idir >= 0 && idir < SpaceDim);
01080   IntVect b(a);
01081   b.vect[idir] = -b.vect[idir] + 2*ref_ix;
01082   return b;
01083 }
01084 
01085 inline
01086 IntVect
01087 coarsen (const IntVect& p,
01088          int            s)
01089 {
01090   assert(s > 0);
01091   return IntVect(
01092                  D_DECL((p.vect[0]<0) ? -abs(p.vect[0]+1)/s-1 : p.vect[0]/s ,
01093                         (p.vect[1]<0) ? -abs(p.vect[1]+1)/s-1 : p.vect[1]/s ,
01094                         (p.vect[2]<0) ? -abs(p.vect[2]+1)/s-1 : p.vect[2]/s ));
01095 }
01096 
01097 inline
01098 IntVect
01099 coarsen (const IntVect& p1,
01100          const IntVect& p2)
01101 {
01102   assert(p2 > IntVect::TheZeroVector());
01103   return IntVect(
01104                  D_DECL(
01105                         (p1.vect[0]<0)?-abs(p1.vect[0]+1)/p2.vect[0]-1:p1.vect[0]/p2.vect[0],
01106                         (p1.vect[1]<0)?-abs(p1.vect[1]+1)/p2.vect[1]-1:p1.vect[1]/p2.vect[1],
01107                         (p1.vect[2]<0)?-abs(p1.vect[2]+1)/p2.vect[2]-1:p1.vect[2]/p2.vect[2])
01108                  );
01109 }
01110 
01111 inline
01112 IntVect&
01113 IntVect::coarsen (int s)
01114 {
01115   assert(s > 0);
01116   for (int i = 0; i < SpaceDim; ++i)
01117     vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
01118   return *this;
01119 }
01120 
01121 inline
01122 IntVect&
01123 IntVect::coarsen (const IntVect& p)
01124 {
01125   assert(p > IntVect::TheZeroVector());
01126   for (int i = 0; i <SpaceDim; ++i)
01127     {
01128       const int s = p.vect[i];
01129       vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
01130     }
01131   return *this;
01132 }
01133 
01134 #endif /* WRAPPER */
01135 
01136 #endif /*CH_INTVECT_H*/
01137 

Generated on Tue Jul 2 10:42:20 2002 for Chombo by doxygen1.2.16