Panda3D

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