tlx
hash_djb2.hpp
Go to the documentation of this file.
1
/*******************************************************************************
2
* tlx/string/hash_djb2.hpp
3
*
4
* Part of tlx - http://panthema.net/tlx
5
*
6
* Copyright (C) 2019 Timo Bingmann <tb@panthema.net>
7
*
8
* All rights reserved. Published under the Boost Software License, Version 1.0
9
******************************************************************************/
10
11
#ifndef TLX_STRING_HASH_DJB2_HEADER
12
#define TLX_STRING_HASH_DJB2_HEADER
13
14
#include <string>
15
16
namespace
tlx
{
17
18
//! \addtogroup tlx_string
19
//! \{
20
21
/*!
22
* Simple, fast, but "insecure" string hash method by Dan Bernstein from
23
* http://www.cse.yorku.ca/~oz/hash.html
24
*/
25
static
inline
26
uint32_t
hash_djb2
(
const
unsigned
char
* str) {
27
uint32_t hash = 5381;
28
unsigned
char
c;
29
while
((c = *str++) != 0) {
30
// hash * 33 + c
31
hash = ((hash << 5) + hash) + c;
32
}
33
return
hash;
34
}
35
36
/*!
37
* Simple, fast, but "insecure" string hash method by Dan Bernstein from
38
* http://www.cse.yorku.ca/~oz/hash.html
39
*/
40
static
inline
41
uint32_t
hash_djb2
(
const
char
* str) {
42
return
hash_djb2
(
reinterpret_cast<
const
unsigned
char
*
>
(str));
43
}
44
45
/*!
46
* Simple, fast, but "insecure" string hash method by Dan Bernstein from
47
* http://www.cse.yorku.ca/~oz/hash.html
48
*/
49
static
inline
50
uint32_t
hash_djb2
(
const
unsigned
char
* str,
size_t
size) {
51
uint32_t hash = 5381;
52
while
(size-- > 0) {
53
// hash * 33 + c
54
hash = ((hash << 5) + hash) + static_cast<unsigned char>(*str++);
55
}
56
return
hash;
57
}
58
59
/*!
60
* Simple, fast, but "insecure" string hash method by Dan Bernstein from
61
* http://www.cse.yorku.ca/~oz/hash.html
62
*/
63
static
inline
64
uint32_t
hash_djb2
(
const
char
* str,
size_t
size) {
65
return
hash_djb2
(
reinterpret_cast<
const
unsigned
char
*
>
(str), size);
66
}
67
68
/*!
69
* Simple, fast, but "insecure" string hash method by Dan Bernstein from
70
* http://www.cse.yorku.ca/~oz/hash.html
71
*/
72
static
inline
73
uint32_t
hash_djb2
(
const
std::string& str) {
74
return
hash_djb2
(str.data(), str.size());
75
}
76
77
//! \}
78
79
}
// namespace tlx
80
81
#endif // !TLX_STRING_HASH_DJB2_HEADER
82
83
/******************************************************************************/
tlx::hash_djb2
static uint32_t hash_djb2(const unsigned char *str)
Simple, fast, but "insecure" string hash method by Dan Bernstein from http://www.cse....
Definition:
hash_djb2.hpp:34
tlx
Definition:
exclusive_scan.hpp:17
tlx
string
hash_djb2.hpp
Generated on Fri Jan 31 2020 00:00:00 for tlx by
1.8.17