tlx
Loading...
Searching...
No Matches
hash_sdbm.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/string/hash_sdbm.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_SDBM_HEADER
12#define TLX_STRING_HASH_SDBM_HEADER
13
14#include <cstdint>
15#include <string>
16
17namespace tlx {
18
19//! \addtogroup tlx_string
20//! \{
21
22/*!
23 * Simple, fast, but "insecure" string hash method by sdbm database from
24 * http://www.cse.yorku.ca/~oz/hash.html
25 */
26static inline
27std::uint32_t hash_sdbm(const unsigned char* str) {
28 std::uint32_t hash = 0;
29 unsigned char c;
30 while ((c = *str++) != 0) {
31 hash = c + (hash << 6) + (hash << 16) - hash;
32 }
33 return hash;
34}
35
36/*!
37 * Simple, fast, but "insecure" string hash method by sdbm database from
38 * http://www.cse.yorku.ca/~oz/hash.html
39 */
40static inline
41std::uint32_t hash_sdbm(const char* str) {
42 return hash_sdbm(reinterpret_cast<const unsigned char*>(str));
43}
44
45/*!
46 * Simple, fast, but "insecure" string hash method by sdbm database from
47 * http://www.cse.yorku.ca/~oz/hash.html
48 */
49static inline
50std::uint32_t hash_sdbm(const unsigned char* str, size_t size) {
51 std::uint32_t hash = 0;
52 while (size-- > 0) {
53 hash = static_cast<unsigned char>(*str++)
54 + (hash << 6) + (hash << 16) - hash;
55 }
56 return hash;
57}
58
59/*!
60 * Simple, fast, but "insecure" string hash method by sdbm database from
61 * http://www.cse.yorku.ca/~oz/hash.html
62 */
63static inline
64std::uint32_t hash_sdbm(const char* str, size_t size) {
65 return hash_sdbm(reinterpret_cast<const unsigned char*>(str), size);
66}
67
68/*!
69 * Simple, fast, but "insecure" string hash method by sdbm database from
70 * http://www.cse.yorku.ca/~oz/hash.html
71 */
72static inline
73std::uint32_t hash_sdbm(const std::string& str) {
74 return hash_sdbm(str.data(), str.size());
75}
76
77//! \}
78
79} // namespace tlx
80
81#endif // !TLX_STRING_HASH_SDBM_HEADER
82
83/******************************************************************************/
static std::uint32_t hash_sdbm(const unsigned char *str)
Simple, fast, but "insecure" string hash method by sdbm database from http://www.cse....
Definition: hash_sdbm.hpp:27