-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.d
163 lines (137 loc) · 3.25 KB
/
hash.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
this module was copied from rt.util.hash.
*/
/**
* The console module contains a hash implementation.
*
* Copyright: Copyright Sean Kelly 2009 - 2009.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Sean Kelly
*/
/* Copyright Sean Kelly 2009 - 2009.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module xtk.hash;
import std.traits;
/**
*/
struct Hashable(T) if (is(T == struct))
{
T t;
alias t this;
this(A...)(A args)
{
t = T(args);
}
bool oEquals(ref const Hashable!T rhs) const
{
auto result = true;
foreach (i, __dummy; t.tupleof)
result = result && this.t.tupleof[i] == rhs.t.tupleof[i];
return result;
}
int opCmp(ref const Hashable!T rhs) const
{
foreach (i, __dummy; t.tupleof)
if (t.tupleof[i] != rhs.t.tupleof[i])
return t.tupleof[i] < rhs.t.tupleof[i] ? -1 : 1;
return 0;
}
hash_t toHash() const
{
hash_t hash = 0;
foreach (i, __dummy; t.tupleof)
{
hash = .getHash(t.tupleof[i], hash);
}
return hash;
}
}
/**
*/
hash_t getHash(T)(ref T t, hash_t seed = 0)
{
static if (isArray!T)
{
auto a = *(cast(void[]*)&t);
return .hashOf(a.ptr, a.length, seed);
}
else
{
static assert(0);
}
}
version( X86 )
version = AnyX86;
version( X86_64 )
version = AnyX86;
version( AnyX86 )
version = HasUnalignedOps;
hash_t hashOf( const (void)* buf, size_t len, hash_t seed = 0 )
{
/*
* This is Paul Hsieh's SuperFastHash algorithm, described here:
* http://www.azillionmonkeys.com/qed/hash.html
* It is protected by the following open source license:
* http://www.azillionmonkeys.com/qed/weblicense.html
*/
version( HasUnalignedOps )
{
static uint get16bits( const (ubyte)* x )
{
return *cast(ushort*) x;
}
}
else
{
static uint get16bits( const (ubyte)* x )
{
return ((cast(uint) x[1]) << 8) + (cast(uint) x[0]);
}
}
// NOTE: SuperFastHash normally starts with a zero hash value. The seed
// value was incorporated to allow chaining.
auto data = cast(const (ubyte)*) buf;
auto hash = seed;
int rem;
if( len <= 0 || data is null )
return 0;
rem = len & 3;
len >>= 2;
for( ; len > 0; len-- )
{
hash += get16bits( data );
auto tmp = (get16bits( data + 2 ) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
data += 2 * ushort.sizeof;
hash += hash >> 11;
}
switch( rem )
{
case 3: hash += get16bits( data );
hash ^= hash << 16;
hash ^= data[ushort.sizeof] << 18;
hash += hash >> 11;
break;
case 2: hash += get16bits( data );
hash ^= hash << 11;
hash += hash >> 17;
break;
case 1: hash += *data;
hash ^= hash << 10;
hash += hash >> 1;
break;
default:
break;
}
/* Force "avalanching" of final 127 bits */
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}