1 /*
2 * Copyright (c) 1996-1998
3 * Silicon Graphics Computer Systems, Inc.
4 *
5 * Permission to use, copy, modify, distribute and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation. Silicon Graphics makes no
10 * representations about the suitability of this software for any
11 * purpose. It is provided "as is" without express or implied warranty.
12 *
13 *
14 * Copyright (c) 1994
15 * Hewlett-Packard Company
16 *
17 * Permission to use, copy, modify, distribute and sell this software
18 * and its documentation for any purpose is hereby granted without fee,
19 * provided that the above copyright notice appear in all copies and
20 * that both that copyright notice and this permission notice appear
21 * in supporting documentation. Hewlett-Packard Company makes no
22 * representations about the suitability of this software for any
23 * purpose. It is provided "as is" without express or implied warranty.
24 *
25 */
26
27 /* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
29 */
30
31 #ifndef _STLP_HASH_FUN_H
32 #define _STLP_HASH_FUN_H
33
34 #ifndef _STLP_INTERNAL_CSTDDEF
35 # include <stl/_cstddef.h>
36 #endif
37
38 _STLP_BEGIN_NAMESPACE
39
40 template <class _Key> struct hash { };
41
42 _STLP_MOVE_TO_PRIV_NAMESPACE
43
__stl_hash_string(const char * __s)44 inline size_t __stl_hash_string(const char* __s) {
45 _STLP_FIX_LITERAL_BUG(__s)
46 unsigned long __h = 0;
47 for ( ; *__s; ++__s)
48 __h = 5*__h + *__s;
49
50 return size_t(__h);
51 }
52
53 _STLP_MOVE_TO_STD_NAMESPACE
54
55 _STLP_TEMPLATE_NULL
56 struct hash<char*> {
57 size_t operator()(const char* __s) const {
58 _STLP_FIX_LITERAL_BUG(__s)
59 return _STLP_PRIV __stl_hash_string(__s);
60 }
61 };
62
63 _STLP_TEMPLATE_NULL
64 struct hash<const char*> {
65 size_t operator()(const char* __s) const {
66 _STLP_FIX_LITERAL_BUG(__s)
67 return _STLP_PRIV __stl_hash_string(__s);
68 }
69 };
70
71 _STLP_TEMPLATE_NULL struct hash<char> {
72 size_t operator()(char __x) const { return __x; }
73 };
74 _STLP_TEMPLATE_NULL struct hash<unsigned char> {
75 size_t operator()(unsigned char __x) const { return __x; }
76 };
77 #if !defined (_STLP_NO_SIGNED_BUILTINS)
78 _STLP_TEMPLATE_NULL struct hash<signed char> {
79 size_t operator()(unsigned char __x) const { return __x; }
80 };
81 #endif
82 _STLP_TEMPLATE_NULL struct hash<short> {
83 size_t operator()(short __x) const { return __x; }
84 };
85 _STLP_TEMPLATE_NULL struct hash<unsigned short> {
86 size_t operator()(unsigned short __x) const { return __x; }
87 };
88 _STLP_TEMPLATE_NULL struct hash<int> {
89 size_t operator()(int __x) const { return __x; }
90 };
91
92 #if !defined (_STLP_MSVC) || (_STLP_MSVC < 1300) || defined (_WIN64)
93 _STLP_TEMPLATE_NULL struct hash<unsigned int> {
94 size_t operator()(unsigned int __x) const { return __x; }
95 };
96 #else
97 /* MSVC .Net since 2002 has a 64 bits portability warning feature. typedef
98 * like size_t are tagged as potential 64 bits variables making them different from
99 * unsigned int. To avoid the warning when a hash container is instanciated with
100 * the size_t key we prefer to grant the size_t specialization rather than the
101 * unsigned int one.
102 */
103 _STLP_TEMPLATE_NULL struct hash<size_t> {
104 size_t operator()(size_t __x) const { return __x; }
105 };
106 #endif
107
108 _STLP_TEMPLATE_NULL struct hash<long> {
109 size_t operator()(long __x) const { return __x; }
110 };
111 _STLP_TEMPLATE_NULL struct hash<unsigned long> {
112 size_t operator()(unsigned long __x) const { return __x; }
113 };
114
115 #if defined (_STLP_LONG_LONG)
116 _STLP_TEMPLATE_NULL struct hash<_STLP_LONG_LONG> {
117 size_t operator()(_STLP_LONG_LONG x) const { return (size_t)x; }
118 };
119 _STLP_TEMPLATE_NULL struct hash<unsigned _STLP_LONG_LONG> {
120 size_t operator()(unsigned _STLP_LONG_LONG x) const { return (size_t)x; }
121 };
122 #endif
123
124 _STLP_TEMPLATE_NULL
125 struct hash<void *>
126 {
127 union __vp {
128 size_t s;
129 void *p;
130 };
131
132 size_t operator()(void *__x) const
133 {
134 __vp vp;
135 vp.p = __x;
136 return vp.s;
137 }
138 };
139
140 _STLP_END_NAMESPACE
141
142 #endif /* _STLP_HASH_FUN_H */
143
144 // Local Variables:
145 // mode:C++
146 // End:
147