Changeset 13703 in webkit for trunk/JavaScriptCore/kxmlcore/HashTraits.h
- Timestamp:
- Apr 5, 2006, 2:19:57 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kxmlcore/HashTraits.h
r12301 r13703 24 24 #define KXMLCORE_HASH_TRAITS_H 25 25 26 #include " RefPtr.h"26 #include "HashFunctions.h" 27 27 #include <utility> 28 28 … … 30 30 31 31 using std::pair; 32 using std::make_pair; 32 33 33 template <typename T> struct IsInteger { static const bool value = false; }; 34 template <> struct IsInteger<bool> { static const bool value = true; }; 35 template <> struct IsInteger<char> { static const bool value = true; }; 36 template <> struct IsInteger<signed char> { static const bool value = true; }; 37 template <> struct IsInteger<unsigned char> { static const bool value = true; }; 38 template <> struct IsInteger<short> { static const bool value = true; }; 39 template <> struct IsInteger<unsigned short> { static const bool value = true; }; 40 template <> struct IsInteger<int> { static const bool value = true; }; 41 template <> struct IsInteger<unsigned int> { static const bool value = true; }; 42 template <> struct IsInteger<long> { static const bool value = true; }; 43 template <> struct IsInteger<unsigned long> { static const bool value = true; }; 44 template <> struct IsInteger<long long> { static const bool value = true; }; 45 template <> struct IsInteger<unsigned long long> { static const bool value = true; }; 34 template<typename T> struct IsInteger { static const bool value = false; }; 35 template<> struct IsInteger<bool> { static const bool value = true; }; 36 template<> struct IsInteger<char> { static const bool value = true; }; 37 template<> struct IsInteger<signed char> { static const bool value = true; }; 38 template<> struct IsInteger<unsigned char> { static const bool value = true; }; 39 template<> struct IsInteger<short> { static const bool value = true; }; 40 template<> struct IsInteger<unsigned short> { static const bool value = true; }; 41 template<> struct IsInteger<int> { static const bool value = true; }; 42 template<> struct IsInteger<unsigned int> { static const bool value = true; }; 43 template<> struct IsInteger<long> { static const bool value = true; }; 44 template<> struct IsInteger<unsigned long> { static const bool value = true; }; 45 template<> struct IsInteger<long long> { static const bool value = true; }; 46 template<> struct IsInteger<unsigned long long> { static const bool value = true; }; 47 48 template<typename T> struct HashTraits; 46 49 47 50 template<bool isInteger, typename T> struct GenericHashTraitsBase; 48 51 template<typename T> struct GenericHashTraitsBase<true, T> { 49 52 typedef T TraitType; 53 typedef HashTraits<typename IntTypes<sizeof(T)>::SignedType> StorageTraits; 50 54 static const bool emptyValueIsZero = true; 51 55 static const bool needsDestruction = false; 52 static TraitType emptyValue() { return 0; }53 56 }; 54 57 template<typename T> struct GenericHashTraitsBase<false, T> { 55 58 typedef T TraitType; 59 typedef HashTraits<T> StorageTraits; 56 60 static const bool emptyValueIsZero = false; 57 61 static const bool needsDestruction = true; 58 static TraitType emptyValue() { return TraitType(); }59 62 }; 60 63 61 template<typename T> struct GenericHashTraits : GenericHashTraitsBase<IsInteger<T>::value, T> { }; 64 template<typename T> struct GenericHashTraits : GenericHashTraitsBase<IsInteger<T>::value, T> { 65 static T emptyValue() { return T(); } 66 static const bool needsRef = false; 67 }; 62 68 63 69 template<typename T> struct HashTraits : GenericHashTraits<T> { }; 64 70 65 // may not be appropriate for all uses since it disallows 0 and -1 as keys 71 // signed integer traits may not be appropriate for all uses since they disallow 0 and -1 as keys 72 template<> struct HashTraits<signed char> : GenericHashTraits<int> { 73 static signed char deletedValue() { return -1; } 74 }; 75 template<> struct HashTraits<short> : GenericHashTraits<int> { 76 static short deletedValue() { return -1; } 77 }; 66 78 template<> struct HashTraits<int> : GenericHashTraits<int> { 67 static TraitType deletedValue() { return -1; } 79 static int deletedValue() { return -1; } 80 }; 81 template<> struct HashTraits<long> : GenericHashTraits<long> { 82 static long deletedValue() { return -1; } 83 }; 84 template<> struct HashTraits<long long> : GenericHashTraits<long long> { 85 static long deletedValue() { return -1; } 68 86 }; 69 87 70 88 template<typename P> struct HashTraits<P*> : GenericHashTraits<P*> { 71 typedef P* TraitType;89 typedef HashTraits<typename IntTypes<sizeof(P*)>::SignedType> StorageTraits; 72 90 static const bool emptyValueIsZero = true; 73 91 static const bool needsDestruction = false; 74 static TraitType emptyValue() { return 0; } 75 static TraitType deletedValue() { return reinterpret_cast<P*>(-1); } 92 static P* deletedValue() { return reinterpret_cast<P*>(-1); } 76 93 }; 77 94 78 95 template<typename P> struct HashTraits<RefPtr<P> > : GenericHashTraits<RefPtr<P> > { 96 typedef HashTraits<typename IntTypes<sizeof(P*)>::SignedType> StorageTraits; 79 97 static const bool emptyValueIsZero = true; 98 static const bool needsRef = true; 99 static void ref(const RefPtr<P>& p) { if (p) p->ref(); } 100 static void deref(const RefPtr<P>& p) { if (p) p->deref(); } 80 101 }; 81 102 82 template<typename T, typename Traits> class DeletedValueAssigner; 103 // template to set deleted values 104 105 template<typename Traits> struct DeletedValueAssigner { 106 static void assignDeletedValue(typename Traits::TraitType& location) { location = Traits::deletedValue(); } 107 }; 83 108 84 109 template<typename T, typename Traits> inline void assignDeleted(T& location) 85 110 { 86 DeletedValueAssigner<T , Traits>::assignDeletedValue(location);111 DeletedValueAssigner<Traits>::assignDeletedValue(location); 87 112 } 88 113 89 template<typename FirstTraits, typename SecondTraits> 90 struct PairHashTraits : GenericHashTraits<pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> > { 91 private: 92 typedef typename FirstTraits::TraitType FirstType; 93 typedef typename SecondTraits::TraitType SecondType; 94 public: 95 typedef pair<FirstType, SecondType> TraitType; 114 // special traits for pairs, helpful for their use in HashMap implementation 115 116 template<typename FirstTraits, typename SecondTraits> struct PairHashTraits; 117 118 template<typename FirstTraitsArg, typename SecondTraitsArg> 119 struct PairBaseHashTraits : GenericHashTraits<pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType> > { 120 typedef FirstTraitsArg FirstTraits; 121 typedef SecondTraitsArg SecondTraits; 122 typedef pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType; 123 124 typedef PairHashTraits<typename FirstTraits::StorageTraits, typename SecondTraits::StorageTraits> StorageTraits; 96 125 97 126 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; 98 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;99 127 100 128 static TraitType emptyValue() 101 129 { 102 return TraitType(FirstTraits::emptyValue(), SecondTraits::emptyValue());130 return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); 103 131 } 132 }; 133 134 template<typename FirstTraits, typename SecondTraits> 135 struct PairHashTraits : PairBaseHashTraits<FirstTraits, SecondTraits> { 136 typedef pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType; 137 138 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; 104 139 105 140 static TraitType deletedValue() … … 110 145 static void assignDeletedValue(TraitType& location) 111 146 { 112 assignDeleted< FirstType, FirstTraits>(location.first);147 assignDeleted<typename FirstTraits::TraitType, FirstTraits>(location.first); 113 148 location.second = SecondTraits::emptyValue(); 114 149 } … … 118 153 struct HashTraits<pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > { }; 119 154 120 template<typename T, typename Traits> struct DeletedValueAssigner {121 static void assignDeletedValue(T& location) { location = Traits::deletedValue(); }122 };123 124 155 template<typename FirstTraits, typename SecondTraits> 125 struct DeletedValueAssigner< pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType>,PairHashTraits<FirstTraits, SecondTraits> >156 struct DeletedValueAssigner<PairHashTraits<FirstTraits, SecondTraits> > 126 157 { 127 158 static void assignDeletedValue(pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType>& location) … … 132 163 133 164 template<typename First, typename Second> 134 struct DeletedValueAssigner< pair<First, Second>,HashTraits<pair<First, Second> > >165 struct DeletedValueAssigner<HashTraits<pair<First, Second> > > 135 166 { 136 167 static void assignDeletedValue(pair<First, Second>& location) … … 140 171 }; 141 172 173 // hassh functions and traits that are equivalent (for code sharing) 174 175 template<typename HashArg, typename TraitsArg> struct HashKeyStorageTraits { 176 typedef HashArg Hash; 177 typedef TraitsArg Traits; 178 }; 179 template<typename P> struct HashKeyStorageTraits<PtrHash<P*>, HashTraits<P*> > { 180 typedef typename IntTypes<sizeof(P*)>::SignedType IntType; 181 typedef IntHash<IntType> Hash; 182 typedef HashTraits<IntType> Traits; 183 }; 184 template<typename P> struct HashKeyStorageTraits<PtrHash<RefPtr<P> >, HashTraits<RefPtr<P> > > { 185 typedef typename IntTypes<sizeof(P*)>::SignedType IntType; 186 typedef IntHash<IntType> Hash; 187 typedef HashTraits<IntType> Traits; 188 }; 142 189 143 190 } // namespace KXMLCore
Note:
See TracChangeset
for help on using the changeset viewer.