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.6 2002/09/05 19:19:03 graves 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;
00104 
00106 
00109   int*  dataPtr();
00110 
00112 
00116   IntVect (const IntVect& rhs);
00117 
00118   IntVect copy() const {return *this;}
00120 
00124   IntVect& operator= (const IntVect& rhs);
00125 
00127 
00132   inline
00133   int& operator[] (int i);
00134 
00136 
00140   inline
00141   int operator[] (int i) const;
00142 
00144 
00148   void setVal (int i,
00149                            int val);
00150   
00152 
00157   const int* getVect () const;
00158 
00160 
00162 
00167   bool operator== (const IntVect& p) const;
00168 
00170 
00175   bool operator!= (const IntVect& p) const;
00176 
00178 
00186   bool operator< (const IntVect& p) const;
00187 
00189 
00197   bool operator<= (const IntVect& p) const;
00198 
00200 
00208   bool operator> (const IntVect& p) const;
00209 
00211 
00220   bool operator>= (const IntVect& p) const;
00221 
00223 
00237   bool lexLT (const IntVect& s) const;
00238 
00240 
00254   bool lexGT (const IntVect& s) const;
00255 
00257 
00259 
00263   IntVect operator+ () const;
00264 
00266 
00270   IntVect operator- () const;
00271 
00273 
00275 
00279   IntVect& operator+= (int s);
00280 
00282 
00286   IntVect& operator+= (const IntVect& p);
00287 
00289 
00293   IntVect operator+ (const IntVect& p) const;
00294 
00296 
00301   IntVect operator+ (int s) const;
00302 
00304 
00309   friend inline IntVect operator+ (int            s,
00310                                    const IntVect& p);
00311 
00313 
00315 
00320   IntVect& operator-= (int s);
00321 
00323 
00327   IntVect& operator-= (const IntVect& p);
00328 
00330 
00334   IntVect operator- (const IntVect& p) const;
00335 
00337 
00342   IntVect operator- (int s) const;
00343 
00345 
00349   friend inline IntVect operator- (int            s,
00350                                    const IntVect& p);
00351 
00353 
00355 
00360   IntVect& operator*= (int s);
00361 
00363 
00367   IntVect& operator*= (const IntVect& p);
00368 
00370 
00374   IntVect operator* (const IntVect& p) const;
00375 
00377 
00382   IntVect operator* (int s) const;
00383 
00385 
00390   friend inline IntVect operator* (int            s,
00391                                    const IntVect& p);
00392 
00394 
00396 
00400   IntVect& operator/= (int s);
00401 
00403 
00408   IntVect& operator/= (const IntVect& p);
00409 
00411 
00415   IntVect operator/ (const IntVect& p) const;
00416 
00418 
00422   IntVect operator/ (int s) const;
00423 
00425 
00427 
00432   IntVect& min (const IntVect& p);
00433 
00435 
00440   friend inline IntVect min (const IntVect& p1,
00441                              const IntVect& p2);
00442 
00444 
00449   IntVect& max (const IntVect& p);
00450 
00452 
00457   friend inline IntVect max (const IntVect& p1,
00458                              const IntVect& p2);
00459 
00461 
00465   IntVect& scale (int s);
00466 
00468 
00473   friend inline IntVect scale (const IntVect& p,
00474                                int            s);
00475 
00477 
00483   IntVect& reflect (int ref_ix,
00484                     int idir);
00485 
00487 
00493   friend inline IntVect reflect(const IntVect& a,
00494                                 int            ref_ix,
00495                                 int            idir);
00496 
00498 
00503   IntVect& shift (int coord,
00504                   int s);
00505 
00507 
00512   IntVect& shift (const IntVect& iv);
00513 
00515 
00519   IntVect& diagShift (int s);
00520 
00522 
00527   friend inline IntVect diagShift (const IntVect& p,
00528                                    int            s);
00529 
00531 
00535   IntVect& coarsen (const IntVect& p);
00536 
00538 
00542   IntVect& coarsen (int p);
00543 
00545 
00550   friend inline IntVect coarsen (const IntVect& p,
00551                                  int            s);
00552 
00554 
00559   friend inline IntVect coarsen (const IntVect& p1,
00560                                  const IntVect& p2);
00561 
00562 
00564 
00566 
00570   void printOn (std::ostream& os) const;
00571 
00573 
00577   void p() const;
00578 
00580 
00584   void dumpOn (std::ostream& os) const;
00585 
00587 
00591   friend std::ostream& operator<< (std::ostream&       os,
00592                               const IntVect& iv);
00593 
00595 
00599   friend std::istream& operator>> (std::istream& os,
00600                               IntVect& iv);
00601 
00603 
00605 
00611   friend inline IntVect BASISV (int dir);
00612 
00614 
00618   static const IntVect Zero;
00619 
00621 
00625   static const IntVect Unit;
00626 
00627 #ifndef WRAPPER
00628 // TheZeroVector is out of favor.  use IntVect::Zero.
00629   static const IntVect TheZeroVector () {return Zero;}
00630 
00631 // TheUnitVector is out of favor.  use IntVect::Unit.
00632   static const IntVect TheUnitVector () {return Unit;}
00633 #endif
00634 
00635 protected:
00636   //
00637   // Box is a friend of ours.
00638   //
00639   friend class Box;
00640 
00641   friend class HDF5Handle;
00642   friend class VolIndex;
00643   friend class FaceIndex;
00644 
00645   int vect[SpaceDim];
00646 
00647   static const size_t IntVectSize;
00648 };
00649 
00650 IntVect UnitVector();
00651 IntVect ZeroVector();
00652 
00653 #ifndef WRAPPER
00654 //
00655 // Inlines.
00656 //
00657 
00658 // try uninitialized IntVect null construction for now.....
00659 
00660 // inline
00661 // IntVect::IntVect ()
00662 // {
00663 //     D_EXPR(vect[0] = 0, vect[1] = 0, vect[2] = 0);
00664 // }
00665 
00666 
00667 
00668 inline
00669 IntVect::IntVect (D_DECL(int i, int j, int k))
00670 {
00671   D_EXPR(vect[0] = i, vect[1] = j, vect[2] = k);
00672 }
00673 
00674 inline
00675 IntVect::IntVect (const int *a)
00676 {
00677   D_EXPR(vect[0] = a[0], vect[1] = a[1], vect[2] = a[2]);
00678 }
00679 
00680 inline
00681 IntVect::IntVect (const IntVect &iv)
00682 {
00683   //D_EXPR(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2]);
00684   memcpy(vect, iv.vect, IntVectSize);
00685 }
00686 
00687 inline
00688 IntVect&
00689 IntVect::operator= (const IntVect &iv)
00690 {
00691   D_EXPR(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2]);
00692   return *this;
00693 }
00694 
00695 inline
00696 int&
00697 IntVect::operator[] (int i)
00698 {
00699   assert(i>=0 && i < SpaceDim);
00700   return vect[i];
00701 }
00702 
00703 inline
00704 int
00705 IntVect::operator[] (int i) const
00706 {
00707   assert(i>=0 && i < SpaceDim);
00708   return vect[i];
00709 }
00710 
00711 inline
00712 void
00713 IntVect::setVal (int i,
00714                  int val)
00715 {
00716   assert(i >=0 && i < SpaceDim);
00717   vect[i] = val;
00718   //  return *this;
00719 }
00720 
00721 inline
00722 const int* 
00723 IntVect::dataPtr() const
00724 {
00725   return vect;
00726 }
00727 
00728 inline
00729 int* 
00730 IntVect::dataPtr()
00731 {
00732   return vect;
00733 }
00734 
00735 inline
00736 const int*
00737 IntVect::getVect () const
00738 {
00739   return vect;
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::operator> (const IntVect& p) const
00773 {
00774   return D_TERM(vect[0] > p[0], && vect[1] > p[1], && vect[2] > p[2]);
00775 }
00776 
00777 inline
00778 bool
00779 IntVect::operator>= (const IntVect& p) const
00780 {
00781   return D_TERM(vect[0] >= p[0], && vect[1] >= p[1], && vect[2] >= p[2]);
00782 }
00783 
00784 inline
00785 bool
00786 IntVect::lexLT (const IntVect &s) const
00787 {
00788 #define LLT0 (vect[0] < s[0])
00789 #define LLT1 ((vect[0] == s[0]) && (vect[1] < s[1]))
00790 #define LLT2 ((vect[1] == s[1]) && (vect[2] < s[2]))
00791 #if   CH_SPACEDIM == 1
00792   return LLT0;
00793 #elif CH_SPACEDIM == 2
00794   return LLT0 || LLT1;
00795 #elif CH_SPACEDIM == 3
00796   return LLT0 || (vect[0]==s[0] && ((vect[1] < s[1] || LLT2)));
00797 #endif
00798 #undef LLT0
00799 #undef LLT1
00800 #undef LLT2
00801 }
00802 
00803 inline
00804 bool
00805 IntVect::lexGT (const IntVect& s) const
00806 {
00807 #define LGT0 (vect[0] > s[0])
00808 #define LGT1 ((vect[0] == s[0]) && (vect[1] > s[1]))
00809 #define LGT2 ((vect[1] == s[1]) && (vect[2] > s[2]))
00810 #if   CH_SPACEDIM == 1
00811   return LGT0;
00812 #elif CH_SPACEDIM == 2
00813   return LGT0 || LGT1;
00814 #elif CH_SPACEDIM == 3
00815   return LGT0 || (vect[0] == s[0] && ((vect[1] > s[1] || LGT2)));
00816 #endif
00817 #undef LGT0
00818 #undef LGT1
00819 #undef LGT2
00820 }
00821 
00822 inline
00823 IntVect
00824 IntVect::operator+ () const
00825 {
00826   return IntVect(*this);
00827 }
00828 
00829 inline
00830 IntVect
00831 IntVect::operator- () const
00832 {
00833   return IntVect(D_DECL(-vect[0], -vect[1], -vect[2] ));
00834 }
00835 
00836 inline
00837 IntVect&
00838 IntVect::operator+= (int s)
00839 {
00840   D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
00841   return *this;
00842 }
00843 
00844 inline
00845 IntVect&
00846 IntVect::operator+= (const IntVect& p)
00847 {
00848   D_EXPR(vect[0] += p[0], vect[1] += p[1], vect[2] += p[2]);
00849   return *this;
00850 }
00851 
00852 inline
00853 IntVect&
00854 IntVect::operator*= (int s)
00855 {
00856   D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
00857   return *this;
00858 }
00859 
00860 inline
00861 IntVect&
00862 IntVect::operator*= (const IntVect &p)
00863 {
00864   D_EXPR(vect[0] *= p[0], vect[1] *= p[1], vect[2] *= p[2]);
00865   return *this;
00866 }
00867 
00868 inline
00869 IntVect&
00870 IntVect::operator/= (int s)
00871 {
00872   D_EXPR(vect[0] /= s, vect[1] /= s, vect[2] /= s);
00873   return *this;
00874 }
00875 
00876 inline
00877 IntVect&
00878 IntVect::operator/= (const IntVect& p)
00879 {
00880   D_EXPR(vect[0] /= p[0], vect[1] /= p[1], vect[2] /= p[2]);
00881   return *this;
00882 }
00883 
00884 inline
00885 IntVect&
00886 IntVect::operator-= (int s)
00887 {
00888   D_EXPR(vect[0] -= s, vect[1] -= s, vect[2] -= s);
00889   return *this;
00890 }
00891 
00892 inline
00893 IntVect&
00894 IntVect::operator-= (const IntVect& p)
00895 {
00896   D_EXPR(vect[0] -= p[0], vect[1] -= p[1], vect[2] -= p[2]);
00897   return *this;
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::operator/ (const IntVect& p) const
00945 {
00946   return IntVect(D_DECL(vect[0] / p[0], vect[1] / p[1], vect[2] / p[2]));
00947 }
00948 
00949 inline
00950 IntVect
00951 IntVect::operator/ (int s) const
00952 {
00953   return IntVect(D_DECL(vect[0] / s, vect[1] / s, vect[2] / s));
00954 }
00955 
00956 inline
00957 IntVect&
00958 IntVect::min (const IntVect& p)
00959 {
00960   D_EXPR(vect[0] = Min(vect[0], p.vect[0]),
00961          vect[1] = Min(vect[1], p.vect[1]),
00962          vect[2] = Min(vect[2], p.vect[2]));
00963   return *this;
00964 }
00965 
00966 inline
00967 IntVect&
00968 IntVect::max (const IntVect& p)
00969 {
00970   D_EXPR(vect[0] = Max(vect[0], p.vect[0]),
00971          vect[1] = Max(vect[1], p.vect[1]),
00972          vect[2] = Max(vect[2], p.vect[2]));
00973   return *this;
00974 }
00975 
00976 inline
00977 IntVect&
00978 IntVect::scale (int s)
00979 {
00980   D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
00981   return *this;
00982 }
00983 
00984 inline
00985 IntVect&
00986 IntVect::reflect (int ref_ix,
00987                   int idir)
00988 {
00989   assert(idir >= 0 && idir < SpaceDim);
00990   vect[idir] = -vect[idir] + 2*ref_ix;
00991   return *this;
00992 }
00993 
00994 inline
00995 IntVect&
00996 IntVect::shift (int coord,
00997                 int s)
00998 {
00999   assert(coord >= 0 && coord < SpaceDim);
01000   vect[coord] += s;
01001   return *this;
01002 }
01003 
01004 inline
01005 IntVect&
01006 IntVect::shift (const IntVect& iv)
01007 {
01008   *this += iv;
01009   return *this;
01010 }
01011 
01012 inline
01013 IntVect&
01014 IntVect::diagShift (int s)
01015 {
01016   D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
01017   return *this;
01018 }
01019 
01020 inline
01021 IntVect
01022 operator+ (int            s,
01023            const IntVect& p)
01024 {
01025   return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
01026 }
01027 
01028 inline
01029 IntVect
01030 operator- (int            s,
01031            const IntVect& p)
01032 {
01033   return IntVect(D_DECL(s - p[0], s - p[1], s - p[2]));
01034 }
01035 
01036 inline
01037 IntVect
01038 operator* (int            s,
01039            const IntVect& p)
01040 {
01041   return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
01042 }
01043 
01044 inline
01045 IntVect
01046 scale (const IntVect& p,
01047        int            s)
01048 {
01049   return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
01050 }
01051 
01052 inline
01053 IntVect
01054 diagShift (const IntVect &p, int s)
01055 {
01056   return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
01057 }
01058 
01059 inline
01060 IntVect
01061 min (const IntVect& p1,
01062      const IntVect& p2)
01063 {
01064   IntVect p(p1);
01065   return p.min(p2);
01066 }
01067 
01068 inline
01069 IntVect
01070 max (const IntVect& p1,
01071      const IntVect& p2)
01072 {
01073   IntVect p(p1);
01074   return p.max(p2);
01075 }
01076 
01077 inline
01078 IntVect
01079 BASISV (int dir)
01080 {
01081   assert(dir >= 0 && dir < SpaceDim);
01082   IntVect tmp = IntVect::TheZeroVector() ;
01083   tmp.vect[dir] = 1;
01084   return tmp;
01085 }
01086 
01087 inline
01088 IntVect
01089 reflect (const IntVect& a,
01090          int            ref_ix,
01091          int            idir)
01092 {
01093   assert(idir >= 0 && idir < SpaceDim);
01094   IntVect b(a);
01095   b.vect[idir] = -b.vect[idir] + 2*ref_ix;
01096   return b;
01097 }
01098 
01099 inline
01100 IntVect
01101 coarsen (const IntVect& p,
01102          int            s)
01103 {
01104   assert(s > 0);
01105   return IntVect(
01106                  D_DECL((p.vect[0]<0) ? -abs(p.vect[0]+1)/s-1 : p.vect[0]/s ,
01107                         (p.vect[1]<0) ? -abs(p.vect[1]+1)/s-1 : p.vect[1]/s ,
01108                         (p.vect[2]<0) ? -abs(p.vect[2]+1)/s-1 : p.vect[2]/s ));
01109 }
01110 
01111 inline
01112 IntVect
01113 coarsen (const IntVect& p1,
01114          const IntVect& p2)
01115 {
01116   assert(p2 > IntVect::TheZeroVector());
01117   return IntVect(
01118                  D_DECL(
01119                         (p1.vect[0]<0)?-abs(p1.vect[0]+1)/p2.vect[0]-1:p1.vect[0]/p2.vect[0],
01120                         (p1.vect[1]<0)?-abs(p1.vect[1]+1)/p2.vect[1]-1:p1.vect[1]/p2.vect[1],
01121                         (p1.vect[2]<0)?-abs(p1.vect[2]+1)/p2.vect[2]-1:p1.vect[2]/p2.vect[2])
01122                  );
01123 }
01124 
01125 inline
01126 IntVect&
01127 IntVect::coarsen (int s)
01128 {
01129   assert(s > 0);
01130   for (int i = 0; i < SpaceDim; ++i)
01131     vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
01132   return *this;
01133 }
01134 
01135 inline
01136 IntVect&
01137 IntVect::coarsen (const IntVect& p)
01138 {
01139   assert(p > IntVect::TheZeroVector());
01140   for (int i = 0; i <SpaceDim; ++i)
01141     {
01142       const int s = p.vect[i];
01143       vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
01144     }
01145   return *this;
01146 }
01147 
01148 #endif /* WRAPPER */
01149 
01150 #endif /*CH_INTVECT_H*/
01151 

Generated on Wed Apr 16 14:26:49 2003 for Chombo by doxygen1.2.16