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_hash_map_set.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 //
12 //
13 
14 
15 
16 #if !defined(KRATOS_POINTER_HASH_MAP_SET_H_INCLUDED )
17 #define KRATOS_POINTER_HASH_MAP_SET_H_INCLUDED
18 
19 
20 
21 // System includes
22 
23 
24 // External includes
25 #include <utility>
26 #include <type_traits>
27 #include <unordered_map>
28 
29 // Project includes
30 #include "includes/define.h"
31 #include "includes/serializer.h"
33 
34 namespace Kratos
35 {
38 
42 
46 
50 
54 
56 
65 template<class TDataType,
66  class THashType = std::hash<TDataType>,
67  class TGetKeyType = SetIdentityFunction<TDataType>,
68  class TPointerType = Kratos::shared_ptr<TDataType> >
69 class PointerHashMapSet final
70 {
71 
72 
73 public:
76 
79 
81  typedef decltype(std::declval<TGetKeyType>()(std::declval<TDataType>())) key_type;
82 
84  typedef TDataType data_type;
85  typedef TDataType value_type;
86  typedef TPointerType pointer_type;
87  typedef TDataType& reference;
88  typedef const TDataType& const_reference;
89  // typedef std::unordered_map<key_type, TPointerType, THashType> ContainerType;
90  typedef std::unordered_map<typename std::remove_reference<key_type>::type, TPointerType, THashType> ContainerType;
91 
92  typedef typename ContainerType::size_type size_type;
93  typedef typename ContainerType::iterator ptr_iterator;
96 
98 
99 private:
102  class iterator_adaptor
103  {
104  ptr_iterator map_iterator;
105  public:
106  using iterator_category = std::forward_iterator_tag;
107  using difference_type = std::ptrdiff_t;
108  using value_type = data_type;
109  using pointer = data_type*;
110  using reference = data_type&;
111 
112  iterator_adaptor(ptr_iterator it) :map_iterator(it) {}
113  iterator_adaptor(const iterator_adaptor& it) : map_iterator(it.map_iterator) {}
114  iterator_adaptor& operator++() { map_iterator++; return *this; }
115  iterator_adaptor operator++(int) { iterator_adaptor tmp(*this); operator++(); return tmp; }
116  bool operator==(const iterator_adaptor& rhs) const { return map_iterator == rhs.map_iterator; }
117  bool operator!=(const iterator_adaptor& rhs) const { return map_iterator != rhs.map_iterator; }
118  data_type& operator*() const { return *(map_iterator->second); }
119  pointer_type operator->() const { return map_iterator->second; }
120  ptr_iterator& base() { return map_iterator; }
121  ptr_iterator const& base() const { return map_iterator; }
122  };
123 
124  class const_iterator_adaptor
125  {
126  ptr_const_iterator map_iterator;
127  public:
128  using iterator_category = std::forward_iterator_tag;
129  using difference_type = std::ptrdiff_t;
130  using value_type = data_type;
131  using pointer = data_type*;
132  using reference = data_type&;
133 
134  const_iterator_adaptor(ptr_const_iterator it) :map_iterator(it) {}
135  const_iterator_adaptor(const const_iterator_adaptor& it) : map_iterator(it.map_iterator) {}
136  const_iterator_adaptor& operator++() { map_iterator++; return *this; }
137  const_iterator_adaptor operator++(int) { const_iterator_adaptor tmp(*this); operator++(); return tmp; }
138  bool operator==(const const_iterator_adaptor& rhs) const { return map_iterator == rhs.map_iterator; }
139  bool operator!=(const const_iterator_adaptor& rhs) const { return map_iterator != rhs.map_iterator; }
140  data_type const& operator*() const { return *(map_iterator->second); }
141  pointer_type operator->() const { return map_iterator->second; }
142  ptr_const_iterator& base() { return map_iterator; }
143  ptr_const_iterator const& base() const { return map_iterator; }
144  };
145 
147 
148 public:
151 
152 
153  typedef iterator_adaptor iterator;
154  typedef const_iterator_adaptor const_iterator;
155 
159 
161  PointerHashMapSet() : mData() {}
162 
163  template <class TInputIteratorType>
164 
165  PointerHashMapSet(TInputIteratorType First, TInputIteratorType Last, size_type NewMaxBufferSize = 1)
166  {
167  for(; First != Last; ++First)
168  insert(begin(), *First);
169  }
170 
172  : mData(rOther.mData) {}
173 
174  PointerHashMapSet(const ContainerType& rContainer) : mData(rContainer)
175  {
176  }
177 
180 
181 
185 
187  {
188  mData = rOther.mData;
189 
190  return *this;
191  }
192 
193  TDataType& operator[](const key_type& Key)
194  {
195  KRATOS_DEBUG_ERROR_IF(mData.find(Key) == mData.end()) << "The key: " << Key << " is not available in the map" << std::endl;
196  return *(mData.find(Key)->second);
197  }
198 
200  {
201  KRATOS_DEBUG_ERROR_IF(mData.find(Key) == mData.end()) << "The key: " << Key << " is not available in the map" << std::endl;
202  return mData.find(Key)->second;
203  }
204 
205  bool operator==( const PointerHashMapSet& r ) const // nothrow
206  {
207  return (mData == r.mData);
208  }
209 
210  bool operator!=(const PointerHashMapSet& r) const // nothrow
211  {
212  return (mData != r.mData);
213  }
214 
215 
219 
221  {
222  return iterator( mData.begin() );
223  }
225  {
226  return const_iterator( mData.begin() );
227  }
229  {
230  return iterator( mData.end() );
231  }
233  {
234  return const_iterator( mData.end() );
235  }
237  {
238  return mData.begin();
239  }
241  {
242  return mData.begin();
243  }
245  {
246  return mData.end();
247  }
249  {
250  return mData.end();
251  }
252 
253  reference front() /* nothrow */
254  {
255  //assert( !empty() );
256  return *(mData.front().second);
257  }
258  const_reference front() const /* nothrow */
259  {
260  //assert( !empty() );
261  return *(mData.front().second);
262  }
263  reference back() /* nothrow */
264  {
265  //assert( !empty() );
266  return *(mData.back().second);
267  }
268  const_reference back() const /* nothrow */
269  {
270  //assert( !empty() );
271  return *(mData.back().second);
272  }
273 
274  size_type size() const
275  {
276  return mData.size();
277  }
278 
279  //size_type max_size() const
280  //{
281  // return mData.max_size();
282  //}
283 
284  //key_compare key_comp() const
285  //{
286  // return TCompareType();
287  //}
288 
289  void swap(PointerHashMapSet& rOther)
290  {
291  mData.swap(rOther.mData);
292  }
293 
294  template<class TOtherDataType>
295  iterator insert(const TOtherDataType& rData)
296  {
297  TDataType* p_new_data = new TDataType(rData);
298  return mData.insert(ContainerType::value_type(TGetKeyOf(rData), p_new_data));
299  }
300 
301  iterator insert(TPointerType pData)
302  {
303  key_type key=KeyOf(*pData);
304  typename ContainerType::value_type item(key, pData);
305  std::pair<typename ContainerType::iterator, bool> result = mData.insert(item);
306  // TODO: I should enable this after adding the KRATOS_ERROR to define.h. Pooyan.
307  //if(result.second != true)
308  // KRATOS_ERROR << "Error in adding the new item" << std::endl
309  return result.first;
310  }
311 
312  template <class InputIterator>
313  void insert(InputIterator First, InputIterator Last)
314  {
315  for(; First != Last; ++First)
316  insert(begin(),*First);
317  }
318 
319 
321  {
322  return mData.erase(pos.base());
323  }
324 
326  {
327  return mData.erase(Key);
328  }
329 
331  {
332  return mData.erase( first.base(), last.base() );
333  }
334 
335  void clear()
336  {
337  mData.clear();
338  }
339 
340  iterator find(const key_type& Key)
341  {
342  return mData.find(Key);
343  }
344 
345  const_iterator find(const key_type& Key) const
346  {
347  return mData.find(Key);
348  }
349 
351  {
352  return find(Key) == mData.end() ? 0 : 1;
353  }
354 
355  void reserve(int reservedsize)
356  {
357  mData.reserve(reservedsize);
358  }
359  int capacity()
360  {
361  return mData.capacity();
362  }
363 
364 
368 
371  {
372  return mData;
373  }
374 
377  {
378  return mData;
379  }
380 
381 
382 
386 
387  bool empty() const
388  {
389  return mData.empty();
390  }
391 
392 
396 
398  std::string Info() const
399  {
400  std::stringstream buffer;
401  buffer << "Pointer hash map set (size = " << size() << ") : ";
402 
403  return buffer.str();
404  }
405 
406 
408  void PrintInfo(std::ostream& rOStream) const
409  {
410  rOStream << Info();
411  }
412 
414  void PrintData(std::ostream& rOStream) const
415  {
416  std::copy(begin(), end(), std::ostream_iterator<TDataType>(rOStream, "\n "));
417  }
418 
419 
423 
424 
426 
427 protected:
430 
431 
435 
436 
440 
441 
445 
446 
450 
451 
455 
456 
460 
461 
463 
464 private:
465 
466 
469 
470 
474 
475  ContainerType mData;
476 
480 
481 
485 
486  key_type KeyOf(iterator i)
487  {
488  return TGetKeyType()(*i);
489  }
490 
491  key_type KeyOf(ptr_iterator i)
492  {
493  return TGetKeyType()(**i);
494  }
495 
496  key_type KeyOf(const TDataType & i)
497  {
498  return TGetKeyType()(i);
499  }
500 
501 
505 
506  friend class Serializer;
507 
508 
509  virtual void save(Serializer& rSerializer) const
510  {
511  size_type size = mData.size();
512 
513  rSerializer.save("size", size);
514 
515  for (ptr_const_iterator i = ptr_begin(); i != ptr_end(); i++)
516  rSerializer.save("E", i->second);
517  }
518 
519  virtual void load(Serializer& rSerializer)
520  {
521  size_type size;
522 
523  rSerializer.load("size", size);
524 
525  for (size_type i = 0; i < size; i++)
526  {
527  pointer_type p = nullptr;// new TDataType;
528  rSerializer.load("E", p);
529  insert(p);
530  }
531  }
532 
533 
537 
538 
542 
543 
547 
548 
550 
551 }; // Class PointerHashMapSet
552 
554 
557 
558 
562 
563 
565 //template<class TDataType,
566 // class TGetKeyType,
567 // class TCompareType,
568 // class TEqualType,
569 // class TPointerType,
570 // class ContainerType>
571 //inline std::istream& operator >> (std::istream& rIStream,
572 // PointerHashMapSet<TDataType, TGetKeyType, TCompareType, TEqualType, TPointerType, ContainerType>& rThis);
573 
575 template<class TDataType,
576  class TGetKeyType,
577  class TCompareType,
578  class TEqualType,
579  class TPointerType,
580  class ContainerType>
581 inline std::ostream& operator << (std::ostream& rOStream,
583 {
584  rThis.PrintInfo(rOStream);
585  rOStream << std::endl;
586  rThis.PrintData(rOStream);
587 
588  return rOStream;
589 }
591 
592 
593 } // namespace Kratos.
594 
595 #endif // KRATOS_POINTER_HASH_MAP_SET_H_INCLUDED defined
PointerHashMapSet is a hash implemenetation of the PointerVectorSet.
Definition: pointer_hash_map_set.h:70
void clear()
Definition: pointer_hash_map_set.h:335
void reserve(int reservedsize)
Definition: pointer_hash_map_set.h:355
ContainerType::iterator ptr_iterator
Definition: pointer_hash_map_set.h:93
ContainerType::difference_type difference_type
Definition: pointer_hash_map_set.h:95
void swap(PointerHashMapSet &rOther)
Definition: pointer_hash_map_set.h:289
PointerHashMapSet & operator=(const PointerHashMapSet &rOther)
Definition: pointer_hash_map_set.h:186
iterator end()
Definition: pointer_hash_map_set.h:228
const_iterator end() const
Definition: pointer_hash_map_set.h:232
bool operator!=(const PointerHashMapSet &r) const
Definition: pointer_hash_map_set.h:210
TDataType value_type
Definition: pointer_hash_map_set.h:85
~PointerHashMapSet()
Destructor.
Definition: pointer_hash_map_set.h:179
bool operator==(const PointerHashMapSet &r) const
Definition: pointer_hash_map_set.h:205
PointerHashMapSet(const ContainerType &rContainer)
Definition: pointer_hash_map_set.h:174
TPointerType pointer_type
Definition: pointer_hash_map_set.h:86
TDataType & reference
Definition: pointer_hash_map_set.h:87
iterator_adaptor iterator
Definition: pointer_hash_map_set.h:153
pointer_type & operator()(const key_type &Key)
Definition: pointer_hash_map_set.h:199
const_iterator find(const key_type &Key) const
Definition: pointer_hash_map_set.h:345
std::string Info() const
Turn back information as a string.
Definition: pointer_hash_map_set.h:398
iterator begin()
Definition: pointer_hash_map_set.h:220
ContainerType & GetContainer()
Definition: pointer_hash_map_set.h:370
ptr_iterator ptr_begin()
Definition: pointer_hash_map_set.h:236
ContainerType::const_iterator ptr_const_iterator
Definition: pointer_hash_map_set.h:94
TDataType & operator[](const key_type &Key)
Definition: pointer_hash_map_set.h:193
const_reference back() const
Definition: pointer_hash_map_set.h:268
const_reference front() const
Definition: pointer_hash_map_set.h:258
iterator find(const key_type &Key)
Definition: pointer_hash_map_set.h:340
reference back()
Definition: pointer_hash_map_set.h:263
iterator erase(iterator first, iterator last)
Definition: pointer_hash_map_set.h:330
const ContainerType & GetContainer() const
Definition: pointer_hash_map_set.h:376
const_iterator_adaptor const_iterator
Definition: pointer_hash_map_set.h:154
ContainerType::size_type size_type
Definition: pointer_hash_map_set.h:92
int capacity()
Definition: pointer_hash_map_set.h:359
bool empty() const
Definition: pointer_hash_map_set.h:387
KRATOS_CLASS_POINTER_DEFINITION(PointerHashMapSet)
Pointer definition of PointerHashMapSet.
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: pointer_hash_map_set.h:414
PointerHashMapSet(const PointerHashMapSet &rOther)
Definition: pointer_hash_map_set.h:171
size_type count(const key_type &Key)
Definition: pointer_hash_map_set.h:350
const TDataType & const_reference
Definition: pointer_hash_map_set.h:88
reference front()
Definition: pointer_hash_map_set.h:253
decltype(std::declval< TGetKeyType >()(std::declval< TDataType >())) typedef key_type
Key type for searching in this container.
Definition: pointer_hash_map_set.h:81
void insert(InputIterator First, InputIterator Last)
Definition: pointer_hash_map_set.h:313
iterator insert(const TOtherDataType &rData)
Definition: pointer_hash_map_set.h:295
PointerHashMapSet(TInputIteratorType First, TInputIteratorType Last, size_type NewMaxBufferSize=1)
Definition: pointer_hash_map_set.h:165
size_type size() const
Definition: pointer_hash_map_set.h:274
size_type erase(key_type const &Key)
Definition: pointer_hash_map_set.h:325
ptr_iterator ptr_end()
Definition: pointer_hash_map_set.h:244
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: pointer_hash_map_set.h:408
std::unordered_map< typename std::remove_reference< key_type >::type, TPointerType, THashType > ContainerType
Definition: pointer_hash_map_set.h:90
iterator insert(TPointerType pData)
Definition: pointer_hash_map_set.h:301
iterator erase(iterator pos)
Definition: pointer_hash_map_set.h:320
TDataType data_type
Data type stores in this container.
Definition: pointer_hash_map_set.h:84
ptr_const_iterator ptr_end() const
Definition: pointer_hash_map_set.h:248
PointerHashMapSet()
Default constructor.
Definition: pointer_hash_map_set.h:161
const_iterator begin() const
Definition: pointer_hash_map_set.h:224
ptr_const_iterator ptr_begin() const
Definition: pointer_hash_map_set.h:240
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
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
Expression::Pointer operator*(const Expression::ConstPointer &rpLeft, const double Right)
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
rhs
Definition: generate_frictional_mortar_condition.py:297
type
Definition: generate_gid_list_file.py:35
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98
def load(f)
Definition: ode_solve.py:307
tuple const
Definition: ode_solve.py:403
p
Definition: sensitivityMatrix.py:52
namespace
Definition: array_1d.h:793
integer i
Definition: TensorModule.f:17
Configure::ContainerType ContainerType
Definition: transfer_utility.h:247