Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_HashUtils.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TEUCHOS_HASHUTILS_H
11#define TEUCHOS_HASHUTILS_H
12
18
19namespace Teuchos
20{
21 using std::string;
22
34 class TEUCHOSCORE_LIB_DLL_EXPORT HashUtils
35 {
36 public:
37 /* Get the next prime in a sequence of hashtable sizes */
38 static int nextPrime(int newCapacity);
39 static int getHashCode(const unsigned char *a, size_t len);
40
41 private:
42
43 // sequence of primes generated via mathematica:
44 // Table[Prime[Round[1.5^x]], {x, 8, 36}]
45 static const int primeCount_;
46 static const int primes_[];
47 /*={101, 163, 271, 443, 733, 1187, 1907, 3061,
48 4919, 7759, 12379, 19543, 30841, 48487, 75989,
49 119089, 185971, 290347, 452027, 703657, 1093237,
50 1695781, 2627993, 4067599, 6290467, 9718019,
51 15000607, 23133937, 35650091};*/
52 };
53
54
58 template <class T> int hashCode(const T& x);
59
63 template <> inline int hashCode(const int& x)
64 {
65 return x;
66 }
67
71 template <> inline int hashCode(const unsigned& x)
72 {
74 reinterpret_cast<const unsigned char *>(&x), sizeof(unsigned));
75 }
76
80 template <> inline int hashCode(const double& x)
81 {
83 reinterpret_cast<const unsigned char *>(&x), sizeof(double));
84 }
85
89 template <> inline int hashCode(const bool& x)
90 {
91 return (int) x;
92 }
93
97 template <> inline int hashCode(const long long& x)
98 {
100 reinterpret_cast<const unsigned char *>(&x), sizeof(long long));
101 }
102
106 template <> inline int hashCode(const long& x)
107 {
109 reinterpret_cast<const unsigned char *>(&x), sizeof(long));
110 }
111
115 template <> inline int hashCode(const std::string& x)
116 {
117 /* This specialization could use the HashUtils::getHashCode as well,
118 * but they are both true hashes anyway, so leaving it !
119 * */
120 const char* str = x.c_str();
121 int len = static_cast<int>(x.length());
122 int step = len/4 + 1;
123 int base = 1;
124 int rtn = 0;
125
126 for (int i=0; i<len/2; i+=step)
127 {
128 rtn += base*(int) str[i];
129 base *= 128;
130 rtn += base*(int) str[len-i-1];
131 base *= 128;
132 }
133
134 if (rtn < 0)
135 {
136 /* Convert the largest -ve int to zero and -1 to
137 * std::numeric_limits<int>::max()
138 * */
139 size_t maxIntBeforeWrap = std::numeric_limits<int>::max();
142 }
143 return rtn;
144 }
145
146}
147#endif // TEUCHOS_HASHUTILS_H
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Utilities for generating hashcodes. This is not a true hash ! For all ints and types less than ints i...
int hashCode(const T &x)
Standard interface for getting the hash code of an object.
int hashCode(const std::string &x)
Get the hash code of a std::string.
int hashCode(const double &x)
Get the hash code of a double.
int hashCode(const long &x)
Get the hash code of a long.
static int getHashCode(const unsigned char *a, size_t len)
int hashCode(const long long &x)
Get the hash code of a long long.
int hashCode(const int &x)
Get the hash code of an int.
int hashCode(const unsigned &x)
Get the hash code of an unsigned.
int hashCode(const bool &x)
Get the hash code of a bool.
Smart reference counting pointer class for automatic garbage collection.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...