~littlegreenbats/littlegreenbats/trunk

« back to all changes in this revision

Viewing changes to libbats/Hashclasses/hashclasses.hh

  • Committer: Sander van Dijk
  • Date: 2012-04-15 18:54:25 UTC
  • Revision ID: sgvandijk@gmail.com-20120415185425-xpbbwjscc7d41scz
- Use unordered_map in Cochlea, no longer need for hashclasses.hh and fixed translateInfo
- Remove some unnecessary includes in header files and add to internal header files

Show diffs side-by-side

added added

removed removed

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