Debian Codegraph @

yo/containers/examples/hashclasses.h

raw
1 #ifndef INCLUDED_HASHCLASSES_H_
2 #define INCLUDED_HASHCLASSES_H_
3
4 #include <string>
5 #include <cctype>
6
7 /*
8 Note that with the Gnu g++ compiler 3.2 (and beyond?) the ext/ header
9 uses the __gnu_cxx namespace for symbols defined in these header files.
10
11 When using compilers before version 3.2, do:
12 #define __gnu_cxx std
13 before including this file to circumvent problems that may occur
14 because of these namespace conventions which were not yet used in versions
15 before 3.2.
16
17 */
18
19 #include <ext/hash_map>
20 #include <algorithm>
21
22 /*
23 This file is copyright (c) GPL, 2001-2004
24 ==========================================
25 august 2004: redundant include guards removed
26
27 october 2002: provisions for using the hashclasses with the g++ 3.2
28 compiler were incorporated.
29
30 april 2002: namespace FBB introduced
31 abbreviated class templates defined,
32 see the END of this comment section for examples of how
33 to use these abbreviations.
34
35 jan 2002: redundant include guards added,
36 required header files adapted,
37 for_each() rather than transform() used
38
39 With hash_maps using char const * for the keys:
40 ============
41
42 * Use `HashCharPtr' as 3rd template argument for case-sensitive keys
43 * Use `HashCaseCharPtr' as 3rd template argument for case-insensitive
44 keys
45
46 * Use `EqualCharPtr' as 4th template argument for case-sensitive keys
47 * Use `EqualCaseCharPtr' as 4th template argument for case-insensitive
48 keys
49
50
51 With hash_maps using std::string for the keys:
52 ===========
53
54 * Use `HashString' as 3rd template argument for case-sensitive keys
55 * Use `HashCaseString' as 3rd template argument for case-insensitive keys
56
57 * OMIT the 4th template argument for case-sensitive keys
58 * Use `EqualCaseString' as 4th template argument for case-insensitive
59 keys
60
61
62 Examples, using int as the value type. Any other type can be used instead
63 for the value type:
64
65 // key is char const *, case sensitive
66 __gnu_cxx::hash_map<char const *, int, FBB::HashCharPtr,
67 FBB::EqualCharPtr >
68 hashtab;
69
70 // key is char const *, case insensitive
71 __gnu_cxx::hash_map<char const *, int, FBB::HashCaseCharPtr,
72 FBB::EqualCaseCharPtr >
73 hashtab;
74
75 // key is std::string, case sensitive
76 __gnu_cxx::hash_map<std::string, int, FBB::HashString>
77 hashtab;
78
79 // key is std::string, case insensitive
80 __gnu_cxx::hash_map<std::string, int, FBB::HashCaseString,
81 FBB::EqualCaseString>
82 hashtab;
83
84 Instead of the above full typedeclarations, the following shortcuts should
85 work as well:
86
87 FBB::CharPtrHash<int> // key is char const *, case sensitive
88 hashtab;
89
90 FBB::CharCasePtrHash<int> // key is char const *, case insensitive
91 hashtab;
92
93 FBB::StringHash<int> // key is std::string, case sensitive
94 hashtab;
95
96 FBB::StringCaseHash<int> // key is std::string, case insensitive
97 hashtab;
98
99 With these template types iterators and other map-members are also
100 available. E.g.,
101
102 --------------------------------------------------------------------------
103 extern FBB::StringHash<int> dh;
104
105 for (FBB::StringHash<int>::iterator it = dh.begin(); it != dh.end(); it++)
106 std::cout << it->first << " - " << it->second << '\n';
107 --------------------------------------------------------------------------
108
109 Feb. 2001 - April 2002
110 Frank B. Brokken ([email protected])
111 */
112
113 namespace FBB
114 {
115
116 class HashCharPtr
117 {
118 public:
119 size_t operator()(char const *str) const
120 {
121 return __gnu_cxx::hash<char const *>()(str);
122 }
123 };
124
125 class EqualCharPtr
126 {
127 public:
128 bool operator()(char const *x, char const *y) const
129 {
130 return !strcmp(x, y);
131 }
132 };
133
134 class HashCaseCharPtr
135 {
136 public:
137 size_t operator()(char const *str) const
138 {
139 std::string s = str;
140 for_each(s.begin(), s.end(), *this);
141 return __gnu_cxx::hash<char const *>()(s.c_str());
142 }
143 void operator()(char &c) const
144 {
145 c = tolower(c);
146 }
147 };
148
149 class EqualCaseCharPtr
150 {
151 public:
152 bool operator()(char const *x, char const *y) const
153 {
154 return !strcasecmp(x, y);
155 }
156 };
157
158 class HashString
159 {
160 public:
161 size_t operator()(std::string const &str) const
162 {
163 return __gnu_cxx::hash<char const *>()(str.c_str());
164 }
165 };
166
167 class HashCaseString: public HashCaseCharPtr
168 {
169 public:
170 size_t operator()(std::string const &str) const
171 {
172 return HashCaseCharPtr::operator()(str.c_str());
173 }
174 };
175
176 class EqualCaseString
177 {
178 public:
179 bool operator()(std::string const &s1, std::string const &s2) const
180 {
181 return !strcasecmp(s1.c_str(), s2.c_str());
182 }
183 };
184
185
186 template<typename Value>
187 class CharPtrHash: public
188 __gnu_cxx::hash_map<char const *, Value, HashCharPtr, EqualCharPtr >
189 {
190 public:
191 CharPtrHash()
192 {}
193 template <typename InputIterator>
194 CharPtrHash(InputIterator first, InputIterator beyond)
195 :
196 __gnu_cxx::hash_map<char const *, Value, HashCharPtr,
197 EqualCharPtr>(first, beyond)
198 {}
199 };
200
201 template<typename Value>
202 class CharCasePtrHash: public
203 __gnu_cxx::hash_map<char const *, Value, HashCaseCharPtr,
204 EqualCaseCharPtr >
205 {
206 public:
207 CharCasePtrHash()
208 {}
209 template <typename InputIterator>
210 CharCasePtrHash(InputIterator first, InputIterator beyond)
211 :
212 __gnu_cxx::hash_map<char const *, Value,
213 HashCaseCharPtr, EqualCaseCharPtr>
214 (first, beyond)
215 {}
216 };
217
218 template<typename Value>
219 class StringHash: public __gnu_cxx::hash_map<std::string, Value,
220 HashString>
221 {
222 public:
223 StringHash()
224 {}
225 template <typename InputIterator>
226 StringHash(InputIterator first, InputIterator beyond)
227 :
228 __gnu_cxx::hash_map<std::string, Value, HashString>
229 (first, beyond)
230 {}
231 };
232
233
234 template<typename Value>
235 class StringCaseHash: public
236 __gnu_cxx::hash_map<std::string, int, HashCaseString,
237 EqualCaseString>
238 {
239 public:
240 StringCaseHash()
241 {}
242 template <typename InputIterator>
243 StringCaseHash(InputIterator first, InputIterator beyond)
244 :
245 __gnu_cxx::hash_map<std::string,
246 int, HashCaseString,
247 EqualCaseString>(first, beyond)
248 {}
249 };
250
251 template<typename Key, typename Value>
252 class Hash: public
253 __gnu_cxx::hash_map<Key, Value,
254 __gnu_cxx::hash<Key>(),
255 equal<Key>())
256 {};
257
258 }
259 #endif