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_map.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_POINTER_VECTOR_MAP_H_INCLUDED )
16 #define KRATOS_POINTER_VECTOR_MAP_H_INCLUDED
17 
18 
19 
20 // System includes
21 #include <vector>
22 #include <functional>
23 #include <string>
24 #include <iostream>
25 #include <sstream>
26 #include <cstddef>
27 
28 // External includes
29 
30 
31 // Project includes
32 #include "includes/define.h"
34 
35 
36 namespace Kratos
37 {
40 
44 
48 
52 
56 
58 
65 template<class TKeyType,class TDataType,
66 
67  class TCompareType = std::less<TKeyType>,
68  class TPointerType = Kratos::shared_ptr<TDataType>,
69  class TContainerType = std::vector<std::pair<TKeyType, TPointerType> > >
70 class PointerVectorMap final
71 {
72 public:
75 
78 
80  typedef TKeyType key_type;
81 
83  typedef TDataType data_type;
84  typedef std::pair<TKeyType, TPointerType> value_type;
85  typedef TCompareType key_compare;
86  typedef TPointerType pointer;
87  typedef TDataType& reference;
88  typedef const TDataType& const_reference;
89  typedef TContainerType ContainerType;
90 
95 
96  typedef typename TContainerType::size_type size_type;
97  typedef typename TContainerType::iterator pair_iterator;
98  typedef typename TContainerType::const_iterator pair_const_iterator;
99  typedef typename TContainerType::reverse_iterator pair_reverse_iterator;
100  typedef typename TContainerType::const_reverse_iterator pair_const_reverse_iterator;
101 
105 
107  PointerVectorMap() : mData(), mSortedPartSize(size_type()), mMaxBufferSize(100) {}
108 
109  PointerVectorMap(const PointerVectorMap& rOther) : mData(rOther.mData), mSortedPartSize(rOther.mSortedPartSize), mMaxBufferSize(rOther.mMaxBufferSize) {}
110 
111  explicit PointerVectorMap(const TContainerType& rContainer) : mData(rContainer), mSortedPartSize(size_type()), mMaxBufferSize(100)
112  {
113  Sort();
114  std::unique(mData.begin(), mData.end(), EqualKeyTo());
115  }
116 
119 
120 
124 
126  {
127  mData = rOther.mData;
128  mSortedPartSize = rOther.mSortedPartSize;
129 
130  return *this;
131  }
132 
133  TDataType& operator[](const key_type& Key)
134  {
135  pair_iterator sorted_part_end;
136 
137  if(mData.size() - mSortedPartSize >= mMaxBufferSize)
138  {
139  Sort();
140  sorted_part_end = mData.end();
141  }
142  else
143  sorted_part_end = mData.begin() + mSortedPartSize;
144 
145  pair_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
146  if (i == sorted_part_end)
147  {
148  mSortedPartSize++;
149  return *(mData.insert(sorted_part_end, value_type(Key, TPointerType(new TDataType)))->second);
150  }
151 
152  if (Key != i->first)
153  if((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end())
154  {
155  mData.push_back(value_type(Key, TPointerType(new TDataType)));
156  return *((--mData.end())->second);
157  }
158 
159  return *(i->second);
160  }
161 
163  {
164  pair_iterator sorted_part_end;
165 
166  if(mData.size() - mSortedPartSize >= mMaxBufferSize)
167  {
168  Sort();
169  sorted_part_end = mData.end();
170  }
171  else
172  sorted_part_end = mData.begin() + mSortedPartSize;
173 
174  pair_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
175  if (i == sorted_part_end)
176  {
177  mSortedPartSize++;
178  return (mData.insert(sorted_part_end, value_type(Key, TPointerType(new TDataType)))->second);
179  }
180 
181  if (Key != i->first)
182  if((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end())
183  {
184  mData.push_back(value_type(Key, TPointerType(new TDataType)));
185  return ((--mData.end())->second);
186  }
187 
188  return (i->second);
189  }
190 
191 
195 
197  {
198  return iterator( mData.begin() );
199  }
201  {
202  return const_iterator( mData.begin() );
203  }
205  {
206  return iterator( mData.end() );
207  }
209  {
210  return const_iterator( mData.end() );
211  }
213  {
214  return reverse_iterator( mData.rbegin() );
215  }
217  {
218  return const_reverse_iterator( mData.rbegin() );
219  }
221  {
222  return reverse_iterator( mData.rend() );
223  }
225  {
226  return const_reverse_iterator( mData.rend() );
227  }
229  {
230  return mData.begin();
231  }
233  {
234  return mData.begin();
235  }
237  {
238  return mData.end();
239  }
241  {
242  return mData.end();
243  }
245  {
246  return mData.rbegin();
247  }
249  {
250  return mData.rbegin();
251  }
253  {
254  return mData.rend();
255  }
257  {
258  return mData.rend();
259  }
260 
261  reference front() /* nothrow */
262  {
263  assert( !empty() );
264  return *(mData.front()->second);
265  }
266  const_reference front() const /* nothrow */
267  {
268  assert( !empty() );
269  return *(mData.front()->second);
270  }
271  reference back() /* nothrow */
272  {
273  assert( !empty() );
274  return *(mData.back()->second);
275  }
276  const_reference back() const /* nothrow */
277  {
278  assert( !empty() );
279  return *(mData.back()->second);
280  }
281 
282  size_type size() const
283  {
284  return mData.size();
285  }
286 
288  {
289  return mData.max_size();
290  }
291 
293  {
294  return TCompareType();
295  }
296 
297  void swap(PointerVectorMap& rOther)
298  {
299  mData.swap(rOther.mData);
300  }
301 
303  {
304  mData.push_back(x);
305  }
306 
307  iterator insert(key_type const& Key, const TDataType& rData)
308  {
309  pair_iterator sorted_part_end;
310 
311  if(mData.size() - mSortedPartSize >= mMaxBufferSize)
312  {
313  Sort();
314  sorted_part_end = mData.end();
315  }
316  else
317  sorted_part_end = mData.begin() + mSortedPartSize;
318 
319  pair_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
320  if (i == sorted_part_end)
321  {
322  mSortedPartSize++;
323  return (mData.insert(sorted_part_end, value_type(Key, TPointerType(new TDataType(rData)))));
324  }
325 
326  if (Key != i->first)
327  if((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end())
328  {
329  mData.push_back(value_type(Key, TPointerType(new TDataType)));
330  return iterator(--mData.end());
331  }
332  *(i->second) = rData;
333  return i;
334  }
335 
336  iterator insert(key_type const& Key, const TPointerType pData)
337  {
338  pair_iterator sorted_part_end;
339 
340  if(mData.size() - mSortedPartSize >= mMaxBufferSize)
341  {
342  Sort();
343  sorted_part_end = mData.end();
344  }
345  else
346  sorted_part_end = mData.begin() + mSortedPartSize;
347 
348  pair_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
349  if (i == sorted_part_end)
350  {
351  mSortedPartSize++;
352  return mData.insert(sorted_part_end, value_type(Key, pData));
353  }
354 
355  if (Key != i->first)
356  if((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end())
357  {
358  mData.push_back(value_type(Key, pData));
359  return iterator(--mData.end());
360  }
361 
362  *(i->second) = *pData;
363  return i;
364  }
365 
367  {
368  return iterator(mData.erase(pos.base()));
369  }
370 
372  {
373  return iterator( mData.erase( first.base(), last.base() ) );
374  }
375 
377  {
378  return erase(find(k));
379  }
380 
381  void clear()
382  {
383  mData.clear();
384  }
385 
386  iterator find(const key_type& Key)
387  {
388  pair_iterator sorted_part_end;
389 
390  if(mData.size() - mSortedPartSize >= mMaxBufferSize)
391  {
392  Sort();
393  sorted_part_end = mData.end();
394  }
395  else
396  sorted_part_end = mData.begin() + mSortedPartSize;
397 
398  pair_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
399  if (i == sorted_part_end || (Key != i->first))
400  if((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end())
401  return mData.end();
402 
403  return i;
404  }
405 
406  const_iterator find(const key_type& Key) const
407  {
408  pair_const_iterator sorted_part_end(mData.begin() + mSortedPartSize);
409 
410  pair_const_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
411  if (i == sorted_part_end || (Key != i->first))
412  if((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end())
413  return mData.end();
414 
415  return const_iterator(i);
416  }
417 
419  {
420  return find(Key) == mData.end() ? 0 : 1;
421  }
422 
423 
424  TDataType& at(const key_type& Key)
425  {
426  pair_iterator sorted_part_end;
427 
428  if(mData.size() - mSortedPartSize >= mMaxBufferSize)
429  {
430  Sort();
431  sorted_part_end = mData.end();
432  }
433  else
434  sorted_part_end = mData.begin() + mSortedPartSize;
435 
436  pair_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
437  if (i == sorted_part_end){
438  if((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end())
439  {
440  KRATOS_ERROR << Key << " not found in this map" << std::endl;
441  }
442  }
443 
444  return *(i->second);
445  }
446 
447  TDataType& at(const key_type& Key) const
448  {
449  pair_iterator sorted_part_end;
450  sorted_part_end = mData.begin() + mSortedPartSize;
451 
452  pair_iterator i(std::lower_bound(mData.begin(), sorted_part_end, Key, CompareKey()));
453  if (i == sorted_part_end){
454  if((i = std::find_if(sorted_part_end, mData.end(), EqualKeyTo(Key))) == mData.end())
455  {
456  KRATOS_ERROR << Key << " not found in this map" << std::endl;
457  }
458  }
459 
460  return *(i->second);
461  }
462 
463 
464  void Sort()
465  {
466  std::sort(mData.begin(), mData.end(), CompareKey());
467  mSortedPartSize = mData.size();
468  }
469 
473 
475  TContainerType& GetContainer()
476  {
477  return mData;
478  }
479 
481  const TContainerType& GetContainer() const
482  {
483  return mData;
484  }
485 
490  {
491  return mMaxBufferSize;
492  }
493 
499  void SetMaxBufferSize(const size_type NewSize)
500  {
501  mMaxBufferSize = NewSize;
502  }
503 
508  {
509  return mSortedPartSize;
510  }
511 
516  void SetSortedPartSize(const size_type NewSize)
517  {
518  mSortedPartSize = NewSize;
519  }
520 
524 
525  bool empty() const
526  {
527  return mData.empty();
528  }
529 
530  bool IsSorted() const
531  {
532  return (mSortedPartSize == mData.size());
533  }
534 
538 
540  std::string Info() const
541  {
542  std::stringstream buffer;
543  buffer << "Pointer vector map (size = " << size() << ") : ";
544 
545  return buffer.str();
546  }
547 
549  void PrintInfo(std::ostream& rOStream) const
550  {
551  rOStream << Info();
552  }
553 
555  void PrintData(std::ostream& rOStream) const
556  {
557  for(typename TContainerType::const_iterator i = mData.begin() ; i != mData.end() ; i++)
558  rOStream << "(" << i->first << " , " << *(i->second) << ")" << std::endl;
559  }
560 
561 
565 
566 
568 
569 protected:
572 
573 
577 
578 
582 
583 
587 
588 
592 
593 
597 
598 
602 
603 
605 
606 private:
607  class CompareKey
608  {
609  public:
610  bool operator()(value_type const& a, key_type b) const
611  {
612  return TCompareType()(a.first, b);
613  }
614  bool operator()(key_type a, value_type const& b) const
615  {
616  return TCompareType()(a, b.first);
617  }
618  bool operator()(value_type const& a, value_type const& b) const
619  {
620  return TCompareType()(a.first, b.first);
621  }
622  };
623 // class CompareValue : public std::binary_function<value_type&, value_type&, bool>{
624 // public:
625 // bool operator()(value_type& a, value_type& b) const
626 // {return TCompareType()(a.first, b.first);}
627 // };
628  class EqualKeyTo
629  {
630  key_type mKey;
631  public:
632  explicit EqualKeyTo(key_type k) : mKey(k) {}
633  bool operator()(value_type const& a, value_type const& b) const
634  {
635  return a.first == b.first;
636  }
637  bool operator()(value_type const& a) const
638  {
639  return a.first == mKey;
640  }
641  };
642 
645 
646 
650 
651  TContainerType mData;
652  size_type mSortedPartSize;
653  size_type mMaxBufferSize;
654 
658 
659 
663 
667 
668  friend class Serializer;
669 
670  virtual void save(Serializer& rSerializer) const
671  {
672  size_type local_size = mData.size();
673 
674  rSerializer.save("size", local_size);
675 
676  for(size_type i = 0 ; i < local_size ; i++){
677  rSerializer.save("Key", mData[i].first);
678  rSerializer.save("Data", mData[i].second);
679  }
680 
681  rSerializer.save("Sorted Part Size",mSortedPartSize);
682  rSerializer.save("Max Buffer Size",mMaxBufferSize);
683  }
684 
685  virtual void load(Serializer& rSerializer)
686  {
688 
689  rSerializer.load("size", local_size);
690 
691  mData.resize(local_size);
692 
693  for(size_type i = 0 ; i < local_size ; i++){
694  rSerializer.load("Key", mData[i].first);
695  rSerializer.load("Data", mData[i].second);
696  }
697 
698  rSerializer.load("Sorted Part Size",mSortedPartSize);
699  rSerializer.load("Max Buffer Size",mMaxBufferSize);
700  }
701 
702 
703 
707 
708 
712 
713 
717 
718 
720 
721 }; // Class PointerVectorMap
722 
724 
727 
728 
732 
733 
735 template<class TDataType,
736  class TGetKeyType,
737  class TCompareType,
738  class TPointerType,
739  class TContainerType>
740 inline std::istream& operator >> (std::istream& rIStream,
742 
744 template<class TDataType,
745  class TGetKeyType,
746  class TCompareType,
747  class TPointerType,
748  class TContainerType>
749 inline std::ostream& operator << (std::ostream& rOStream,
751 {
752  rThis.PrintInfo(rOStream);
753  rOStream << std::endl;
754  rThis.PrintData(rOStream);
755 
756  return rOStream;
757 }
759 
760 
761 } // namespace Kratos.
762 
763 #endif // KRATOS_POINTER_VECTOR_MAP_H_INCLUDED defined
PointerVectorMap is a sorted associative container like stl map but using a vector to store pointers ...
Definition: pointer_vector_map.h:71
reverse_iterator rbegin()
Definition: pointer_vector_map.h:212
~PointerVectorMap()
Destructor.
Definition: pointer_vector_map.h:118
TContainerType & GetContainer()
Definition: pointer_vector_map.h:475
const_iterator end() const
Definition: pointer_vector_map.h:208
void swap(PointerVectorMap &rOther)
Definition: pointer_vector_map.h:297
TContainerType::size_type size_type
Definition: pointer_vector_map.h:96
void Sort()
Definition: pointer_vector_map.h:464
bool IsSorted() const
Definition: pointer_vector_map.h:530
size_type count(const key_type &Key)
Definition: pointer_vector_map.h:418
iterator erase(const key_type &k)
Definition: pointer_vector_map.h:376
PointerVectorMapIterator< typename TContainerType::const_iterator, TDataType > const_iterator
Definition: pointer_vector_map.h:92
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: pointer_vector_map.h:549
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: pointer_vector_map.h:555
TDataType & reference
Definition: pointer_vector_map.h:87
TDataType & at(const key_type &Key) const
Definition: pointer_vector_map.h:447
const TContainerType & GetContainer() const
Definition: pointer_vector_map.h:481
iterator erase(iterator first, iterator last)
Definition: pointer_vector_map.h:371
TContainerType::const_reverse_iterator pair_const_reverse_iterator
Definition: pointer_vector_map.h:100
TDataType & at(const key_type &Key)
Definition: pointer_vector_map.h:424
iterator erase(iterator pos)
Definition: pointer_vector_map.h:366
pair_iterator pair_end()
Definition: pointer_vector_map.h:236
pair_reverse_iterator pair_rbegin()
Definition: pointer_vector_map.h:244
reference back()
Definition: pointer_vector_map.h:271
PointerVectorMapIterator< typename TContainerType::iterator, TDataType > iterator
Definition: pointer_vector_map.h:91
const TDataType & const_reference
Definition: pointer_vector_map.h:88
iterator insert(key_type const &Key, const TPointerType pData)
Definition: pointer_vector_map.h:336
TDataType data_type
data type stores in this container.
Definition: pointer_vector_map.h:83
TContainerType::const_iterator pair_const_iterator
Definition: pointer_vector_map.h:98
pair_const_iterator pair_end() const
Definition: pointer_vector_map.h:240
bool empty() const
Definition: pointer_vector_map.h:525
pair_const_reverse_iterator pair_rbegin() const
Definition: pointer_vector_map.h:248
TCompareType key_compare
Definition: pointer_vector_map.h:85
PointerVectorMap & operator=(const PointerVectorMap &rOther)
Definition: pointer_vector_map.h:125
size_type size() const
Definition: pointer_vector_map.h:282
size_type GetSortedPartSize() const
Get the sorted part size of buffer used in the container.
Definition: pointer_vector_map.h:507
PointerVectorMap()
Default constructor.
Definition: pointer_vector_map.h:107
TContainerType ContainerType
Definition: pointer_vector_map.h:89
TPointerType pointer
Definition: pointer_vector_map.h:86
size_type max_size() const
Definition: pointer_vector_map.h:287
void clear()
Definition: pointer_vector_map.h:381
std::pair< TKeyType, TPointerType > value_type
Definition: pointer_vector_map.h:84
key_compare key_comp() const
Definition: pointer_vector_map.h:292
std::string Info() const
Turn back information as a string.
Definition: pointer_vector_map.h:540
void push_back(value_type x)
Definition: pointer_vector_map.h:302
TKeyType key_type
Key type for searching in this container.
Definition: pointer_vector_map.h:80
KRATOS_CLASS_POINTER_DEFINITION(PointerVectorMap)
Pointer definition of PointerVectorMap.
TContainerType::reverse_iterator pair_reverse_iterator
Definition: pointer_vector_map.h:99
pair_iterator pair_begin()
Definition: pointer_vector_map.h:228
iterator end()
Definition: pointer_vector_map.h:204
const_reverse_iterator rend() const
Definition: pointer_vector_map.h:224
iterator insert(key_type const &Key, const TDataType &rData)
Definition: pointer_vector_map.h:307
pair_const_iterator pair_begin() const
Definition: pointer_vector_map.h:232
reference front()
Definition: pointer_vector_map.h:261
const_reverse_iterator rbegin() const
Definition: pointer_vector_map.h:216
PointerVectorMapIterator< typename TContainerType::reverse_iterator, TDataType > reverse_iterator
Definition: pointer_vector_map.h:93
pair_const_reverse_iterator pair_rend() const
Definition: pointer_vector_map.h:256
PointerVectorMapIterator< typename TContainerType::const_reverse_iterator, TDataType > const_reverse_iterator
Definition: pointer_vector_map.h:94
size_type GetMaxBufferSize() const
Get the maximum size of buffer used in the container.
Definition: pointer_vector_map.h:489
TContainerType::iterator pair_iterator
Definition: pointer_vector_map.h:97
iterator begin()
Definition: pointer_vector_map.h:196
const_reference back() const
Definition: pointer_vector_map.h:276
friend class Serializer
Definition: pointer_vector_map.h:668
pointer operator()(const key_type &Key)
Definition: pointer_vector_map.h:162
reverse_iterator rend()
Definition: pointer_vector_map.h:220
const_reference front() const
Definition: pointer_vector_map.h:266
const_iterator begin() const
Definition: pointer_vector_map.h:200
PointerVectorMap(const TContainerType &rContainer)
Definition: pointer_vector_map.h:111
PointerVectorMap(const PointerVectorMap &rOther)
Definition: pointer_vector_map.h:109
TDataType & operator[](const key_type &Key)
Definition: pointer_vector_map.h:133
void SetSortedPartSize(const size_type NewSize)
Set the sorted part size of buffer used in the container.
Definition: pointer_vector_map.h:516
pair_reverse_iterator pair_rend()
Definition: pointer_vector_map.h:252
void SetMaxBufferSize(const size_type NewSize)
Set the maximum size of buffer used in the container.
Definition: pointer_vector_map.h:499
const_iterator find(const key_type &Key) const
Definition: pointer_vector_map.h:406
iterator find(const key_type &Key)
Definition: pointer_vector_map.h:386
Short class definition.
Definition: pointer_vector_map_iterator.h:63
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
#define KRATOS_ERROR
Definition: exception.h:161
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
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
a
Definition: generate_stokes_twofluid_element.py:77
int local_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:17
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
def load(f)
Definition: ode_solve.py:307
tuple const
Definition: ode_solve.py:403
int k
Definition: quadrature.py:595
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17