-
Notifications
You must be signed in to change notification settings - Fork 29
/
Hash_map.hpp
135 lines (125 loc) · 4.42 KB
/
Hash_map.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//=========================================================
// @Modify: Chen Xin (xchen@ir.hit.edu.cn)
// @Date: 2011/03/11
// @Brief: Change default hash func to BKDR.
//==========================================================
/************************************************************
unsigned int BKDRHash(const std::string& str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = (hash * seed) + str[i];
}
return hash;
}
*************************************************************/
/*
* vi:ts=4:tw=78:shiftwidth=4:expandtab
* vim600:fdm=marker
*
* hash_map.hpp - wrapper header as a workaround for several different ways
* of using hash_map/hash_set since this is not ISO standard.
*
* After inclusion of this file hash and hash_map are exported into the global
* namespace.
*
* Copyright (C) 2004 by Zhang Le <ejoy@users.sourceforge.net>
* Begin : 26-Jun-2004
* Last Change : 25-Dec-2004.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef HASH_MAP_HPP
#define HASH_MAP_HPP
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#if defined(_STLPORT_VERSION)
#include <hash_map>
#include <hash_set>
using std::hash;
using std::hash_map;
using hash_set;
#else // not using STLPORT
#ifdef __GNUC__
#if __GNUC__ >= 3
#include <ext/hash_map>
#include <ext/hash_set>
namespace __gnu_cxx {
template <>
struct hash<std::string> {
size_t operator()(const std::string& s) const {
unsigned int _seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int _hash = 0;
for(std::size_t i = 0; i < s.size(); i++)
{
_hash = (_hash * _seed) + s[i];
}
return size_t(_hash);
}
};
};
using __gnu_cxx::hash_map;
using __gnu_cxx::hash_set;
using __gnu_cxx::hash;
#else // GCC 2.x
#include <hash_map>
#include <hash_set>
namespace std {
struct hash<std::string> {
size_t operator()(const std::string& s) const {
unsigned int _seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int _hash = 0;
for(std::size_t i = 0; i < s.size(); i++)
{
_hash = (_hash * _seed) + s[i];
}
return size_t(_hash);
}
};
};
using std::hash_map;
using hash_set;
using std::hash;
#endif // end GCC >= 3
#elif defined(_MSC_VER) && ((_MSC_VER >= 1300) || defined(__INTEL_COMPILER))
// we only support MSVC7+ and Intel C++ 8.0
#include <hash_map>
#include <hash_set>
namespace stdext {
inline size_t hash_value(const std::string& s) {
unsigned int _seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int _hash = 0;
for(std::size_t i = 0; i < s.size(); i++)
{
_hash = (_hash * _seed) + s[i];
}
return size_t(_hash);
}
}
using stdext::hash_map; // _MSC_EXTENSIONS, though DEPRECATED
using stdext::hash_set;
#else
#error unknown compiler
#endif //GCC or MSVC7+
#endif // end STLPORT
#endif /* ifndef HASH_MAP_HPP */