monotone

monotone Mtn Source Tree

Root/src/hash_map.hh

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

Archive Download this file

Branches

Tags

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