1 | /* $Id: strhash.h 36597 2011-04-06 19:46:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Internal header containing inline string hashing functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef ___internal_strhash_h
|
---|
28 | #define ___internal_strhash_h
|
---|
29 |
|
---|
30 | #include <iprt/types.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /* sdbm:
|
---|
34 | This algorithm was created for sdbm (a public-domain reimplementation of
|
---|
35 | ndbm) database library. it was found to do well in scrambling bits,
|
---|
36 | causing better distribution of the keys and fewer splits. it also happens
|
---|
37 | to be a good general hashing function with good distribution. the actual
|
---|
38 | function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below
|
---|
39 | is the faster version used in gawk. [there is even a faster, duff-device
|
---|
40 | version] the magic constant 65599 was picked out of thin air while
|
---|
41 | experimenting with different constants, and turns out to be a prime.
|
---|
42 | this is one of the algorithms used in berkeley db (see sleepycat) and
|
---|
43 | elsewhere. */
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Hash string, return hash + length.
|
---|
47 | */
|
---|
48 | DECLINLINE(uint32_t) sdbm(const char *str, size_t *pcch)
|
---|
49 | {
|
---|
50 | uint8_t *pu8 = (uint8_t *)str;
|
---|
51 | uint32_t hash = 0;
|
---|
52 | int c;
|
---|
53 |
|
---|
54 | while ((c = *pu8++))
|
---|
55 | hash = c + (hash << 6) + (hash << 16) - hash;
|
---|
56 |
|
---|
57 | *pcch = (uintptr_t)pu8 - (uintptr_t)str - 1;
|
---|
58 | return hash;
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Hash up to N bytes, return hash + hashed length.
|
---|
64 | */
|
---|
65 | DECLINLINE(uint32_t) sdbmN(const char *str, size_t cchMax, size_t *pcch)
|
---|
66 | {
|
---|
67 | uint8_t *pu8 = (uint8_t *)str;
|
---|
68 | uint32_t hash = 0;
|
---|
69 | int c;
|
---|
70 |
|
---|
71 | while ((c = *pu8++) && cchMax-- > 0)
|
---|
72 | hash = c + (hash << 6) + (hash << 16) - hash;
|
---|
73 |
|
---|
74 | *pcch = (uintptr_t)pu8 - (uintptr_t)str - 1;
|
---|
75 | return hash;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Incremental hashing.
|
---|
80 | */
|
---|
81 | DECLINLINE(uint32_t) sdbmInc(const char *str, uint32_t hash)
|
---|
82 | {
|
---|
83 | uint8_t *pu8 = (uint8_t *)str;
|
---|
84 | int c;
|
---|
85 |
|
---|
86 | while ((c = *pu8++))
|
---|
87 | hash = c + (hash << 6) + (hash << 16) - hash;
|
---|
88 |
|
---|
89 | return hash;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | #endif
|
---|
94 |
|
---|