Panda3D
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 
27 template<class Key, class Compare = std::less<Key> >
28 class stl_hash_compare : public stdext::hash_compare<Key, Compare> {
29 public:
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.
40 template<class Key, class Compare = std::less<Key> >
41 class stl_hash_compare : public Compare {
42 public:
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  */
59 template<class Key>
61 public:
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  */
72 template<class Key>
73 class compare_to {
74 public:
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  */
84 template<class Key>
86 public:
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  */
95 template<class Key>
97 public:
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  */
108 template<class Key>
110 public:
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  */
121 template<class Key, class Compare = std::less<Key> >
122 class integer_hash : public stl_hash_compare<Key, Compare> {
123 public:
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  */
131 class pointer_hash : public stl_hash_compare<const void *, std::less<const void *> > {
132 public:
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  */
139 template<class Key>
141 public:
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  */
153 template<class Key, class Compare = std::less<Key> >
154 class sequence_hash : public stl_hash_compare<Key, Compare> {
155 public:
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  */
167 template<class Key, class Compare = std::less<Key> >
168 class method_hash : public stl_hash_compare<Key, Compare> {
169 public:
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  */
181 template<class Key, class Compare>
182 class indirect_method_hash : public stl_hash_compare<Key, Compare> {
183 public:
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  */
198 template<class Key>
200 public:
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 
214 template<class Key>
215 class indirect_less_hash : public indirect_method_hash<Key, indirect_less<Key> > {
216 };
217 
218 template<class Key>
219 class indirect_compare_to_hash : public indirect_method_hash<Key, indirect_compare_to<Key> > {
220 };
221 
222 template<class Key>
223 class 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 ...
Definition: stl_compares.h:73
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
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:38
This hash_compare class hashes a float or a double.
Definition: stl_compares.h:140
size_t operator()(const Key &key) const
Computes a size_t hash from the float.
Definition: stl_compares.I:131
size_t add_hash(size_t start, const Key &key) const
Adds the indicated key into a running hash.
Definition: stl_compares.I:149
Compares two floating point numbers, within threshold of equivalence.
Definition: stl_compares.h:60
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:29
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
Definition: stl_compares.h:109
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:83
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
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
Definition: stl_compares.h:96
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:65
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
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
Definition: stl_compares.h:199
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
size_t operator()(const Key &key) const
Calls the Key's get_hash() method.
Definition: stl_compares.I:202
An STL function object class, this is intended to be used on any ordered collection of pointers to cl...
Definition: stl_compares.h:85
bool operator()(const Key &a, const Key &b) const
Returns true if a sorts before b, false otherwise.
Definition: stl_compares.I:56
This hash_compare class hashes a pointer to a class object.
Definition: stl_compares.h:182
size_t operator()(const Key &key) const
Calls the Key's get_hash() method.
Definition: stl_compares.I:193
This is the default hash_compare class, which assumes the Key is a size_t value or can be implicitly ...
Definition: stl_compares.h:122
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:101
This hash_compare class hashes a class object.
Definition: stl_compares.h:168
size_t operator()(const Key &key) const
Calls the Key's get_hash() method.
Definition: stl_compares.I:184
This is the default hash_compare class, which assumes the Key is a pointer value.
Definition: stl_compares.h:131
static size_t add_hash(size_t start, const void *key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:110
This hash_compare class hashes a string.
Definition: stl_compares.h:154
size_t operator()(const Key &key) const
Trivially computes a size_t hash from the components of the string.
Definition: stl_compares.I:159
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
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.