Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
field_impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015: G-CSC, Goethe University Frankfurt
3 * Author: Sebastian Reiter
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#ifndef __H__UG_field_impl__
34#define __H__UG_field_impl__
35
36#include <cstring>
37#include <algorithm>
38
39namespace ug{
40
41template <class T> Field<T>::
42Field() :
43 m_width(0),
44 m_height(0),
45 m_capacity(0),
46 m_data(NULL)
49
50template <class T> Field<T>::
51Field(size_t width, size_t height) :
52 m_width(width),
53 m_height(height)
55 m_capacity = m_width * m_height;
56 m_data = new T[m_capacity];
57}
58
59template <class T> Field<T>::
60Field(size_t width, size_t height, const T& value) :
61 m_width(width),
62 m_height(height)
63{
66 fill_all(value);
68
69template <class T> Field<T>::
70Field(const Field& f){
71 m_width = f.width();
72 m_height = f.height();
73 m_capacity = m_width * m_height;
74 m_data = new T[m_capacity];
75 memcpy(m_data, f.data(), m_capacity * sizeof(T));
77
78template <class T> Field<T>::
80 if(m_data)
81 delete[] m_data;
82}
83
84template <class T> Field<T>& Field<T>::
85operator=(const Field& f){
86 m_width = f.m_width;
87 m_height = f.m_height;
88 if(m_data && (m_capacity != m_width * m_height)){
89 delete[] m_data;
90 m_data = NULL;
91 }
92 m_capacity = m_width * m_height;
93 if(!m_data)
94 m_data = new T[m_capacity];
95 memcpy(m_data, f.data(), m_capacity * sizeof(T));
96 return *this;
97}
98
99template <class T> void Field<T>::
100resize_no_copy(size_t width, size_t height){
101
102 if(width * height > m_capacity){
103 if(m_data)
104 delete[] m_data;
105 m_capacity += width * height;
106 m_data = new T[m_capacity];
107 }
108 m_width = width;
109 m_height = height;
110}
111
112template <class T> T& Field<T>::
113at(size_t x, size_t y){
114 return m_data[array_index(x, y)];
115}
116
117template <class T> const T& Field<T>::
118at(size_t x, size_t y) const{
119 return m_data[array_index(x, y)];
120}
121
122template <class T> void Field<T>::
123fill(size_t x, size_t y, size_t w, size_t h, const T& value)
124{
125 using std::min;
126 using std::max;
127
128 const size_t xEnd = max(x + w, m_width);
129 const size_t yEnd = max(y + h, m_height);
130
131 for(size_t iy = max(y, size_t(0)); iy < yEnd; ++iy){
132 for(size_t ix = max(x, size_t(0)); ix < xEnd; ++ix){
133 m_data[array_index(ix, iy)] = value;
134 }
135 }
136}
137
138template <class T> void Field<T>::
139fill_all(const T& value)
140{
141 const size_t dataSize = size();
142 for(size_t i = 0; i < dataSize; ++i)
143 m_data[i] = value;
144}
145
146template <class T> void Field<T>::
147copy(size_t x, size_t y, const Field& f){
148 using std::min;
149 using std::max;
150
151 const size_t xEnd = max(x + f.width(), m_width);
152 const size_t yEnd = max(y + f.height(), m_height);
153
154 for(size_t iy = max(y, size_t(0)); iy < yEnd; ++iy){
155 for(size_t ix = max(x, size_t(0)); ix < xEnd; ++ix){
156 m_data[array_index(ix, iy)] = f.at(ix, iy);
157 }
158 }
159}
160
161template <class T> void Field<T>::
162swap(Field& f){
163 using std::swap;
164 swap(m_width, f.m_width);
165 swap(m_height, f.m_height);
166 swap(m_capacity, f.m_capacity);
167 swap(m_data, f.m_data);
168}
169
170
171template <class T> size_t Field<T>::
172array_index(size_t x, size_t y) const{
173 return x + y * m_width;
174}
175
176
177template <class T>
178template <class Archive>
180save( Archive& ar, const unsigned int version) const
181{
182 ar & m_width;
183 ar & m_height;
184 const size_t s = size();
185 for(size_t i = 0; i < s; ++i){
186 ar & m_data[i];
187 }
188}
189
190template <class T>
191template <class Archive>
193load( Archive& ar, const unsigned int version)
194{
195 size_t w = 0, h = 0;
196 ar & w;
197 ar & h;
198 resize_no_copy(w, h);
199 const size_t s = size();
200 for(size_t i = 0; i < s; ++i){
201 ar & m_data[i];
202 }
203}
204
205}// end of namespace
206
207#endif //__H__UG_field_impl__
parameterString s
Definition field.h:42
size_t width() const
Definition field.h:57
T * m_data
Definition field.h:87
size_t m_height
Definition field.h:85
~Field()
Definition field_impl.hpp:79
T & at(size_t x, size_t y)
Definition field_impl.hpp:113
void save(Archive &ar, const unsigned int version) const
Definition field_impl.hpp:180
T * data()
Definition field.h:61
void copy(size_t x, size_t y, const Field &f)
Definition field_impl.hpp:147
size_t array_index(size_t x, size_t y) const
Definition field_impl.hpp:172
size_t m_width
Definition field.h:84
void fill_all(const T &value)
Definition field_impl.hpp:139
size_t height() const
Definition field.h:58
void fill(size_t x, size_t y, size_t w, size_t h, const T &value)
Definition field_impl.hpp:123
Field()
Definition field_impl.hpp:42
size_t m_capacity
Definition field.h:86
void load(Archive &ar, const unsigned int version)
Definition field_impl.hpp:193
void swap(Field &f)
Definition field_impl.hpp:162
void resize_no_copy(size_t width, size_t height)
Definition field_impl.hpp:100
Field & operator=(const Field &field)
Definition field_impl.hpp:85
the ug namespace