• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

WTF

  • sources
  • kde-4.14
  • kdelibs
  • kjs
  • wtf
HashFunctions.h
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 4 -*-
2 /*
3  * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #ifndef WTF_HashFunctions_h
23 #define WTF_HashFunctions_h
24 
25 #include "RefPtr.h"
26 #include <kjs/global.h>
27 #ifdef HAVE_STDINT_H
28 #include <stdint.h>
29 #endif
30 
31 namespace WTF {
32 
33  template<size_t size> struct IntTypes;
34  template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; };
35  template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
36  template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
37  template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
38 
39  // integer hash function
40 
41  // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
42  inline unsigned intHash(uint8_t key8)
43  {
44  unsigned key = key8;
45  key += ~(key << 15);
46  key ^= (key >> 10);
47  key += (key << 3);
48  key ^= (key >> 6);
49  key += ~(key << 11);
50  key ^= (key >> 16);
51  return key;
52  }
53 
54  // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
55  inline unsigned intHash(uint16_t key16)
56  {
57  unsigned key = key16;
58  key += ~(key << 15);
59  key ^= (key >> 10);
60  key += (key << 3);
61  key ^= (key >> 6);
62  key += ~(key << 11);
63  key ^= (key >> 16);
64  return key;
65  }
66 
67  // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
68  inline unsigned intHash(uint32_t key)
69  {
70  key += ~(key << 15);
71  key ^= (key >> 10);
72  key += (key << 3);
73  key ^= (key >> 6);
74  key += ~(key << 11);
75  key ^= (key >> 16);
76  return key;
77  }
78 
79  // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
80  inline unsigned intHash(uint64_t key)
81  {
82  key += ~(key << 32);
83  key ^= (key >> 22);
84  key += ~(key << 13);
85  key ^= (key >> 8);
86  key += (key << 3);
87  key ^= (key >> 15);
88  key += ~(key << 27);
89  key ^= (key >> 31);
90  return static_cast<unsigned>(key);
91  }
92 
93  template<typename T> struct IntHash {
94  static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); }
95  static bool equal(T a, T b) { return a == b; }
96  static const bool safeToCompareToEmptyOrDeleted = true;
97  };
98 
99  template<typename T> struct FloatHash {
100  static unsigned hash(T key) { return intHash(*reinterpret_cast<typename IntTypes<sizeof(T)>::UnsignedType*>(&key)); }
101  static bool equal(T a, T b) { return a == b; }
102  static const bool safeToCompareToEmptyOrDeleted = true;
103  };
104 
105  // pointer identity hash function
106 
107  template<typename T> struct PtrHash {
108  static unsigned hash(T key)
109  {
110 #if COMPILER(MSVC)
111 #pragma warning(push)
112 #pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's conversion warnings
113 #endif
114  return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
115 #if COMPILER(MSVC)
116 #pragma warning(pop)
117 #endif
118  }
119  static bool equal(T a, T b) { return a == b; }
120  static const bool safeToCompareToEmptyOrDeleted = true;
121  };
122  template<typename P> struct PtrHash<RefPtr<P> > : PtrHash<P*> {
123  using PtrHash<P*>::hash;
124  static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); }
125  using PtrHash<P*>::equal;
126  static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
127  static bool equal(P* a, const RefPtr<P>& b) { return a == b; }
128  static bool equal(const RefPtr<P>& a, P* b) { return a == b; }
129  };
130 
131  // default hash function for each type
132 
133  template<typename T> struct DefaultHash;
134 
135  template<typename T, typename U> struct PairHash {
136  static unsigned hash(const std::pair<T, U>& p)
137  {
138  return intHash((static_cast<uint64_t>(DefaultHash<T>::Hash::hash(p.first)) << 32 | DefaultHash<U>::Hash::hash(p.second)));
139  }
140  static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b)
141  {
142  return DefaultHash<T>::Hash::equal(a.first, b.first) && DefaultHash<U>::Hash::equal(a.second, b.second);
143  }
144  static const bool safeToCompareToEmptyOrDeleted = DefaultHash<T>::Hash::safeToCompareToEmptyOrDeleted
145  && DefaultHash<U>::Hash::safeToCompareToEmptyOrDeleted;
146  };
147 
148  // make IntHash the default hash function for many integer types
149 
150  template<> struct DefaultHash<short> { typedef IntHash<unsigned> Hash; };
151  template<> struct DefaultHash<unsigned short> { typedef IntHash<unsigned> Hash; };
152  template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; };
153  template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; };
154  template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; };
155  template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; };
156  template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
157  template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; };
158 
159 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
160  template<> struct DefaultHash<wchar_t> { typedef IntHash<wchar_t> Hash; };
161 #endif
162 
163  template<> struct DefaultHash<float> { typedef FloatHash<float> Hash; };
164  template<> struct DefaultHash<double> { typedef FloatHash<double> Hash; };
165 
166  // make PtrHash the default hash function for pointer types that don't specialize
167 
168  template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
169  template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
170 
171  template<typename T, typename U> struct DefaultHash<std::pair<T, U> > { typedef PairHash<T, U> Hash; };
172 
173 } // namespace WTF
174 
175 using WTF::DefaultHash;
176 using WTF::IntHash;
177 using WTF::PtrHash;
178 
179 #endif // WTF_HashFunctions_h
WTF::IntTypes< 2 >::SignedType
int16_t SignedType
Definition: HashFunctions.h:35
WTF::PairHash
Definition: HashFunctions.h:135
WTF::DefaultHash
Definition: HashFunctions.h:133
WTF::IntTypes< 1 >::UnsignedType
uint8_t UnsignedType
Definition: HashFunctions.h:34
WTF::FloatHash::safeToCompareToEmptyOrDeleted
static const bool safeToCompareToEmptyOrDeleted
Definition: HashFunctions.h:102
WTF::intHash
unsigned intHash(uint8_t key8)
Definition: HashFunctions.h:42
WTF::FloatHash
Definition: HashFunctions.h:99
WTF::DefaultHash< wchar_t >::Hash
IntHash< wchar_t > Hash
Definition: HashFunctions.h:160
WTF::DefaultHash< unsigned long long >::Hash
IntHash< unsigned long long > Hash
Definition: HashFunctions.h:157
WTF::DefaultHash< unsigned >::Hash
IntHash< unsigned > Hash
Definition: HashFunctions.h:153
WTF::IntHash::equal
static bool equal(T a, T b)
Definition: HashFunctions.h:95
WTF::PtrHash
Definition: HashFunctions.h:107
WTF::PtrHash::hash
static unsigned hash(T key)
Definition: HashFunctions.h:108
WTF::FloatHash::hash
static unsigned hash(T key)
Definition: HashFunctions.h:100
WTF::PairHash::equal
static bool equal(const std::pair< T, U > &a, const std::pair< T, U > &b)
Definition: HashFunctions.h:140
RefPtr.h
WTF::DefaultHash< int >::Hash
IntHash< unsigned > Hash
Definition: HashFunctions.h:152
WTF::RefPtr
Definition: Forward.h:31
WTF::DefaultHash< unsigned long >::Hash
IntHash< unsigned long > Hash
Definition: HashFunctions.h:155
WTF::IntTypes< 4 >::UnsignedType
uint32_t UnsignedType
Definition: HashFunctions.h:36
WTF::DefaultHash< std::pair< T, U > >::Hash
PairHash< T, U > Hash
Definition: HashFunctions.h:171
WTF::DefaultHash< unsigned short >::Hash
IntHash< unsigned > Hash
Definition: HashFunctions.h:151
WTF::DefaultHash< P * >::Hash
PtrHash< P * > Hash
Definition: HashFunctions.h:168
WTF::PtrHash< RefPtr< P > >::equal
static bool equal(const RefPtr< P > &a, const RefPtr< P > &b)
Definition: HashFunctions.h:126
WTF::FloatHash::equal
static bool equal(T a, T b)
Definition: HashFunctions.h:101
WTF::DefaultHash< float >::Hash
FloatHash< float > Hash
Definition: HashFunctions.h:163
WTF::DefaultHash< long long >::Hash
IntHash< unsigned long long > Hash
Definition: HashFunctions.h:156
WTF::PairHash::safeToCompareToEmptyOrDeleted
static const bool safeToCompareToEmptyOrDeleted
Definition: HashFunctions.h:144
WTF::PtrHash::safeToCompareToEmptyOrDeleted
static const bool safeToCompareToEmptyOrDeleted
Definition: HashFunctions.h:120
WTF::IntTypes
Definition: HashFunctions.h:33
WTF::IntHash::safeToCompareToEmptyOrDeleted
static const bool safeToCompareToEmptyOrDeleted
Definition: HashFunctions.h:96
WTF::PtrHash::equal
static bool equal(T a, T b)
Definition: HashFunctions.h:119
WTF::IntTypes< 1 >::SignedType
int8_t SignedType
Definition: HashFunctions.h:34
WTF::IntTypes< 4 >::SignedType
int32_t SignedType
Definition: HashFunctions.h:36
WTF::RefPtr::get
T * get() const
Definition: RefPtr.h:56
WTF::DefaultHash< short >::Hash
IntHash< unsigned > Hash
Definition: HashFunctions.h:150
WTF::PairHash::hash
static unsigned hash(const std::pair< T, U > &p)
Definition: HashFunctions.h:136
WTF::PtrHash< RefPtr< P > >::hash
static unsigned hash(const RefPtr< P > &key)
Definition: HashFunctions.h:124
WTF::IntTypes< 2 >::UnsignedType
uint16_t UnsignedType
Definition: HashFunctions.h:35
WTF::IntHash::hash
static unsigned hash(T key)
Definition: HashFunctions.h:94
WTF::PtrHash< RefPtr< P > >::equal
static bool equal(P *a, const RefPtr< P > &b)
Definition: HashFunctions.h:127
WTF::DefaultHash< long >::Hash
IntHash< unsigned long > Hash
Definition: HashFunctions.h:154
WTF::DefaultHash< RefPtr< P > >::Hash
PtrHash< RefPtr< P > > Hash
Definition: HashFunctions.h:169
WTF::IntHash
Definition: HashFunctions.h:93
WTF::IntTypes< 8 >::UnsignedType
uint64_t UnsignedType
Definition: HashFunctions.h:37
WTF::IntTypes< 8 >::SignedType
int64_t SignedType
Definition: HashFunctions.h:37
WTF::PtrHash< RefPtr< P > >::equal
static bool equal(const RefPtr< P > &a, P *b)
Definition: HashFunctions.h:128
WTF::DefaultHash< double >::Hash
FloatHash< double > Hash
Definition: HashFunctions.h:164
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:46 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

WTF

Skip menu "WTF"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal