/usr/lib/gcc/x86_64-linux-gnu/5/include/d/core/internal/hash.d is in libphobos-5-dev 5.5.0-12ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | /**
* Written in the D programming language.
* This module provides functions to uniform calculating hash values for different types
*
* Copyright: Copyright Igor Stepanov 2013-2013.
* License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Igor Stepanov
* Source: $(DRUNTIMESRC core/internal/_hash.d)
*/
module core.internal.hash;
import core.internal.convert;
//enum hash. CTFE depends on base type
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (is(T == enum))
{
static if (is(T EType == enum)) //for EType
{
EType e_val = cast(EType)val;
return hashOf(e_val, seed);
}
else
{
static assert(0);
}
}
//CTFE ready (depends on base type). Can be merged with dynamic array hash
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && __traits(isStaticArray, T))
{
size_t cur_hash = seed;
foreach (ref cur; val)
{
cur_hash = hashOf(cur, cur_hash);
}
return cur_hash;
}
//dynamic array hash
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && !is(T : typeof(null)) && is(T S: S[]) && !__traits(isStaticArray, T))
{
alias ElementType = typeof(val[0]);
static if (is(ElementType == interface) || is(ElementType == class) ||
((is(ElementType == struct) || is(ElementType == union))
&& is(typeof(val[0].toHash()) == size_t)))
//class or interface array or struct array with toHash(); CTFE depend on toHash() method
{
size_t hash = seed;
foreach (o; val)
{
hash = hashOf(o, hash);
}
return hash;
}
else static if (is(typeof(toUbyte(val)) == const(ubyte)[]))
//ubyteble array (arithmetic types and structs without toHash) CTFE ready for arithmetic types and structs without reference fields
{
auto bytes = toUbyte(val);
return bytesHash(bytes.ptr, bytes.length, seed);
}
else //Other types. CTFE unsupported
{
assert(!__ctfe, "unable to compute hash of "~T.stringof);
return bytesHash(val.ptr, ElementType.sizeof*val.length, seed);
}
}
//arithmetic type hash
@trusted nothrow pure
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && __traits(isArithmetic, T))
{
static if(__traits(isFloating, val))
{
T data = (val != val) ? T.nan : val;
auto bytes = toUbyte(data);
return bytesHash(bytes.ptr, bytes.length, seed);
}
else
{
auto bytes = toUbyte(val);
return bytesHash(bytes.ptr, bytes.length, seed);
}
}
//typeof(null) hash. CTFE supported
@trusted nothrow pure
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T : typeof(null)))
{
return hashOf(cast(void*)null);
}
//Pointers hash. CTFE unsupported if not null
@trusted nothrow pure
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T V : V*) && !is(T : typeof(null)))
{
if(__ctfe)
{
if(val is null)
{
return hashOf(cast(size_t)0);
}
else
{
assert(0, "Unable to calculate hash of non-null pointer at compile time");
}
}
return hashOf(cast(size_t)val);
}
//struct or union hash
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && (is(T == struct) || is(T == union)))
{
static if (is(typeof(val.toHash()) == size_t)) //CTFE depends on toHash()
{
return hashOf(val.toHash(), seed);
}
else
{
static if(__traits(hasMember, T, "toHash") && is(typeof(T.toHash) == function))
{
pragma(msg, "Warning: struct "~__traits(identifier, T)~" has method toHash, however it cannot be called with "~T.stringof~" this.");
}
static if (is(typeof(toUbyte(val)) == const(ubyte)[]))//CTFE ready for structs without reference fields
{
auto bytes = toUbyte(val);
return bytesHash(bytes.ptr, bytes.length, seed);
}
else // CTFE unsupproreted for structs with reference fields
{
assert(!__ctfe, "unable to compute hash of "~T.stringof);
const(ubyte)[] bytes = (cast(const(ubyte)*)&val)[0 .. T.sizeof];
return bytesHash(bytes.ptr, bytes.length, seed);
}
}
}
//delegate hash. CTFE unsupported
@trusted nothrow pure
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T == delegate))
{
assert(!__ctfe, "unable to compute hash of "~T.stringof);
const(ubyte)[] bytes = (cast(const(ubyte)*)&val)[0 .. T.sizeof];
return bytesHash(bytes.ptr, bytes.length, seed);
}
//class or interface hash. CTFE depends on toHash
size_t hashOf(T)(auto ref T val, size_t seed = 0) if (!is(T == enum) && is(T == interface) || is(T == class))
{
return hashOf(val ? (cast(Object)val).toHash() : 0, seed);
}
//associative array hash. CTFE depends on base types
size_t hashOf(T)(auto ref T aa, size_t seed = 0) if (!is(T == enum) && __traits(isAssociativeArray, T))
{
if (!aa.length) return hashOf(0, seed);
size_t h = 0;
// The computed hash is independent of the foreach traversal order.
foreach (key, ref val; aa)
{
size_t[2] hpair;
hpair[0] = key.hashOf();
hpair[1] = val.hashOf();
h ^= hpair.hashOf();
}
return h.hashOf(seed);
}
unittest
{
static struct Foo
{
int a = 99;
float b = 4.0;
size_t toHash() const pure @safe nothrow
{
return a;
}
}
static struct Bar
{
char c = 'x';
int a = 99;
float b = 4.0;
void* d = null;
}
static struct Boom
{
char c = 'M';
int* a = null;
}
interface IBoo
{
void boo();
}
static class Boo: IBoo
{
override void boo()
{
}
override size_t toHash()
{
return 1;
}
}
static struct Goo
{
size_t toHash() pure @safe nothrow
{
return 1;
}
}
enum Gun: long
{
A = 99,
B = 17
}
enum double dexpr = 3.14;
enum float fexpr = 2.71;
enum wstring wsexpr = "abcdef"w;
enum string csexpr = "abcdef";
enum int iexpr = 7;
enum long lexpr = 42;
enum int[2][3] saexpr = [[1, 2], [3, 4], [5, 6]];
enum int[] daexpr = [7,8,9];
enum Foo thsexpr = Foo();
enum Bar vsexpr = Bar();
enum int[int] aaexpr = [99:2, 12:6, 45:4];
enum Gun eexpr = Gun.A;
enum cdouble cexpr = 7+4i;
enum Foo[] staexpr = [Foo(), Foo(), Foo()];
enum Bar[] vsaexpr = [Bar(), Bar(), Bar()];
enum realexpr = 7.88;
enum raexpr = [8.99L+86i, 3.12L+99i, 5.66L+12i];
enum nullexpr = null;
//No CTFE:
Boom rstructexpr = Boom();
Boom[] rstrarrexpr = [Boom(), Boom(), Boom()];
int delegate() dgexpr = (){return 78;};
void* ptrexpr = &dgexpr;
//CTFE hashes
enum h1 = dexpr.hashOf();
enum h2 = fexpr.hashOf();
enum h3 = wsexpr.hashOf();
enum h4 = csexpr.hashOf();
enum h5 = iexpr.hashOf();
enum h6 = lexpr.hashOf();
enum h7 = saexpr.hashOf();
enum h8 = daexpr.hashOf();
enum h9 = thsexpr.hashOf();
enum h10 = vsexpr.hashOf();
enum h11 = aaexpr.hashOf();
enum h12 = eexpr.hashOf();
enum h13 = cexpr.hashOf();
enum h14 = hashOf(new Boo);
enum h15 = staexpr.hashOf();
enum h16 = hashOf([new Boo, new Boo, new Boo]);
enum h17 = hashOf([cast(IBoo)new Boo, cast(IBoo)new Boo, cast(IBoo)new Boo]);
enum h18 = hashOf(cast(IBoo)new Boo);
enum h19 = vsaexpr.hashOf();
enum h20 = hashOf(cast(Foo[3])staexpr);
//BUG: cannot cast [Boo(), Boo(), Boo()][0] to object.Object at compile time
auto h21 = hashOf(cast(Boo[3])[new Boo, new Boo, new Boo]);
auto h22 = hashOf(cast(IBoo[3])[cast(IBoo)new Boo, cast(IBoo)new Boo, cast(IBoo)new Boo]);
enum h23 = hashOf(cast(Bar[3])vsaexpr);
//NO CTFE (Compute, but don't check correctness):
auto h24 = rstructexpr.hashOf();
auto h25 = rstrarrexpr.hashOf();
auto h26 = dgexpr.hashOf();
auto h27 = ptrexpr.hashOf();
enum h28 = realexpr.hashOf();
enum h29 = raexpr.hashOf();
enum h30 = nullexpr.hashOf();
auto v1 = dexpr;
auto v2 = fexpr;
auto v3 = wsexpr;
auto v4 = csexpr;
auto v5 = iexpr;
auto v6 = lexpr;
auto v7 = saexpr;
auto v8 = daexpr;
auto v9 = thsexpr;
auto v10 = vsexpr;
auto v11 = aaexpr;
auto v12 = eexpr;
auto v13 = cexpr;
auto v14 = new Boo;
auto v15 = staexpr;
auto v16 = [new Boo, new Boo, new Boo];
auto v17 = [cast(IBoo)new Boo, cast(IBoo)new Boo, cast(IBoo)new Boo];
auto v18 = cast(IBoo)new Boo;
auto v19 = vsaexpr;
auto v20 = cast(Foo[3])staexpr;
auto v21 = cast(Boo[3])[new Boo, new Boo, new Boo];
auto v22 = cast(IBoo[3])[cast(IBoo)new Boo, cast(IBoo)new Boo, cast(IBoo)new Boo];
auto v23 = cast(Bar[3])vsaexpr;
auto v30 = null;
//NO CTFE:
/*auto v24 = rstructexpr;
auto v25 = rstrarrexpr;
auto v26 = dgexpr;
auto v27 = ptrexpr;
auto v28 = realexpr;
auto v29 = raexpr;*/
//runtime hashes
auto rth1 = hashOf(v1);
auto rth2 = hashOf(v2);
auto rth3 = hashOf(v3);
auto rth4 = hashOf(v4);
auto rth5 = hashOf(v5);
auto rth6 = hashOf(v6);
auto rth7 = hashOf(v7);
auto rth8 = hashOf(v8);
auto rth9 = hashOf(v9);
auto rth10 = hashOf(v10);
auto rth11 = hashOf(v11);
auto rth12 = hashOf(v12);
auto rth13 = hashOf(v13);
auto rth14 = hashOf(v14);
auto rth15 = hashOf(v15);
auto rth16 = hashOf(v16);
auto rth17 = hashOf(v17);
auto rth18 = hashOf(v18);
auto rth19 = hashOf(v19);
auto rth20 = hashOf(v20);
auto rth21 = hashOf(v21);
auto rth22 = hashOf(v22);
auto rth23 = hashOf(v23);
auto rth30 = hashOf(v30);
/*//NO CTFE:
auto rth24 = hashOf(v24);
auto rth25 = hashOf(v25);
auto rth26 = hashOf(v26);
auto rth27 = hashOf(v27);
auto rth28 = hashOf(v28);
auto rth29 = hashOf(v29);*/
assert(h1 == rth1);
assert(h2 == rth2);
assert(h3 == rth3);
assert(h4 == rth4);
assert(h5 == rth5);
assert(h6 == rth6);
assert(h7 == rth7);
assert(h8 == rth8);
assert(h9 == rth9);
assert(h10 == rth10);
assert(h11 == rth11);
assert(h12 == rth12);
assert(h13 == rth13);
assert(h14 == rth14);
assert(h15 == rth15);
assert(h16 == rth16);
assert(h17 == rth17);
assert(h18 == rth18);
assert(h19 == rth19);
assert(h20 == rth20);
assert(h21 == rth21);
assert(h22 == rth22);
assert(h23 == rth23);
/*assert(h24 == rth24);
assert(h25 == rth25);
assert(h26 == rth26);
assert(h27 == rth27);
assert(h28 == rth28);
assert(h29 == rth29);*/
assert(h30 == rth30);
}
// MurmurHash3 was written by Austin Appleby, and is placed in the public
// domain. The author hereby disclaims copyright to this source code.
version(X86)
version = AnyX86;
version(X86_64)
version = AnyX86;
version(AnyX86)
{
version(DigitalMars)
{
}
else
{
version = HasUnalignedOps;
}
}
@trusted pure nothrow
size_t bytesHash(const(void)* buf, size_t len, size_t seed = 0)
{
static uint rotl32(uint n)(in uint x) pure nothrow @safe
{
return (x << n) | (x >> (32 - n));
}
//-----------------------------------------------------------------------------
// Block read - if your platform needs to do endian-swapping or can only
// handle aligned reads, do the conversion here
static uint get32bits(const (ubyte)* x) pure nothrow
{
//Compiler can optimize this code to simple *cast(uint*)x if it possible.
version(HasUnalignedOps)
{
if (!__ctfe)
return *cast(uint*)x; //BUG: Can't be inlined by DMD
}
version(BigEndian)
{
return ((cast(uint) x[0]) << 24) | ((cast(uint) x[1]) << 16) | ((cast(uint) x[2]) << 8) | (cast(uint) x[3]);
}
else
{
return ((cast(uint) x[3]) << 24) | ((cast(uint) x[2]) << 16) | ((cast(uint) x[1]) << 8) | (cast(uint) x[0]);
}
}
//-----------------------------------------------------------------------------
// Finalization mix - force all bits of a hash block to avalanche
static uint fmix32(uint h) pure nothrow @safe
{
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
auto data = cast(const(ubyte)*)buf;
auto nblocks = len / 4;
uint h1 = cast(uint)seed;
enum uint c1 = 0xcc9e2d51;
enum uint c2 = 0x1b873593;
enum uint c3 = 0xe6546b64;
//----------
// body
auto end_data = data+nblocks*uint.sizeof;
for(; data!=end_data; data += uint.sizeof)
{
uint k1 = get32bits(data);
k1 *= c1;
k1 = rotl32!15(k1);
k1 *= c2;
h1 ^= k1;
h1 = rotl32!13(h1);
h1 = h1*5+c3;
}
//----------
// tail
uint k1 = 0;
switch(len & 3)
{
case 3: k1 ^= data[2] << 16; goto case;
case 2: k1 ^= data[1] << 8; goto case;
case 1: k1 ^= data[0];
k1 *= c1; k1 = rotl32!15(k1); k1 *= c2; h1 ^= k1;
goto default;
default:
}
//----------
// finalization
h1 ^= len;
h1 = fmix32(h1);
return h1;
}
// Check that bytesHash works with CTFE
unittest
{
size_t ctfeHash(string x)
{
return bytesHash(x.ptr, x.length);
}
enum test_str = "Sample string";
enum size_t hashVal = ctfeHash(test_str);
assert(hashVal == bytesHash(test_str.ptr, test_str.length));
}
|