1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
#ifndef __HASHMAP_HH
#define __HASHMAP_HH
#include <functional>
namespace hashmap {
template<typename T>
class equal_to : public std::equal_to<T>
{
// bool operator()(T const & b, T const & b) const;
};
template<typename T>
class less : public std::less<T>
{
// bool operator()(T const & b, T const & b) const;
};
template<typename T>
struct hash
{
// size_t operator()(T const & t) const;
};
template<>
struct hash<unsigned int>
{
size_t operator()(unsigned int t) const
{
return t;
}
};
template<>
struct hash<unsigned long>
{
size_t operator()(unsigned long t) const
{
return t;
}
};
}
#if HAVE_TR1_UNORDERED_MAP_AND_SET && HAVE_WORKING_TR1_UNORDERED_MAP_AND_SET
#define HASHMAP_PRESENT
#include <tr1/functional>
#include <tr1/unordered_map>
#include <tr1/unordered_set>
namespace hashmap {
template<>
struct hash<std::string>
{
size_t operator()(std::string const & s) const
{
return std::tr1::hash<std::string>()(s);
}
};
template<typename _Key, typename _Value>
class hash_map : public std::tr1::unordered_map<_Key,
_Value,
hash<_Key>,
equal_to<_Key> >
{};
template<typename _Key>
class hash_set : public std::tr1::unordered_set<_Key,
hash<_Key>,
equal_to<_Key> >
{};
template<typename _Key, typename _Value>
class hash_multimap : public std::tr1::unordered_multimap<_Key,
_Value,
hash<_Key>,
equal_to<_Key> >
{};
}
#elif defined(HAVE_GNUCXX_HASHMAP)
#define HASHMAP_PRESENT
#include <ext/hash_map>
#include <ext/hash_set>
namespace hashmap {
template<>
struct hash<std::string>
{
size_t operator()(std::string const & s) const
{
return __gnu_cxx::__stl_hash_string(s.c_str());
}
};
template<typename _Key, typename _Value>
class hash_map : public __gnu_cxx::hash_map<_Key,
_Value,
hash<_Key>,
equal_to<_Key> >
{};
template<typename _Key>
class hash_set : public __gnu_cxx::hash_set<_Key,
hash<_Key>,
equal_to<_Key> >
{};
template<typename _Key, typename _Value>
class hash_multimap : public __gnu_cxx::hash_multimap<_Key,
_Value,
hash<_Key>,
equal_to<_Key> >
{};
}
#elif HAVE_STLPORT_HASHMAP
#define HASHMAP_PRESENT
#include <hash_map>
#include <hash_set>
namespace hashmap {
template<>
struct hash<std::string>
{
size_t operator()(std::string const & s) const
{
const char* s2=s.c_str();
unsigned long h = 0;
for ( ; *s2; ++s2)
h = 5*h + *s2;
return size_t(h);
}
};
template<typename _Key, typename _Value>
class hash_map : public std::hash_map<_Key,
_Value,
hash<_Key>,
equal_to<_Key> >
{};
template<typename _Key>
class hash_set : public std::hash_set<_Key,
hash<_Key>,
equal_to<_Key> >
{};
template<typename _Key, typename _Value>
class hash_multimap : public std::hash_multimap<_Key,
_Value,
hash<_Key>,
equal_to<_Key> >
{};
}
#elif _MSC_VER
#define HASHMAP_PRESENT
#include <hash_map>
#include <hash_set>
namespace hashmap
{
template<>
struct hash<std::string>
{
size_t operator()(std::string const & s) const
{
const char* s2=s.c_str();
unsigned long h = 0;
for ( ; *s2; ++s2)
h = 5*h + *s2;
return size_t(h);
}
};
template<typename T>
struct hash_traits
{
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
less<T> lt;
hash<T> h;
size_t operator( )(T const & s) const
{
return h(s);
}
bool operator( )(T const & a,
T const & b) const
{
return lt(a, b);
}
};
template <typename _Key, typename _Value>
struct hash_map : public stdext::hash_map<_Key,
_Value,
hash_traits<_Key> >
{};
template <typename _Key, typename _Value>
struct hash_multimap : public stdext::hash_multimap<_Key,
_Value,
hash_traits<_Key> >
{};
template <typename _Key>
struct hash_set : public stdext::hash_set<_Key,
hash_traits<_Key> >
{};
}
#endif
#ifndef HASHMAP_PRESENT
#error need wrapper for hash_map for your compiler
#endif
#undef HASHMAP_PRESENT
// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
#endif
|