aboutsummaryrefslogtreecommitdiff
path: root/src/stable/hash.cpp
blob: 59572ecddbab7d4f25c800508462fcce5a7a3ed7 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/hash.h"

namespace Bu { subExceptionDef( HashException ) }

template<>
uint32_t Bu::__calcHashCode<const char *>( const char * const &k )
{
	if (k == NULL)
	{
		return 0;
	}
	
	unsigned long int nPos = 0;
	for( const char *s = k; *s; s++ )
	{
		nPos = *s + (nPos << 6) + (nPos << 16) - nPos;
	}

	return nPos;
}

template<> bool Bu::__cmpHashKeys<const char *>( const char * const &a, const char * const &b )
{
	if( a == b )
		return true;

	for(int j=0; a[j] == b[j]; j++ )
		if( a[j] == '\0' )
			return true;

	return false;
}

template<>
uint32_t Bu::__calcHashCode<char *>( char * const &k )
{
	if (k == NULL)
	{
		return 0;
	}
	
	unsigned long int nPos = 0;
	for( const char *s = k; *s; s++ )
	{
		nPos = *s + (nPos << 6) + (nPos << 16) - nPos;
	}

	return nPos;
}

template<> bool Bu::__cmpHashKeys<char *>( char * const &a, char * const &b )
{
	if( a == b )
		return true;

	for(int j=0; a[j] == b[j]; j++ )
		if( a[j] == '\0' )
			return true;

	return false;
}