libdatatypes 0.3.2
Abstract datatypes for C.
Loading...
Searching...
No Matches
hash.c
Go to the documentation of this file.
1/***************************************************************************
2 begin........: May 2012
3 copyright....: Sebastian Fedrau
4 email........: sebastian.fedrau@gmail.com
5 ***************************************************************************/
6
7/***************************************************************************
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License v3 as published by
10 the Free Software Foundation.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License v3 for more details.
16 ***************************************************************************/
22#include <stdio.h>
23#include <assert.h>
24
25#include "hash.h"
26
27uint32_t
28str_hash(const void *ptr)
29{
30 assert(ptr != NULL);
31
32 const char *plain = ptr;
33 uint32_t hash = 0;
34
35 while(*plain)
36 {
37 hash = *plain++ + (hash << 6) + (hash << 16) - hash;
38 }
39
40 return hash;
41}
42
43uint32_t
44direct_hash(const void *ptr)
45{
46 uintptr_t v = (uintptr_t)ptr;
47
48 if(v > UINT32_MAX)
49 {
50 fprintf(stderr, "%s(): integer overflow.\n", __func__);
51 v = UINT32_MAX;
52 }
53
54 return (uint32_t)v;
55}
56
uint32_t str_hash(const void *ptr)
Definition hash.c:28
uint32_t direct_hash(const void *ptr)
Definition hash.c:44
Hash functions.