Click
Loading...
Searching...
No Matches
hashcode.hh
1#ifndef CLICK_HASHCODE_HH
2#define CLICK_HASHCODE_HH
3CLICK_DECLS
4
5// Notes about the hashcode template: On GCC 4.3.0, "template <>" is required
6// on the specializations or they aren't used. Just plain overloaded
7// functions aren't used. The specializations must be e.g. "const char &",
8// not "char", or GCC complains about a specialization not matching the
9// general template. The main template takes a const reference for two
10// reasons. First, providing both "hashcode_t hashcode(T)" and "hashcode_t
11// hashcode(const T&)" leads to ambiguity errors. Second, providing only
12// "hashcode_t hashcode(T)" is slower by looks like 8% when T is a String,
13// because of copy constructors; for types with more expensive non-default
14// copy constructors this would probably be worse.
15
16typedef size_t hashcode_t;
17
18template <typename T>
19inline hashcode_t hashcode(T const &x) {
20 return x.hashcode();
21}
22
23template <>
24inline hashcode_t hashcode(char const &x) {
25 return x;
26}
27
28template <>
29inline hashcode_t hashcode(signed char const &x) {
30 return x;
31}
32
33template <>
34inline hashcode_t hashcode(unsigned char const &x) {
35 return x;
36}
37
38template <>
39inline hashcode_t hashcode(short const &x) {
40 return x;
41}
42
43template <>
44inline hashcode_t hashcode(unsigned short const &x) {
45 return x;
46}
47
48template <>
49inline hashcode_t hashcode(int const &x) {
50 return x;
51}
52
53template <>
54inline hashcode_t hashcode(unsigned const &x) {
55 return x;
56}
57
58template <>
59inline hashcode_t hashcode(long const &x) {
60 return x;
61}
62
63template <>
64inline hashcode_t hashcode(unsigned long const &x) {
65 return x;
66}
67
68#if HAVE_LONG_LONG
69template <>
70inline hashcode_t hashcode(long long const &x) {
71 return (x >> 32) ^ x;
72}
73
74template <>
75inline hashcode_t hashcode(unsigned long long const &x) {
76 return (x >> 32) ^ x;
77}
78#endif
79
80#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
81template <>
82inline hashcode_t hashcode(int64_t const &x) {
83 return (x >> 32) ^ x;
84}
85
86template <>
87inline hashcode_t hashcode(uint64_t const &x) {
88 return (x >> 32) ^ x;
89}
90#endif
91
92template <typename T>
93inline hashcode_t hashcode(T * const &x) {
94 return reinterpret_cast<uintptr_t>(x) >> 3;
95}
96
97template <typename T>
98inline typename T::key_const_reference hashkey(const T &x) {
99 return x.hashkey();
100}
101
102CLICK_ENDDECLS
103#endif