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.
key_hash.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: Vicente Mataix Ferrandiz
11 //
12 
13 #pragma once
14 
15 // System includes
16 
17 // External includes
18 
19 // Project includes
20 #include "includes/dof.h"
21 
22 namespace Kratos
23 {
26 
29 
33 
35  using IndexType = std::size_t;
36 
38  using HashType = std::size_t;
39 
43 
47 
55  template <class TClassType>
56  inline void HashCombine(
57  HashType& Seed,
58  const TClassType& Value
59  )
60  {
61  std::hash<TClassType> hasher;
62  Seed ^= hasher(Value) + 0x9e3779b9 + (Seed<<6) + (Seed>>2);
63  }
64 
72  template <class TClassType>
74  TClassType First,
75  TClassType Last
76  )
77  {
78  HashType seed = 0;
79 
80  while (First!=Last) {
81  HashCombine(seed, *First);
82  ++First;
83  }
84 
85  return seed;
86  }
87 
91 
92  class VariableData; // forward declaration
93 
98  template<class TClassType>
100  {
107  const TClassType& first,
108  const TClassType& second
109  ) const
110  {
111  if(first.size() != second.size()) {
112  return false;
113  }
114 
115  auto it_first = first.begin();
116  auto it_second = second.begin();
117 
118  while(it_first != first.end()) { // NOTE: We already checked that are same size
119  if(*it_first != *it_second) {
120  return false;
121  }
122  if(it_first != first.end()) {
123  ++it_first;
124  ++it_second;
125  }
126  }
127 
128  return true;
129  }
130  };
131 
136  template<class TClassType>
138  {
144  HashType operator()(const TClassType& rRange) const
145  {
146  return HashRange(rRange.begin(), rRange.end());
147  }
148  };
149 
153  struct KRATOS_API(KRATOS_CORE) VariableHasher
154  {
160  HashType operator()(const VariableData& rVariable) const;
161  };
162 
166  struct KRATOS_API(KRATOS_CORE) VariableComparator
167  {
173  bool operator()(
174  const VariableData& rFirst,
175  const VariableData& rSecond
176  ) const;
177  };
178 
182  struct KRATOS_API(KRATOS_CORE) pVariableHasher
183  {
189  HashType operator()(const VariableData* pVariable) const;
190  };
191 
195  struct KRATOS_API(KRATOS_CORE) pVariableComparator
196  {
202  bool operator()(
203  const VariableData* pFirst,
204  const VariableData* pSecond
205  ) const;
206  };
207 
212  template<class TIndexedObject>
214  {
220  HashType operator()(const TIndexedObject& rIndexedObject) const
221  {
222  return rIndexedObject.Id();
223  }
224  };
225 
230  template<class TIndexedObject>
232  {
239  const TIndexedObject& rFirst,
240  const TIndexedObject& rSecond
241  ) const
242  {
243  return rFirst.Id() == rSecond.Id();
244  }
245  };
246 
252  template<class TpIndexedObject>
254  {
260  HashType operator()(const TpIndexedObject pIndexedObject) const
261  {
262  return pIndexedObject->Id();
263  }
264  };
265 
271  template<class TpIndexedObject>
273  {
280  const TpIndexedObject pFirst,
281  const TpIndexedObject pSecond
282  ) const
283  {
284  return pFirst->Id() == pSecond->Id();
285  }
286  };
287 
292  template<class TSharedPointer>
294  {
300  HashType operator()(const TSharedPointer& pPointer) const
301  {
302  return reinterpret_cast<HashType>(pPointer.get());
303  }
304  };
305 
310  template<class TSharedPointer>
312  {
319  const TSharedPointer& first,
320  const TSharedPointer& second
321  ) const
322  {
323  return first.get() == second.get();
324  }
325  };
326 
331  template<class TVectorIndex>
333  {
339  HashType operator()(const TVectorIndex& k) const
340  {
341  return HashRange(k.begin(), k.end());
342  }
343  };
344 
349  template<class TVectorIndex>
351  {
357  bool operator()(const TVectorIndex& lhs, const TVectorIndex& rhs) const
358  {
359  if(lhs.size() != rhs.size())
360  return false;
361 
362  for(IndexType i = 0; i < lhs.size(); i++) {
363  if(lhs[i] != rhs[i]) return false;
364  }
365 
366  return true;
367  }
368  };
369 
375  {
381  {
382  HashType seed = 0;
383  HashCombine(seed, pDoF->Id());
384  HashCombine(seed, (pDoF->GetVariable()).Key());
385  return seed;
386  }
387  };
388 
394  {
400  bool operator()(const Dof<double>::Pointer& pDoF1, const Dof<double>::Pointer& pDoF2) const
401  {
402  return (((pDoF1->Id() == pDoF2->Id() && (pDoF1->GetVariable()).Key()) == (pDoF2->GetVariable()).Key()));
403  }
404  };
405 
410  template<class TType1, class TType2>
411  struct PairHasher
412  {
417  HashType operator()(const std::pair<TType1, TType2>& rPair) const
418  {
419  HashType seed = 0;
420  HashCombine<TType1>(seed, rPair.first);
421  HashCombine<TType2>(seed, rPair.second);
422  return seed;
423  }
424  };
425 
430  template<class TType1, class TType2>
432  {
438  bool operator()(const std::pair<TType1, TType2>& rPair1, const std::pair<TType1, TType2>& rPair2) const
439  {
440  return ((std::get<0>(rPair1) == std::get<0>(rPair2)) && (std::get<1>(rPair1) == std::get<1>(rPair2)));
441  }
442  };
443 
447 
448 } // namespace Kratos.
449 
453 namespace std
454 {
462  template<typename T1, typename T2>
463  struct hash<std::pair<T1, T2>>
464  {
470  size_t operator()(const std::pair<T1, T2>& p) const
471  {
472  size_t seed = 0;
473  const size_t h1 = std::hash<T1>()(p.first);
474  const size_t h2 = std::hash<T2>()(p.second);
477  return seed;
478  }
479  };
480 } // namespace std.
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
const VariableData & GetVariable() const
Definition: dof.h:303
IndexType Id() const
Definition: dof.h:292
This class is the base of variables and variable's components which contains their common data.
Definition: variable_data.h:49
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
bool operator()(const TClassType &first, const TClassType &second) const
This is the () operator.
Definition: key_hash.h:106
HashType operator()(const std::pair< TType1, TType2 > &rPair) const
The () operator.
Definition: key_hash.h:417
HashType operator()(const Dof< double >::Pointer &pDoF) const
The () operator.
Definition: key_hash.h:380
HashType operator()(const TSharedPointer &pPointer) const
This is the () operator.
Definition: key_hash.h:300
void HashCombine(HashType &Seed, const TClassType &Value)
This method creates an "unique" hash for the input value.
Definition: key_hash.h:56
bool operator()(const TpIndexedObject pFirst, const TpIndexedObject pSecond) const
This is the () operator.
Definition: key_hash.h:279
HashType operator()(const TClassType &rRange) const
This is the () operator.
Definition: key_hash.h:144
bool operator()(const TIndexedObject &rFirst, const TIndexedObject &rSecond) const
This is the () operator.
Definition: key_hash.h:238
bool operator()(const TVectorIndex &lhs, const TVectorIndex &rhs) const
This is the () operator.
Definition: key_hash.h:357
bool operator()(const std::pair< TType1, TType2 > &rPair1, const std::pair< TType1, TType2 > &rPair2) const
The () operator.
Definition: key_hash.h:438
bool operator()(const TSharedPointer &first, const TSharedPointer &second) const
This is the () operator.
Definition: key_hash.h:318
HashType operator()(const TIndexedObject &rIndexedObject) const
This is the () operator.
Definition: key_hash.h:220
size_t operator()(const std::pair< T1, T2 > &p) const
Calculates the hash value of a given pair of values in a way that combines the hash values of the ind...
Definition: key_hash.h:470
HashType operator()(const TVectorIndex &k) const
This is the () operator.
Definition: key_hash.h:339
HashType operator()(const TpIndexedObject pIndexedObject) const
This is the () operator.
Definition: key_hash.h:260
HashType HashRange(TClassType First, TClassType Last)
This method combines hash until it reaches the last class in order to obtain a corresponding seed.
Definition: key_hash.h:73
std::size_t HashType
The definition of the hash type.
Definition: key_hash.h:38
bool operator()(const Dof< double >::Pointer &pDoF1, const Dof< double >::Pointer &pDoF2) const
The () operator.
Definition: key_hash.h:400
int seed
Definition: GenerateWind.py:138
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
rhs
Definition: generate_frictional_mortar_condition.py:297
lhs
Definition: generate_frictional_mortar_condition.py:297
int k
Definition: quadrature.py:595
p
Definition: sensitivityMatrix.py:52
namespace
Definition: array_1d.h:793
integer i
Definition: TensorModule.f:17
This is a key comparer between two dof pointers.
Definition: key_hash.h:394
This is a hasher for a dof pointers.
Definition: key_hash.h:375
This is a key comparer between two indexed objects.
Definition: key_hash.h:232
This is a hasher for indexed objects.
Definition: key_hash.h:214
This is a key comparer between two indexed objects (pointer)
Definition: key_hash.h:273
This is a hasher for indexed objects (pointer)
Definition: key_hash.h:254
This is a key comparer of general pourpose between two classes.
Definition: key_hash.h:100
This is a hasher of general pourpose.
Definition: key_hash.h:138
This is a key comparer between two indexes pairs.
Definition: key_hash.h:432
This is a hasher for pairs.
Definition: key_hash.h:412
This is a key comparer between two shared pointers.
Definition: key_hash.h:312
This is a hasher for shared pointers.
Definition: key_hash.h:294
This is a key comparer between two variables.
Definition: key_hash.h:167
This is a hasher for variables.
Definition: key_hash.h:154
This is a key comparer between two vectors of indexes.
Definition: key_hash.h:351
This is a hasher between two vectors of indexes.
Definition: key_hash.h:333
This is a key comparer between two variables pointers.
Definition: key_hash.h:196
This is a hasher for variables pointers.
Definition: key_hash.h:183