dynarrays.h

来自「C++ Math Lib. C++ Builder must use.」· C头文件 代码 · 共 687 行 · 第 1/2 页

H
687
字号
}

//  friend IArr1D operator+(const IArr1D&, const IArr1D&);
//  friend IArr1D operator-(const IArr1D&, const IArr1D&);
//  friend IArr1D operator*(const IArr1D&, const int);
//  friend IArr1D operator*(const int x, const IArr1D& a2){ return a2 * x; }

  // Parenthesis operator function: Allows access to values of IArr1D via (i1).
  // Example: a(1) = 2*b(2);
  int& operator() (int i1) {
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking
    return data[ (i1-l1) ];  // Access appropriate value
  }

  // Parenthesis operator function (const version).
  const int& operator() (int i1) const{
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking
    return data[ (i1-l1) ];  // Access appropriate value
  }

  // Fills all elements of a vector to a given value.
  void fill(int value);

  void swap1(int i1, int j1);

//*********************************************************************
private:
  int dd;  // Data Dimension
  int* data;     // Pointer used to allocate memory for data.

  // Private copy function.
  // Copies values from one IArr1D object to another.
  void copy(const IArr1D& vec);

}; // Class IArr1D

// IArr2D =============================================================
//    l2    h2
// l1| 1| 2| 3|
//   | 4| 5| 6|
//   | 7| 8| 9|
// h1|10|11|12|
class IArr2D : public Lim2D
{

public:

  // Default Constructor: Creates a 1 by 1 matrix; sets value to zero.
  IArr2D ();

  // 1-based Constructor: Creates an aD1 by aD1 matrix; sets values to zero.
  IArr2D(int aD1, int aD2);

  // General Constructor: Creates an aD1 by aD1 matrix; sets values to zero.
  IArr2D(int aL1, int aH1, int aL2, int aH2);

  // Copy Constructor: Used when a copy of an object is produced
  // (e.g., passing to a function by value)
  IArr2D(const IArr2D& mat) {
    this->copy(mat);   // Call private copy function.
  }

  // Destructor: Called when a IArr2D object goes out of scope.
  ~IArr2D() {
    delete [] data;   // Release allocated memory
  }

  // Assignment operator function.
  // Overloads the equal sign operator to work with IArr2D objects.
  IArr2D& operator=(const IArr2D& mat) {
  if( this == &mat ) return *this;  // If two sides equal, do nothing.
  delete [] data;                  // Delete data on left hand side
  this->copy(mat);                  // Copy right hand side to l.h.s.
  return *this;
}


  // Parenthesis operator function: Allows access to values of IArr2D via (i,j) pair.
  // Example: a(1,1) = 2*b(2,3);
  int& operator() (int i1, int i2) {
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking for rows
    assert(i2 >= l2 && i2 <= h2);          // Bounds checking for columns
    return data[ d2*(i1-l1) + (i2-l2) ];  // Access appropriate value
  }

  // Parenthesis operator function (const version).
  const int& operator() (int i1, int i2) const{
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking for rows
    assert(i2 >= l2 && i2 <= h2);          // Bounds checking for columns
    return data[ d2*(i1-l1) + (i2-l2) ];  // Access appropriate value
  }

  // Fills all elements of a matrix to a given value.
  void fill(int value);

//*********************************************************************
private:

  int dd;  // Data Dimension

  int* data;     // Pointer used to allocate memory for data.

  // Private copy function.
  // Copies values from one IArr2D object to another.
  void copy(const IArr2D& mat);

}; // Class IArr2D


// FArr3D =============================================================
/*  _________
   |\        \ l1         i1=11          i1=11+1
   | \        \
   |  \ _______\ h1        l3    h3      l3    h3
   |   |  |  |  | l2    l2| 1| 2| 3|   l2|13|14|15|
    \  |  |  |  |         | 4| 5| 6|     |16|17|18|
     \ |  |  |  |         | 7| 8| 9|     |19|20|21|
      \|  |  |  | h2    h2|10|11|12|   h2|22|23|24|
        l3    h3
*/
class FArr3D : public Lim3D
{

public:

