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#ifdef HAVE_GNUCXX_HASHMAP
47#define HASHMAP_PRESENT
48#include <ext/hash_map>
49#include <ext/hash_set>
50#include <string>
51
52namespace hashmap {
53 template<>
54 struct hash<std::string>
55 {
56 size_t operator()(std::string const & s) const
57 {
58 return __gnu_cxx::__stl_hash_string(s.c_str());
59 }
60 };
61
62 template<typename _Key, typename _Value>
63 class hash_map : public __gnu_cxx::hash_map<_Key,
64 _Value,
65 hash<_Key>,
66 equal_to<_Key> >
67 {};
68
69 template<typename _Key>
70 class hash_set : public __gnu_cxx::hash_set<_Key,
71 hash<_Key>,
72 equal_to<_Key> >
73 {};
74
75 template<typename _Key, typename _Value>
76 class hash_multimap : public __gnu_cxx::hash_multimap<_Key,
77 _Value,
78 hash<_Key>,
79 equal_to<_Key> >
80 {};
81
82
83}
84#endif
85
86#ifdef HAVE_STLPORT_HASHMAP
87#define HASHMAP_PRESENT
88#include <hash_map>
89#include <hash_set>
90#include <string>
91
92namespace hashmap {
93 template<>
94 struct hash<std::string>
95 {
96 size_t operator()(std::string const & s) const
97 {
98 const char* s2=s.c_str();
99 unsigned long h = 0;
100 for ( ; *s2; ++s2)
101 h = 5*h + *s2;
102 return size_t(h);
103 }
104 };
105
106 template<typename _Key, typename _Value>
107 class hash_map : public std::hash_map<_Key,
108 _Value,
109 hash<_Key>,
110 equal_to<_Key> >
111 {};
112
113 template<typename _Key>
114 class hash_set : public std::hash_set<_Key,
115 hash<_Key>,
116 equal_to<_Key> >
117 {};
118
119 template<typename _Key, typename _Value>
120 class hash_multimap : public std::hash_multimap<_Key,
121 _Value,
122 hash<_Key>,
123 equal_to<_Key> >
124 {};
125}
126#endif
127
128#ifdef _MSC_VER
129#define HASHMAP_PRESENT
130#include <hash_map>
131#include <hash_set>
132
133namespace hashmap
134{
135 template<>
136 struct hash<std::string>
137 {
138 size_t operator()(std::string const & s) const
139 {
140 const char* s2=s.c_str();
141 unsigned long h = 0;
142 for ( ; *s2; ++s2)
143 h = 5*h + *s2;
144 return size_t(h);
145 }
146 };
147
148 template<typename T>
149 struct hash_traits
150 {
151 static const size_t bucket_size = 4;
152 static const size_t min_buckets = 8;
153 less<T> lt;
154 hash<T> h;
155
156 size_t operator( )(T const & s) const
157 {
158 return h(s);
159 }
160 bool operator( )(T const & a,
161 T const & b) const
162 {
163 return lt(a, b);
164 }
165 };
166 template <typename _Key, typename _Value>
167 struct hash_map : public stdext::hash_map<_Key,
168 _Value,
169 hash_traits<_Key> >
170 {};
171 template <typename _Key, typename _Value>
172 struct hash_multimap : public stdext::hash_multimap<_Key,
173 _Value,
174 hash_traits<_Key> >
175 {};
176 template <typename _Key>
177 struct hash_set : public stdext::hash_set<_Key,
178 hash_traits<_Key> >
179 {};
180}
181#endif
182
183#ifndef HASHMAP_PRESENT
184#error need wrapper for hash_map for your compiler
185#endif
186
187#undef HASHMAP_PRESENT
188
189// Local Variables:
190// mode: C++
191// fill-column: 76
192// c-file-style: "gnu"
193// indent-tabs-mode: nil
194// End:
195// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
196
197#endif

Archive Download this file

Branches

Tags

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