Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
tuliphash.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef TLP_HASH_H
22 #define TLP_HASH_H
23 
24 /**
25  * @brief This file defines what class is used to provide a hashmap.
26  * The TLP_HASH_MAP macro defines which implementation is used for hash maps.
27  * The TLP_HASH_SET macro defines which implementation is used for hash sets.
28  *
29  * TLP_BEGIN_HASH_NAMESPACE is defined to open the namespace in which the hash classes are defined, to define new hashes (e.g. for Edge).
30  * TLP_END_HASH_NAMESPACE is definde to close the namespace (only used when using std::tr1)
31  * TLP_HASH_NAMESPACE allows to use a specific hasher class when declaring a hash set or a hash map.
32  */
33 
34 //VS2010 and later can use C++0x's unordered_map; vs2008 uses boost's tr1 implementation
35 #if defined(_MSC_VER) && (_MSC_VER > 1500)
36 # define TLP_USE_UNORDERED_MAP
37 #elif defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
38 # define TLP_USE_UNORDERED_MAP
39 #endif
40 
41 #ifdef TLP_USE_UNORDERED_MAP
42 # include <unordered_map>
43 # include <unordered_set>
44 # define TLP_HASH_MAP std::unordered_map
45 # define TLP_HASH_SET std::unordered_set
46 # define TLP_HASH_NAMESPACE std
47 # define TLP_BEGIN_HASH_NAMESPACE namespace std
48 # define TLP_END_HASH_NAMESPACE
49 //clang, and GCC versions prior to the 4.x series do not have tr1; using ext
50 #elif (! defined _MSC_VER && (__GNUC__ < 4 || __GNUC_MINOR__ < 1))
51 # include <tulip/tulipconf.h>
52 # if (__GNUC__ < 3)
53 # include <hash_map>
54 # include <hash_set>
55 # else
56 # include <ext/hash_map>
57 # include <ext/hash_set>
58 # endif
59 # define TLP_HASH_MAP stdext::hash_map
60 # define TLP_HASH_SET stdext::hash_set
61 # define TLP_HASH_NAMESPACE stdext
62 # define TLP_BEGIN_HASH_NAMESPACE namespace stdext
63 # define TLP_END_HASH_NAMESPACE
64 
65 # include <string>
66 
67 namespace stdext {
68 template<> struct hash<const std::string> {
69  size_t operator()(const std::string &s) const {
70  return hash<const char *>()(s.c_str());
71  }
72 };
73 template<> struct hash<std::string> {
74  size_t operator()(const std::string &s) const {
75  return hash<const char *>()(s.c_str());
76  }
77 };
78 template<>
79 struct hash<double> {
80  size_t operator()(const double s) const {
81  return (size_t) s;
82  }
83 };
84 template<>
85 struct hash<float> {
86  size_t operator()(const float s) const {
87  return hash<unsigned int>()(*((unsigned int *) &s));
88  }
89 };
90 }
91 //MSVC < 2010 use tr1 from boost, and GCC 4.X provides tr1 too.
92 #else
93 # include <tr1/unordered_map>
94 # include <tr1/unordered_set>
95 # define TLP_HASH_MAP std::tr1::unordered_map
96 # define TLP_HASH_SET std::tr1::unordered_set
97 # define TLP_HASH_NAMESPACE std::tr1
98 # define TLP_BEGIN_HASH_NAMESPACE namespace std { namespace tr1
99 # define TLP_END_HASH_NAMESPACE }
100 #endif
101 
102 // The hash_combine function from the boost library
103 // Call it repeatedly to incrementally create a hash value from several variables.
104 
105 // Explanation of the formula from StackOverflow (http://stackoverflow.com/questions/4948780/magic-numbers-in-boosthash-combine) :
106 // The magic number 0x9e3779b9 = 2^32 / ((1 + sqrt(5)) / 2) is the reciprocal of the golden ratio.
107 // It is supposed to be 32 random bits, where each is equally likely to be 0 or 1, and with no simple correlation between the bits.
108 // So including this number "randomly" changes each bit of the seed; as you say, this means that consecutive values will be far apart.
109 // Including the shifted versions of the old seed makes sure that, even if hash_value() has a fairly small range of values,
110 // differences will soon be spread across all the bits.
111 TLP_BEGIN_HASH_NAMESPACE {
112  template <class T>
113  inline void hash_combine(std::size_t & seed, const T & v) {
114  hash<T> hasher;
115  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
116  }
117 } TLP_END_HASH_NAMESPACE
118 
119 #endif
120 ///@endcond