aboutsummaryrefslogtreecommitdiff
path: root/src/hash.cpp
blob: d428dd65cf4c6699904dd254a0e14d9d0b56381b (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "hash.h"

subExceptionDef( HashException )

template<> uint32_t __calcHashCode<const int>( const int k )
{
	return k;
}

template<> bool __cmpHashKeys<const int>( const int a, const int b )
{
	return a == b;
}

template<> uint32_t __calcHashCode<int>( int k )
{
	return k;
}

template<> bool __cmpHashKeys<int>( int a, int b )
{
	return a == b;
}

template<> uint32_t __calcHashCode<const unsigned int>( const unsigned int k )
{
	return k;
}

template<> bool __cmpHashKeys<const unsigned int>( const unsigned int a, const unsigned int b )
{
	return a == b;
}

template<> uint32_t __calcHashCode<unsigned int>( unsigned int k )
{
	return k;
}

template<> bool __cmpHashKeys<unsigned int>( unsigned int a, unsigned int b )
{
	return a == b;
}

template<>
uint32_t __calcHashCode<const char *>( const char * 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 __cmpHashKeys<const char *>( const char *a, const char *b )
{
	if( a == b )
		return true;

	for(; *a == *b; a++, b++ )
		if( *a == '\0' )
			return true;

	return false;
}

template<>
uint32_t __calcHashCode<char *>( char *k )
{
	return __calcHashCode<const char *>((const char *)k );
}

template<> bool __cmpHashKeys<char *>( char *a, char *b )
{
	return __cmpHashKeys<const char *>((const char *)a, (const char *)b );
}

template<> uint32_t __calcHashCode<const std::string>( const std::string k )
{
	std::string::size_type j, sz = k.size();
	const char *s = k.c_str();

	unsigned long int nPos = 0;
	for( j = 0; j < sz; j++, s++ )
	{
		nPos = *s + (nPos << 6) + (nPos << 16) - nPos;
	}

	return nPos;
}

template<> bool __cmpHashKeys<const std::string>( const std::string a, const std::string b )
{
	return a == b;
}

template<> uint32_t __calcHashCode<std::string>( std::string k )
{
	return __calcHashCode<const std::string>( k );
}

template<> bool __cmpHashKeys<std::string>( std::string a, std::string b )
{
	return __cmpHashKeys<const std::string>( a, b );
}

template<> uint32_t __calcHashCode<Hashable &>( Hashable &k )
{
	return k.getHashCode();
}

template<> bool __cmpHashKeys<Hashable &>( Hashable &a, Hashable &b )
{
	return a.compareForHash( b );
}