hash.hh 1.26 KB
Newer Older
1
2
3
#ifndef SPOT_MISC_HASH_HH
#  define SPOT_MISC_HASH_HH

4
#  include <string>
5

6
// See the G++ FAQ for details about the following.
7
8
9
10
#  ifdef __GNUC__
#  if __GNUC__ < 3
#    include <hash_map.h>
#    include <hash_set.h>
11
12
13
14
15
16
    namespace Sgi
    { // inherit globals
      using ::hash_map;
      using ::hash_set;
      using ::hash;
    };
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#  else
#    include <ext/hash_map>
#    include <ext/hash_set>
#    if __GNUC_MINOR__ == 0
      namespace Sgi = std;               // GCC 3.0
#    else
      namespace Sgi = ::__gnu_cxx;       // GCC 3.1 and later
#    endif
#  endif
#  else      // ...  there are other compilers, right?
#   include <hash_map>
#   include <hash_set>
    namespace Sgi = std;
#  endif

namespace spot
{

  /// A hash function for pointers.
  template <class T>
  struct ptr_hash
  {
39
    size_t operator()(const T* p) const
40
    {
41
42
43
44
45
46
47
48
49
50
51
52
      return reinterpret_cast<const char*>(p) - static_cast<const char*>(0);
    }
  };

  /// A hash function for strings.
  struct string_hash : Sgi::hash<const char*>
  {
    size_t operator()(const std::string& s) const
    {
      // We are living dangerously.  Be sure to call operator()
      // from the super-class, not this one.
      return Sgi::hash<const char*>::operator()(s.c_str());
53
54
55
56
57
    }
  };
}

#endif // SPOT_MISC_HASH_HH