Panda3D
Loading...
Searching...
No Matches
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 */
17template<class Key>
19floating_point_threshold(Key threshold) :
20 _threshold(threshold)
21{
22}
23
24/**
25 * Returns true if a sorts before b, false otherwise.
26 */
27template<class Key>
29operator () (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 */
36template<class Key>
38operator () (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 */
45template<class Key>
47is_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 */
54template<class Key>
56operator () (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 */
63template<class Key>
65operator () (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 */
72template<class Key>
74is_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 */
81template<class Key>
83operator () (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 */
90template<class Key>
92is_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 */
99template<class Key, class Compare>
101add_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 */
109INLINE size_t pointer_hash::
110add_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 */
119template<class Key>
121floating_point_hash(Key threshold) :
122 _threshold(threshold)
123{
124}
125
126/**
127 * Computes a size_t hash from the float.
128 */
129template<class Key>
131operator () (const Key &key) const {
132 return add_hash(0, key);
133}
134
135/**
136 * Returns true if a sorts before b, false otherwise.
137 */
138template<class Key>
140operator () (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 */
147template<class Key>
149add_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 */
157template<class Key, class Compare>
159operator () (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 */
166template<class Key, class Compare>
168add_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 */
182template<class Key, class Compare>
184operator () (const Key &key) const {
185 return key.get_hash();
186}
187
188/**
189 * Calls the Key's get_hash() method.
190 */
191template<class Key, class Compare>
193operator () (const Key &key) const {
194 return (*key).get_hash();
195}
196
197/**
198 * Calls the Key's get_hash() method.
199 */
200template<class Key>
202operator () (const Key &key) const {
203 return (*key).get_hash();
204}
205
206/**
207 * Returns true if a is equal to b, false otherwise.
208 */
209template<class Key>
211is_equal(const Key &a, const Key &b) const {
212 return (a == b || (*a) == (*b));
213}
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
bool is_equal(const Key &a, const Key &b) const
Returns true if a is equivalent to b, false otherwise.
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
This hash_compare class hashes a float or a double.
size_t operator()(const Key &key) const
Computes a size_t hash from the float.
size_t add_hash(size_t start, const Key &key) const
Adds the indicated key into a running hash.
Compares two floating point numbers, within threshold of equivalence.
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
bool is_equal(const Key &a, const Key &b) const
Returns true if a is equivalent to b, false otherwise.
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
bool is_equal(const Key &a, const Key &b) const
Returns true if a is equivalent to b, false otherwise.
bool is_equal(const Key &a, const Key &b) const
Returns true if a is equal to b, false otherwise.
size_t operator()(const Key &key) const
Calls the Key's get_hash() method.
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
size_t operator()(const Key &key) const
Calls the Key's get_hash() method.
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
size_t operator()(const Key &key) const
Calls the Key's get_hash() method.
static size_t add_hash(size_t start, const void *key)
Adds the indicated key into a running hash.
size_t operator()(const Key &key) const
Trivially computes a size_t hash from the components of the string.
static size_t add_hash(size_t start, const Key &key)
Adds the elements of the indicated key into a running hash.