⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intvect.h

📁 自适应网格划分通用程序包
💻 H
字号:
// This software is copyright (C) by the Lawrence Berkeley
// National Laboratory.  Permission is granted to reproduce
// this software for non-commercial purposes provided that
// this notice is left intact.
// 
// It is acknowledged that the U.S. Government has rights to
// this software under Contract DE-AC03-765F00098 between
// the U.S.  Department of Energy and the University of
// California.
//
// This software is provided as a professional and academic
// contribution for joint exchange. Thus it is experimental,
// is provided ``as is'', with no warranties of any kind
// whatsoever, no support, no promise of updates, or printed
// documentation. By using this software, you acknowledge
// that the Lawrence Berkeley National Laboratory and
// Regents of the University of California shall have no
// liability with respect to the infringement of other
// copyrights by any part of this software.
//

#ifndef CH_INTVECT_H
#define CH_INTVECT_H

//
// $Id: IntVect_8H-source.html,v 1.2 2005/05/13 21:35:19 dbs Exp $
//

#ifndef WRAPPER
#include <cstddef>
#include <cstdlib>
#include <cstring>
/*zhouhc
using std::memset ;
using std::memcpy ;
*/
#include <iostream>


class HDF5Handle;

#include "Misc.H"
#include <cassert>

#endif

#include "SPACE.H"


class IntVect
{
public:



  IntVect (){;}


  ~IntVect() {;}


  IntVect (D_DECL(int i, int j, int k));


  explicit IntVect (const int* a);


  IntVect (const IntVect& rhs);

  IntVect copy() const {return *this;}

  IntVect& operator= (const IntVect& rhs);


  inline
  int& operator[] (int i);


  inline
  int operator[] (int i) const;


  void setVal (int i,
               int val);
  


  const int* getVect () const;


  const int*  dataPtr() const;


  int*  dataPtr();



  bool operator== (const IntVect& p) const;


  bool operator!= (const IntVect& p) const;


  bool operator< (const IntVect& p) const;


  bool operator<= (const IntVect& p) const;


  bool operator> (const IntVect& p) const;


  bool operator>= (const IntVect& p) const;


  bool lexLT (const IntVect& s) const;


  bool lexGT (const IntVect& s) const;



  IntVect operator+ () const;


  IntVect operator- () const;



  IntVect& operator+= (int s);


  IntVect& operator+= (const IntVect& p);


  IntVect operator+ (const IntVect& p) const;


  IntVect operator+ (int s) const;


  friend inline IntVect operator+ (int            s,
                                   const IntVect& p);



  IntVect& operator-= (int s);


  IntVect& operator-= (const IntVect& p);


  IntVect operator- (const IntVect& p) const;


  IntVect operator- (int s) const;


  friend inline IntVect operator- (int            s,
                                   const IntVect& p);



  IntVect& operator*= (int s);


  IntVect& operator*= (const IntVect& p);


  IntVect operator* (const IntVect& p) const;


  IntVect operator* (int s) const;


  friend inline IntVect operator* (int            s,
                                   const IntVect& p);



  IntVect& operator/= (int s);


  IntVect& operator/= (const IntVect& p);


  IntVect operator/ (const IntVect& p) const;


  IntVect operator/ (int s) const;



  IntVect& min (const IntVect& p);


  friend inline IntVect min (const IntVect& p1,
                             const IntVect& p2);


  IntVect& max (const IntVect& p);


  friend inline IntVect max (const IntVect& p1,
                             const IntVect& p2);


  IntVect& scale (int s);


  friend inline IntVect scale (const IntVect& p,
                               int            s);


  IntVect& reflect (int ref_ix,
                    int idir);
  

  friend inline IntVect reflect(const IntVect& a,
                                int            ref_ix,
                                int            idir);


  IntVect& shift (int coord,
                  int s);


  IntVect& shift (const IntVect& iv);


  IntVect& diagShift (int s);


  friend inline IntVect diagShift (const IntVect& p,
                                   int            s);


  IntVect& coarsen (const IntVect& p);


  IntVect& coarsen (int p);


