www.mooseframework.org
MooseArray.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include <vector>
13 #include <memory>
14 #include "MooseError.h"
15 
16 template <typename T>
18 {
19 public:
20  typedef T value_type;
21 
25  MooseArray() : _data(nullptr), _size(0), _allocated_size(0) {}
26 
30  explicit MooseArray(const unsigned int size) : _data(nullptr), _size(0), _allocated_size(0)
31  {
32  resize(size);
33  }
34 
39  explicit MooseArray(const unsigned int size, const T & default_value)
40  : _data(nullptr), _size(0), _allocated_size(0)
41  {
42  resize(size);
43 
44  setAllValues(default_value);
45  }
46 
47  explicit MooseArray(const MooseArray & rhs) : _data(nullptr), _size(0), _allocated_size(0)
48  {
49  resize(rhs._size);
50 
51  for (unsigned int i = 0; i < _size; i++)
52  _data[i] = rhs._data[i];
53  }
54 
55  ~MooseArray() = default;
56 
61  void setAllValues(const T & value);
62 
66  void release()
67  {
68  if (_data_ptr)
69  {
70  _data_ptr.reset();
71  _data = nullptr;
72  _allocated_size = _size = 0;
73  }
74  }
75 
84  void clear();
85 
101  template <bool value_initialize = false>
102  void resize(unsigned int size);
103 
117  void resize(unsigned int size, const T & default_value);
118 
123  unsigned int size() const;
124 
128  T & operator[](const unsigned int i);
129 
133  const T & operator[](const unsigned int i) const;
134 
139  void swap(MooseArray & rhs);
140 
146  void shallowCopy(const MooseArray & rhs);
147 
153  void shallowCopy(std::vector<T> & rhs);
154 
160  MooseArray<T> & operator=(const std::vector<T> & rhs);
161 
167  MooseArray<T> & operator=(const MooseArray<T> & rhs);
168 
174  std::vector<T> stdVector() const;
175 
179  const T * data() const { return _data; }
180 
181 private:
183  std::unique_ptr<T[]> _data_ptr;
184 
185  // Actual data pointer (from inside of the smart pointer).
186  T * _data;
187 
189  unsigned int _size;
190 
192  unsigned int _allocated_size;
193 };
194 
195 template <typename T>
196 inline void
198 {
199  for (unsigned int i = 0; i < _size; i++)
200  _data[i] = value;
201 }
202 
203 template <typename T>
204 inline void
206 {
207  _size = 0;
208 }
209 
210 template <typename T>
211 template <bool value_initialize>
212 inline void
213 MooseArray<T>::resize(unsigned int size)
214 {
215  if (size > _allocated_size)
216  {
217  if constexpr (value_initialize)
218  _data_ptr.reset(new T[size]());
219  else
220  _data_ptr = std::make_unique<T[]>(size);
221  mooseAssert(_data_ptr, "Failed to allocate MooseArray memory!");
222 
223  _data = _data_ptr.get();
224  _allocated_size = size;
225  }
226 
227  _size = size;
228 }
229 
230 template <typename T>
231 inline void
232 MooseArray<T>::resize(unsigned int size, const T & default_value)
233 {
234  if (size > _allocated_size)
235  {
236  auto new_pointer = std::make_unique<T[]>(size);
237  mooseAssert(new_pointer, "Failed to allocate MooseArray memory!");
238 
239  if (_data)
240  for (unsigned int i = 0; i < _size; i++)
241  new_pointer[i] = _data[i];
242 
243  _data_ptr = std::move(new_pointer);
244  _data = _data_ptr.get();
245  _allocated_size = size;
246  }
247 
248  for (unsigned int i = _size; i < size; i++)
249  _data[i] = default_value;
250 
251  _size = size;
252 }
253 
254 template <typename T>
255 inline unsigned int
257 {
258  return _size;
259 }
260 
261 template <typename T>
262 inline T &
263 MooseArray<T>::operator[](const unsigned int i)
264 {
265  mooseAssert(i < _size,
266  "Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
267 
268  return _data[i];
269 }
270 
271 template <typename T>
272 inline const T &
273 MooseArray<T>::operator[](const unsigned int i) const
274 {
275  mooseAssert(i < _size,
276  "Access out of bounds in MooseArray (i: " << i << " size: " << _size << ")");
277 
278  return _data[i];
279 }
280 
281 template <typename T>
282 inline void
284 {
285  _data_ptr.swap(rhs._data_ptr);
286  std::swap(_data, rhs._data);
287  std::swap(_size, rhs._size);
288  std::swap(_allocated_size, rhs._allocated_size);
289 }
290 
291 template <typename T>
292 inline void
294 {
295  _data_ptr.reset();
296  _data = rhs._data;
297  _size = rhs._size;
298  _allocated_size = rhs._allocated_size;
299 }
300 
301 template <typename T>
302 inline void
303 MooseArray<T>::shallowCopy(std::vector<T> & rhs)
304 {
305  _data_ptr.reset();
306  _data = &rhs[0];
307  _size = rhs.size();
308  _allocated_size = rhs.size();
309 }
310 
311 template <typename T>
312 inline MooseArray<T> &
313 MooseArray<T>::operator=(const std::vector<T> & rhs)
314 {
315  unsigned int rhs_size = rhs.size();
316 
317  resize(rhs_size);
318 
319  for (unsigned int i = 0; i < rhs_size; i++)
320  _data[i] = rhs[i];
321 
322  return *this;
323 }
324 
325 template <typename T>
326 inline MooseArray<T> &
328 {
329  // mooseError("Shouldn't be doing this!");
330  resize(rhs._size);
331  // memcpy(_data,rhs._data,sizeof(T)*_size);
332 
333  for (unsigned int i = 0; i < _size; i++)
334  _data[i] = rhs._data[i];
335 
336  return *this;
337 }
338 
339 template <class T>
340 std::vector<T>
342 {
343  return std::vector<T>(_data, _data + _size);
344 }
345 
346 template <class T>
347 void
349 {
350  for (unsigned int i = 0; i < a.size(); i++)
351  a[i].release();
352  a.release();
353 }
MooseArray(const unsigned int size, const T &default_value)
Definition: MooseArray.h:39
~MooseArray()=default
MooseArray()
Default constructor.
Definition: MooseArray.h:25
void swap(std::vector< T > &data, const std::size_t idx0, const std::size_t idx1, const libMesh::Parallel::Communicator &comm)
Swap function for serial or distributed vector of data.
Definition: Shuffle.h:494
const T * data() const
Reference to first element of array.
Definition: MooseArray.h:179
void swap(MooseArray &rhs)
Swap memory in this object with the &#39;rhs&#39; object.
Definition: MooseArray.h:283
void shallowCopy(const MooseArray &rhs)
Doesn&#39;t actually make a copy of the data.
Definition: MooseArray.h:293
void setAllValues(const T &value)
Sets all values of the array to the passed in value.
Definition: MooseArray.h:197
void freeDoubleMooseArray(MooseArray< MooseArray< T >> &a)
Definition: MooseArray.h:348
MooseArray< T > & operator=(const std::vector< T > &rhs)
Actual operator=...
Definition: MooseArray.h:313
unsigned int _size
The current number of elements the array can hold.
Definition: MooseArray.h:189
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:256
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
unsigned int _allocated_size
Number of allocated memory positions for storage.
Definition: MooseArray.h:192
std::vector< T > stdVector() const
Extremely inefficient way to produce a std::vector from a MooseArray!
Definition: MooseArray.h:341
MooseArray(const MooseArray &rhs)
Definition: MooseArray.h:47
std::unique_ptr< T[]> _data_ptr
Smart pointer storage.
Definition: MooseArray.h:183
forward declarations
Definition: MooseArray.h:17
void release()
Manually deallocates the data pointer.
Definition: MooseArray.h:66
void resize(unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:213
void clear()
Change the number of elements the array can store to zero.
Definition: MooseArray.h:205
T & operator[](const unsigned int i)
Get element i out of the array.
Definition: MooseArray.h:263
MooseArray(const unsigned int size)
Definition: MooseArray.h:30