2013-07-20 21:15:54 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
2013-09-13 10:35:29 +00:00
|
|
|
#include "util/debug.h"
|
2014-09-30 00:54:17 +00:00
|
|
|
#include "util/int64.h"
|
2013-07-20 21:15:54 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
|
2013-07-22 11:03:46 +00:00
|
|
|
void mix(unsigned & a, unsigned & b, unsigned & c);
|
|
|
|
|
2013-07-24 18:00:29 +00:00
|
|
|
unsigned hash_str(unsigned len, char const * str, unsigned init_value);
|
2013-07-20 21:15:54 +00:00
|
|
|
|
|
|
|
inline unsigned hash(unsigned h1, unsigned h2) {
|
|
|
|
h2 -= h1; h2 ^= (h1 << 8);
|
|
|
|
h1 -= h2; h2 ^= (h1 << 16);
|
|
|
|
h2 -= h1; h2 ^= (h1 << 10);
|
|
|
|
return h2;
|
|
|
|
}
|
|
|
|
|
2014-09-30 00:54:17 +00:00
|
|
|
inline uint64 hash(uint64 h1, uint64 h2) {
|
|
|
|
h2 -= h1; h2 ^= (h1 << 16);
|
|
|
|
h1 -= h2; h2 ^= (h1 << 32);
|
|
|
|
h2 -= h1; h2 ^= (h1 << 20);
|
|
|
|
return h2;
|
|
|
|
}
|
|
|
|
|
2013-07-22 11:03:46 +00:00
|
|
|
template<typename H>
|
|
|
|
unsigned hash(unsigned n, H h, unsigned init_value = 31) {
|
|
|
|
unsigned a, b, c;
|
2014-07-02 16:56:50 +00:00
|
|
|
if (n == 0)
|
|
|
|
return init_value;
|
2013-07-22 11:03:46 +00:00
|
|
|
|
|
|
|
a = b = 0x9e3779b9;
|
|
|
|
c = 11;
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
a += init_value;
|
|
|
|
b = h(0);
|
|
|
|
mix(a, b, c);
|
|
|
|
return c;
|
|
|
|
case 2:
|
|
|
|
a += init_value;
|
|
|
|
b += h(0);
|
|
|
|
c += h(1);
|
|
|
|
mix(a, b, c);
|
|
|
|
return c;
|
|
|
|
case 3:
|
|
|
|
a += h(0);
|
|
|
|
b += h(1);
|
|
|
|
c += h(2);
|
|
|
|
mix(a, b, c);
|
|
|
|
a += init_value;
|
|
|
|
mix(a, b, c);
|
|
|
|
return c;
|
|
|
|
default:
|
|
|
|
while (n >= 3) {
|
|
|
|
n--;
|
|
|
|
a += h(n);
|
|
|
|
n--;
|
|
|
|
b += h(n);
|
|
|
|
n--;
|
|
|
|
c += h(n);
|
|
|
|
mix(a, b, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
a += init_value;
|
|
|
|
switch (n) {
|
|
|
|
case 2: b += h(1);
|
|
|
|
case 1: c += h(0);
|
|
|
|
}
|
|
|
|
mix(a, b, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
2013-07-20 21:15:54 +00:00
|
|
|
}
|