hash.hh 3.32 KB
Newer Older
1
// Copyright (C) 2008, 2011 Laboratoire de Recherche et Développement
Guillaume Sadegh's avatar
Guillaume Sadegh committed
2
3
// de l'Epita (LRDE).
// Copyright (C) 2003, 2004, 2005 Laboratoire d'Informatique de
4
5
// Paris 6 (LIP6), dpartement Systmes Rpartis Coopratifs (SRC),
// Universit Pierre et Marie Curie.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
6
7
8
9
10
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 3 of the License, or
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
12
13
14
15
16
17
18
19
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
20
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
21

22
23
24
#ifndef SPOT_MISC_HASH_HH
#  define SPOT_MISC_HASH_HH

25
#  include <string>
26
#  include <functional>
27
28
#  include "misc/hashfunc.hh"
#  include "misc/_config.h"
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifdef SPOT_HAVE_UNORDERED_MAP
#  include <unordered_map>
#  include <unordered_set>
   namespace Sgi = std;
#  define hash_map unordered_map
#  define hash_multimap unordered_multimap
#  define hash_set unordered_set
#else
#ifdef SPOT_HAVE_TR1_UNORDERED_MAP
#  include <tr1/unordered_map>
#  include <tr1/unordered_set>
   namespace Sgi = std::tr1;
#  define hash_map unordered_map
#  define hash_multimap unordered_multimap
#  define hash_set unordered_set
#else
#ifdef SPOT_HAVE_EXT_HASH_MAP
#  include <ext/hash_map>
#  include <ext/hash_set>
#  if __GNUC__ == 3 && __GNUC_MINOR__ == 0
     namespace Sgi = std;               // GCC 3.0
#  else
     namespace Sgi = ::__gnu_cxx;       // GCC 3.1 to 4.2
#  endif
#else
#  if defined(__GNUC__) && (__GNUC__ < 3)
56
57
#    include <hash_map.h>
#    include <hash_set.h>
58
59
60
    namespace Sgi
    { // inherit globals
      using ::hash_map;
61
      using ::hash_multimap;
62
63
      using ::hash_set;
      using ::hash;
64
    }
65
66
67
68
69
#  else
#   include <hash_map>
#   include <hash_set>
    namespace Sgi = std;
#  endif
70
71
72
#endif
#endif
#endif
73
74
75
76

namespace spot
{

77
  /// \ingroup hash_funcs
78
  /// \brief A hash function for pointers.
79
  template <class T>
80
81
  struct ptr_hash :
    public std::unary_function<const T*, size_t>
82
  {
83
    size_t operator()(const T* p) const
84
    {
85
86
      return knuth32_hash(reinterpret_cast<const char*>(p)
			  - static_cast<const char*>(0));
87
88
89
    }
  };

90
  /// \ingroup hash_funcs
91
  /// \brief A hash function for strings.
92
  /// @{
93
94
95
#if defined(SPOT_HAVE_UNORDERED_MAP) || defined(SPOT_HAVE_TR1_UNORDERED_MAP)
  typedef Sgi::hash<std::string> string_hash;
#else // e.g. GCC < 4.3
96
  struct string_hash:
97
98
    public Sgi::hash<const char*>,
    public std::unary_function<const std::string&, size_t>
99
100
101
102
103
104
  {
    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());
105
106
    }
  };
107
  /// @}
108
#endif
109
110

  /// \ingroup hash_funcs
111
  /// \brief A hash function that returns identity
112
113
114
115
116
117
118
119
120
  template<typename T>
  struct identity_hash:
    public std::unary_function<const T&, size_t>
  {
    size_t operator()(const T& s) const
    {
      return s;
    }
  };
121
122
123
}

#endif // SPOT_MISC_HASH_HH