2 * Eddie Kohler, Yandong Mao, Robert Morris
3 * Copyright (c) 2012-2013 President and Fellows of Harvard College
4 * Copyright (c) 2012-2013 Massachusetts Institute of Technology
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, subject to the conditions
9 * listed in the Masstree LICENSE file. These conditions include: you must
10 * preserve this copyright notice, and you cannot mention the copyright
11 * holders in advertising related to the Software without their permission.
12 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13 * notice is a summary of the Masstree LICENSE file; the license in that file
16 #ifndef CLICK_HASHCODE_HH
17 #define CLICK_HASHCODE_HH
24 // Notes about the hashcode template: On GCC 4.3.0, "template <>" is required
25 // on the specializations or they aren't used. Just plain overloaded
26 // functions aren't used. The specializations must be e.g. "const char &",
27 // not "char", or GCC complains about a specialization not matching the
28 // general template. The main template takes a const reference for two
29 // reasons. First, providing both "hashcode_t hashcode(T)" and "hashcode_t
30 // hashcode(const T&)" leads to ambiguity errors. Second, providing only
31 // "hashcode_t hashcode(T)" is slower by looks like 8% when T is a String,
32 // because of copy constructors; for types with more expensive non-default
33 // copy constructors this would probably be worse.
35 typedef size_t hashcode_t; ///< Typical type for a hashcode() value.
38 inline hashcode_t hashcode(T const &x) {
43 inline hashcode_t hashcode(char const &x) {
48 inline hashcode_t hashcode(signed char const &x) {
53 inline hashcode_t hashcode(unsigned char const &x) {
58 inline hashcode_t hashcode(short const &x) {
63 inline hashcode_t hashcode(unsigned short const &x) {
68 inline hashcode_t hashcode(int const &x) {
73 inline hashcode_t hashcode(unsigned const &x) {
78 inline hashcode_t hashcode(long const &x) {
83 inline hashcode_t hashcode(unsigned long const &x) {
88 inline hashcode_t hashcode(long long const &x) {
93 inline hashcode_t hashcode(unsigned long long const &x) {
97 #if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
99 inline hashcode_t hashcode(int64_t const &x) {
100 return (x >> 32) ^ x;
104 inline hashcode_t hashcode(uint64_t const &x) {
105 return (x >> 32) ^ x;
109 template <typename T>
110 inline hashcode_t hashcode(T * const &x) {
111 return reinterpret_cast<uintptr_t>(x) >> 3;
114 template <typename T>
115 inline typename T::key_const_reference hashkey(const T &x) {