VirtualBox

source: vbox/trunk/src/VBox/Runtime/include/internal/strhash.h@ 104465

Last change on this file since 104465 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1/* $Id: strhash.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Internal header containing inline string hashing functions.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef IPRT_INCLUDED_INTERNAL_strhash_h
38#define IPRT_INCLUDED_INTERNAL_strhash_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43#include <iprt/types.h>
44
45
46/* sdbm:
47 This algorithm was created for sdbm (a public-domain reimplementation of
48 ndbm) database library. it was found to do well in scrambling bits,
49 causing better distribution of the keys and fewer splits. it also happens
50 to be a good general hashing function with good distribution. the actual
51 function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below
52 is the faster version used in gawk. [there is even a faster, duff-device
53 version] the magic constant 65599 was picked out of thin air while
54 experimenting with different constants, and turns out to be a prime.
55 this is one of the algorithms used in berkeley db (see sleepycat) and
56 elsewhere. */
57
58/**
59 * Hash string, return hash + length.
60 */
61DECLINLINE(uint32_t) sdbm(const char *str, size_t *pcch)
62{
63 uint8_t *pu8 = (uint8_t *)str;
64 uint32_t hash = 0;
65 int c;
66
67 while ((c = *pu8++))
68 hash = c + (hash << 6) + (hash << 16) - hash;
69
70 *pcch = (uintptr_t)pu8 - (uintptr_t)str - 1;
71 return hash;
72}
73
74
75/**
76 * Hash up to N bytes, return hash + hashed length.
77 */
78DECLINLINE(uint32_t) sdbmN(const char *str, size_t cchMax, size_t *pcch)
79{
80 uint8_t *pu8 = (uint8_t *)str;
81 uint32_t hash = 0;
82 int c;
83
84 while ((c = *pu8++) && cchMax-- > 0)
85 hash = c + (hash << 6) + (hash << 16) - hash;
86
87 *pcch = (uintptr_t)pu8 - (uintptr_t)str - 1;
88 return hash;
89}
90
91
92/**
93 * Incremental hashing.
94 */
95DECLINLINE(uint32_t) sdbmInc(const char *str, uint32_t hash)
96{
97 uint8_t *pu8 = (uint8_t *)str;
98 int c;
99
100 while ((c = *pu8++))
101 hash = c + (hash << 6) + (hash << 16) - hash;
102
103 return hash;
104}
105
106/**
107 * Incremental hashing with length limitation.
108 */
109DECLINLINE(uint32_t) sdbmIncN(const char *psz, size_t cchMax, uint32_t uHash)
110{
111 uint8_t *pu8 = (uint8_t *)psz;
112 int c;
113
114 while ((c = *pu8++) && cchMax-- > 0)
115 uHash = c + (uHash << 6) + (uHash << 16) - uHash;
116
117 return uHash;
118}
119
120
121#endif /* !IPRT_INCLUDED_INTERNAL_strhash_h */
122
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette