Panda3D
stl_compares.I
1 // Filename: stl_compares.I
2 // Created by: drose (28Sep04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 ////////////////////////////////////////////////////////////////////
16 // Function: floating_point_threshold::Constructor
17 // Access: Public
18 // Description:
19 ////////////////////////////////////////////////////////////////////
20 template<class Key>
22 floating_point_threshold(Key threshold) :
23  _threshold(threshold)
24 {
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: floating_point_threshold::operator ()
29 // Access: Public
30 // Description: Returns true if a sorts before b, false otherwise.
31 ////////////////////////////////////////////////////////////////////
32 template<class Key>
34 operator () (const Key &a, const Key &b) const {
35  return cfloor(a / _threshold + 0.5f) < cfloor(b / _threshold + 0.5f);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: compare_to::operator ()
40 // Access: Public
41 // Description: Returns true if a sorts before b, false otherwise.
42 ////////////////////////////////////////////////////////////////////
43 template<class Key>
44 INLINE bool compare_to<Key>::
45 operator () (const Key &a, const Key &b) const {
46  return (a.compare_to(b) < 0);
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: compare_to::is_equal
51 // Access: Public
52 // Description: Returns true if a is equivalent to b, false otherwise.
53 ////////////////////////////////////////////////////////////////////
54 template<class Key>
55 INLINE bool compare_to<Key>::
56 is_equal(const Key &a, const Key &b) const {
57  return (a.compare_to(b) == 0);
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: indirect_less::operator ()
62 // Access: Public
63 // Description: Returns true if a sorts before b, false otherwise.
64 ////////////////////////////////////////////////////////////////////
65 template<class Key>
66 INLINE bool indirect_less<Key>::
67 operator () (const Key &a, const Key &b) const {
68  return (a != b && (*a) < (*b));
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: indirect_compare_to::operator ()
73 // Access: Public
74 // Description: Returns true if a sorts before b, false otherwise.
75 ////////////////////////////////////////////////////////////////////
76 template<class Key>
78 operator () (const Key &a, const Key &b) const {
79  return (a != b && (*a).compare_to(*b) < 0);
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: indirect_compare_to::is_equal
84 // Access: Public
85 // Description: Returns true if a is equivalent to b, false otherwise.
86 ////////////////////////////////////////////////////////////////////
87 template<class Key>
89 is_equal(const Key &a, const Key &b) const {
90  return (a == b || (*a).compare_to(*b) == 0);
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: indirect_compare_names::operator ()
95 // Access: Public
96 // Description: Returns true if a sorts before b, false otherwise.
97 ////////////////////////////////////////////////////////////////////
98 template<class Key>
100 operator () (const Key &a, const Key &b) const {
101  return (a != b && (*a).get_name() < (*b).get_name());
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: indirect_compare_names::is_equal
106 // Access: Public
107 // Description: Returns true if a is equivalent to b, false otherwise.
108 ////////////////////////////////////////////////////////////////////
109 template<class Key>
111 is_equal(const Key &a, const Key &b) const {
112  return (a == b || (*a).get_name() == (*b).get_name());
113 }
114 
115 ////////////////////////////////////////////////////////////////////
116 // Function: integer_hash::add_hash
117 // Access: Public, Static
118 // Description: Adds the indicated key into a running hash.
119 ////////////////////////////////////////////////////////////////////
120 template<class Key, class Compare>
121 INLINE size_t integer_hash<Key, Compare>::
122 add_hash(size_t hash, const Key &key) {
123  PN_uint32 key32 = (PN_uint32)(key);
124  return AddHash::add_hash(hash, &key32, 1);
125 }
126 
127 ////////////////////////////////////////////////////////////////////
128 // Function: pointer_hash::add_hash
129 // Access: Public, Static
130 // Description: Adds the indicated key into a running hash.
131 ////////////////////////////////////////////////////////////////////
132 INLINE size_t pointer_hash::
133 add_hash(size_t hash, const void *key) {
134  // We don't mind if this loses precision.
135  PN_uint32 key32 = (PN_uint32)reinterpret_cast<unsigned long>(key);
136  return AddHash::add_hash(hash, &key32, 1);
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: floating_point_hash::Constructor
141 // Access: Public
142 // Description:
143 ////////////////////////////////////////////////////////////////////
144 template<class Key>
146 floating_point_hash(Key threshold) :
147  _threshold(threshold)
148 {
149 }
150 
151 ////////////////////////////////////////////////////////////////////
152 // Function: floating_point_hash::operator ()
153 // Access: Public
154 // Description: Computes a size_t hash from the float.
155 ////////////////////////////////////////////////////////////////////
156 template<class Key>
157 INLINE size_t floating_point_hash<Key>::
158 operator () (const Key &key) const {
159  return add_hash(0, key);
160 }
161 
162 ////////////////////////////////////////////////////////////////////
163 // Function: floating_point_hash::operator () (two parameters)
164 // Access: Public
165 // Description: Returns true if a sorts before b, false otherwise.
166 ////////////////////////////////////////////////////////////////////
167 template<class Key>
168 INLINE bool floating_point_hash<Key>::
169 operator () (const Key &a, const Key &b) const {
170  return cfloor(a / _threshold + 0.5f) < cfloor(b / _threshold + 0.5f);
171 }
172 
173 ////////////////////////////////////////////////////////////////////
174 // Function: floating_point_hash::add_hash
175 // Access: Public
176 // Description: Adds the indicated key into a running hash.
177 ////////////////////////////////////////////////////////////////////
178 template<class Key>
179 INLINE size_t floating_point_hash<Key>::
180 add_hash(size_t hash, const Key &key) const {
181  PN_uint32 key32 = (PN_uint32)(key / _threshold + 0.5f);
182  return AddHash::add_hash(hash, &key32, 1);
183 }
184 
185 ////////////////////////////////////////////////////////////////////
186 // Function: sequence_hash::operator ()
187 // Access: Public
188 // Description: Trivially computes a size_t hash from the components
189 // of the string.
190 ////////////////////////////////////////////////////////////////////
191 template<class Key, class Compare>
192 INLINE size_t sequence_hash<Key, Compare>::
193 operator () (const Key &key) const {
194  return add_hash(0, key);
195 }
196 
197 ////////////////////////////////////////////////////////////////////
198 // Function: sequence_hash::add_hash
199 // Access: Public, Static
200 // Description: Adds the elements of the indicated key into a running
201 // hash.
202 ////////////////////////////////////////////////////////////////////
203 template<class Key, class Compare>
204 INLINE size_t sequence_hash<Key, Compare>::
205 add_hash(size_t hash, const Key &key) {
206 #ifdef _DEBUG
207  // We assume that the sequence is laid out sequentially in memory.
208  if (key.size() > 0) {
209  assert(&key[key.size() - 1] - &key[0] == key.size() - 1);
210  }
211 #endif
212  size_t num_bytes = (key.size() * sizeof(key[0]));
213  return AddHash::add_hash(hash, (const PN_uint8 *)&key[0], num_bytes);
214 }
215 
216 ////////////////////////////////////////////////////////////////////
217 // Function: method_hash::operator ()
218 // Access: Public
219 // Description: Calls the Key's get_hash() method.
220 ////////////////////////////////////////////////////////////////////
221 template<class Key, class Compare>
222 INLINE size_t method_hash<Key, Compare>::
223 operator () (const Key &key) const {
224  return key.get_hash();
225 }
226 
227 ////////////////////////////////////////////////////////////////////
228 // Function: indirect_method_hash::operator ()
229 // Access: Public
230 // Description: Calls the Key's get_hash() method.
231 ////////////////////////////////////////////////////////////////////
232 template<class Key, class Compare>
234 operator () (const Key &key) const {
235  return (*key).get_hash();
236 }
237 
238 ////////////////////////////////////////////////////////////////////
239 // Function: indirect_equals_hash::operator ()
240 // Access: Public
241 // Description: Calls the Key's get_hash() method.
242 ////////////////////////////////////////////////////////////////////
243 template<class Key>
244 INLINE size_t indirect_equals_hash<Key>::
245 operator () (const Key &key) const {
246  return (*key).get_hash();
247 }
248 
249 ////////////////////////////////////////////////////////////////////
250 // Function: indirect_equals_hash::is_equal
251 // Access: Public
252 // Description: Returns true if a is equal to b, false otherwise.
253 ////////////////////////////////////////////////////////////////////
254 template<class Key>
255 INLINE bool indirect_equals_hash<Key>::
256 is_equal(const Key &a, const Key &b) const {
257  return (a == b || (*a) == (*b));
258 }
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:34
static size_t add_hash(size_t start, const void *key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:133
size_t operator()(const Key &key) const
Calls the Key&#39;s get_hash() method.
Definition: stl_compares.I:223
bool is_equal(const Key &a, const Key &b) const
Returns true if a is equivalent to b, false otherwise.
Definition: stl_compares.I:111
size_t operator()(const Key &key) const
Calls the Key&#39;s get_hash() method.
Definition: stl_compares.I:234
size_t operator()(const Key &key) const
Calls the Key&#39;s get_hash() method.
Definition: stl_compares.I:245
static size_t add_hash(size_t start, const PN_uint32 *words, size_t num_words)
Adds a linear sequence of uint32 words to the hash.
Definition: addHash.I:22
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:122
This hash_compare class hashes a float or a double.
Definition: stl_compares.h:158
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:45
size_t operator()(const Key &key) const
Computes a size_t hash from the float.
Definition: stl_compares.I:158
bool is_equal(const Key &a, const Key &b) const
Returns true if a is equivalent to b, false otherwise.
Definition: stl_compares.I:56
size_t operator()(const Key &key) const
Trivially computes a size_t hash from the components of the string.
Definition: stl_compares.I:193
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:78
Compares two floating point numbers, within threshold of equivalence.
Definition: stl_compares.h:64
bool is_equal(const Key &a, const Key &b) const
Returns true if a is equivalent to b, false otherwise.
Definition: stl_compares.I:89
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:100
size_t add_hash(size_t start, const Key &key) const
Adds the indicated key into a running hash.
Definition: stl_compares.I:180
bool is_equal(const Key &a, const Key &b) const
Returns true if a is equal to b, false otherwise.
Definition: stl_compares.I:256
static size_t add_hash(size_t start, const Key &key)
Adds the elements of the indicated key into a running hash.
Definition: stl_compares.I:205
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:67