Teuchos - Trilinos Tools Package
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
core
src
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
16
17
#include "
Teuchos_ConfigDefs.hpp
"
18
19
namespace
Teuchos
20
{
21
using
std::string;
22
33
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
{
73
return
HashUtils::getHashCode
(
74
reinterpret_cast<
const
unsigned
char
*
>
(&x),
sizeof
(
unsigned
));
75
}
76
80
template
<>
inline
int
hashCode
(
const
double
& x)
81
{
82
return
HashUtils::getHashCode
(
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
{
99
return
HashUtils::getHashCode
(
100
reinterpret_cast<
const
unsigned
char
*
>
(&x),
sizeof
(
long
long
));
101
}
102
106
template
<>
inline
int
hashCode
(
const
long
& x)
107
{
108
return
HashUtils::getHashCode
(
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();
140
maxIntBeforeWrap ++;
141
rtn += maxIntBeforeWrap;
142
}
143
return
rtn;
144
}
145
146
}
147
#endif
// TEUCHOS_HASHUTILS_H
Teuchos_ConfigDefs.hpp
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Teuchos::HashUtils
Utilities for generating hashcodes. This is not a true hash ! For all ints and types less than ints i...
Definition
Teuchos_HashUtils.hpp:35
Teuchos::HashUtils::hashCode
int hashCode(const T &x)
Standard interface for getting the hash code of an object.
Teuchos::HashUtils::hashCode
int hashCode(const std::string &x)
Get the hash code of a std::string.
Definition
Teuchos_HashUtils.hpp:115
Teuchos::HashUtils::hashCode
int hashCode(const double &x)
Get the hash code of a double.
Definition
Teuchos_HashUtils.hpp:80
Teuchos::HashUtils::hashCode
int hashCode(const long &x)
Get the hash code of a long.
Definition
Teuchos_HashUtils.hpp:106
Teuchos::HashUtils::getHashCode
static int getHashCode(const unsigned char *a, size_t len)
Definition
Teuchos_HashUtils.cpp:52
Teuchos::HashUtils::hashCode
int hashCode(const long long &x)
Get the hash code of a long long.
Definition
Teuchos_HashUtils.hpp:97
Teuchos::HashUtils::hashCode
int hashCode(const int &x)
Get the hash code of an int.
Definition
Teuchos_HashUtils.hpp:63
Teuchos::HashUtils::hashCode
int hashCode(const unsigned &x)
Get the hash code of an unsigned.
Definition
Teuchos_HashUtils.hpp:71
Teuchos::HashUtils::hashCode
int hashCode(const bool &x)
Get the hash code of a bool.
Definition
Teuchos_HashUtils.hpp:89
Teuchos
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Generated by
1.17.0