  // Default Constructor: Creates a 1 x 1 x 1 matrix; sets value to zero.
  FArr3D ();

  // 1-based Constructor: Creates an aD1 by aD1 matrix; sets values to zero.
  FArr3D(int aD1, int aD2, int aD3);

  // General Constructor: Creates an aD1 by aD1 matrix; sets values to zero.
  FArr3D(int aL1, int aH1, int aL2, int aH2, int aL3, int aH3);

  // Copy Constructor: Used when a copy of an object is produced
  // (e.g., passing to a function by value)
  FArr3D(const FArr3D& mat) {
    this->copy(mat);   // Call private copy function.
  }

  // Destructor: Called when a FArr2D object goes out of scope.
  ~FArr3D() {
    delete [] data;   // Release allocated memory
  }

  // Assignment operator function.
  // Overloads the equal sign operator to work with FArr2D objects.
  FArr3D& operator=(const FArr3D& mat) {
  if( this == &mat ) return *this;  // If two sides equal, do nothing.
  delete [] data;                  // Delete data on left hand side
  this->copy(mat);                  // Copy right hand side to l.h.s.
  return *this;
}

  // Parenthesis operator function: Allows access to values of FArr2D via (i,j) pair.
  // Example: a(1,1) = 2*b(2,3);
  double& operator() (int i1, int i2, int i3) {
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking for rows
    assert(i2 >= l2 && i2 <= h2);          // Bounds checking for columns
    assert(i3 >= l3 && i3 <= h3);          // Bounds checking
    return data[ d3*d2*(i1-l1) + d3*(i2-l2) + (i3-l3) ];  // Access appropriate value
  }

  // Parenthesis operator function (const version).
  const double& operator() (int i1, int i2, int i3) const{
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking for rows
    assert(i2 >= l2 && i2 <= h2);          // Bounds checking for columns
    assert(i3 >= l3 && i3 <= h3);          // Bounds checking
    return data[ d3*d2*(i1-l1) + d3*(i2-l2) + (i3-l3) ];  // Access appropriate value
  }

  // Fills all elements of a matrix to a given value.
  void fill(double value);

  void GetFArr2D(int i1, FArr2D& m);

  void SetFArr2D(int i1, FArr2D& m);

//*********************************************************************
private:

  int dd;  // Data Dimension

  double* data;     // Pointer used to allocate memory for data.

  // Private copy function.
  // Copies values from one FArr2D object to another.
  void copy(const FArr3D& mat);

}; // Class FArr3D


// Sparse2D =============================================================
//    l2    h2
// l1| 1| 2| 3|
//   | 4| 5| 6|
//   | 7| 8| 9|
// h1|10|11|12|
class Sparse2D : public Lim2D
{
public:

  // Default Constructor: Creates a 1 by 1 matrix; sets value to zero.
  Sparse2D ();

  // 1-based Constructor: Creates an aD1 by aD1 matrix; sets values to zero.
  Sparse2D(int aD1, int aD2);

  // General Constructor: Creates an aD1 by aD1 matrix; sets values to zero.
  Sparse2D(int aL1, int aH1, int aL2, int aH2);

  // Copy Constructor: Used when a copy of an object is produced
  // (e.g., passing to a function by value)
  Sparse2D(const Sparse2D& mat) {
    this->copy(mat);   // Call private copy function.
  }

  // Destructor: Called when a Sparse2D object goes out of scope.
  ~Sparse2D() {
    delete [] data;   // Release allocated memory
  }

  // Assignment operator function.
  // Overloads the equal sign operator to work with Sparse2D objects.
  Sparse2D& operator=(const Sparse2D& mat) {
  if( this == &mat ) return *this;  // If two sides equal, do nothing.
  delete [] data;                  // Delete data on left hand side
  this->copy(mat);                  // Copy right hand side to l.h.s.
  return *this;
}

