HashFunctions.h 5.77 KB
Newer Older
mjs's avatar
mjs committed
1 2 3
// -*- mode: c++; c-basic-offset: 4 -*-
/*
 * This file is part of the KDE libraries
darin's avatar
darin committed
4
 * Copyright (C) 2005, 2006 Apple Computer, Inc.
mjs's avatar
mjs committed
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
staikos's avatar
staikos committed
18 19
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
mjs's avatar
mjs committed
20 21 22
 *
 */

mjs's avatar
mjs committed
23 24
#ifndef WTF_HashFunctions_h
#define WTF_HashFunctions_h
mjs's avatar
mjs committed
25

mjs's avatar
mjs committed
26
#include "RefPtr.h"
darin's avatar
darin committed
27
#include <stdint.h>
mjs's avatar
mjs committed
28

mjs's avatar
mjs committed
29
namespace WTF {
mjs's avatar
mjs committed
30

darin's avatar
darin committed
31 32 33 34 35
    template<size_t size> struct IntTypes;
    template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; };
    template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
    template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
    template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
mjs's avatar
mjs committed
36

darin's avatar
darin committed
37
    // integer hash function
mjs's avatar
mjs committed
38

aroben@apple.com's avatar
aroben@apple.com committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
    inline unsigned intHash(uint8_t key8)
    {
        unsigned key = key8;
        key += ~(key << 15);
        key ^= (key >> 10);
        key += (key << 3);
        key ^= (key >> 6);
        key += ~(key << 11);
        key ^= (key >> 16);
        return key;
    }

    // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
    inline unsigned intHash(uint16_t key16)
    {
        unsigned key = key16;
        key += ~(key << 15);
        key ^= (key >> 10);
        key += (key << 3);
        key ^= (key >> 6);
        key += ~(key << 11);
        key ^= (key >> 16);
        return key;
    }

darin's avatar
darin committed
65 66
    // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
    inline unsigned intHash(uint32_t key) 
mjs's avatar
mjs committed
67 68 69 70 71 72 73 74 75 76
    {
        key += ~(key << 15);
        key ^= (key >> 10);
        key += (key << 3);
        key ^= (key >> 6);
        key += ~(key << 11);
        key ^= (key >> 16);
        return key;
    }
    
darin's avatar
darin committed
77 78
    // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
    inline unsigned intHash(uint64_t key)
mjs's avatar
mjs committed
79 80 81 82 83 84 85 86 87
    {
        key += ~(key << 32);
        key ^= (key >> 22);
        key += ~(key << 13);
        key ^= (key >> 8);
        key += (key << 3);
        key ^= (key >> 15);
        key += ~(key << 27);
        key ^= (key >> 31);
darin's avatar
darin committed
88
        return static_cast<unsigned>(key);
mjs's avatar
mjs committed
89
    }
mjs's avatar
mjs committed
90

darin's avatar
darin committed
91 92
    template<typename T> struct IntHash {
        static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); }
mjs's avatar
mjs committed
93
        static bool equal(T a, T b) { return a == b; }
mjs's avatar
mjs committed
94
        static const bool safeToCompareToEmptyOrDeleted = true;
mjs's avatar
mjs committed
95
    };
mjs's avatar
mjs committed
96

oliver's avatar
oliver committed
97 98 99
    template<typename T> struct FloatHash {
        static unsigned hash(T key) { return intHash(*reinterpret_cast<typename IntTypes<sizeof(T)>::UnsignedType*>(&key)); }
        static bool equal(T a, T b) { return a == b; }
mjs's avatar
mjs committed
100
        static const bool safeToCompareToEmptyOrDeleted = true;
oliver's avatar
oliver committed
101 102
    };

darin's avatar
darin committed
103 104 105
    // pointer identity hash function

    template<typename T> struct PtrHash {
darin's avatar
darin committed
106 107 108 109 110 111 112 113 114 115 116
        static unsigned hash(T key)
        {
#if COMPILER(MSVC)
#pragma warning(push)
#pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's conversion warnings
#endif
            return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
#if COMPILER(MSVC)
#pragma warning(pop)
#endif
        }
darin's avatar
darin committed
117
        static bool equal(T a, T b) { return a == b; }
mjs's avatar
mjs committed
118
        static const bool safeToCompareToEmptyOrDeleted = true;
darin's avatar
darin committed
119
    };
mjs's avatar
mjs committed
120
    template<typename P> struct PtrHash<RefPtr<P> > {
darin's avatar
darin committed
121
        static unsigned hash(const RefPtr<P>& key) { return PtrHash<P*>::hash(key.get()); }
mjs's avatar
mjs committed
122
        static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
mjs's avatar
mjs committed
123
        static const bool safeToCompareToEmptyOrDeleted = true;
mjs's avatar
mjs committed
124
    };
mjs's avatar
mjs committed
125

darin's avatar
darin committed
126 127 128 129 130 131
    // default hash function for each type

    template<typename T> struct DefaultHash;

    // make IntHash the default hash function for many integer types

132 133
    template<> struct DefaultHash<short> { typedef IntHash<unsigned> Hash; };
    template<> struct DefaultHash<unsigned short> { typedef IntHash<unsigned> Hash; };
darin's avatar
darin committed
134 135 136 137 138 139 140
    template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; };
    template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; };
    template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; };
    template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; };
    template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
    template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; };

141 142 143 144
#if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    template<> struct DefaultHash<wchar_t> { typedef IntHash<wchar_t> Hash; };
#endif

oliver's avatar
oliver committed
145 146 147
    template<> struct DefaultHash<float> { typedef FloatHash<float> Hash; };
    template<> struct DefaultHash<double> { typedef FloatHash<double> Hash; };

darin's avatar
darin committed
148 149 150 151
    // make PtrHash the default hash function for pointer types that don't specialize

    template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
    template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
mjs's avatar
mjs committed
152

mjs's avatar
mjs committed
153
} // namespace WTF
mjs's avatar
mjs committed
154

mjs's avatar
mjs committed
155 156 157
using WTF::DefaultHash;
using WTF::IntHash;
using WTF::PtrHash;
mjs's avatar
mjs committed
158

mjs's avatar
mjs committed
159
#endif // WTF_HashFunctions_h