Tulip  4.3.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  */
32 
33 //VS2010 and later can use C++0x's unordered_map; vs2008 uses boost's tr1 implementation
34 #if defined(_MSC_VER) && (_MSC_VER > 1500)
35 # define TLP_USE_UNORDERED_MAP
36 #elif defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
37 # define TLP_USE_UNORDERED_MAP
38 #endif
39 
40 #ifdef TLP_USE_UNORDERED_MAP
41 # include <unordered_map>
42 # include <unordered_set>
43 # define TLP_HASH_MAP std::unordered_map
44 # define TLP_HASH_SET std::unordered_set
45 # define TLP_BEGIN_HASH_NAMESPACE namespace std
46 # define TLP_END_HASH_NAMESPACE
47 //clang, and GCC versions prior to the 4.x series do not have tr1; using ext
48 #elif (! defined _MSC_VER && (__GNUC__ < 4 || __GNUC_MINOR__ < 1))
49 # include <tulip/tulipconf.h>
50 # if (__GNUC__ < 3)
51 # include <hash_map>
52 # include <hash_set>
53 # else
54 # include <ext/hash_map>
55 # include <ext/hash_set>
56 # endif
57 # define TLP_HASH_MAP stdext::hash_map
58 # define TLP_HASH_SET stdext::hash_set
59 # define TLP_BEGIN_HASH_NAMESPACE namespace stdext
60 # define TLP_END_HASH_NAMESPACE
61 
62 # include <string>
63 
64 namespace stdext {
65 template<> struct hash<const std::string> {
66  size_t operator()(const std::string &s) const {
67  return hash<const char *>()(s.c_str());
68  }
69 };
70 template<> struct hash<std::string> {
71  size_t operator()(const std::string &s) const {
72  return hash<const char *>()(s.c_str());
73  }
74 };
75 template<>
76 struct hash<double> {
77  size_t operator()(const double s) const {
78  return (size_t) s;
79  }
80 };
81 template<>
82 struct hash<float> {
83  size_t operator()(const float s) const {
84  return hash<unsigned int>()(*((unsigned int *) &s));
85  }
86 };
87 }
88 //MSVC < 2010 use tr1 from boost, and GCC 4.X provides tr1 too.
89 #else
90 # include <tr1/unordered_map>
91 # include <tr1/unordered_set>
92 # define TLP_HASH_MAP std::tr1::unordered_map
93 # define TLP_HASH_SET std::tr1::unordered_set
94 # define TLP_BEGIN_HASH_NAMESPACE namespace std { namespace tr1
95 # define TLP_END_HASH_NAMESPACE }
96 #endif
97 
98 // The hash_combine function from the boost library
99 // Call it repeatedly to incrementally create a hash value from several variables.
100 
101 // Explanation of the formula from StackOverflow (http://stackoverflow.com/questions/4948780/magic-numbers-in-boosthash-combine) :
102 // The magic number 0x9e3779b9 = 2^32 / ((1 + sqrt(5)) / 2) is the reciprocal of the golden ratio.
103 // 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.
104 // So including this number "randomly" changes each bit of the seed; as you say, this means that consecutive values will be far apart.
105 // Including the shifted versions of the old seed makes sure that, even if hash_value() has a fairly small range of values,
106 // differences will soon be spread across all the bits.
107 TLP_BEGIN_HASH_NAMESPACE {
108  template <class T>
109  inline void hash_combine(std::size_t & seed, const T & v) {
110  hash<T> hasher;
111  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
112  }
113 } TLP_END_HASH_NAMESPACE
114 
115 #endif
116 ///@endcond