libamxc  1.10.3
C Generic Data Containers
amxc_hash_func.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include "amxc/amxc_hash.h"
56 
57 unsigned int amxc_RS_hash(const char* str, const unsigned int len) {
58  unsigned int b = 378551;
59  unsigned int a = 63689;
60  unsigned int hash = 0;
61  unsigned int i = 0;
62 
63  for(i = 0; i < len; str++, i++) {
64  hash = hash * a + (*str);
65  a = a * b;
66  }
67 
68  return hash;
69 }
70 
71 unsigned int amxc_JS_hash(const char* str, const unsigned int len) {
72  unsigned int hash = 1315423911;
73  unsigned int i = 0;
74 
75  for(i = 0; i < len; str++, i++) {
76  hash ^= ((hash << 5) + (*str) + (hash >> 2));
77  }
78 
79  return hash;
80 }
81 
82 unsigned int amxc_PJW_hash(const char* str, const unsigned int len) {
83  const unsigned int BitsInUnsignedInt = (unsigned int) (sizeof(unsigned int) * 8);
84  const unsigned int ThreeQuarters = (unsigned int) ((BitsInUnsignedInt * 3) / 4);
85  const unsigned int OneEighth = (unsigned int) (BitsInUnsignedInt / 8);
86  const unsigned int HighBits = (unsigned int) (0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
87  unsigned int hash = 0;
88  unsigned int test = 0;
89  unsigned int i = 0;
90 
91  for(i = 0; i < len; str++, i++) {
92  hash = (hash << OneEighth) + (*str);
93 
94  if((test = hash & HighBits) != 0) {
95  hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
96  }
97  }
98 
99  return hash;
100 }
101 
102 unsigned int amxc_ELF_hash(const char* str, const unsigned int len) {
103  unsigned int hash = 0;
104  unsigned int x = 0;
105  unsigned int i = 0;
106 
107  for(i = 0; i < len; str++, i++) {
108  hash = (hash << 4) + (*str);
109  if((x = hash & 0xF0000000L) != 0) {
110  hash ^= (x >> 24);
111  }
112  hash &= ~x;
113  }
114 
115  return hash;
116 }
117 
118 unsigned int amxc_BKDR_hash(const char* str, const unsigned int len) {
119  unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */
120  unsigned int hash = 0;
121  unsigned int i = 0;
122 
123  for(i = 0; i < len; str++, i++) {
124  hash = (hash * seed) + (*str);
125  }
126 
127  return hash;
128 }
129 
130 unsigned int amxc_SDBM_hash(const char* str, const unsigned int len) {
131  unsigned int hash = 0;
132  unsigned int i = 0;
133 
134  for(i = 0; i < len; str++, i++) {
135  hash = (*str) + (hash << 6) + (hash << 16) - hash;
136  }
137 
138  return hash;
139 }
140 
141 unsigned int amxc_DJB_hash(const char* str, const unsigned int len) {
142  unsigned int hash = 5381;
143  unsigned int i = 0;
144 
145  for(i = 0; i < len; str++, i++) {
146  hash = ((hash << 5) + hash) + (*str);
147  }
148 
149  return hash;
150 }
151 
152 unsigned int amxc_DEK_hash(const char* str, const unsigned int len) {
153  unsigned int hash = len;
154  unsigned int i = 0;
155 
156  for(i = 0; i < len; str++, i++) {
157  hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
158  }
159  return hash;
160 }
161 
162 unsigned int amxc_BP_hash(const char* str, const unsigned int len) {
163  unsigned int hash = 0;
164  unsigned int i = 0;
165  for(i = 0; i < len; str++, i++) {
166  hash = hash << 7 ^ (*str);
167  }
168 
169  return hash;
170 }
171 
172 unsigned int amxc_FNV_hash(const char* str, const unsigned int len) {
173  const unsigned int fnv_prime = 0x811C9DC5;
174  unsigned int hash = 0;
175  unsigned int i = 0;
176 
177  for(i = 0; i < len; str++, i++) {
178  hash *= fnv_prime;
179  hash ^= (int) (*str);
180  }
181 
182  return hash;
183 }
184 
185 unsigned int amxc_AP_hash(const char* str, const unsigned int len) {
186  unsigned int hash = 0xAAAAAAAA;
187  unsigned int i = 0;
188 
189  for(i = 0; i < len; str++, i++) {
190  hash ^= ((i & 1) == 0) ? ((hash << 7) ^ (*str) * (hash >> 3)) :
191  (~((hash << 11) + ((*str) ^ (hash >> 5))));
192  }
193 
194  return hash;
195 }
Ambiorix string hash functions header file.
unsigned int amxc_BKDR_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_ELF_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_DEK_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_FNV_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_BP_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_RS_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_SDBM_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_JS_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_AP_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_DJB_hash(const char *str, const unsigned int len)
Calculate a hash from a string.
unsigned int amxc_PJW_hash(const char *str, const unsigned int len)
Calculate a hash from a string.