OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2009, Google Inc. | |
3 * All rights reserved. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions are | |
7 * met: | |
8 * | |
9 * * Redistributions of source code must retain the above copyright | |
10 * notice, this list of conditions and the following disclaimer. | |
11 * * Redistributions in binary form must reproduce the above | |
12 * copyright notice, this list of conditions and the following disclaimer | |
13 * in the documentation and/or other materials provided with the | |
14 * distribution. | |
15 * * Neither the name of Google Inc. nor the names of its | |
16 * contributors may be used to endorse or promote products derived from | |
17 * this software without specific prior written permission. | |
18 * | |
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 */ | |
31 | |
32 | |
33 // This file declares STL hash map classes in a cross-compiler way, providing a | |
34 // GCC-compatible (not MSVC) interface. | |
35 | |
36 #ifndef O3D_BASE_CROSS_STD_HASH_H_ | |
37 #define O3D_BASE_CROSS_STD_HASH_H_ | |
38 | |
39 #include <build/build_config.h> | |
40 | |
41 #if defined(COMPILER_MSVC) | |
42 #include <hash_map> | |
43 #include <hash_set> | |
44 namespace o3d { | |
45 namespace base { | |
46 | |
47 struct PortableHashBase { | |
48 // These two public members are required by msvc. 4 and 8 are the | |
49 // default values. | |
50 static const size_t bucket_size = 4; | |
51 static const size_t min_buckets = 8; | |
52 }; | |
53 | |
54 template <typename Key> | |
55 struct hash; | |
56 | |
57 // These are missing from MSVC. | |
58 template<> struct hash<int> { | |
59 size_t operator()(int n) const { | |
60 return static_cast<size_t>(n); | |
61 } | |
62 }; | |
63 | |
64 template<> struct hash<unsigned int> { | |
65 size_t operator()(unsigned int n) const { | |
66 return static_cast<size_t>(n); | |
67 } | |
68 }; | |
69 | |
70 template<typename T> struct hash<T*> { | |
71 size_t operator()(T* t) const { | |
72 return reinterpret_cast<size_t>(t); | |
73 } | |
74 }; | |
75 | |
76 // If the 3rd template parameter of the GNU interface (KeyEqual) is | |
77 // omitted, then we know that it's using the == operator, so we can | |
78 // safely use the < operator. | |
79 // | |
80 // If the third parameter is specified, then we get a compile time | |
81 // error, and we know we have to go back and add some #ifdefs. | |
82 template <typename Key, typename Hash> | |
83 struct HashAndLessOperator : PortableHashBase { | |
84 bool operator()(const Key& a, const Key& b) const { | |
85 return a < b; | |
86 } | |
87 size_t operator()(const Key& key) const { | |
88 return hasher_(key); | |
89 } | |
90 Hash hasher_; | |
91 }; | |
92 | |
93 template <class Key, class Hash = hash<Key> > | |
94 class hash_set : public stdext::hash_set<Key, HashAndLessOperator<Key, Hash> > { | |
95 public: | |
96 hash_set() {} | |
97 explicit hash_set(int buckets) {} | |
98 typedef std::equal_to<Key> key_equal; | |
99 size_type bucket_count() const { | |
100 return size() / bucket_size; | |
101 } | |
102 }; | |
103 | |
104 template <class Key, class Val, class Hash = hash<Key> > | |
105 class hash_map : public stdext::hash_map< | |
106 Key, Val, HashAndLessOperator<Key, Hash> > { | |
107 public: | |
108 hash_map() {} | |
109 explicit hash_map(int buckets) {} | |
110 typedef std::equal_to<Key> key_equal; | |
111 size_type bucket_count() const { | |
112 return size() / bucket_size; | |
113 } | |
114 }; | |
115 | |
116 template <class Key, class Hash = hash<Key> > | |
117 class hash_multiset : public stdext::hash_multiset< | |
118 Key, HashAndLessOperator<Key, Hash> > { | |
119 public: | |
120 hash_multiset() {} | |
121 explicit hash_multiset(int buckets) {} | |
122 typedef std::equal_to<Key> key_equal; | |
123 size_type bucket_count() const { | |
124 return size() / bucket_size; | |
125 } | |
126 }; | |
127 | |
128 template <class Key, class Val, class Hash = hash<Key> > | |
129 class hash_multimap : public stdext::hash_multimap< | |
130 Key, Val, HashAndLessOperator<Key, Hash> > { | |
131 public: | |
132 hash_multimap() {} | |
133 explicit hash_multimap(int buckets) {} | |
134 typedef std::equal_to<Key> key_equal; | |
135 size_type bucket_count() const { | |
136 return size() / bucket_size; | |
137 } | |
138 }; | |
139 | |
140 } // namespace base | |
141 } // namespace o3d | |
142 #elif defined COMPILER_GCC | |
143 #include <ext/hash_map> | |
144 #include <ext/hash_set> | |
145 #include <tr1/functional> | |
146 namespace __gnu_cxx { | |
147 template <class T> struct hash<T*> { | |
148 size_t operator() (const T* x) const { | |
149 return hash<size_t>()(reinterpret_cast<size_t>(x)); | |
150 } | |
151 }; | |
152 } // namespace __gnu_cxx | |
153 | |
154 namespace o3d { | |
155 namespace base { | |
156 using __gnu_cxx::hash_map; | |
157 using __gnu_cxx::hash_multimap; | |
158 using __gnu_cxx::hash_set; | |
159 using __gnu_cxx::hash_multiset; | |
160 using __gnu_cxx::hash; | |
161 } // namespace base | |
162 } // namespace o3d | |
163 #endif // COMPILER_MSVC | |
164 | |
165 #endif // O3D_BASE_CROSS_STD_HASH_H_ | |
OLD | NEW |