forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashcode.hh
119 lines (100 loc) · 3.14 KB
/
hashcode.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef CLICK_HASHCODE_HH
#define CLICK_HASHCODE_HH
#include <stddef.h>
#include <inttypes.h>
#if HAVE_STD_HASH
#include <functional>
#endif
// Notes about the hashcode template: On GCC 4.3.0, "template <>" is required
// on the specializations or they aren't used. Just plain overloaded
// functions aren't used. The specializations must be e.g. "const char &",
// not "char", or GCC complains about a specialization not matching the
// general template. The main template takes a const reference for two
// reasons. First, providing both "hashcode_t hashcode(T)" and "hashcode_t
// hashcode(const T&)" leads to ambiguity errors. Second, providing only
// "hashcode_t hashcode(T)" is slower by looks like 8% when T is a String,
// because of copy constructors; for types with more expensive non-default
// copy constructors this would probably be worse.
typedef size_t hashcode_t; ///< Typical type for a hashcode() value.
template <typename T>
inline hashcode_t hashcode(T const &x) {
return x.hashcode();
}
template <>
inline hashcode_t hashcode(char const &x) {
return x;
}
template <>
inline hashcode_t hashcode(signed char const &x) {
return x;
}
template <>
inline hashcode_t hashcode(unsigned char const &x) {
return x;
}
template <>
inline hashcode_t hashcode(short const &x) {
return x;
}
template <>
inline hashcode_t hashcode(unsigned short const &x) {
return x;
}
template <>
inline hashcode_t hashcode(int const &x) {
return x;
}
template <>
inline hashcode_t hashcode(unsigned const &x) {
return x;
}
template <>
inline hashcode_t hashcode(long const &x) {
return x;
}
template <>
inline hashcode_t hashcode(unsigned long const &x) {
return x;
}
template <>
inline hashcode_t hashcode(long long const &x) {
return (x >> 32) ^ x;
}
template <>
inline hashcode_t hashcode(unsigned long long const &x) {
return (x >> 32) ^ x;
}
#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
template <>
inline hashcode_t hashcode(int64_t const &x) {
return (x >> 32) ^ x;
}
template <>
inline hashcode_t hashcode(uint64_t const &x) {
return (x >> 32) ^ x;
}
#endif
template <typename T>
inline hashcode_t hashcode(T * const &x) {
return reinterpret_cast<uintptr_t>(x) >> 3;
}
template <typename T>
inline typename T::key_const_reference hashkey(const T &x) {
return x.hashkey();
}
#endif