NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
container.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2018 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19
20#ifndef CONTAINER_HPP
21#define CONTAINER_HPP
22
23// required for size_t
24#include <cstddef>
25#include <vector>
26
27#include "../ui/error.hpp"
28
29// declare the namespace
30namespace NumeRe
31{
32 // Template class for a generic container, which will
33 // handle the copying of a two-dimensional pointer
34 // Will take ownership of the passed pointer as long
35 // as it exists and is not copied
36 // During the copy process, the storage pointer will
37 // move to the copied object
38 template <class T>
40 {
41 private:
42 // Internal storage including the dimensions
43 // variables are declared as mutable to allow
44 // to be const copy constructed although the
45 // ownership of the storage will move from
46 // one to another container
47 mutable T** storage;
48 mutable size_t rows;
49 mutable size_t cols;
50
51 // Clean up function
53 {
54 // Ensure that the storage is not empty
55 if (storage)
56 {
57 // the first dimension are always rows
58 // free the memory
59 for (size_t i = 0; i < rows; i++)
60 {
61 delete[] storage[i];
62 }
63 delete[] storage;
64
65 // reset the members
66 storage = nullptr;
67 rows = 0;
68 cols = 0;
69 }
70 }
71
72 // preparation function
73 void prepareStorage(size_t _rows, size_t _cols)
74 {
75 // Clean the storage
76 if (storage)
78
79 // Create a new storage
80 storage = new T*[_rows];
81 for (size_t i = 0; i < _rows; i++)
82 storage[i] = new T[_cols];
83 rows = _rows;
84 cols = _cols;
85 }
86
87 public:
88 // default constructor
89 Container() : storage(nullptr), rows(0), cols(0) {}
90
91 // default preparation constructor
92 Container(size_t _rows, size_t _cols) : Container()
93 {
94 prepareStorage(_rows, _cols);
95 }
96
97 // Copy constructor, will move the contents
98 Container(const Container<T>& _container) : Container()
99 {
100 // Avoid copying empty storage
101 if (!_container.storage)
102 return;
103
104 // Ensure that the storage is empty
105 if (storage)
106 {
107 freeStorage();
108 }
109
110 // Copy the storage and the dimensions
111 storage = _container.storage;
112 rows = _container.rows;
113 cols = _container.cols;
114
115 // reset the passed container
116 _container.storage = nullptr;
117 _container.rows = 0;
118 _container.cols = 0;
119 }
120
121 // special constructor for an external pointer
122 Container(T** (&extStorage), size_t _rows, size_t _cols) : Container()
123 {
124 // avoid copying empty data fields
125 if(!extStorage)
126 return;
127
128 // Copy the storage and the dimensons
129 storage = extStorage;
130 rows = _rows;
131 cols = _cols;
132
133 // reset the external storage
134 extStorage = nullptr;
135 }
136
137 // special vector constructor
138 Container(const std::vector<std::vector<T> >& extStorage) : Container()
139 {
140 // Prepare the storage
141 prepareStorage(extStorage.size(), extStorage[0].size());
142
143 // Copy the contents
144 for (size_t i = 0; i < extStorage.size(); i++)
145 {
146 for (size_t j = 0; j < extStorage[i].size(); j++)
147 this->set(i, j, extStorage[i][j]);
148 }
149 }
150
151 // Destuctor: will free memory, if available
153 {
154 freeStorage();
155 }
156
157 // Assignment operator
159 {
160 // Avoid copying empty storage
161 if (!_container.storage)
162 return *this;
163
164 // Ensure that the storage is empty
165 if (storage)
166 {
167 freeStorage();
168 }
169
170 // Copy the storage and the dimensions
171 storage = _container.storage;
172 rows = _container.rows;
173 cols = _container.cols;
174
175 // reset the passed container
176 _container.storage = nullptr;
177 _container.rows = 0;
178 _container.cols = 0;
179
180 return *this;
181 }
182
183
184 // Accessors
185 // read accessor
186 T& get(size_t row, size_t col)
187 {
188 // Ensure that the dimensions are matching
189 if (storage && row < rows && col < cols)
190 return storage[row][col];
191
192 // Otherwise throw an error
194 }
195
196 // write accessor
197 void set(size_t row, size_t col, T val)
198 {
199 // Ensure that the dimensions are matching
200 if (storage && row < rows && col < cols)
201 storage[row][col] = val;
202 }
203
204 // dimensions
205 size_t getRows() const
206 {
207 return rows;
208 }
209 size_t getCols() const
210 {
211 return cols;
212 }
213 };
214
215} // namespace NumeRe
216
217#endif // CONTAINER_HPP
218
size_t getCols() const
Definition: container.hpp:209
T & get(size_t row, size_t col)
Definition: container.hpp:186
Container(size_t _rows, size_t _cols)
Definition: container.hpp:92
Container< T > & operator=(const Container< T > &_container)
Definition: container.hpp:158
void prepareStorage(size_t _rows, size_t _cols)
Definition: container.hpp:73
size_t getRows() const
Definition: container.hpp:205
Container(const Container< T > &_container)
Definition: container.hpp:98
void set(size_t row, size_t col, T val)
Definition: container.hpp:197
Container(const std::vector< std::vector< T > > &extStorage)
Definition: container.hpp:138
Container(T **(&extStorage), size_t _rows, size_t _cols)
Definition: container.hpp:122
Common exception class for all exceptions thrown in NumeRe.
Definition: error.hpp:32
@ INVALID_DATA_ACCESS
Definition: error.hpp:125
static size_t invalid_position
Definition: error.hpp:235