-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhash.h
67 lines (55 loc) · 1.15 KB
/
xhash.h
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
#pragma once
#ifndef _XHASH_H
#define _XHASH_H
#include"vector.h"
#include"list.h"
#include"string.h"
#include"utility.h"
namespace ministl
{
const size_t HASH_SEED = 0xdeadbeef;
template<class T> inline
size_t hash_seq(const T& val)
{
const size_t _FNV_offset_basis = 2166136261U;
const size_t _FNV_prime = 16777619U;
size_t ans = _FNV_offset_basis;
for (auto it = val.begin(); it != val.end(); it++)
{
ans ^= *it;
ans *= _FNV_prime;
}
return ans;
}
template<class T> inline
size_t hash_value(const T& val)
{
return ((size_t)val ^HASH_SEED);
}
template<class T> inline
size_t hash_value(const vector<T>& val)
{
return hash_seq(val);
}
template<class T> inline
size_t hash_value(const list<T>& val)
{
return hash_seq(val);
}
#ifdef _UTILITY_H
template<typename K, typename T> inline
size_t hash_value(const map_pair<K, T>& rhs)
{
return hash_value(rhs.first);
}
#endif // ! _UTILITY_H
template<typename T>
struct xhash
{
size_t operator() (const T& data, size_t bucket_size)
{
return hash_value(data) % bucket_size;
}
};
}
#endif