  friend inline IntVect coarsen (const IntVect& p,
                                 int            s);


  friend inline IntVect coarsen (const IntVect& p1,
                                 const IntVect& p2);



  void printOn (std::ostream& os) const;


  void p() const;


  void dumpOn (std::ostream& os) const;


  friend std::ostream& operator<< (std::ostream&       os,
                                   const IntVect& iv);


  friend std::istream& operator>> (std::istream& os,
                                   IntVect& iv);



  friend inline IntVect BASISV (int dir);

  static const IntVect Zero;

  static const IntVect Unit;

#ifndef WRAPPER
  static const IntVect TheZeroVector () {return Zero;}

  static const IntVect TheUnitVector () {return Unit;}
#endif

protected:
  //
  // Box is a friend of ours.
  //
  friend class Box;

  friend class HDF5Handle;
  friend class VolIndex;
  friend class FaceIndex;

  int vect[SpaceDim];

  static const size_t IntVectSize;
};

IntVect UnitVector();
IntVect ZeroVector();

#ifndef WRAPPER
//
// Inlines.
//

// try uninitialized IntVect null construction for now.....

// inline
// IntVect::IntVect ()
// {
//     D_EXPR(vect[0] = 0, vect[1] = 0, vect[2] = 0);
// }



inline
IntVect::IntVect (D_DECL(int i, int j, int k))
{
  D_EXPR(vect[0] = i, vect[1] = j, vect[2] = k);
}

inline
IntVect::IntVect (const int *a)
{
  D_EXPR(vect[0] = a[0], vect[1] = a[1], vect[2] = a[2]);
}

inline
IntVect::IntVect (const IntVect &iv)
{
  //D_EXPR(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2]);
  memcpy(vect, iv.vect, IntVectSize);
}

inline
IntVect&
IntVect::operator= (const IntVect &iv)
{
  D_EXPR(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2]);
  return *this;
}

inline
int&
IntVect::operator[] (int i)
{
  assert(i>=0 && i < SpaceDim);
  return vect[i];
}

inline
int
IntVect::operator[] (int i) const
{
  assert(i>=0 && i < SpaceDim);
  return vect[i];
}

inline
void
IntVect::setVal (int i,
                 int val)
{
  assert(i >=0 && i < SpaceDim);
  vect[i] = val;
  //  return *this;
}

inline
const int* 
IntVect::dataPtr() const
{
  return vect;
}

inline
int* 
IntVect::dataPtr()
{
  return vect;
}

inline
const int*
IntVect::getVect () const
{
  return vect;
}

inline
bool
IntVect::operator== (const IntVect& p) const
{
  return D_TERM(vect[0] == p[0], && vect[1] == p[1], && vect[2] == p[2]);
}

inline
bool
IntVect::operator!= (const IntVect& p) const
{
  return D_TERM(vect[0] != p[0], || vect[1] != p[1], || vect[2] != p[2]);
}

inline
bool
IntVect::operator< (const IntVect& p) const
{
  return D_TERM(vect[0] < p[0], && vect[1] < p[1], && vect[2] < p[2]);
}

inline
bool
IntVect::operator<= (const IntVect& p) const
{
  return D_TERM(vect[0] <= p[0], && vect[1] <= p[1], && vect[2] <= p[2]);
}

inline
bool
IntVect::operator> (const IntVect& p) const
{
  return D_TERM(vect[0] > p[0], && vect[1] > p[1], && vect[2] > p[2]);
}

inline
bool
IntVect::operator>= (const IntVect& p) const
{
  return D_TERM(vect[0] >= p[0], && vect[1] >= p[1], && vect[2] >= p[2]);
}

inline
bool
IntVect::lexLT (const IntVect &s) const
{
#define LLT0 (vect[0] < s[0])
#define LLT1 ((vect[0] == s[0]) && (vect[1] < s[1]))
#define LLT2 ((vect[1] == s[1]) && (vect[2] < s[2]))
#if   CH_SPACEDIM == 1
  return LLT0;
#elif CH_SPACEDIM == 2
  return LLT0 || LLT1;
#elif CH_SPACEDIM == 3
  return LLT0 || (vect[0]==s[0] && ((vect[1] < s[1] || LLT2)));
#endif
#undef LLT0
#undef LLT1
#undef LLT2
}

