00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef STL_COMPARES_H
00016 #define STL_COMPARES_H
00017
00018 #include "dtoolbase.h"
00019 #include "cmath.h"
00020 #include "nearly_zero.h"
00021 #include "addHash.h"
00022
00023 #include <assert.h>
00024
00025 #ifdef HAVE_STL_HASH
00026 #include <hash_map>
00027
00028 template<class Key, class Compare = less<Key> >
00029 class stl_hash_compare : public stdext::hash_compare<Key, Compare> {
00030 public:
00031 INLINE bool is_equal(const Key &a, const Key &b) const {
00032 return !operator()(a, b) && !operator()(b, a);
00033 }
00034 };
00035
00036
00037 #else
00038
00039 #include <map>
00040
00041
00042
00043 template<class Key, class Compare = less<Key> >
00044 class stl_hash_compare : public Compare {
00045 public:
00046 INLINE size_t operator () (const Key &key) const {
00047 return (size_t)key;
00048 }
00049 INLINE bool operator () (const Key &a, const Key &b) const {
00050 return Compare::operator ()(a, b);
00051 }
00052 INLINE bool is_equal(const Key &a, const Key &b) const {
00053 return !operator()(a, b) && !operator()(b, a);
00054 }
00055 };
00056
00057 #endif // HAVE_STL_HASH
00058
00059
00060
00061
00062
00063
00064 template<class Key>
00065 class floating_point_threshold {
00066 public:
00067 INLINE floating_point_threshold(Key threshold = get_nearly_zero_value((Key)0));
00068 INLINE bool operator () (const Key &a, const Key &b) const;
00069 const Key _threshold;
00070 };
00071
00072
00073
00074
00075
00076
00077
00078
00079 template<class Key>
00080 class compare_to {
00081 public:
00082 INLINE bool operator () (const Key &a, const Key &b) const;
00083 INLINE bool is_equal(const Key &a, const Key &b) const;
00084 };
00085
00086
00087
00088
00089
00090
00091
00092
00093 template<class Key>
00094 class indirect_less {
00095 public:
00096 INLINE bool operator () (const Key &a, const Key &b) const;
00097 };
00098
00099
00100
00101
00102
00103
00104
00105
00106 template<class Key>
00107 class indirect_compare_to {
00108 public:
00109 INLINE bool operator () (const Key &a, const Key &b) const;
00110 INLINE bool is_equal(const Key &a, const Key &b) const;
00111 };
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 template<class Key>
00123 class indirect_compare_names {
00124 public:
00125 INLINE bool operator () (const Key &a, const Key &b) const;
00126 INLINE bool is_equal(const Key &a, const Key &b) const;
00127 };
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 template<class Key, class Compare = less<Key> >
00138 class integer_hash : public stl_hash_compare<Key, Compare> {
00139 public:
00140 INLINE static size_t add_hash(size_t start, const Key &key);
00141 };
00142
00143
00144
00145
00146
00147
00148
00149 class pointer_hash : public stl_hash_compare<const void *, less<const void *> > {
00150 public:
00151 INLINE static size_t add_hash(size_t start, const void *key);
00152 };
00153
00154
00155
00156
00157
00158 template<class Key>
00159 class floating_point_hash : public stl_hash_compare<Key> {
00160 public:
00161 INLINE floating_point_hash(Key threshold = get_nearly_zero_value((Key)0));
00162 INLINE size_t operator () (const Key &key) const;
00163 INLINE bool operator () (const Key &a, const Key &b) const;
00164 INLINE size_t add_hash(size_t start, const Key &key) const;
00165 const Key _threshold;
00166 };
00167
00168
00169
00170
00171
00172
00173
00174 template<class Key, class Compare = less<Key> >
00175 class sequence_hash : public stl_hash_compare<Key, Compare> {
00176 public:
00177 INLINE size_t operator () (const Key &key) const;
00178 INLINE bool operator () (const Key &a, const Key &b) const {
00179 return stl_hash_compare<Key, Compare>::operator () (a, b);
00180 }
00181 INLINE static size_t add_hash(size_t start, const Key &key);
00182 };
00183
00184
00185
00186
00187
00188
00189
00190 template<class Key, class Compare = less<Key> >
00191 class method_hash : public stl_hash_compare<Key, Compare> {
00192 public:
00193 INLINE size_t operator () (const Key &key) const;
00194 INLINE bool operator () (const Key &a, const Key &b) const {
00195 return stl_hash_compare<Key, Compare>::operator () (a, b);
00196 }
00197 };
00198
00199
00200
00201
00202
00203
00204
00205
00206 template<class Key, class Compare>
00207 class indirect_method_hash : public stl_hash_compare<Key, Compare> {
00208 public:
00209 INLINE size_t operator () (const Key &key) const;
00210 INLINE bool operator () (const Key &a, const Key &b) const {
00211 return stl_hash_compare<Key, Compare>::operator () (a, b);
00212 }
00213 };
00214
00215 #include "stl_compares.I"
00216
00217 typedef floating_point_hash<float> float_hash;
00218 typedef floating_point_hash<double> double_hash;
00219 typedef integer_hash<int> int_hash;
00220 typedef integer_hash<size_t> size_t_hash;
00221 typedef sequence_hash<string> string_hash;
00222 typedef sequence_hash<wstring> wstring_hash;
00223
00224 template<class Key>
00225 class indirect_less_hash : public indirect_method_hash<Key, indirect_less<Key> > {
00226 };
00227
00228 template<class Key>
00229 class indirect_compare_to_hash : public indirect_method_hash<Key, indirect_compare_to<Key> > {
00230 };
00231
00232 template<class Key>
00233 class indirect_compare_names_hash : public indirect_method_hash<Key, indirect_compare_names<Key> > {
00234 };
00235
00236 #endif
00237
00238