ug4
Loading...
Searching...
No Matches
variable_array_impl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-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 "common/common.h"
40#include <algorithm> // for min
41#include <cstring>
42
43namespace ug{
44
45// 'tors
46template<typename T>
48{
49 n = 0;
50 values = NULL;
51}
52
53
54template<typename T>
56{
57 n = 0;
58 values = NULL;
59 resize(n_, false);
60}
61
62template<typename T>
64{
65 if(this == &other) return;
66 n = 0;
67 values = NULL;
68 resize(other.size(), false);
69 for(size_type i=0; i<n; i++)
70 values[i] = other[i];
71}
72
73template<typename T>
75{
76 if(values) { delete[] values; values = NULL; }
77 n = 0;
79
80
81// Capacity
82
83template<typename T>
84inline size_t
86{
87 return n;
88}
89
90template<typename T>
91bool
92VariableArray1<T>::resize(size_t newN, bool bCopyValues)
93{
94 if(newN == n) return true;
95
96 if(newN <= 0)
97 {
98 if(values) delete[] values;
99 values = NULL;
100 n = 0;
101 return true;
102 }
103 value_type *new_values = new T[newN];
104 UG_ASSERT(new_values != NULL, "out of memory");
105 if(new_values == NULL) return false;
106 memset(reinterpret_cast<void *> (new_values), 0, sizeof(T)*newN); // todo: think about that
107
108 if(bCopyValues)
110 /*
111 if(storage_traits<value_type>::is_static)
112 {
113 for(int i=0; i<n; i++)
114 new_values[i] = values[i];
115 }
116 else {
117 // we are using swap to avoid re-allocations
118 */
119 size_t minN = std::min(n, newN);
120 for(size_t i=0; i<minN; i++)
121 std::swap(new_values[i], values[i]);
122 }
123
124 if(values) delete[] values;
125 values = new_values;
126 n = newN;
127 return true;
128}
129
130template<typename T>
131inline size_t
133{
134 return n;
135}
136
137
138// use stl::vector if you want to use reserve
139template<typename T>
140inline bool
141VariableArray1<T>::reserve(size_t newCapacity) const
142{
143 return true;
144}
145
146
147// Element access
148
149template<typename T>
150T &
153 assert(values);
154 assert(i<n);
155 return values[i];
157
158template<typename T>
159const T &
162 assert(values);
163 assert(i<n);
164 return values[i];
165}
166
167template<typename T>
168std::ostream &operator << (std::ostream &out, const VariableArray1<T> &arr)
169{
170 //out << "VariableArray (n=" << arr.size() << ") [ ";
171 for(size_t i=0; i<arr.size(); i++)
172 out << arr[i] << " ";
173 out << "]";
174 return out;
175}
176
177
179
180
181template<typename T, eMatrixOrdering T_ordering>
183{
184 values = NULL;
185 rows = 0;
186 cols = 0;
187}
188
189
190template<typename T, eMatrixOrdering T_ordering>
192{
193 values = NULL;
194 rows = 0;
195 cols = 0;
196 resize(rows, cols);
197}
199template<typename T, eMatrixOrdering T_ordering>
201{
202 if(this == &other) return;
203 values = NULL;
204 rows = 0;
205 cols = 0;
206 resize(other.num_rows(), other.num_cols(), false);
207 for(size_type i=0; i<rows*cols; i++)
208 values[i] = other.values[i];
209}
210
211template<typename T, eMatrixOrdering T_ordering>
213{
214 if(values) { delete[] values; values = NULL; }
215 rows = cols = 0;
216}
217
218// Capacity
219
220template<typename T, eMatrixOrdering T_ordering>
221size_t
223{
224 return rows;
225}
226
227template<typename T, eMatrixOrdering T_ordering>
228size_t
230{
231 return cols;
232}
233
234
235template<typename T, eMatrixOrdering T_ordering>
236bool
237VariableArray2<T, T_ordering>::resize(size_t newRows, size_t newCols, bool bCopyValues)
238{
239 if(newRows == rows && newCols == cols) return true;
240
241 if(newRows == 0 && newCols == 0)
242 {
243 rows = cols = 0;
244 if(values) delete[] values;
245 values = NULL;
246 return true;
247 }
248
249 value_type *new_values = new T[newRows*newCols];
250 memset(reinterpret_cast<void *> (new_values), 0, sizeof(T)*newRows*newCols); // todo: think about that
251 UG_ASSERT(new_values != NULL, "out of memory");
252 if(new_values==NULL) return false;
253 /*
254 if(storage_traits<value_type>::is_static)
255 {
256 ...
257 }
258 else {
259
260 */
261 if(bCopyValues)
262 {
263 size_t minRows = std::min(rows, newRows);
264 size_t minCols = std::min(cols, newCols);
265
266 // we are using swap to avoid re-allocations
267 if(T_ordering==RowMajor)
268 for(size_t r=0; r<minRows; r++)
269 for(size_t c=0; c<minCols; c++)
270 std::swap(new_values[c+r*newCols], values[c+r*cols]);
271 else
272 for(size_t r=0; r<minRows; r++)
273 for(size_t c=0; c<minCols; c++)
274 std::swap(new_values[r+c*newRows], values[r+c*rows]);
275 }
276
277 if(values) delete[] values;
278 rows = newRows;
279 cols = newCols;
280 values = new_values;
281 return true;
282}
283
284
285template<typename T, eMatrixOrdering T_ordering>
286T &
288{
289 UG_ASSERT(r<rows, "r = " << r << ", rows = " << rows);
290 UG_ASSERT(c<cols, "c = " << c << ", cols = " << cols);
291 if(T_ordering==RowMajor)
292 return values[c+r*cols];
293 else
294 return values[r+c*rows];
295}
296
297template<typename T, eMatrixOrdering T_ordering>
298const T &
300{
301 UG_ASSERT(r<rows, "r = " << r << ", rows = " << rows);
302 UG_ASSERT(c<cols, "c = " << c << ", cols = " << cols);
303 if(T_ordering==RowMajor)
304 return values[c+r*cols];
305 else
306 return values[r+c*rows];
307}
308
309template<typename T, eMatrixOrdering T_ordering>
310std::ostream &operator << (std::ostream &out, const VariableArray2<T, T_ordering> &arr)
311{
312 out << "[ ";
313 //out << "VariableArray2 (" << arr.num_rows() << "x" << arr.num_cols() << "), " << ((T_ordering == ColMajor) ? "ColMajor" : "RowMajor") << endl;
314 typedef size_t size_type;
315 for(size_type r=0; r<arr.num_rows(); r++)
316 {
317 for(size_type c=0; c<arr.num_cols(); c++)
318 out << arr(r, c) << " ";
319 if(r != arr.num_rows()-1) out << "| ";
320 }
321 out << "]";
322 return out;
323}
324
325}
326#endif // __H__UG__COMMON__VARIABLE_ARRAY_IMPL_H__
Definition variable_array.h:59
~VariableArray1()
Definition variable_array_impl.h:74
bool reserve(size_type n) const
Definition variable_array_impl.h:141
size_t size_type
Definition variable_array.h:62
size_type size() const
Definition variable_array_impl.h:85
T value_type
Definition variable_array.h:61
VariableArray1()
Definition variable_array_impl.h:47
const T & operator[](size_type i) const
Definition variable_array_impl.h:160
bool resize(size_type n, bool bCopyValues=true)
Definition variable_array_impl.h:92
size_type capacity() const
Definition variable_array_impl.h:132
Definition variable_array.h:139
T value_type
Definition variable_array.h:141
T * values
Definition variable_array.h:206
VariableArray2()
Definition variable_array_impl.h:182
bool resize(size_type newRows, size_type newCols, bool bCopyValues=true)
Definition variable_array_impl.h:237
size_type num_rows() const
Definition variable_array_impl.h:222
size_type num_cols() const
Definition variable_array_impl.h:229
const T & operator()(size_type r, size_type c) const
Definition variable_array_impl.h:299
size_t size_type
Definition variable_array.h:142
~VariableArray2()
Definition variable_array_impl.h:212
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)