Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

Array2D.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/exceptions.h>
36 
37 namespace ogdf {
38 
39 
41 
46 template<class E> class Array2D
47 {
48 public:
49  // constructors
50 
52  Array2D() { construct(0,-1,0,-1); }
53 
55  Array2D(int a, int b, int c, int d) {
56  construct(a,b,c,d); initialize();
57  }
58 
60  Array2D(int a, int b, int c, int d, const E &x) {
61  construct(a,b,c,d); initialize(x);
62  }
63 
65  Array2D(const Array2D<E> &A) {
66  copy(A);
67  }
68 
70 
75  m_a(A.m_a), m_b(A.m_b), m_c(A.m_c), m_d(A.m_d)
76  {
77  A.construct(0,-1,0,-1);
78  }
79 
82  deconstruct();
83  }
84 
86  int low1() const { return m_a; }
87 
89  int high1() const { return m_b; }
90 
92  int low2() const { return m_c; }
93 
95  int high2() const { return m_d; }
96 
98  int size() const { return size1() * size2(); }
99 
101  int size1() const { return m_b - m_a + 1; }
102 
104  int size2() const { return m_lenDim2; }
105 
107 
108  float det() const;
109 
111  const E &operator()(int i, int j) const {
112  OGDF_ASSERT(m_a <= i);
113  OGDF_ASSERT(i <= m_b);
114  OGDF_ASSERT(m_c <= j);
115  OGDF_ASSERT(j <= m_d);
116  return m_vpStart[size_t(i-m_a)*m_lenDim2+j];
117  }
118 
120  E &operator()(int i, int j) {
121  OGDF_ASSERT(m_a <= i);
122  OGDF_ASSERT(i <= m_b);
123  OGDF_ASSERT(m_c <= j);
124  OGDF_ASSERT(j <= m_d);
125  return m_vpStart[size_t(i-m_a)*m_lenDim2+j];
126  }
127 
129  void init() { init(0,-1,0,-1); }
130 
132  void init(int a, int b, int c, int d) {
133  deconstruct();
134  construct(a,b,c,d);
135  initialize();
136  }
137 
139  void init(int a, int b, int c, int d, const E &x) {
140  deconstruct();
141  construct(a,b,c,d);
142  initialize(x);
143  }
144 
146  Array2D<E> &operator=(const Array2D<E> &array2) {
147  deconstruct();
148  copy(array2);
149  return *this;
150  }
151 
153 
157  deconstruct();
158 
159  m_vpStart = A.m_vpStart;
160  m_pStart = A.m_pStart;
161  m_pStop = A.m_pStop;
162  m_lenDim2 = A.m_lenDim2;
163  m_a = A.m_a;
164  m_b = A.m_b;
165  m_c = A.m_c;
166  m_d = A.m_d;
167 
168  A.construct(0,-1,0,-1);
169  return *this;
170  }
171 
173  void fill(const E &x) {
174  E *pDest = m_pStop;
175  while(pDest > m_pStart)
176  *--pDest = x;
177  }
178 
179 private:
181  int m_lenDim2;
182  E *m_pStart;
183  E *m_pStop;
184 
185  int m_a;
186  int m_b;
187  int m_c;
188  int m_d;
189 
190  void construct(int a, int b, int c, int d);
191 
192  void initialize();
193  void initialize(const E &x);
194 
195  void deconstruct();
196 
197  void copy(const Array2D<E> &array2);
198 };
199 
200 
202 template<class E>
203 void Array2D<E>::construct(int a, int b, int c, int d)
204 {
205  m_a = a;
206  m_b = b;
207  m_c = c;
208  m_d = d;
209 
210  size_t lenDim1 = b-a+1;
211  m_lenDim2 = d-c+1;
212 
213  if (lenDim1 < 1 || m_lenDim2 < 1) {
214  m_pStart = m_vpStart = m_pStop = nullptr;
215 
216  } else {
217  size_t len = lenDim1*m_lenDim2;
218  m_pStart = static_cast<E *>( malloc(len*sizeof(E)) );
219  if (m_pStart == nullptr)
221 
222  m_vpStart = m_pStart - c;
223  m_pStop = m_pStart + len;
224  }
225 }
226 
227 
229 template<class E>
231 {
232  E *pDest = m_pStart;
233  try {
234  for (; pDest < m_pStop; pDest++)
235  new(pDest) E;
236  } catch (...) {
237  while(--pDest >= m_pStart)
238  pDest->~E();
239  free(m_pStart);
240  throw;
241  }
242 }
243 
244 
246 template<class E>
247 void Array2D<E>::initialize(const E &x)
248 {
249  E *pDest = m_pStart;
250  try {
251  for (; pDest < m_pStop; pDest++)
252  new(pDest) E(x);
253  } catch (...) {
254  while(--pDest >= m_pStart)
255  pDest->~E();
256  free(m_pStart);
257  throw;
258  }
259 }
260 
261 
263 template<class E>
265 {
266  if (!std::is_trivially_destructible<E>::value) {
267  for (E *pDest = m_pStart; pDest < m_pStop; pDest++)
268  pDest->~E();
269  }
270  free(m_pStart);
271 }
272 
274 template<class E>
275 void Array2D<E>::copy(const Array2D<E> &array2)
276 {
277  construct(array2.m_a, array2.m_b, array2.m_c, array2.m_d);
278 
279  if (m_pStart != 0) {
280  E *pSrc = array2.m_pStop;
281  E *pDest = m_pStop;
282  while(pDest > m_pStart)
283  new (--pDest) E(*--pSrc);
284  }
285 }
286 
287 
289 template<class E>
290 float Array2D<E>::det() const
291 {
292  // matrix must be quadratic
293  OGDF_ASSERT(size1() == size2());
294 
295  int a = m_a;
296  int b = m_b;
297  int c = m_c;
298  int d = m_d;
299  int n = m_lenDim2;
300 
301  int i, j;
302  int column;
303 
304  float determinant = 0.0;
305 
306  switch(n) {
307  case 0:
308  break;
309  case 1:
310  determinant = (float)((*this)(a, c));
311  break;
312  case 2:
313  determinant = (float)((*this)(a, c) * (*this)(b, d) - (*this)(a, d) * (*this)(b, c));
314  break;
315 
316  // Expanding along the first row (Laplace's Formula)
317  default:
318  Array2D<E> remMatrix(0, n-2, 0, n-2); // the remaining matrix
319  for(column = c; column <= d; column++) {
320  int rem_i = 0;
321  int rem_j = 0;
322  for(i = a; i <= b; i++) {
323  for(j = c; j <= d; j++) {
324  if(i != a && j != column) {
325  remMatrix(rem_i, rem_j) = (*this)(i, j);
326  if(rem_j < n-2) {
327  rem_j++;
328  }
329  else {
330  rem_i++;
331  rem_j = 0;
332  }
333  }
334  }
335  }
336  determinant += pow(-1.0,(a+column)) * (*this)(a,column) * remMatrix.det();
337  }
338  }
339 
340  return determinant;
341 }
342 
343 
344 
345 } // end namespace ogdf
int high1() const
Returns the maximal array index in dimension 1.
Definition: Array2D.h:89
void copy(const Array2D< E > &array2)
Copy array2.
Definition: Array2D.h:275
void deconstruct()
Call destructor of all elements.
Definition: Array2D.h:264
int size2() const
Returns the length of the index interval (number of entries) in dimension 2.
Definition: Array2D.h:104
Array2D(int a, int b, int c, int d)
Creates a two-dimensional array with index set [a, ..., b]*[c, ..., d].
Definition: Array2D.h:55
~Array2D()
Destructor.
Definition: Array2D.h:81
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:60
E * m_vpStart
The virtual start of the array (address of A[0,0]).
Definition: Array2D.h:180
E * m_pStop
Successor of last element (address of A[high1,high2+1]).
Definition: Array2D.h:183
E * m_pStart
The real start of the array (address of A[low1,low2]).
Definition: Array2D.h:182
int m_lenDim2
The number of elements in dimension 2.
Definition: Array2D.h:181
Array2D(const Array2D< E > &A)
Creates a two-dimensional array that is a copy of A.
Definition: Array2D.h:65
Array2D(Array2D< E > &&A)
Creates a two-dimensional array containing the elements of A (move semantics).
Definition: Array2D.h:73
void init(int a, int b, int c, int d)
Reinitializes the array to an array with index set [a, ..., b]*[c, ..., d].
Definition: Array2D.h:132
int size() const
Returns the size (number of elements) of the array.
Definition: Array2D.h:98
int m_b
The highest index in dimension 1.
Definition: Array2D.h:186
float det() const
Returns the determinant of the matrix.
Definition: Array2D.h:290
void init()
Reinitializes the array to an array with empty index set.
Definition: Array2D.h:129
Array2D(int a, int b, int c, int d, const E &x)
Creates a two-dimensional array with index set [a, ..., b]*[c, ..., d] and initailizes all elements w...
Definition: Array2D.h:60
void construct(int a, int b, int c, int d)
Constructs the array with index set [a, ..., b]*[c, ..., d].
Definition: Array2D.h:203
int m_c
The lowest index in dimension 2.
Definition: Array2D.h:187
Definition of exception classes.
int high2() const
Returns the maximal array index in dimension 2.
Definition: Array2D.h:95
The parameterized class Array2D implements dynamic two-dimensional arrays.
Definition: Array2D.h:46
Exception thrown when not enough memory is available to execute an algorithm.
Definition: exceptions.h:235
const E & operator()(int i, int j) const
Returns a reference to the element with index (i,j).
Definition: Array2D.h:111
E & operator()(int i, int j)
Returns a reference to the element with index (i,j).
Definition: Array2D.h:120
int size1() const
Returns the length of the index interval (number of entries) in dimension 1.
Definition: Array2D.h:101
int low2() const
Returns the minimal array index in dimension 2.
Definition: Array2D.h:92
void init(int a, int b, int c, int d, const E &x)
Reinitializes the array to an array with index set [a, ..., b]*[c, ..., d] and initializes all entrie...
Definition: Array2D.h:139
void fill(const E &x)
Sets all elements to x.
Definition: Array2D.h:173
int m_a
The lowest index in dimension 1.
Definition: Array2D.h:185
Array2D< E > & operator=(Array2D< E > &&A)
Assignment operator (move semantics).
Definition: Array2D.h:156
The namespace for all OGDF objects.
int m_d
The highest index in dimension 2.
Definition: Array2D.h:188
void initialize()
Initializes the array with default constructor of E.
Definition: Array2D.h:230
Array2D< E > & operator=(const Array2D< E > &array2)
Assignment operator.
Definition: Array2D.h:146
int low1() const
Returns the minimal array index in dimension 1.
Definition: Array2D.h:86
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
Array2D()
Creates a two-dimensional array with empty index set.
Definition: Array2D.h:52