Xmipp  v3.23.11-Nereus
Public Member Functions | List of all members
Array2D< T > Struct Template Reference

#include <array_2D.h>

Public Member Functions

 Array2D ()
 
 Array2D (int xSize, int ySize)
 
 Array2D (const Array2D &other)
 Copy constructor. More...
 
 ~Array2D ()
 Destructor. More...
 
void allocateZeros (int xSize, int ySize)
 Allocate memory and initialized with zeros. More...
 
void clear ()
 Clear memory. More...
 
Array2Doperator= (const Array2D &other)
 Assignment. More...
 
T & operator() (int x, int y) const
 
int getXSize () const
 get Xsize More...
 
int getYSize () const
 get Ysize More...
 
bool inRange (int x, int y) const
 check if in range More...
 
bool inRangeX (int x) const
 check x in range More...
 
bool inRangeY (int y) const
 check Y in range More...
 
T * getRow (int y) const
 get y-th row More...
 

Detailed Description

template<typename T>
struct Array2D< T >

struct is used for compatibility with OpenCL / C for Cuda Instance of this struct represent a 2D array of arbitrary type. Data are being stored as a dynamic 2D array (i.e. not continuous block of memory). Access to elements will be the fastest if traversing in Y -> X order.

Definition at line 38 of file array_2D.h.

Constructor & Destructor Documentation

◆ Array2D() [1/3]

template<typename T>
Array2D< T >::Array2D ( )
inline

Empty constructor

Definition at line 41 of file array_2D.h.

41 : xSize(0), ySize(0), data(nullptr) {};

◆ Array2D() [2/3]

template<typename T>
Array2D< T >::Array2D ( int  xSize,
int  ySize 
)
inline

Constructor, allocates the data immediately

Definition at line 44 of file array_2D.h.

44  :
45  xSize(xSize), ySize(ySize) {
46  allocateZeros(xSize, ySize);
47  }
void allocateZeros(int xSize, int ySize)
Allocate memory and initialized with zeros.
Definition: array_2D.h:60

◆ Array2D() [3/3]

template<typename T>
Array2D< T >::Array2D ( const Array2D< T > &  other)
inline

Copy constructor.

Definition at line 50 of file array_2D.h.

50  {
51  *this=other;
52  }

◆ ~Array2D()

template<typename T>
Array2D< T >::~Array2D ( )
inline

Destructor.

Definition at line 55 of file array_2D.h.

55  {
56  clear();
57  }
void clear()
Clear memory.
Definition: array_2D.h:72

Member Function Documentation

◆ allocateZeros()

template<typename T>
void Array2D< T >::allocateZeros ( int  xSize,
int  ySize 
)
inline

Allocate memory and initialized with zeros.

Definition at line 60 of file array_2D.h.

61  {
62  data = new T*[ySize];
63  for (int y = 0; y < ySize; y++)
64  data[y] = new T[xSize]();
65  #if DEBUG
66  std::cout << "Array2D created (" << getXSize() << "x" << getYSize()
67  << ") at " << data << std::endl;
68  #endif
69  }
int getXSize() const
get Xsize
Definition: array_2D.h:114
static double * y
int getYSize() const
get Ysize
Definition: array_2D.h:119

◆ clear()

template<typename T>
void Array2D< T >::clear ( )
inline

Clear memory.

Definition at line 72 of file array_2D.h.

73  {
74  for (int y = 0; y < ySize; y++) {
75  delete[] data[y];
76  }
77  delete[] data;
78  #if DEBUG
79  std::cout << "Array2D deleted (" << getXSize() << "x" << getYSize()
80  << ") at " << data << std::endl;
81  #endif
82  data = nullptr;
83  }
int getXSize() const
get Xsize
Definition: array_2D.h:114
static double * y
int getYSize() const
get Ysize
Definition: array_2D.h:119

◆ getRow()

template<typename T>
T* Array2D< T >::getRow ( int  y) const
inline

get y-th row

Definition at line 139 of file array_2D.h.

139  {
140  return data[y];
141  }
static double * y

◆ getXSize()

template<typename T>
int Array2D< T >::getXSize ( ) const
inline

get Xsize

Definition at line 114 of file array_2D.h.

114  {
115  return xSize;
116  }

◆ getYSize()

template<typename T>
int Array2D< T >::getYSize ( ) const
inline

get Ysize

Definition at line 119 of file array_2D.h.

119  {
120  return ySize;
121  }

◆ inRange()

template<typename T>
bool Array2D< T >::inRange ( int  x,
int  y 
) const
inline

check if in range

Definition at line 124 of file array_2D.h.

124  {
125  return inRangeX(x) && inRangeY(y);
126  }
bool inRangeX(int x) const
check x in range
Definition: array_2D.h:129
static double * y
doublereal * x
bool inRangeY(int y) const
check Y in range
Definition: array_2D.h:134

◆ inRangeX()

template<typename T>
bool Array2D< T >::inRangeX ( int  x) const
inline

check x in range

Definition at line 129 of file array_2D.h.

129  {
130  return (x >= 0) && (x < xSize);
131  }
doublereal * x

◆ inRangeY()

template<typename T>
bool Array2D< T >::inRangeY ( int  y) const
inline

check Y in range

Definition at line 134 of file array_2D.h.

134  {
135  return (y >= 0) && (y < ySize);
136  }
static double * y

◆ operator()()

template<typename T>
T& Array2D< T >::operator() ( int  x,
int  y 
) const
inline

Method to access elements of the array

Definition at line 102 of file array_2D.h.

102  {
103  #if DEBUG
104  if (0 > x || x >= xSize)
105  std::cout << "Array2D " << data << " X=" << x << " out of range [0.." << getXSize() << ")\n";
106  if (0 > y || y >= ySize)
107  std::cout << "Array2D " << data << " Y=" << y << " out of range [0.." << getYSize() << ")\n";
108  std::cout << std::flush;
109  #endif
110  return data[y][x];
111  }
int getXSize() const
get Xsize
Definition: array_2D.h:114
static double * y
doublereal * x
int getYSize() const
get Ysize
Definition: array_2D.h:119

◆ operator=()

template<typename T>
Array2D& Array2D< T >::operator= ( const Array2D< T > &  other)
inline

Assignment.

Definition at line 86 of file array_2D.h.

87  {
88  clear();
89  xSize=other.xSize;
90  ySize=other.ySize;
91 
92  data = new T*[ySize];
93  for (int y = 0; y < ySize; y++) {
94  data[y] = new T[xSize];
95  memcpy(data[y],other.data[y],xSize*sizeof(T));
96  }
97 
98  return *this;
99  }
static double * y
void clear()
Clear memory.
Definition: array_2D.h:72

The documentation for this struct was generated from the following file: