File: Hash.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (55 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (3)
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
#include "stdafx.h"
#include "Hash.h"
#include "Handle.h"

namespace storm {

	/**
	 * Inspired from http://burtleburtle.net/bob/hash/integer.html (public domain) and
	 * http://www.burtleburtle.net/bob/hash/doobs.html (also public domain)
	 */

	Nat byteHash(Byte v) {
		return natHash(Nat(v));
	}

	Nat intHash(Int v) {
		return natHash(Nat(v));
	}

	Nat natHash(Nat v) {
		v = (v ^ 0xDEADBEEF) + (v << 4);
		v = v ^ (v >> 10);
		v = v + (v << 7);
		v = v ^ (v >> 13);
		return v;
	}

	Nat longHash(Long v) {
		return wordHash(Word(v));
	}

	Nat wordHash(Word v) {
		v += ~(v << 32);
        v ^= (v >> 22);
        v += ~(v << 13);
        v ^= (v >> 8);
        v += (v << 3);
        v ^= (v >> 15);
        v += ~(v << 27);
        v ^= (v >> 31);
		return Nat(v);
	}

	Nat ptrHash(const void *v) {
		if (sizeof(v) == sizeof(Nat))
			return natHash(Nat(size_t(v)));
		else
			return wordHash(Word(size_t(v)));
	}

	void checkHashHandle(const Handle &h) {
		assert(h.hashFn && h.hasEqual(), L"Incomplete handle for use in a hash container.");
	}

}