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