monotone

monotone Mtn Source Tree

Root/hash_map.hh

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status