  // Parenthesis operator function: Allows access to values of Sparse2D via (i,j) pair.
  // Example: a(1,1) = 2*b(2,3);
  double& operator() (int i1, int i2) {
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking for rows
    assert(i2 >= l2 && i2 <= h2);          // Bounds checking for columns
    return data[ d2*(i1-l1) + (i2-l2) ];  // Access appropriate value
  }

  // Parenthesis operator function (const version).
  const double& operator() (int i1, int i2) const{
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking for rows
    assert(i2 >= l2 && i2 <= h2);          // Bounds checking for columns
    return data[ d2*(i1-l1) + (i2-l2) ];  // Access appropriate value
  }

  // Fills all elements of a matrix to a given value.
  void fill(double value);

//*********************************************************************
private:

  int dd;  // Data Dimension

  double* data;     // Pointer used to allocate memory for data.

  // Private copy function.
  // Copies values from one Sparse2D object to another.
  void copy(const Sparse2D& mat);

}; // Class Sparse2D


// Sparse3D =============================================================
/*  _________
   |\        \ l1         i1=11          i1=11+1
   | \        \
   |  \ _______\ h1        l3    h3      l3    h3
   |   |  |  |  | l2    l2| 1| 2| 3|   l2|13|14|15|
    \  |  |  |  |         | 4| 5| 6|     |16|17|18|
     \ |  |  |  |         | 7| 8| 9|     |19|20|21|
      \|  |  |  | h2    h2|10|11|12|   h2|22|23|24|
        l3    h3
*/
class Sparse3D : public Lim3D
{

public:

  // Default Constructor: Creates a 1 x 1 x 1 matrix; sets value to zero.
  Sparse3D ();

  // 1-based Constructor: Creates an aD1 by aD1 matrix; sets values to zero.
  Sparse3D(int aD1, int aD2, int aD3);

  // General Constructor: Creates an aD1 by aD1 matrix; sets values to zero.
  Sparse3D(int aL1, int aH1, int aL2, int aH2, int aL3, int aH3);

  // Copy Constructor: Used when a copy of an object is produced
  // (e.g., passing to a function by value)
  Sparse3D(const Sparse3D& mat) {
    this->copy(mat);   // Call private copy function.
  }

  // Destructor: Called when a FArr2D object goes out of scope.
  ~Sparse3D() {
    delete [] data;   // Release allocated memory
  }

  // Assignment operator function.
  // Overloads the equal sign operator to work with FArr2D objects.
  Sparse3D& operator=(const Sparse3D& mat) {
  if( this == &mat ) return *this;  // If two sides equal, do nothing.
  delete [] data;                  // Delete data on left hand side
  this->copy(mat);                  // Copy right hand side to l.h.s.
  return *this;
}

  // Parenthesis operator function: Allows access to values of FArr2D via (i,j) pair.
  // Example: a(1,1) = 2*b(2,3);
  double& operator() (int i1, int i2, int i3) {
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking for rows
    assert(i2 >= l2 && i2 <= h2);          // Bounds checking for columns
    assert(i3 >= l3 && i3 <= h3);          // Bounds checking
    return data[ d3*d2*(i1-l1) + d3*(i2-l2) + (i3-l3) ];  // Access appropriate value
  }

  // Parenthesis operator function (const version).
  const double& operator() (int i1, int i2, int i3) const{
    assert(i1 >= l1 && i1 <= h1);          // Bounds checking for rows
    assert(i2 >= l2 && i2 <= h2);          // Bounds checking for columns
    assert(i3 >= l3 && i3 <= h3);          // Bounds checking
    return data[ d3*d2*(i1-l1) + d3*(i2-l2) + (i3-l3) ];  // Access appropriate value
  }

  // Fills all elements of a matrix to a given value.
  void fill(double value);

//*********************************************************************
private:

  int dd;  // Data Dimension

  double* data;     // Pointer used to allocate memory for data.

  // Private copy function.
  // Copies values from one FArr2D object to another.
  void copy(const Sparse3D& mat);

}; // Class Sparse3D

#endif

⌨️ 快捷键说明

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