Xmipp  v3.23.11-Nereus
array_2D.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: David Strelak (davidstrelak@gmail.com)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef ARRAY_2D_H_
27 #define ARRAY_2D_H_
28 
32 
37 template<typename T>
38 struct Array2D {
39 public:
41  Array2D() : xSize(0), ySize(0), data(nullptr) {};
42 
44  Array2D(int xSize, int ySize) :
45  xSize(xSize), ySize(ySize) {
46  allocateZeros(xSize, ySize);
47  }
48 
50  Array2D(const Array2D &other) {
51  *this=other;
52  }
53 
56  clear();
57  }
58 
60  void allocateZeros(int xSize, int ySize)
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  }
70 
72  void clear()
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  }
84 
86  Array2D& operator=(const Array2D &other)
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  }
100 
102  T& operator()(int x, int y) const {
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  }
112 
114  int getXSize() const {
115  return xSize;
116  }
117 
119  int getYSize() const {
120  return ySize;
121  }
122 
124  bool inRange(int x, int y) const {
125  return inRangeX(x) && inRangeY(y);
126  }
127 
129  bool inRangeX(int x) const {
130  return (x >= 0) && (x < xSize);
131  }
132 
134  bool inRangeY(int y) const {
135  return (y >= 0) && (y < ySize);
136  }
137 
139  T* getRow(int y) const {
140  return data[y];
141  }
142 private:
143  int xSize;
144  int ySize;
145  T** data;
146 };
148 #endif /* ARRAY_2D_H_ */
149 
int getXSize() const
get Xsize
Definition: array_2D.h:114
bool inRangeX(int x) const
check x in range
Definition: array_2D.h:129
Array2D()
Definition: array_2D.h:41
static double * y
bool inRange(int x, int y) const
check if in range
Definition: array_2D.h:124
doublereal * x
int getYSize() const
get Ysize
Definition: array_2D.h:119
T & operator()(int x, int y) const
Definition: array_2D.h:102
~Array2D()
Destructor.
Definition: array_2D.h:55
bool inRangeY(int y) const
check Y in range
Definition: array_2D.h:134
void clear()
Clear memory.
Definition: array_2D.h:72
void allocateZeros(int xSize, int ySize)
Allocate memory and initialized with zeros.
Definition: array_2D.h:60
Array2D & operator=(const Array2D &other)
Assignment.
Definition: array_2D.h:86
Array2D(int xSize, int ySize)
Definition: array_2D.h:44
T * getRow(int y) const
get y-th row
Definition: array_2D.h:139
Array2D(const Array2D &other)
Copy constructor.
Definition: array_2D.h:50