inline
bool
IntVect::lexGT (const IntVect& s) const
{
#define LGT0 (vect[0] > s[0])
#define LGT1 ((vect[0] == s[0]) && (vect[1] > s[1]))
#define LGT2 ((vect[1] == s[1]) && (vect[2] > s[2]))
#if   CH_SPACEDIM == 1
  return LGT0;
#elif CH_SPACEDIM == 2
  return LGT0 || LGT1;
#elif CH_SPACEDIM == 3
  return LGT0 || (vect[0] == s[0] && ((vect[1] > s[1] || LGT2)));
#endif
#undef LGT0
#undef LGT1
#undef LGT2
}

inline
IntVect
IntVect::operator+ () const
{
  return IntVect(*this);
}

inline
IntVect
IntVect::operator- () const
{
  return IntVect(D_DECL(-vect[0], -vect[1], -vect[2] ));
}

inline
IntVect&
IntVect::operator+= (int s)
{
  D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
  return *this;
}

inline
IntVect&
IntVect::operator+= (const IntVect& p)
{
  D_EXPR(vect[0] += p[0], vect[1] += p[1], vect[2] += p[2]);
  return *this;
}

inline
IntVect&
IntVect::operator*= (int s)
{
  D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
  return *this;
}

inline
IntVect&
IntVect::operator*= (const IntVect &p)
{
  D_EXPR(vect[0] *= p[0], vect[1] *= p[1], vect[2] *= p[2]);
  return *this;
}

inline
IntVect&
IntVect::operator/= (int s)
{
  D_EXPR(vect[0] /= s, vect[1] /= s, vect[2] /= s);
  return *this;
}

inline
IntVect&
IntVect::operator/= (const IntVect& p)
{
  D_EXPR(vect[0] /= p[0], vect[1] /= p[1], vect[2] /= p[2]);
  return *this;
}

inline
IntVect&
IntVect::operator-= (int s)
{
  D_EXPR(vect[0] -= s, vect[1] -= s, vect[2] -= s);
  return *this;
}

inline
IntVect&
IntVect::operator-= (const IntVect& p)
{
  D_EXPR(vect[0] -= p[0], vect[1] -= p[1], vect[2] -= p[2]);
  return *this;
}

inline
IntVect
IntVect::operator+ (const IntVect& p) const
{
  return IntVect(D_DECL(vect[0] + p[0], vect[1] + p[1], vect[2] + p[2]));
}

inline
IntVect
IntVect::operator+ (int s) const
{
  return IntVect(D_DECL(vect[0] + s, vect[1] + s, vect[2] + s));
}

inline
IntVect
IntVect::operator- (const IntVect& p) const
{
  return IntVect(D_DECL(vect[0] - p[0], vect[1] - p[1], vect[2] - p[2]));
}

inline
IntVect
IntVect::operator- (int s) const
{
  return IntVect(D_DECL(vect[0] - s, vect[1] - s, vect[2] - s));
}

inline
IntVect
IntVect::operator* (const IntVect& p) const
{
  return IntVect(D_DECL(vect[0] * p[0], vect[1] * p[1], vect[2] * p[2]));
}

inline
IntVect
IntVect::operator* (int s) const
{
  return IntVect(D_DECL(vect[0] * s, vect[1] * s, vect[2] * s));
}

inline
IntVect
IntVect::operator/ (const IntVect& p) const
{
  return IntVect(D_DECL(vect[0] / p[0], vect[1] / p[1], vect[2] / p[2]));
}

inline
IntVect
IntVect::operator/ (int s) const
{
  return IntVect(D_DECL(vect[0] / s, vect[1] / s, vect[2] / s));
}

inline
IntVect&
IntVect::min (const IntVect& p)
{
  D_EXPR(vect[0] = Min(vect[0], p.vect[0]),
         vect[1] = Min(vect[1], p.vect[1]),
         vect[2] = Min(vect[2], p.vect[2]));
  return *this;
}

