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.
weak_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 #if !defined(KRATOS_WEAK_POINTER_VECTOR_H_INCLUDED )
16 #define KRATOS_WEAK_POINTER_VECTOR_H_INCLUDED
17 
18 // System includes
19 #include <functional>
20 #include <string>
21 #include <iostream>
22 #include <sstream>
23 #include <vector>
24 
25 // External includes
26 
27 
28 // Project includes
29 #include "includes/define.h"
31 
32 namespace Kratos
33 {
36 
40 
44 
48 
52 
54 
63 template<class TDataType,
64  class TPointerType = typename TDataType::WeakPointer,
65  class TContainerType = std::vector<TPointerType> >
66 class WeakPointerVector final
67 {
68 public:
71 
74 
76  typedef TDataType data_type;
77  typedef TPointerType value_type;
78  typedef TPointerType pointer;
79  typedef const TPointerType const_pointer;
80  typedef TDataType& reference;
81  typedef const TDataType& const_reference;
82  typedef TContainerType ContainerType;
83 
88 
89  typedef typename TContainerType::size_type size_type;
90  typedef typename TContainerType::iterator ptr_iterator;
91  typedef typename TContainerType::const_iterator ptr_const_iterator;
92  typedef typename TContainerType::reverse_iterator ptr_reverse_iterator;
93  typedef typename TContainerType::const_reverse_iterator ptr_const_reverse_iterator;
94 
98 
100  WeakPointerVector() : mData() {}
101 
102  template <class TInputIteratorType>
103  WeakPointerVector(TInputIteratorType First, TInputIteratorType Last)
104  : mData(First, Last)
105  {
106  }
107 
108  WeakPointerVector(const WeakPointerVector& rOther) : mData(rOther.mData) {}
109 
110  explicit WeakPointerVector(const TContainerType& rContainer) : mData(rContainer)
111  {
112  }
113 
116 
117 
121 
123  {
124  mData = rOther.mData;
125  return *this;
126  }
127 
128  TDataType& operator[](const size_type& i)
129  {
130  return *(mData[i].lock());
131  }
132 
133  TDataType const& operator[](const size_type& i) const
134  {
135  return *(mData[i].lock());
136  }
137 
139  {
140  return mData[i];
141  }
142 
144  {
145  return mData[i];
146  }
147 
148  bool operator==( const WeakPointerVector& r ) const // nothrow
149  {
150  if( size() != r.size() )
151  return false;
152  else
153  return std::equal(mData.begin(), mData.end(), r.mData.begin());
154  }
155 
159 
161  {
162  return iterator( mData.begin() );
163  }
165  {
166  return const_iterator( mData.begin() );
167  }
169  {
170  return iterator( mData.end() );
171  }
173  {
174  return const_iterator( mData.end() );
175  }
177  {
178  return reverse_iterator( mData.rbegin() );
179  }
181  {
182  return const_reverse_iterator( mData.rbegin() );
183  }
185  {
186  return reverse_iterator( mData.rend() );
187  }
189  {
190  return const_reverse_iterator( mData.rend() );
191  }
193  {
194  return mData.begin();
195  }
197  {
198  return mData.begin();
199  }
201  {
202  return mData.end();
203  }
205  {
206  return mData.end();
207  }
209  {
210  return mData.rbegin();
211  }
213  {
214  return mData.rbegin();
215  }
217  {
218  return mData.rend();
219  }
221  {
222  return mData.rend();
223  }
224 
225  reference front() /* nothrow */
226  {
227  assert( !empty() );
228  return *((mData.front()).lock());
229  }
230  const_reference front() const /* nothrow */
231  {
232  assert( !empty() );
233  return *((mData.front()).lock());
234  }
235  reference back() /* nothrow */
236  {
237  assert( !empty() );
238  return *((mData.back()).lock());
239  }
240  const_reference back() const /* nothrow */
241  {
242  assert( !empty() );
243  return *((mData.back()).lock());
244  }
245 
246  size_type size() const
247  {
248  return mData.size();
249  }
250  void resize(size_type new_dim) const
251  {
252  return mData.resize(new_dim);
253  }
254  void resize(size_type new_dim)
255  {
256  mData.resize(new_dim);
257  }
258 
260  {
261  return mData.max_size();
262  }
263 
264  void swap(WeakPointerVector& rOther)
265  {
266  mData.swap(rOther.mData);
267  }
268 
269  void push_back(TPointerType x)
270  {
271  mData.push_back(x);
272  }
273 
275  {
276  push_back(TPointerType(new TDataType(x)));
277  }
278 
279  iterator insert(iterator Position, const TDataType& rData)
280  {
281  return iterator(mData.insert(Position, TPointerType(new TDataType(rData))));
282  }
283 
284  iterator insert(iterator Position, const TPointerType pData)
285  {
286  return iterator(mData.insert(Position, pData));
287  }
288 
289  template <class InputIterator>
290  void insert(InputIterator First, InputIterator Last)
291  {
292  for(; First != Last; ++First)
293  insert(*First);
294  }
295 
296 
298  {
299  return iterator(mData.erase(pos.base()));
300  }
301 
303  {
304  return iterator( mData.erase( first.base(), last.base() ) );
305  }
306 
307 
308  void clear()
309  {
310  mData.clear();
311  }
312 
313  void reserve(int dim)
314  {
315  mData.reserve(dim);
316  }
317 
318  int capacity()
319  {
320  return mData.capacity();
321  }
322 
326 
328  TContainerType& GetContainer()
329  {
330  return mData;
331  }
332 
334  const TContainerType& GetContainer() const
335  {
336  return mData;
337  }
338 
339 
340 
344 
345  bool empty() const
346  {
347  return mData.empty();
348  }
349 
353 
355  std::string Info() const
356  {
357  std::stringstream buffer;
358  buffer << "WeakPointerVector (size = " << size() << ") : ";
359 
360  return buffer.str();
361  }
362 
364  void PrintInfo(std::ostream& rOStream) const
365  {
366  rOStream << Info();
367  }
368 
370  void PrintData(std::ostream& rOStream) const
371  {
372  std::copy(begin(), end(), std::ostream_iterator<TDataType>(rOStream, "\t "));
373  }
374 
375 
379 
380 
382 
383 protected:
386 
387 
391 
392 
396 
397 
401 
402 
406 
407 
411 
412 
416 
417 
419 
420 private:
421 
424 
425 
429 
430  TContainerType mData;
431 
435 
436 
440 
441 
445 
446 
450 
451 
455 
456 
458 
459 }; // Class WeakPointerVector
460 
462 
465 
466 
470 
471 
473 template<class TDataType,
474  class TPointerType,
475  class TContainerType>
476 inline std::istream& operator >> (std::istream& rIStream,
478 
480 template<class TDataType,
481  class TPointerType,
482  class TContainerType>
483 inline std::ostream& operator << (std::ostream& rOStream,
485 {
486  rThis.PrintInfo(rOStream);
487  rOStream << std::endl;
488  rThis.PrintData(rOStream);
489 
490  return rOStream;
491 }
493 
494 
495 } // namespace Kratos.
496 
497 #endif // KRATOS_WEAK_POINTER_VECTOR_H_INCLUDED defined
WeakPointerVector is a container like stl vector but using a vector to store pointers to its data.
Definition: weak_pointer_vector.h:67
const_reverse_iterator rend() const
Definition: weak_pointer_vector.h:188
void swap(WeakPointerVector &rOther)
Definition: weak_pointer_vector.h:264
TDataType & operator[](const size_type &i)
Definition: weak_pointer_vector.h:128
void insert(InputIterator First, InputIterator Last)
Definition: weak_pointer_vector.h:290
WeakPointerVectorIterator< typename TContainerType::const_iterator, TDataType > const_iterator
Definition: weak_pointer_vector.h:85
ptr_reverse_iterator ptr_rend()
Definition: weak_pointer_vector.h:216
const TPointerType const_pointer
Definition: weak_pointer_vector.h:79
iterator end()
Definition: weak_pointer_vector.h:168
void push_back(const_reference x)
Definition: weak_pointer_vector.h:274
reverse_iterator rbegin()
Definition: weak_pointer_vector.h:176
void clear()
Definition: weak_pointer_vector.h:308
reference back()
Definition: weak_pointer_vector.h:235
WeakPointerVector & operator=(const WeakPointerVector &rOther)
Definition: weak_pointer_vector.h:122
void push_back(TPointerType x)
Definition: weak_pointer_vector.h:269
size_type max_size() const
Definition: weak_pointer_vector.h:259
TDataType & reference
Definition: weak_pointer_vector.h:80
TDataType const & operator[](const size_type &i) const
Definition: weak_pointer_vector.h:133
iterator erase(iterator pos)
Definition: weak_pointer_vector.h:297
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: weak_pointer_vector.h:364
TContainerType::reverse_iterator ptr_reverse_iterator
Definition: weak_pointer_vector.h:92
void reserve(int dim)
Definition: weak_pointer_vector.h:313
WeakPointerVector(const TContainerType &rContainer)
Definition: weak_pointer_vector.h:110
WeakPointerVector(const WeakPointerVector &rOther)
Definition: weak_pointer_vector.h:108
reverse_iterator rend()
Definition: weak_pointer_vector.h:184
iterator begin()
Definition: weak_pointer_vector.h:160
const TDataType & const_reference
Definition: weak_pointer_vector.h:81
iterator insert(iterator Position, const TDataType &rData)
Definition: weak_pointer_vector.h:279
reference front()
Definition: weak_pointer_vector.h:225
iterator erase(iterator first, iterator last)
Definition: weak_pointer_vector.h:302
ptr_iterator ptr_end()
Definition: weak_pointer_vector.h:200
std::string Info() const
Turn back information as a string.
Definition: weak_pointer_vector.h:355
iterator insert(iterator Position, const TPointerType pData)
Definition: weak_pointer_vector.h:284
TContainerType ContainerType
Definition: weak_pointer_vector.h:82
const_iterator end() const
Definition: weak_pointer_vector.h:172
KRATOS_CLASS_POINTER_DEFINITION(WeakPointerVector)
Pointer definition of WeakPointerVector.
TDataType data_type
data type stores in this container.
Definition: weak_pointer_vector.h:76
~WeakPointerVector()
Destructor.
Definition: weak_pointer_vector.h:115
ptr_reverse_iterator ptr_rbegin()
Definition: weak_pointer_vector.h:208
const_reverse_iterator rbegin() const
Definition: weak_pointer_vector.h:180
ptr_const_iterator ptr_end() const
Definition: weak_pointer_vector.h:204
TContainerType::size_type size_type
Definition: weak_pointer_vector.h:89
ptr_const_reverse_iterator ptr_rbegin() const
Definition: weak_pointer_vector.h:212
WeakPointerVectorIterator< typename TContainerType::reverse_iterator, TDataType > reverse_iterator
Definition: weak_pointer_vector.h:86
TPointerType pointer
Definition: weak_pointer_vector.h:78
ptr_const_iterator ptr_begin() const
Definition: weak_pointer_vector.h:196
WeakPointerVectorIterator< typename TContainerType::const_reverse_iterator, TDataType > const_reverse_iterator
Definition: weak_pointer_vector.h:87
ptr_iterator ptr_begin()
Definition: weak_pointer_vector.h:192
TContainerType & GetContainer()
Definition: weak_pointer_vector.h:328
const_reference front() const
Definition: weak_pointer_vector.h:230
size_type size() const
Definition: weak_pointer_vector.h:246
ptr_const_reverse_iterator ptr_rend() const
Definition: weak_pointer_vector.h:220
WeakPointerVector(TInputIteratorType First, TInputIteratorType Last)
Definition: weak_pointer_vector.h:103
const_iterator begin() const
Definition: weak_pointer_vector.h:164
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: weak_pointer_vector.h:370
bool operator==(const WeakPointerVector &r) const
Definition: weak_pointer_vector.h:148
TContainerType::const_iterator ptr_const_iterator
Definition: weak_pointer_vector.h:91
int capacity()
Definition: weak_pointer_vector.h:318
const_reference back() const
Definition: weak_pointer_vector.h:240
WeakPointerVector()
Default constructor.
Definition: weak_pointer_vector.h:100
const TContainerType & GetContainer() const
Definition: weak_pointer_vector.h:334
pointer & operator()(const size_type &i)
Definition: weak_pointer_vector.h:138
TPointerType value_type
Definition: weak_pointer_vector.h:77
void resize(size_type new_dim) const
Definition: weak_pointer_vector.h:250
void resize(size_type new_dim)
Definition: weak_pointer_vector.h:254
bool empty() const
Definition: weak_pointer_vector.h:345
WeakPointerVectorIterator< typename TContainerType::iterator, TDataType > iterator
Definition: weak_pointer_vector.h:84
TContainerType::const_reverse_iterator ptr_const_reverse_iterator
Definition: weak_pointer_vector.h:93
const_pointer & operator()(const size_type &i) const
Definition: weak_pointer_vector.h:143
TContainerType::iterator ptr_iterator
Definition: weak_pointer_vector.h:90
Short class definition.
Definition: weak_pointer_vector_iterator.h:63
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
tuple const
Definition: ode_solve.py:403
x
Definition: sensitivityMatrix.py:49
int dim
Definition: sensitivityMatrix.py:25
integer i
Definition: TensorModule.f:17