monotone

monotone Mtn Source Tree

Root/hash_map.hh

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

Archive Download this file

Branches

Tags

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