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

4
#  include <string>
5
#  include <functional>
6

7
// See the G++ FAQ for details about the following.
8
9
10
11
#  ifdef __GNUC__
#  if __GNUC__ < 3
#    include <hash_map.h>
#    include <hash_set.h>
12
13
14
15
16
17
    namespace Sgi
    { // inherit globals
      using ::hash_map;
      using ::hash_set;
      using ::hash;
    };
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#  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>
38
39
  struct ptr_hash :
    public std::unary_function<const T*, size_t>
40
  {
41
    size_t operator()(const T* p) const
42
    {
43
44
45
46
47
      return reinterpret_cast<const char*>(p) - static_cast<const char*>(0);
    }
  };

  /// A hash function for strings.
48
49
50
  struct string_hash :
    public Sgi::hash<const char*>,
    public std::unary_function<const std::string&, size_t>
51
52
53
54
55
56
  {
    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());
57
58
59
60
61
    }
  };
}

#endif // SPOT_MISC_HASH_HH