Panda3D
Loading...
Searching...
No Matches
stl_compares.h
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.h
10 * @author drose
11 * @date 2004-09-28
12 */
13
14#ifndef STL_COMPARES_H
15#define STL_COMPARES_H
16
17#include "dtoolbase.h"
18#include "cmath.h"
19#include "nearly_zero.h"
20#include "addHash.h"
21
22#include <assert.h>
23
24#ifdef HAVE_STL_HASH
25#include <hash_map> // for hash_compare
26
27template<class Key, class Compare = std::less<Key> >
28class stl_hash_compare : public stdext::hash_compare<Key, Compare> {
29public:
30 INLINE bool is_equal(const Key &a, const Key &b) const {
31 return !operator()(a, b) && !operator()(b, a);
32 }
33};
34
35#else
36
37#include <map> // for less
38
39// This is declared for the cases in which we don't have STL_HASH available.
40template<class Key, class Compare = std::less<Key> >
41class stl_hash_compare : public Compare {
42public:
43 INLINE size_t operator () (const Key &key) const {
44 return (size_t)key;
45 }
46 INLINE bool operator () (const Key &a, const Key &b) const {
47 return Compare::operator ()(a, b);
48 }
49 INLINE bool is_equal(const Key &a, const Key &b) const {
50 return !operator()(a, b) && !operator()(b, a);
51 }
52};
53
54#endif // HAVE_STL_HASH
55
56/**
57 * Compares two floating point numbers, within threshold of equivalence.
58 */
59template<class Key>
61public:
62 INLINE floating_point_threshold(Key threshold = get_nearly_zero_value((Key)0));
63 INLINE bool operator () (const Key &a, const Key &b) const;
64 const Key _threshold;
65};
66
67/**
68 * An STL function object class, this is intended to be used on any ordered
69 * collection of class objects that contain a compare_to() method. It defines
70 * the order of the objects via compare_to().
71 */
72template<class Key>
74public:
75 INLINE bool operator () (const Key &a, const Key &b) const;
76 INLINE bool is_equal(const Key &a, const Key &b) const;
77};
78
79/**
80 * An STL function object class, this is intended to be used on any ordered
81 * collection of pointers to classes that contain an operator <() method. It
82 * defines the order of the pointers via operator <().
83 */
84template<class Key>
86public:
87 INLINE bool operator () (const Key &a, const Key &b) const;
88};
89
90/**
91 * An STL function object class, this is intended to be used on any ordered
92 * collection of pointers to classes that contain a compare_to() method. It
93 * defines the order of the pointers via compare_to().
94 */
95template<class Key>
97public:
98 INLINE bool operator () (const Key &a, const Key &b) const;
99 INLINE bool is_equal(const Key &a, const Key &b) const;
100};
101
102/**
103 * An STL function object class, this is intended to be used on any ordered
104 * collection of pointers to classes that define a get_name() method,
105 * particularly for things that derive from Namable. It defines the order of
106 * the pointers by case-sensitive name comparison.
107 */
108template<class Key>
110public:
111 INLINE bool operator () (const Key &a, const Key &b) const;
112 INLINE bool is_equal(const Key &a, const Key &b) const;
113};
114
115/**
116 * This is the default hash_compare class, which assumes the Key is a size_t
117 * value or can be implicitly converted to a size_t value (for instance, via a
118 * size_t typecast operator). It is the same as the system-provided
119 * hash_compare.
120 */
121template<class Key, class Compare = std::less<Key> >
122class integer_hash : public stl_hash_compare<Key, Compare> {
123public:
124 INLINE static size_t add_hash(size_t start, const Key &key);
125};
126
127/**
128 * This is the default hash_compare class, which assumes the Key is a pointer
129 * value. It is the same as the system-provided hash_compare.
130 */
131class pointer_hash : public stl_hash_compare<const void *, std::less<const void *> > {
132public:
133 INLINE static size_t add_hash(size_t start, const void *key);
134};
135
136/**
137 * This hash_compare class hashes a float or a double.
138 */
139template<class Key>
141public:
142 INLINE floating_point_hash(Key threshold = get_nearly_zero_value((Key)0));
143 INLINE size_t operator () (const Key &key) const;
144 INLINE bool operator () (const Key &a, const Key &b) const;
145 INLINE size_t add_hash(size_t start, const Key &key) const;
146 const Key _threshold;
147};
148
149/**
150 * This hash_compare class hashes a string. It assumes the Key is a string or
151 * provides begin() and end() methods that iterate through Key::value_type.
152 */
153template<class Key, class Compare = std::less<Key> >
154class sequence_hash : public stl_hash_compare<Key, Compare> {
155public:
156 INLINE size_t operator () (const Key &key) const;
157 INLINE bool operator () (const Key &a, const Key &b) const {
159 }
160 INLINE static size_t add_hash(size_t start, const Key &key);
161};
162
163/**
164 * This hash_compare class hashes a class object. It assumes the Key provides
165 * a method called get_hash() that returns a size_t.
166 */
167template<class Key, class Compare = std::less<Key> >
168class method_hash : public stl_hash_compare<Key, Compare> {
169public:
170 INLINE size_t operator () (const Key &key) const;
171 INLINE bool operator () (const Key &a, const Key &b) const {
173 }
174};
175
176/**
177 * This hash_compare class hashes a pointer to a class object. It assumes the
178 * Key is a pointer to a class that provides a method called get_hash() that
179 * returns a size_t.
180 */
181template<class Key, class Compare>
182class indirect_method_hash : public stl_hash_compare<Key, Compare> {
183public:
184 INLINE size_t operator () (const Key &key) const;
185 INLINE bool operator () (const Key &a, const Key &b) const {
187 }
188};
189
190/**
191 * An STL function object class, this is intended to be used on any ordered
192 * collection of pointers to classes that contain an operator ==() method. It
193 * defines the equality of the pointers via operator ==().
194 *
195 * Since it doesn't define the ordering of the pointers, it can only be used
196 * with hash containers.
197 */
198template<class Key>
200public:
201 INLINE size_t operator () (const Key &key) const;
202 INLINE bool is_equal(const Key &a, const Key &b) const;
203};
204
205#include "stl_compares.I"
206
213
214template<class Key>
215class indirect_less_hash : public indirect_method_hash<Key, indirect_less<Key> > {
216};
217
218template<class Key>
219class indirect_compare_to_hash : public indirect_method_hash<Key, indirect_compare_to<Key> > {
220};
221
222template<class Key>
223class indirect_compare_names_hash : public indirect_method_hash<Key, indirect_compare_names<Key> > {
224};
225
226#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An STL function object class, this is intended to be used on any ordered collection of class objects ...
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.
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
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.
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
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.
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
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.
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
This hash_compare class hashes a pointer to a class object.
size_t operator()(const Key &key) const
Calls the Key's get_hash() method.
This is the default hash_compare class, which assumes the Key is a size_t value or can be implicitly ...
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
This hash_compare class hashes a class object.
size_t operator()(const Key &key) const
Calls the Key's get_hash() method.
This is the default hash_compare class, which assumes the Key is a pointer value.
static size_t add_hash(size_t start, const void *key)
Adds the indicated key into a running hash.
This hash_compare class hashes a string.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.