Changeset 13703 in webkit for trunk/JavaScriptCore/kxmlcore/HashFunctions.h
- Timestamp:
- Apr 5, 2006, 2:19:57 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kxmlcore/HashFunctions.h
r12366 r13703 2 2 /* 3 3 * This file is part of the KDE libraries 4 * Copyright (C) 2005 Apple Computer, Inc.4 * Copyright (C) 2005, 2006 Apple Computer, Inc. 5 5 * 6 6 * This library is free software; you can redistribute it and/or … … 24 24 #define KXMLCORE_HASH_FUNCTIONS_H 25 25 26 #include "RefPtr.h" 26 27 #include <stdint.h> 27 28 #include "RefPtr.h"29 28 30 29 namespace KXMLCore { 31 30 32 template<typename T> class DefaultHash; 31 template<size_t size> struct IntTypes; 32 template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; }; 33 template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; }; 34 template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; }; 35 template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; }; 33 36 34 template<int size> unsigned pointerHash(void *pointer);37 // integer hash function 35 38 36 // Thomas Wang's 32 bit mix 37 // http://www.cris.com/~Ttwang/tech/inthash.htm 38 template<> inline unsigned pointerHash<4>(void *pointer) 39 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm 40 inline unsigned intHash(uint32_t key) 39 41 { 40 uint32_t key = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(pointer));41 42 key += ~(key << 15); 42 43 key ^= (key >> 10); … … 48 49 } 49 50 50 // Thomas Wang's 64 bit mix 51 // http://www.cris.com/~Ttwang/tech/inthash.htm 52 template<> inline unsigned pointerHash<8>(void *pointer) 51 // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm 52 inline unsigned intHash(uint64_t key) 53 53 { 54 uint64_t key = reinterpret_cast<uint64_t>(pointer);55 54 key += ~(key << 32); 56 55 key ^= (key >> 22); … … 64 63 } 65 64 66 // pointer identity hash - default for pointer types that don't 67 // explicitly specialize otherwise; also should work for integer 68 // types 69 template<typename T> struct PtrHash { 70 static unsigned hash(T key) { return pointerHash<sizeof(void *)>((void *)key); } 65 template<typename T> struct IntHash { 66 static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); } 71 67 static bool equal(T a, T b) { return a == b; } 72 68 }; 73 69 70 // pointer identity hash function 71 72 template<typename T> struct PtrHash { 73 static unsigned hash(T key) { return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); } 74 static bool equal(T a, T b) { return a == b; } 75 }; 74 76 template<typename P> struct PtrHash<RefPtr<P> > { 75 static unsigned hash(const RefPtr<P>& key) { return pointerHash<sizeof(void *)>((void *)key.get()); }77 static unsigned hash(const RefPtr<P>& key) { return PtrHash<P*>::hash(key.get()); } 76 78 static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; } 77 79 }; 78 80 79 template<typename P> struct DefaultHash<P *> { 80 typedef PtrHash<P *> Hash; 81 }; 81 // default hash function for each type 82 82 83 template<> struct DefaultHash<int> { 84 typedef PtrHash<int> Hash; 85 }; 86 83 template<typename T> struct DefaultHash; 84 85 // make IntHash the default hash function for many integer types 86 87 template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; }; 88 template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; }; 89 template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; }; 90 template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; }; 91 template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; }; 92 template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; }; 93 94 // make PtrHash the default hash function for pointer types that don't specialize 95 96 template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; }; 97 template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; }; 98 87 99 } // namespace KXMLCore 88 100 89 101 using KXMLCore::DefaultHash; 102 using KXMLCore::IntHash; 90 103 using KXMLCore::PtrHash; 91 104
Note:
See TracChangeset
for help on using the changeset viewer.