KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
pointer_vector.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 // Riccardo Rossi
12 //
13 //
14 
15 
16 #if !defined(KRATOS_POINTER_VECTOR_H_INCLUDED )
17 #define KRATOS_POINTER_VECTOR_H_INCLUDED
18 
19 
20 
21 // System includes
22 #include <functional>
23 #include <string>
24 #include <iostream>
25 #include <sstream>
26 #include <vector>
27 
28 // External includes
29 #include <boost/iterator/indirect_iterator.hpp>
30 
31 
32 // Project includes
33 #include "includes/define.h"
34 #include "includes/serializer.h"
35 
36 
37 namespace Kratos
38 {
41 
45 
49 
53 
57 
59 
68 template<class TDataType,
69  class TPointerType = typename TDataType::Pointer,
70  class TContainerType = std::vector<TPointerType> >
71 class PointerVector final
72 {
73 public:
76 
79 
81  typedef TDataType data_type;
82  typedef TDataType value_type;
83  typedef TPointerType pointer;
84  typedef const TPointerType const_pointer;
85  typedef TDataType& reference;
86  typedef const TDataType& const_reference;
87  typedef TContainerType ContainerType;
88 
89  typedef boost::indirect_iterator<typename TContainerType::iterator> iterator;
90  typedef boost::indirect_iterator<typename TContainerType::const_iterator> const_iterator;
91  typedef boost::indirect_iterator<typename TContainerType::reverse_iterator> reverse_iterator;
92  typedef boost::indirect_iterator<typename TContainerType::const_reverse_iterator> const_reverse_iterator;
93 
94  typedef typename TContainerType::size_type size_type;
95  typedef typename TContainerType::iterator ptr_iterator;
96  typedef typename TContainerType::const_iterator ptr_const_iterator;
97  typedef typename TContainerType::reverse_iterator ptr_reverse_iterator;
98  typedef typename TContainerType::const_reverse_iterator ptr_const_reverse_iterator;
99  typedef typename TContainerType::difference_type difference_type;
100 
104 
106  PointerVector() : mData() {}
107 
108  template <class TInputIteratorType>
109  PointerVector(TInputIteratorType First, TInputIteratorType Last)
110  : mData(First, Last)
111  {
112  }
113 
114  PointerVector(const PointerVector& rOther) : mData(rOther.mData) {}
115 
116  explicit PointerVector(const TContainerType& rContainer) : mData(rContainer)
117  {
118  }
119 
120  explicit PointerVector(std::size_t NewSize) : mData(NewSize)
121  {
122  }
123 
126 
130 
132  {
133  mData = rOther.mData;
134  return *this;
135  }
136 
137  TDataType& operator[](const size_type& i)
138  {
139  return *(mData[i]);
140  }
141 
142  TDataType const& operator[](const size_type& i) const
143  {
144  return *(mData[i]);
145  }
146 
148  {
149  return mData[i];
150  }
151 
153  {
154  return mData[i];
155  }
156 
157  bool operator==( const PointerVector& r ) const // nothrow
158  {
159  if( size() != r.size() )
160  return false;
161  else
162  return std::equal(mData.begin(), mData.end(), r.mData.begin(), this->EqualKeyTo());
163  }
164 
168 
170  {
171  return iterator( mData.begin() );
172  }
174  {
175  return const_iterator( mData.begin() );
176  }
178  {
179  return iterator( mData.end() );
180  }
182  {
183  return const_iterator( mData.end() );
184  }
186  {
187  return reverse_iterator( mData.rbegin() );
188  }
190  {
191  return const_reverse_iterator( mData.rbegin() );
192  }
194  {
195  return reverse_iterator( mData.rend() );
196  }
198  {
199  return const_reverse_iterator( mData.rend() );
200  }
202  {
203  return mData.begin();
204  }
206  {
207  return mData.begin();
208  }
210  {
211  return mData.end();
212  }
214  {
215  return mData.end();
216  }
218  {
219  return mData.rbegin();
220  }
222  {
223  return mData.rbegin();
224  }
226  {
227  return mData.rend();
228  }
230  {
231  return mData.rend();
232  }
233 
234  reference front() /* nothrow */
235  {
236  assert( !empty() );
237  return *(mData.front());
238  }
239  const_reference front() const /* nothrow */
240  {
241  assert( !empty() );
242  return *(mData.front());
243  }
244  reference back() /* nothrow */
245  {
246  assert( !empty() );
247  return *(mData.back());
248  }
249  const_reference back() const /* nothrow */
250  {
251  assert( !empty() );
252  return *(mData.back());
253  }
254 
255  size_type size() const
256  {
257  return mData.size();
258  }
259 
261  {
262  return mData.max_size();
263  }
264 
265  void swap(PointerVector& rOther)
266  {
267  mData.swap(rOther.mData);
268  }
269 
270  void push_back(const TPointerType& x)
271  {
272  mData.push_back(x);
273  }
274 
275  void push_back(TPointerType&& rX)
276  {
277  mData.push_back(std::move(rX));
278  }
279 
280  template<class... Args>
281  void emplace_back(Args&&... args)
282  {
283  mData.emplace_back(std::forward<Args>(args)...);
284  }
285 
286  iterator insert(iterator Position, const TPointerType pData)
287  {
288  return iterator(mData.insert(Position, pData));
289  }
290 
291  template <class InputIterator>
292  void insert(InputIterator First, InputIterator Last)
293  {
294  for(; First != Last; ++First)
295  insert(*First);
296  }
297 
298 
300  {
301  return iterator(mData.erase(pos.base()));
302  }
303 
305  {
306  return iterator( mData.erase( first.base(), last.base() ) );
307  }
308 
309  void clear()
310  {
311  mData.clear();
312  }
313 
315  {
316  mData.resize(dim);
317  }
318 
320  {
321  mData.reserve(dim);
322  }
323 
324  int capacity()
325  {
326  return mData.capacity();
327  }
328 
332 
334  TContainerType& GetContainer()
335  {
336  return mData;
337  }
338 
340  const TContainerType& GetContainer() const
341  {
342  return mData;
343  }
344 
348 
349  bool empty() const
350  {
351  return mData.empty();
352  }
353 
357 
359  std::string Info() const
360  {
361  std::stringstream buffer;
362  buffer << "PointerVector (size = " << size() << ") : ";
363 
364  return buffer.str();
365  }
366 
368  void PrintInfo(std::ostream& rOStream) const
369  {
370  rOStream << Info();
371  }
372 
374  void PrintData(std::ostream& rOStream) const
375  {
376  std::copy(begin(), end(), std::ostream_iterator<TDataType>(rOStream, "\n "));
377  }
378 
380 
381 protected:
384 
385 
389 
390 
394 
395 
399 
400 
404 
405 
409 
410 
414 
415 
417 
418 private:
419 
422 
423 
427 
428  TContainerType mData;
429 
433 
434 
438 
442 
443  friend class Serializer;
444 
445  virtual void save(Serializer& rSerializer) const
446  {
447  size_type local_size = mData.size();
448 
449  rSerializer.save("size", local_size);
450 
451  for(size_type i = 0 ; i < local_size ; i++)
452  rSerializer.save("E", mData[i]);
453  }
454 
455  virtual void load(Serializer& rSerializer)
456  {
458 
459  rSerializer.load("size", local_size);
460 
461  mData.resize(local_size);
462 
463  for(size_type i = 0 ; i < local_size ; i++)
464  rSerializer.load("E", mData[i]);
465  }
466 
467 
471 
472 
476 
477 
481 
482 
484 
485 }; // Class PointerVector
486 
488 
491 
492 
496 
497 
499 template<class TDataType,
500  class TPointerType,
501  class TContainerType>
502 inline std::istream& operator >> (std::istream& rIStream,
504 
506 template<class TDataType,
507  class TPointerType,
508  class TContainerType>
509 inline std::ostream& operator << (std::ostream& rOStream,
511 {
512  rThis.PrintInfo(rOStream);
513  rOStream << std::endl;
514  rThis.PrintData(rOStream);
515 
516  return rOStream;
517 }
519 
520 
521 } // namespace Kratos.
522 
523 #endif // KRATOS_POINTER_VECTOR_SET_H_INCLUDED defined
PointerVector is a container like stl vector but using a vector to store pointers to its data.
Definition: pointer_vector.h:72
TDataType & operator[](const size_type &i)
Definition: pointer_vector.h:137
void clear()
Definition: pointer_vector.h:309
const TPointerType const_pointer
Definition: pointer_vector.h:84
std::string Info() const
Turn back information as a string.
Definition: pointer_vector.h:359
KRATOS_CLASS_POINTER_DEFINITION(PointerVector)
Pointer definition of PointerVector.
const_reverse_iterator rbegin() const
Definition: pointer_vector.h:189
boost::indirect_iterator< typename TContainerType::const_reverse_iterator > const_reverse_iterator
Definition: pointer_vector.h:92
TContainerType::const_iterator ptr_const_iterator
Definition: pointer_vector.h:96
void swap(PointerVector &rOther)
Definition: pointer_vector.h:265
TContainerType ContainerType
Definition: pointer_vector.h:87
size_type size() const
Definition: pointer_vector.h:255
const_reference back() const
Definition: pointer_vector.h:249
int capacity()
Definition: pointer_vector.h:324
const_iterator end() const
Definition: pointer_vector.h:181
const_reference front() const
Definition: pointer_vector.h:239
ptr_const_reverse_iterator ptr_rend() const
Definition: pointer_vector.h:229
TDataType const & operator[](const size_type &i) const
Definition: pointer_vector.h:142
reverse_iterator rbegin()
Definition: pointer_vector.h:185
iterator erase(iterator pos)
Definition: pointer_vector.h:299
boost::indirect_iterator< typename TContainerType::reverse_iterator > reverse_iterator
Definition: pointer_vector.h:91
TContainerType::size_type size_type
Definition: pointer_vector.h:94
ptr_iterator ptr_end()
Definition: pointer_vector.h:209
TDataType value_type
Definition: pointer_vector.h:82
PointerVector(TInputIteratorType First, TInputIteratorType Last)
Definition: pointer_vector.h:109
ptr_const_iterator ptr_begin() const
Definition: pointer_vector.h:205
PointerVector(const PointerVector &rOther)
Definition: pointer_vector.h:114
iterator end()
Definition: pointer_vector.h:177
size_type max_size() const
Definition: pointer_vector.h:260
void reserve(size_type dim)
Definition: pointer_vector.h:319
ptr_reverse_iterator ptr_rbegin()
Definition: pointer_vector.h:217
ptr_const_reverse_iterator ptr_rbegin() const
Definition: pointer_vector.h:221
boost::indirect_iterator< typename TContainerType::const_iterator > const_iterator
Definition: pointer_vector.h:90
TPointerType pointer
Definition: pointer_vector.h:83
reference front()
Definition: pointer_vector.h:234
PointerVector & operator=(const PointerVector &rOther)
Definition: pointer_vector.h:131
const_iterator begin() const
Definition: pointer_vector.h:173
boost::indirect_iterator< typename TContainerType::iterator > iterator
Definition: pointer_vector.h:89
reference back()
Definition: pointer_vector.h:244
TContainerType::const_reverse_iterator ptr_const_reverse_iterator
Definition: pointer_vector.h:98
const_reverse_iterator rend() const
Definition: pointer_vector.h:197
TContainerType::reverse_iterator ptr_reverse_iterator
Definition: pointer_vector.h:97
const TDataType & const_reference
Definition: pointer_vector.h:86
iterator erase(iterator first, iterator last)
Definition: pointer_vector.h:304
void push_back(TPointerType &&rX)
Definition: pointer_vector.h:275
ptr_const_iterator ptr_end() const
Definition: pointer_vector.h:213
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: pointer_vector.h:368
TContainerType::iterator ptr_iterator
Definition: pointer_vector.h:95
~PointerVector()
Destructor.
Definition: pointer_vector.h:125
TDataType data_type
data type stores in this container.
Definition: pointer_vector.h:81
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: pointer_vector.h:374
PointerVector(const TContainerType &rContainer)
Definition: pointer_vector.h:116
PointerVector()
Default constructor.
Definition: pointer_vector.h:106
void insert(InputIterator First, InputIterator Last)
Definition: pointer_vector.h:292
void emplace_back(Args &&... args)
Definition: pointer_vector.h:281
pointer & operator()(const size_type &i)
Definition: pointer_vector.h:147
TContainerType::difference_type difference_type
Definition: pointer_vector.h:99
const_pointer & operator()(const size_type &i) const
Definition: pointer_vector.h:152
ptr_reverse_iterator ptr_rend()
Definition: pointer_vector.h:225
iterator begin()
Definition: pointer_vector.h:169
bool operator==(const PointerVector &r) const
Definition: pointer_vector.h:157
PointerVector(std::size_t NewSize)
Definition: pointer_vector.h:120
void push_back(const TPointerType &x)
Definition: pointer_vector.h:270
TContainerType & GetContainer()
Definition: pointer_vector.h:334
bool empty() const
Definition: pointer_vector.h:349
const TContainerType & GetContainer() const
Definition: pointer_vector.h:340
iterator insert(iterator Position, const TPointerType pData)
Definition: pointer_vector.h:286
TDataType & reference
Definition: pointer_vector.h:85
reverse_iterator rend()
Definition: pointer_vector.h:193
ptr_iterator ptr_begin()
Definition: pointer_vector.h:201
void resize(size_type dim)
Definition: pointer_vector.h:314
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
void load(std::string const &rTag, TDataType &rObject)
Definition: serializer.h:207
void save(std::string const &rTag, std::array< TDataType, TDataSize > const &rObject)
Definition: serializer.h:545
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
args
Definition: generate_gid_list_file.py:37
int local_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:17
def load(f)
Definition: ode_solve.py:307
tuple const
Definition: ode_solve.py:403
x
Definition: sensitivityMatrix.py:49
int dim
Definition: sensitivityMatrix.py:25
integer i
Definition: TensorModule.f:17