00001 // Filename: stl_compares.I 00002 // Created by: drose (28Sep04) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 //////////////////////////////////////////////////////////////////// 00016 // Function: floating_point_threshold::Constructor 00017 // Access: Public 00018 // Description: 00019 //////////////////////////////////////////////////////////////////// 00020 template<class Key> 00021 INLINE floating_point_threshold<Key>:: 00022 floating_point_threshold(Key threshold) : 00023 _threshold(threshold) 00024 { 00025 } 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: floating_point_threshold::operator () 00029 // Access: Public 00030 // Description: Returns true if a sorts before b, false otherwise. 00031 //////////////////////////////////////////////////////////////////// 00032 template<class Key> 00033 INLINE bool floating_point_threshold<Key>:: 00034 operator () (const Key &a, const Key &b) const { 00035 return cfloor(a / _threshold + 0.5f) < cfloor(b / _threshold + 0.5f); 00036 } 00037 00038 //////////////////////////////////////////////////////////////////// 00039 // Function: compare_to::operator () 00040 // Access: Public 00041 // Description: Returns true if a sorts before b, false otherwise. 00042 //////////////////////////////////////////////////////////////////// 00043 template<class Key> 00044 INLINE bool compare_to<Key>:: 00045 operator () (const Key &a, const Key &b) const { 00046 return (a.compare_to(b) < 0); 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: compare_to::is_equal 00051 // Access: Public 00052 // Description: Returns true if a is equivalent to b, false otherwise. 00053 //////////////////////////////////////////////////////////////////// 00054 template<class Key> 00055 INLINE bool compare_to<Key>:: 00056 is_equal(const Key &a, const Key &b) const { 00057 return (a.compare_to(b) == 0); 00058 } 00059 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: indirect_less::operator () 00062 // Access: Public 00063 // Description: Returns true if a sorts before b, false otherwise. 00064 //////////////////////////////////////////////////////////////////// 00065 template<class Key> 00066 INLINE bool indirect_less<Key>:: 00067 operator () (const Key &a, const Key &b) const { 00068 return (a != b && (*a) < (*b)); 00069 } 00070 00071 //////////////////////////////////////////////////////////////////// 00072 // Function: indirect_compare_to::operator () 00073 // Access: Public 00074 // Description: Returns true if a sorts before b, false otherwise. 00075 //////////////////////////////////////////////////////////////////// 00076 template<class Key> 00077 INLINE bool indirect_compare_to<Key>:: 00078 operator () (const Key &a, const Key &b) const { 00079 return (a != b && (*a).compare_to(*b) < 0); 00080 } 00081 00082 //////////////////////////////////////////////////////////////////// 00083 // Function: indirect_compare_to::is_equal 00084 // Access: Public 00085 // Description: Returns true if a is equivalent to b, false otherwise. 00086 //////////////////////////////////////////////////////////////////// 00087 template<class Key> 00088 INLINE bool indirect_compare_to<Key>:: 00089 is_equal(const Key &a, const Key &b) const { 00090 return (a == b || (*a).compare_to(*b) == 0); 00091 } 00092 00093 //////////////////////////////////////////////////////////////////// 00094 // Function: indirect_compare_names::operator () 00095 // Access: Public 00096 // Description: Returns true if a sorts before b, false otherwise. 00097 //////////////////////////////////////////////////////////////////// 00098 template<class Key> 00099 INLINE bool indirect_compare_names<Key>:: 00100 operator () (const Key &a, const Key &b) const { 00101 return (a != b && (*a).get_name() < (*b).get_name()); 00102 } 00103 00104 //////////////////////////////////////////////////////////////////// 00105 // Function: indirect_compare_names::is_equal 00106 // Access: Public 00107 // Description: Returns true if a is equivalent to b, false otherwise. 00108 //////////////////////////////////////////////////////////////////// 00109 template<class Key> 00110 INLINE bool indirect_compare_names<Key>:: 00111 is_equal(const Key &a, const Key &b) const { 00112 return (a == b || (*a).get_name() == (*b).get_name()); 00113 } 00114 00115 //////////////////////////////////////////////////////////////////// 00116 // Function: integer_hash::add_hash 00117 // Access: Public, Static 00118 // Description: Adds the indicated key into a running hash. 00119 //////////////////////////////////////////////////////////////////// 00120 template<class Key, class Compare> 00121 INLINE size_t integer_hash<Key, Compare>:: 00122 add_hash(size_t hash, const Key &key) { 00123 PN_uint32 key32 = (PN_uint32)(key); 00124 return AddHash::add_hash(hash, &key32, 1); 00125 } 00126 00127 //////////////////////////////////////////////////////////////////// 00128 // Function: pointer_hash::add_hash 00129 // Access: Public, Static 00130 // Description: Adds the indicated key into a running hash. 00131 //////////////////////////////////////////////////////////////////// 00132 INLINE size_t pointer_hash:: 00133 add_hash(size_t hash, const void *key) { 00134 // We don't mind if this loses precision. 00135 PN_uint32 key32 = (PN_uint32)reinterpret_cast<unsigned long>(key); 00136 return AddHash::add_hash(hash, &key32, 1); 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: floating_point_hash::Constructor 00141 // Access: Public 00142 // Description: 00143 //////////////////////////////////////////////////////////////////// 00144 template<class Key> 00145 INLINE floating_point_hash<Key>:: 00146 floating_point_hash(Key threshold) : 00147 _threshold(threshold) 00148 { 00149 } 00150 00151 //////////////////////////////////////////////////////////////////// 00152 // Function: floating_point_hash::operator () 00153 // Access: Public 00154 // Description: Computes a size_t hash from the float. 00155 //////////////////////////////////////////////////////////////////// 00156 template<class Key> 00157 INLINE size_t floating_point_hash<Key>:: 00158 operator () (const Key &key) const { 00159 return add_hash(0, key); 00160 } 00161 00162 //////////////////////////////////////////////////////////////////// 00163 // Function: floating_point_hash::operator () (two parameters) 00164 // Access: Public 00165 // Description: Returns true if a sorts before b, false otherwise. 00166 //////////////////////////////////////////////////////////////////// 00167 template<class Key> 00168 INLINE bool floating_point_hash<Key>:: 00169 operator () (const Key &a, const Key &b) const { 00170 return cfloor(a / _threshold + 0.5f) < cfloor(b / _threshold + 0.5f); 00171 } 00172 00173 //////////////////////////////////////////////////////////////////// 00174 // Function: floating_point_hash::add_hash 00175 // Access: Public 00176 // Description: Adds the indicated key into a running hash. 00177 //////////////////////////////////////////////////////////////////// 00178 template<class Key> 00179 INLINE size_t floating_point_hash<Key>:: 00180 add_hash(size_t hash, const Key &key) const { 00181 PN_uint32 key32 = (PN_uint32)(key / _threshold + 0.5f); 00182 return AddHash::add_hash(hash, &key32, 1); 00183 } 00184 00185 //////////////////////////////////////////////////////////////////// 00186 // Function: sequence_hash::operator () 00187 // Access: Public 00188 // Description: Trivially computes a size_t hash from the components 00189 // of the string. 00190 //////////////////////////////////////////////////////////////////// 00191 template<class Key, class Compare> 00192 INLINE size_t sequence_hash<Key, Compare>:: 00193 operator () (const Key &key) const { 00194 return add_hash(0, key); 00195 } 00196 00197 //////////////////////////////////////////////////////////////////// 00198 // Function: sequence_hash::add_hash 00199 // Access: Public, Static 00200 // Description: Adds the elements of the indicated key into a running 00201 // hash. 00202 //////////////////////////////////////////////////////////////////// 00203 template<class Key, class Compare> 00204 INLINE size_t sequence_hash<Key, Compare>:: 00205 add_hash(size_t hash, const Key &key) { 00206 #ifdef _DEBUG 00207 // We assume that the sequence is laid out sequentially in memory. 00208 if (key.size() > 0) { 00209 assert(&key[key.size() - 1] - &key[0] == key.size() - 1); 00210 } 00211 #endif 00212 size_t num_bytes = (key.size() * sizeof(key[0])); 00213 return AddHash::add_hash(hash, (const PN_uint8 *)&key[0], num_bytes); 00214 } 00215 00216 //////////////////////////////////////////////////////////////////// 00217 // Function: method_hash::operator () 00218 // Access: Public 00219 // Description: Calls the Key's get_hash() method. 00220 //////////////////////////////////////////////////////////////////// 00221 template<class Key, class Compare> 00222 INLINE size_t method_hash<Key, Compare>:: 00223 operator () (const Key &key) const { 00224 return key.get_hash(); 00225 } 00226 00227 //////////////////////////////////////////////////////////////////// 00228 // Function: indirect_method_hash::operator () 00229 // Access: Public 00230 // Description: Calls the Key's get_hash() method. 00231 //////////////////////////////////////////////////////////////////// 00232 template<class Key, class Compare> 00233 INLINE size_t indirect_method_hash<Key, Compare>:: 00234 operator () (const Key &key) const { 00235 return (*key).get_hash(); 00236 }