Reference documentation for deal.II version 9.1.0-pre
vector_view.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_vector_view_h
17 #define dealii_vector_view_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/subscriptor.h>
24 
25 #include <deal.II/lac/vector.h>
26 
27 #include <cstdio>
28 
29 DEAL_II_NAMESPACE_OPEN
30 
31 
134 template <typename Number>
135 class DEAL_II_DEPRECATED VectorView : public Vector<Number>
136 {
137 public:
141  using size_type = types::global_dof_index;
142 
148  VectorView(const size_type new_size, Number *ptr);
149 
160  VectorView(const size_type new_size, const Number *ptr);
161 
166  ~VectorView() override;
167 
208  virtual void
209  reinit(const size_type N, const bool omit_zeroing_entries = false) override;
210 
215  void
216  reinit(const size_type N, Number *ptr);
217 
223  void
224  reinit(const size_type N, const Number *ptr);
225 
230  virtual void
231  swap(Vector<Number> &v) override;
232 };
233 
234 
235 
237 /*----------------------- Inline functions ----------------------------------*/
238 
239 #ifndef DOXYGEN
240 
241 template <typename Number>
242 inline VectorView<Number>::VectorView(const size_type new_size, Number *ptr)
243 {
244  this->vec_size = new_size;
245  this->max_vec_size = new_size;
246  // release the pointer, but do not delete the object pointed to
247  this->values.release();
248  this->values.reset(ptr);
249 }
250 
251 
252 
253 template <typename Number>
254 inline VectorView<Number>::VectorView(const size_type new_size,
255  const Number * ptr)
256 {
257  this->vec_size = new_size;
258  this->max_vec_size = new_size;
259  this->values.reset(const_cast<Number *>(ptr));
260 }
261 
262 
263 
264 template <typename Number>
266 {
267  // avoid that the base class releases
268  // memory it doesn't own
269  this->vec_size = 0;
270  this->max_vec_size = 0;
271 
272  // release the pointer, but do not delete the object pointed to
273  this->values.release();
274 }
275 
276 
277 template <typename Number>
278 inline void
279 VectorView<Number>::reinit(const size_type N, const bool omit_zeroing_entries)
280 {
281  this->vec_size = N;
282  this->max_vec_size = N;
283  if (omit_zeroing_entries == false)
284  Vector<Number>::operator=(static_cast<Number>(0));
285 }
286 
287 
288 template <typename Number>
289 inline void
290 VectorView<Number>::reinit(const size_type new_size, Number *ptr)
291 {
292  this->vec_size = new_size;
293  this->max_vec_size = new_size;
294  // release the pointer, but do not delete the object pointed to
295  this->values.release();
296  this->values.reset(ptr);
297 }
298 
299 
300 template <typename Number>
301 inline void
302 VectorView<Number>::reinit(const size_type new_size, const Number *ptr)
303 {
304  this->vec_size = new_size;
305  this->max_vec_size = new_size;
306  // release the pointer, but do not delete the object pointed to
307  this->values.release();
308  this->values.reset(const_cast<Number *>(ptr));
309 }
310 
311 
312 template <typename Number>
313 inline void
315 {
316  AssertThrow(false, ExcMessage("Can't swap a VectorView with a Vector!"));
317 }
318 
319 #endif
320 
321 DEAL_II_NAMESPACE_CLOSE
322 
323 #endif
#define AssertThrow(cond, exc)
Definition: exceptions.h:1329
Vector< Number > & operator=(const Number s)
unsigned long long int global_dof_index
Definition: types.h:72
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false) override
~VectorView() override
static::ExceptionBase & ExcMessage(std::string arg1)
virtual void swap(Vector< Number > &v) override
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1353
VectorView(const size_type new_size, Number *ptr)