File: hash.c

package info (click to toggle)
bibutils 7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,040 kB
  • sloc: ansic: 112,579; sh: 462; makefile: 42
file content (36 lines) | stat: -rw-r--r-- 734 bytes parent folder | download | duplicates (2)
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
/*
 * hash.c - Bob Jenkin's one-at-a-time hash
 *
 * Copyright (c) Chris Putnam 2020-2021
 *
 * Source code released under the GPL version 2
 */

#include <stdlib.h>
#include <string.h>
#include "hash.h"

/*
 * Bob Jenkin's one-at-a-time hash
 */

static unsigned int
one_at_a_time_hash( const char *key, const unsigned int len, unsigned int HASH_SIZE )
{
	unsigned int hash = 0, i;
	for ( i=0; i<len; ++i ) {
		hash += key[i];
		hash += ( hash << 10 );
		hash ^= ( hash >> 6 );
	}
	hash += ( hash << 3 );
	hash ^= ( hash >> 11 );
	hash += ( hash << 15 );
	return hash % ( HASH_SIZE - 1 );
}

unsigned int
calculate_hash_char( const char *key, unsigned int HASH_SIZE )
{
	return one_at_a_time_hash( key, strlen( key ), HASH_SIZE );
}