-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
56 lines (45 loc) · 1.39 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/***************************************************************************
begin........: May 2012
copyright....: Sebastian Fedrau
email........: [email protected]
***************************************************************************/
/***************************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License v3 for more details.
***************************************************************************/
/**
* \file hash.c
* \brief Hash functions.
* \author Sebastian Fedrau <[email protected]>
*/
#include <stdio.h>
#include <assert.h>
#include "hash.h"
uint32_t
str_hash(const void *ptr)
{
assert(ptr != NULL);
const char *plain = ptr;
uint32_t hash = 0;
while(*plain)
{
hash = *plain++ + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
uint32_t
direct_hash(const void *ptr)
{
uintptr_t v = (uintptr_t)ptr;
if(v > UINT32_MAX)
{
fprintf(stderr, "%s(): integer overflow.\n", __func__);
v = UINT32_MAX;
}
return (uint32_t)v;
}