inline
IntVect&
IntVect::max (const IntVect& p)
{
  D_EXPR(vect[0] = Max(vect[0], p.vect[0]),
         vect[1] = Max(vect[1], p.vect[1]),
         vect[2] = Max(vect[2], p.vect[2]));
  return *this;
}

inline
IntVect&
IntVect::scale (int s)
{
  D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
  return *this;
}

inline
IntVect&
IntVect::reflect (int ref_ix,
                  int idir)
{
  assert(idir >= 0 && idir < SpaceDim);
  vect[idir] = -vect[idir] + 2*ref_ix;
  return *this;
}

inline
IntVect&
IntVect::shift (int coord,
                int s)
{
  assert(coord >= 0 && coord < SpaceDim);
  vect[coord] += s;
  return *this;
}

inline
IntVect&
IntVect::shift (const IntVect& iv)
{
  *this += iv;
  return *this;
}

inline
IntVect&
IntVect::diagShift (int s)
{
  D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
  return *this;
}

inline
IntVect
operator+ (int            s,
           const IntVect& p)
{
  return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
}

inline
IntVect
operator- (int            s,
           const IntVect& p)
{
  return IntVect(D_DECL(s - p[0], s - p[1], s - p[2]));
}

inline
IntVect
operator* (int            s,
           const IntVect& p)
{
  return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
}

inline
IntVect
scale (const IntVect& p,
       int            s)
{
  return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
}

inline
IntVect
diagShift (const IntVect &p, int s)
{
  return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
}

inline
IntVect
min (const IntVect& p1,
     const IntVect& p2)
{
  IntVect p(p1);
  return p.min(p2);
}

inline
IntVect
max (const IntVect& p1,
     const IntVect& p2)
{
  IntVect p(p1);
  return p.max(p2);
}

inline
IntVect
BASISV (int dir)
{
  assert(dir >= 0 && dir < SpaceDim);
  IntVect tmp = IntVect::TheZeroVector() ;
  tmp.vect[dir] = 1;
  return tmp;
}

inline
IntVect
reflect (const IntVect& a,
         int            ref_ix,
         int            idir)
{
  assert(idir >= 0 && idir < SpaceDim);
  IntVect b(a);
  b.vect[idir] = -b.vect[idir] + 2*ref_ix;
  return b;
}

inline
IntVect
coarsen (const IntVect& p,
         int            s)
{
  assert(s > 0);
  return IntVect(
                 D_DECL((p.vect[0]<0) ? -abs(p.vect[0]+1)/s-1 : p.vect[0]/s ,
                        (p.vect[1]<0) ? -abs(p.vect[1]+1)/s-1 : p.vect[1]/s ,
                        (p.vect[2]<0) ? -abs(p.vect[2]+1)/s-1 : p.vect[2]/s ));
}

inline
IntVect
coarsen (const IntVect& p1,
         const IntVect& p2)
{
  assert(p2 > IntVect::TheZeroVector());
  return IntVect(
                 D_DECL(
                        (p1.vect[0]<0)?-abs(p1.vect[0]+1)/p2.vect[0]-1:p1.vect[0]/p2.vect[0],
                        (p1.vect[1]<0)?-abs(p1.vect[1]+1)/p2.vect[1]-1:p1.vect[1]/p2.vect[1],
                        (p1.vect[2]<0)?-abs(p1.vect[2]+1)/p2.vect[2]-1:p1.vect[2]/p2.vect[2])
                 );
}

inline
IntVect&
IntVect::coarsen (int s)
{
  assert(s > 0);
  for (int i = 0; i < SpaceDim; ++i)
    vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
  return *this;
}

inline
IntVect&
IntVect::coarsen (const IntVect& p)
{
  assert(p > IntVect::TheZeroVector());
  for (int i = 0; i <SpaceDim; ++i)
    {
      const int s = p.vect[i];
      vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
    }
  return *this;
}

#endif /* WRAPPER */

#endif /*CH_INTVECT_H*/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -