0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TclHash.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 #ifndef HYPERTABLE_TCLHASH_H
27 #define HYPERTABLE_TCLHASH_H
28 
29 #include "Common/String.h"
30 
31 namespace Hypertable {
32 
44 inline size_t tcl_hash(const char *s) {
45  size_t ret = 0;
46 
47  for (; *s; ++s)
48  ret += (ret << 3) + (unsigned)*s;
49 
50  return ret;
51 }
52 
61 inline size_t tcl_hash(const void *data, size_t len, size_t seed) {
62  size_t ret = seed;
63  const uint8_t *dp = (uint8_t *)data, *end = dp + len;
64 
65  for (; dp < end; ++dp)
66  ret += (ret << 3) + *dp;
67 
68  return ret;
69 }
70 
71 #define HT_TCLHASH_DO2(p, i) \
72  ret += (ret << 3) + p[i]; ret += (ret << 3) + p[i+1]
73 
74 #define HT_TCLHASH_DO4(p, i) HT_TCLHASH_DO2(p, i); HT_TCLHASH_DO2(p, i+2);
75 #define HT_TCLHASH_DO8(p, i) HT_TCLHASH_DO4(p, i); HT_TCLHASH_DO4(p, i+4);
76 
85 inline size_t tcl_hash2(const void *data, size_t len, size_t seed) {
86  size_t ret = seed;
87  const uint8_t *dp = (uint8_t *)data;
88 
89  if (len >= 8) do {
90  HT_TCLHASH_DO8(dp, 0);
91  dp += 8;
92  len -= 8;
93  } while (len >= 8);
94 
95  if (len > 0) do {
96  ret += (ret << 3) + *dp++;
97  } while (--len);
98 
99  return ret;
100 }
101 
109 struct TclHash {
111  size_t operator()(const void *start, size_t len, size_t seed = 0) const {
112  return tcl_hash(start, len, seed);
113  }
114 
116  size_t operator()(const String& s) const {
117  return tcl_hash(s.c_str(), s.length(), 0);
118  }
119 
121  size_t operator()(const char *s) const { return tcl_hash(s); }
122 };
123 
131 struct TclHash2 {
133  size_t operator()(const void *start, size_t len, size_t seed = 0) const {
134  return tcl_hash2(start, len, seed);
135  }
136 
138  size_t operator()(const String& s) const {
139  return tcl_hash2(s.c_str(), s.length(), 0);
140  }
141 
143  size_t operator()(const char *s) const {
144  return tcl_hash2(s, strlen(s), 0);
145  }
146 };
147 
150 } // namespace Hypertable
151 
152 #endif // HYPERTABLE_TCLHASH_H
#define HT_TCLHASH_DO8(p, i)
Definition: TclHash.h:75
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Helper structure using overloaded operator() to calculate the Tcl hashes of various input types (usin...
Definition: TclHash.h:131
size_t operator()(const void *start, size_t len, size_t seed=0) const
Returns hash of a memory buffer with an optional seed.
Definition: TclHash.h:111
size_t operator()(const String &s) const
Returns hash of a String.
Definition: TclHash.h:116
size_t operator()(const String &s) const
Returns hash of a String.
Definition: TclHash.h:138
Helper structure using overloaded operator() to calculate the Tcl hashes of various input types...
Definition: TclHash.h:109
Hypertable definitions
size_t operator()(const char *s) const
Returns hash of a null-terminated c-String.
Definition: TclHash.h:143
size_t operator()(const char *s) const
Returns hash of a null-terminated c-String.
Definition: TclHash.h:121
size_t tcl_hash(const char *s)
The Tcl hash by John Ousterhout for null-terminated c-strings, preferably alpha-numeric characters on...
Definition: TclHash.h:44
A String class based on std::string.
size_t tcl_hash2(const void *data, size_t len, size_t seed)
Unrolled Tcl hash, up to 20% faster for longer (> 8 bytes) strings.
Definition: TclHash.h:85
size_t operator()(const void *start, size_t len, size_t seed=0) const
Returns hash of a memory buffer with an optional seed.
Definition: TclHash.h:133