ug4
reservable_array_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 
34 #ifndef __H__UG__COMMON__VARIABLE_ARRAY_IMPL_H__
35 #define __H__UG__COMMON__VARIABLE_ARRAY_IMPL_H__
36 
37 #include "storage.h"
38 #include "variable_array.h"
39 #include <algorithm> // for min
40 #include <cstring>
41 
42 namespace ug{
43 
44 
46 
47 
48 template<typename T, eMatrixOrdering T_ordering>
50 {
51  values = NULL;
52  rows = 0;
53  cols = 0;
54  arraySize = 0;
55 }
56 
57 
58 template<typename T, eMatrixOrdering T_ordering>
60 {
61  values = NULL;
62  rows = 0;
63  cols = 0;
64  arraySize = 0;
65  resize(rows, cols);
66 }
67 
68 template<typename T, eMatrixOrdering T_ordering>
70 {
71  if(this == &other) return;
72  if(values) { delete[] values; values = NULL; }
73  rows = 0;
74  cols = 0;
75  arraySize = 0;
76  resize(other.num_rows(), other.num_cols());
77  for(size_type i=0; i<rows*cols; i++)
78  values[i] = other.values[i];
79 }
80 
81 template<typename T, eMatrixOrdering T_ordering>
83 {
84  if(values) { delete[] values; values = NULL; }
85  rows = cols = 0;
86 }
87 
88 // Capacity
89 
90 template<typename T, eMatrixOrdering T_ordering>
91 size_t
93 {
94  return rows;
95 }
96 
97 template<typename T, eMatrixOrdering T_ordering>
98 size_t
100 {
101  return cols;
102 }
103 
104 
105 template<typename T, eMatrixOrdering T_ordering>
106 bool
107 ReservableArray2<T, T_ordering>::resize(size_t newRows, size_t newCols, bool bCopyValues)
108 {
109  assert(newRows >= 0 && newCols >= 0);
110  if(newRows == rows && newCols == cols) return true;
111 
112  if(newRows == 0 && newCols == 0)
113  {
114  rows = cols = 0;
115  if(values) delete[] values;
116  values = NULL;
117  return true;
118  }
119 
120  if(bCopyValues)
121  {
122  size_t minRows = std::min(rows, newRows);
123  size_t minCols = std::min(cols, newCols);
124  if(newRows*newCols > arraySize)
125  {
126  value_type *new_values = new T[newRows*newCols];
127  arraySize = newRows*newCols;
128  memset(new_values, 0, sizeof(T)*newRows*newCols); // todo: think about that
129  UG_ASSERT(new_values != NULL, "out of memory");
130  if(new_values==NULL) return false;
131  /*
132  if(storage_traits<value_type>::is_static)
133  {
134  ...
135  }
136  else {
137 
138  */
139 
140  // we are using swap to avoid re-allocations
141  if(T_ordering==RowMajor)
142  for(size_t r=0; r<minRows; r++)
143  for(size_t c=0; c<minCols; c++)
144  std::swap(new_values[c+r*newCols], values[c+r*cols]);
145  else
146  for(size_t r=0; r<minRows; r++)
147  for(size_t c=0; c<minCols; c++)
148  std::swap(new_values[r+c*newRows], values[r+c*rows]);
149 
150  if(values) delete[] values;
151  }
152  else
153  {
154  if(T_ordering==RowMajor)
155  {
156  if(newCols < cols)
157  {
158  for(size_t r=1; r<minRows; r++)
159  for(size_t c=0; c<minCols; c++)
160  std::swap(values[c+r*newCols], values[c+r*cols]);
161 
162  }
163  else if(newCols > cols)
164  {
165  for(size_t r=minRows-1; r>0; r--)
166  {
167  size_t c=newCols-1;
168  do
169  std::swap(values[c+r*newCols], values[c+r*cols]);
170  while(--c > 0);
171  }
172  }
173  else
174  {
175  // nothing to do
176  }
177  }
178  else
179  {
180  if(newRows < rows)
181  {
182  for(size_t c=1; c<minCols; c++)
183  for(size_t r=0; r<minRows; r++)
184  std::swap(values[r+c*newRows], values[r+c*rows]);
185 
186  }
187  else if(newRows > rows)
188  {
189  for(size_t c=minCols-1; c>0; c--)
190  {
191  size_t r=newRows-1;
192  do
193  std::swap(values[r+c*newRows], values[r+c*rows]);
194  while(--r > 0);
195  }
196  }
197  else
198  {
199  // nothing to do
200  }
201  }
202 
203  }
204  }
205  rows = newRows;
206  cols = newCols;
207  values = new_values;
208  return true;
209 }
210 
211 
212 template<typename T, eMatrixOrdering T_ordering>
213 T &
215 {
216  assert(r>=0 && r<rows);
217  assert(c>=0 && c<cols);
218  if(T_ordering==RowMajor)
219  return values[c+r*cols];
220  else
221  return values[r+c*rows];
222 }
223 
224 template<typename T, eMatrixOrdering T_ordering>
225 const T &
227 {
228  assert(r>=0 && r<rows);
229  assert(c>=0 && c<cols);
230  if(T_ordering==RowMajor)
231  return values[c+r*cols];
232  else
233  return values[r+c*rows];
234 }
235 
236 template<typename T, eMatrixOrdering T_ordering>
237 std::ostream &operator << (std::ostream &out, const ReservableArray2<T, T_ordering> &arr)
238 {
239  out << "[ ";
240  //out << "ReservableArray2 (" << arr.num_rows() << "x" << arr.num_cols() << "), " << ((T_ordering == ColMajor) ? "ColMajor" : "RowMajor") << endl;
241  typedef size_t size_type;
242  for(size_type r=0; r<arr.num_rows(); r++)
243  {
244  for(size_type c=0; c<arr.num_cols(); c++)
245  out << arr(r, c) << " ";
246  if(r != arr.num_rows()-1) out << "| ";
247  }
248  out << "]";
249  return out;
250 }
251 
252 }
253 #endif // __H__UG__COMMON__VARIABLE_ARRAY_IMPL_H__
Definition: reservable_array.h:54
T * values
Definition: reservable_array.h:121
~ReservableArray2()
Definition: reservable_array_impl.h:82
ReservableArray2()
Definition: reservable_array_impl.h:49
T value_type
Definition: reservable_array.h:56
const T & operator()(size_type r, size_type c) const
Definition: reservable_array_impl.h:226
size_type num_rows() const
Definition: reservable_array_impl.h:92
size_type num_cols() const
Definition: reservable_array_impl.h:99
bool resize(size_type newRows, size_type newCols, bool bCopyValues=true)
Definition: reservable_array_impl.h:107
std::ostream & operator<<(std::ostream &outStream, const ug::MathMatrix< 2, 2 > &m)
Definition: math_matrix.cpp:38
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
the ug namespace
@ RowMajor
Definition: storage.h:48
bool resize(size_t newRows, size_t newCols)