MorphoGraphX  2.0-1-227
Hash.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2019 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef HASH_HPP
12 #define HASH_HPP
13 
14 #include <QtGlobal>
15 #include <Config.hpp>
16 #include <Vertex.hpp>
17 #include <QString>
18 #include <utility>
19 #include <functional>
20 #include <boost/functional/hash.hpp>
21 
22 // Add hash for unordered maps
23 namespace std
24 {
26  template <typename T1, typename T2>
27  struct hash<std::pair<T1, T2> >
28  {
29  size_t operator()(const std::pair<T1, T2> &v) const
30  {
31  std::size_t seed = 0;
32  boost::hash_combine(seed, v.first);
33  boost::hash_combine(seed, v.second);
34  return seed;
35  }
36  };
37 
39  template <typename T1, typename T2>
40  struct hash<std::pair< mgx::Vertex<T1>, mgx::Vertex<T2> > >
41  {
42  size_t operator()(const std::pair<mgx::Vertex<T1>, mgx::Vertex<T2> > &v) const
43  {
44  std::size_t seed = 0;
45  boost::hash_combine(seed, v.first.id());
46  boost::hash_combine(seed, v.second.id());
47  return seed;
48  }
49  };
50 
51  // Hash QString
52  // BJL 2021-04-19: As of Qt 5.14, Qt itself defines std::hash<QString>.
53 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
54  template <> struct hash<QString>
55  {
56 
57  size_t operator()(const QString &s) const
58  {
59  int hash = 0, strlen = s.length(), i;
60  QChar character;
61  if (strlen == 0)
62  return hash;
63  for (i = 0; i < strlen; i++) {
64  character = s.at(i);
65  hash = (31 * hash) + (character.unicode());
66  }
67  return hash;
68  }
69  };
70 #endif // Qt version < 5.14
71 }
72 #endif
Vertex.hpp
std::hash< std::pair< T1, T2 > >::operator()
size_t operator()(const std::pair< T1, T2 > &v) const
Definition: Hash.hpp:29
std::hash< std::pair< mgx::Vertex< T1 >, mgx::Vertex< T2 > > >::operator()
size_t operator()(const std::pair< mgx::Vertex< T1 >, mgx::Vertex< T2 > > &v) const
Definition: Hash.hpp:42
mgx::Vertex
Definition: Vertex.hpp:58