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.
global_pointers_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: Riccardo Rossi
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <vector>
17 #include <iostream>
18 
19 // External includes
20 #include <boost/iterator/indirect_iterator.hpp>
21 
22 // Project includes
23 #include "includes/define.h"
25 #include "includes/serializer.h"
26 
27 namespace Kratos
28 {
31 
34 
38 
42 
46 
50 
59 template< class TDataType >
61 {
62 public:
65 
68 
69  using TContainerType = std::vector<GlobalPointer<TDataType>>;
71  using data_type = TDataType;
74  using const_pointer = const TPointerType;
75  using reference = TDataType&;
76  using const_reference = const TDataType&;
78 
79  using iterator = boost::indirect_iterator<typename TContainerType::iterator>;
80  using const_iterator = boost::indirect_iterator<typename TContainerType::const_iterator>;
81  using reverse_iterator = boost::indirect_iterator<typename TContainerType::reverse_iterator>;
82  using const_reverse_iterator = boost::indirect_iterator<typename TContainerType::const_reverse_iterator>;
83 
84  using size_type = typename TContainerType::size_type;
85  using ptr_iterator = typename TContainerType::iterator;
86  using ptr_const_iterator = typename TContainerType::const_iterator;
87  using ptr_reverse_iterator = typename TContainerType::reverse_iterator;
88  using ptr_const_reverse_iterator = typename TContainerType::const_reverse_iterator;
89  using difference_type = typename TContainerType::difference_type;
90 
94 
96  GlobalPointersVector() : mData() {}
97 
98  GlobalPointersVector(const std::initializer_list<GlobalPointer<TDataType>>& l)
99  : mData(l)
100  {}
101 
104 
110  template <class TContainerType>
112  {
113  this->reserve(rContainer.size());
114  for (auto& r_item : rContainer) {
115  this->push_back(GlobalPointer<TDataType>(&r_item));
116  }
117  }
118 
122  void Sort()
123  {
124  std::sort(mData.begin(), mData.end(), GlobalPointerCompare<TDataType>());
125  }
126 
131  void Unique()
132  {
133  Sort();
134  auto end_it = mData.end();
135  auto new_end_it = std::unique(mData.begin(), mData.end(), GlobalPointerComparor<TDataType>());
136  this->erase(new_end_it, end_it);
137  }
138 
143  {
144  mData.shrink_to_fit();
145  }
146 
150 
157  {
158  mData = rOther.mData;
159  return *this;
160  }
161 
167  TDataType& operator[](const size_type& i)
168  {
169  return *(mData[i]);
170  }
171 
177  TDataType const& operator[](const size_type& i) const
178  {
179  return *(mData[i]);
180  }
181 
188  {
189  return mData[i];
190  }
191 
198  {
199  return mData[i];
200  }
201 
209  bool operator==(const GlobalPointersVector& r) const // nothrow
210  {
211  if (size() != r.size())
212  return false;
213  else
214  return std::equal(mData.begin(), mData.end(), r.mData.begin(), this->EqualKeyTo());
215  }
216 
220 
222  {
223  return iterator( mData.begin() );
224  }
226  {
227  return const_iterator( mData.begin() );
228  }
230  {
231  return iterator( mData.end() );
232  }
234  {
235  return const_iterator( mData.end() );
236  }
238  {
239  return reverse_iterator( mData.rbegin() );
240  }
242  {
243  return const_reverse_iterator( mData.rbegin() );
244  }
246  {
247  return reverse_iterator( mData.rend() );
248  }
250  {
251  return const_reverse_iterator( mData.rend() );
252  }
254  {
255  return mData.begin();
256  }
258  {
259  return mData.begin();
260  }
262  {
263  return mData.end();
264  }
266  {
267  return mData.end();
268  }
270  {
271  return mData.rbegin();
272  }
274  {
275  return mData.rbegin();
276  }
278  {
279  return mData.rend();
280  }
282  {
283  return mData.rend();
284  }
285 
286  reference front() /* nothrow */
287  {
288  assert( !mData.empty() );
289  return *(mData.front());
290  }
291  const_reference front() const /* nothrow */
292  {
293  assert( !mData.empty() );
294  return *(mData.front());
295  }
296  reference back() /* nothrow */
297  {
298  assert( !mData.empty() );
299  return *(mData.back());
300  }
301  const_reference back() const /* nothrow */
302  {
303  assert( !mData.empty() );
304  return *(mData.back());
305  }
306 
307  size_type size() const
308  {
309  return mData.size();
310  }
311 
313  {
314  return mData.max_size();
315  }
316 
318  {
319  mData.swap(rOther.mData);
320  }
321 
323  {
324  mData.push_back(x);
325  }
326 
327  // template<class TOtherDataType>
328  // void push_back(TOtherDataType const& x)
329  // {
330  // push_back(TPointerType(new TOtherDataType(x)));
331  // }
332 
333  // template<class TOtherDataType>
334  // iterator insert(iterator Position, const TOtherDataType& rData)
335  // {
336  // return iterator(mData.insert(Position, TPointerType(new TOtherDataType(rData))));
337  // }
338 
339  iterator insert(iterator Position, const TPointerType pData)
340  {
341  return iterator(mData.insert(Position, pData));
342  }
343 
344  template <class InputIterator>
345  void insert(InputIterator First, InputIterator Last)
346  {
347  for(; First != Last; ++First)
348  insert(*First);
349  }
350 
352  {
353  return iterator(mData.erase(pos.base()));
354  }
355 
357  {
358  return iterator( mData.erase( first.base(), last.base() ) );
359  }
360 
361  void clear()
362  {
363  mData.clear();
364  }
365 
366  void resize(size_type new_dim) const
367  {
368  return mData.resize(new_dim);
369  }
370  void resize(size_type new_dim)
371  {
372  mData.resize(new_dim);
373  }
374 
375  void reserve(int dim)
376  {
377  mData.reserve(dim);
378  }
379 
380  int capacity()
381  {
382  return mData.capacity();
383  }
384 
388 
392 
396 
398  std::string Info() const
399  {
400  std::stringstream buffer;
401  buffer << "GlobalPointersVector" ;
402  return buffer.str();
403  }
404 
406  void PrintInfo(std::ostream& rOStream) const
407  {
408  rOStream << "GlobalPointersVector";
409  }
410 
412  void PrintData(std::ostream& rOStream) const {}
413 
417 
420  {
421  return mData;
422  }
423 
426  {
427  return mData;
428  }
429 
433 
435 protected:
438 
442 
446 
450 
454 
458 
462 
464 private:
467 
471 
472  TContainerType mData;
473 
477 
478  friend class Serializer;
479 
485  void save(Serializer& rSerializer) const
486  {
487  rSerializer.save("Size", this->size());
488 
489  for (std::size_t i = 0; i < this->size(); i++) {
490  rSerializer.save("Data", mData[i]);
491  }
492  }
493 
499  void load(Serializer& rSerializer)
500  {
501  std::size_t size;
502 
503  rSerializer.load("Size", size);
504 
505  for (std::size_t i = 0; i < size; i++) {
506  GlobalPointer<TDataType> p(nullptr);
507  rSerializer.load("Data", p);
508  this->push_back(p);
509  }
510  }
511 
515 
519 
523 
527 
529 }; // Class GlobalPointersVector
530 
534 
538 
540 template< class TDataType >
541 inline std::istream& operator >> (std::istream& rIStream,
543 {
544  return rIStream;
545 }
546 
548 template< class TDataType >
549 inline std::ostream& operator << (std::ostream& rOStream,
550  const GlobalPointersVector<TDataType>& rThis)
551 {
552  rThis.PrintInfo(rOStream);
553  rOStream << std::endl;
554  rThis.PrintData(rOStream);
555 
556  return rOStream;
557 }
559 
561 
562 } // namespace Kratos.
This class is a wrapper for a pointer to a data that is located in a different rank.
Definition: global_pointer.h:44
This class is a vector which stores global pointers.
Definition: global_pointers_vector.h:61
boost::indirect_iterator< typename TContainerType::iterator > iterator
Definition: global_pointers_vector.h:79
ptr_iterator ptr_begin()
Definition: global_pointers_vector.h:253
reverse_iterator rbegin()
Definition: global_pointers_vector.h:237
TDataType & reference
Definition: global_pointers_vector.h:75
const_reverse_iterator rbegin() const
Definition: global_pointers_vector.h:241
reference front()
Definition: global_pointers_vector.h:286
ptr_reverse_iterator ptr_rend()
Definition: global_pointers_vector.h:277
size_type max_size() const
Definition: global_pointers_vector.h:312
~GlobalPointersVector()
Destructor.
Definition: global_pointers_vector.h:103
const_iterator end() const
Definition: global_pointers_vector.h:233
boost::indirect_iterator< typename TContainerType::const_iterator > const_iterator
Definition: global_pointers_vector.h:80
const_reference front() const
Definition: global_pointers_vector.h:291
TDataType & operator[](const size_type &i)
Access an element in the container by index.
Definition: global_pointers_vector.h:167
bool operator==(const GlobalPointersVector &r) const
Equality comparison operator to check if two GlobalPointersVector objects are equal.
Definition: global_pointers_vector.h:209
TDataType data_type
Definition: global_pointers_vector.h:71
void Sort()
Sort the elements in the container.
Definition: global_pointers_vector.h:122
iterator insert(iterator Position, const TPointerType pData)
Definition: global_pointers_vector.h:339
KRATOS_CLASS_POINTER_DEFINITION(GlobalPointersVector)
Pointer definition of GlobalPointersVector.
iterator erase(iterator pos)
Definition: global_pointers_vector.h:351
typename TContainerType::size_type size_type
Definition: global_pointers_vector.h:84
void swap(GlobalPointersVector &rOther)
Definition: global_pointers_vector.h:317
void push_back(TPointerType x)
Definition: global_pointers_vector.h:322
const TContainerType & GetContainer() const
Definition: global_pointers_vector.h:425
boost::indirect_iterator< typename TContainerType::const_reverse_iterator > const_reverse_iterator
Definition: global_pointers_vector.h:82
typename TContainerType::const_reverse_iterator ptr_const_reverse_iterator
Definition: global_pointers_vector.h:88
typename TContainerType::const_iterator ptr_const_iterator
Definition: global_pointers_vector.h:86
iterator begin()
Definition: global_pointers_vector.h:221
TDataType const & operator[](const size_type &i) const
Access a constant element in the container by index.
Definition: global_pointers_vector.h:177
typename TContainerType::difference_type difference_type
Definition: global_pointers_vector.h:89
ptr_const_iterator ptr_end() const
Definition: global_pointers_vector.h:265
TContainerType ContainerType
Definition: global_pointers_vector.h:77
void shrink_to_fit()
Reduce the capacity of the container to fit its size.
Definition: global_pointers_vector.h:142
GlobalPointersVector()
Default constructor.
Definition: global_pointers_vector.h:96
const_reference back() const
Definition: global_pointers_vector.h:301
ptr_const_reverse_iterator ptr_rbegin() const
Definition: global_pointers_vector.h:273
std::string Info() const
Turn back information as a string.
Definition: global_pointers_vector.h:398
void clear()
Definition: global_pointers_vector.h:361
pointer & operator()(const size_type &i)
Access an element in the container by index.
Definition: global_pointers_vector.h:187
int capacity()
Definition: global_pointers_vector.h:380
iterator erase(iterator first, iterator last)
Definition: global_pointers_vector.h:356
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: global_pointers_vector.h:406
ptr_reverse_iterator ptr_rbegin()
Definition: global_pointers_vector.h:269
void reserve(int dim)
Definition: global_pointers_vector.h:375
TContainerType & GetContainer()
Definition: global_pointers_vector.h:419
void FillFromContainer(TContainerType &rContainer)
Fill the container from another container.
Definition: global_pointers_vector.h:111
const_iterator begin() const
Definition: global_pointers_vector.h:225
size_type size() const
Definition: global_pointers_vector.h:307
void resize(size_type new_dim)
Definition: global_pointers_vector.h:370
GlobalPointersVector(const std::initializer_list< GlobalPointer< TDataType >> &l)
Definition: global_pointers_vector.h:98
typename TContainerType::iterator ptr_iterator
Definition: global_pointers_vector.h:85
ptr_const_iterator ptr_begin() const
Definition: global_pointers_vector.h:257
void insert(InputIterator First, InputIterator Last)
Definition: global_pointers_vector.h:345
ptr_const_reverse_iterator ptr_rend() const
Definition: global_pointers_vector.h:281
reverse_iterator rend()
Definition: global_pointers_vector.h:245
const_pointer & operator()(const size_type &i) const
Access a constant element in the container by index.
Definition: global_pointers_vector.h:197
typename TContainerType::reverse_iterator ptr_reverse_iterator
Definition: global_pointers_vector.h:87
void Unique()
Remove duplicate elements from the container.
Definition: global_pointers_vector.h:131
GlobalPointersVector & operator=(const GlobalPointersVector &rOther)
Assignment operator to copy the contents of another GlobalPointersVector.
Definition: global_pointers_vector.h:156
boost::indirect_iterator< typename TContainerType::reverse_iterator > reverse_iterator
Definition: global_pointers_vector.h:81
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: global_pointers_vector.h:412
const_reverse_iterator rend() const
Definition: global_pointers_vector.h:249
friend class Serializer
Definition: global_pointers_vector.h:478
GlobalPointer< TDataType > TPointerType
Definition: global_pointers_vector.h:70
void resize(size_type new_dim) const
Definition: global_pointers_vector.h:366
const TDataType & const_reference
Definition: global_pointers_vector.h:76
ptr_iterator ptr_end()
Definition: global_pointers_vector.h:261
iterator end()
Definition: global_pointers_vector.h:229
reference back()
Definition: global_pointers_vector.h:296
std::vector< GlobalPointer< TDataType > > TContainerType
Definition: global_pointers_vector.h:69
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
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
def load(f)
Definition: ode_solve.py:307
tuple const
Definition: ode_solve.py:403
p
Definition: sensitivityMatrix.py:52
x
Definition: sensitivityMatrix.py:49
int dim
Definition: sensitivityMatrix.py:25
integer i
Definition: TensorModule.f:17
integer l
Definition: TensorModule.f:17
This is a key compare between two pointers to the object object.
Definition: global_pointer.h:464
This is a key comparer between two dof pointers checking for equal keys.
Definition: global_pointer.h:441