diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-03-15 06:57:03 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-03-15 06:57:03 +0000 |
commit | 505410a5c3b93f81deda8b9653c2237a433be5c1 (patch) | |
tree | e4ac25af48878cdf466571a3d70577de9d3f890a /src/hash.cpp | |
parent | cf847cc534a2a7ad06750c446028b7c6d126fe21 (diff) | |
download | libbu++-505410a5c3b93f81deda8b9653c2237a433be5c1.tar.gz libbu++-505410a5c3b93f81deda8b9653c2237a433be5c1.tar.bz2 libbu++-505410a5c3b93f81deda8b9653c2237a433be5c1.tar.xz libbu++-505410a5c3b93f81deda8b9653c2237a433be5c1.zip |
This version may break hashing of strings, but at least you can hash FStrings...
templates are confusing.
Diffstat (limited to 'src/hash.cpp')
-rw-r--r-- | src/hash.cpp | 82 |
1 files changed, 36 insertions, 46 deletions
diff --git a/src/hash.cpp b/src/hash.cpp index d428dd6..004d6dd 100644 --- a/src/hash.cpp +++ b/src/hash.cpp | |||
@@ -2,48 +2,57 @@ | |||
2 | 2 | ||
3 | subExceptionDef( HashException ) | 3 | subExceptionDef( HashException ) |
4 | 4 | ||
5 | template<> uint32_t __calcHashCode<const int>( const int k ) | 5 | template<> uint32_t __calcHashCode<int>( const int &k ) |
6 | { | 6 | { |
7 | return k; | 7 | return k; |
8 | } | 8 | } |
9 | 9 | ||
10 | template<> bool __cmpHashKeys<const int>( const int a, const int b ) | 10 | template<> bool __cmpHashKeys<int>( const int &a, const int &b ) |
11 | { | 11 | { |
12 | return a == b; | 12 | return a == b; |
13 | } | 13 | } |
14 | 14 | ||
15 | template<> uint32_t __calcHashCode<int>( int k ) | 15 | template<> uint32_t __calcHashCode<unsigned int>( const unsigned int &k ) |
16 | { | 16 | { |
17 | return k; | 17 | return k; |
18 | } | 18 | } |
19 | 19 | ||
20 | template<> bool __cmpHashKeys<int>( int a, int b ) | 20 | template<> bool __cmpHashKeys<unsigned int>( const unsigned int &a, const unsigned int &b ) |
21 | { | 21 | { |
22 | return a == b; | 22 | return a == b; |
23 | } | 23 | } |
24 | 24 | ||
25 | template<> uint32_t __calcHashCode<const unsigned int>( const unsigned int k ) | 25 | template<> |
26 | uint32_t __calcHashCode<const char *>( const char * const &k ) | ||
26 | { | 27 | { |
27 | return k; | 28 | if (k == NULL) |
28 | } | 29 | { |
30 | return 0; | ||
31 | } | ||
32 | |||
33 | unsigned long int nPos = 0; | ||
34 | for( const char *s = k; *s; s++ ) | ||
35 | { | ||
36 | nPos = *s + (nPos << 6) + (nPos << 16) - nPos; | ||
37 | } | ||
29 | 38 | ||
30 | template<> bool __cmpHashKeys<const unsigned int>( const unsigned int a, const unsigned int b ) | 39 | return nPos; |
31 | { | ||
32 | return a == b; | ||
33 | } | 40 | } |
34 | 41 | ||
35 | template<> uint32_t __calcHashCode<unsigned int>( unsigned int k ) | 42 | template<> bool __cmpHashKeys<const char *>( const char * const &a, const char * const &b ) |
36 | { | 43 | { |
37 | return k; | 44 | if( a == b ) |
38 | } | 45 | return true; |
39 | 46 | ||
40 | template<> bool __cmpHashKeys<unsigned int>( unsigned int a, unsigned int b ) | 47 | for(int j=0; a[j] == b[j]; j++ ) |
41 | { | 48 | if( *a == '\0' ) |
42 | return a == b; | 49 | return true; |
50 | |||
51 | return false; | ||
43 | } | 52 | } |
44 | 53 | ||
45 | template<> | 54 | template<> |
46 | uint32_t __calcHashCode<const char *>( const char * k ) | 55 | uint32_t __calcHashCode<char *>( char * const &k ) |
47 | { | 56 | { |
48 | if (k == NULL) | 57 | if (k == NULL) |
49 | { | 58 | { |
@@ -59,30 +68,19 @@ uint32_t __calcHashCode<const char *>( const char * k ) | |||
59 | return nPos; | 68 | return nPos; |
60 | } | 69 | } |
61 | 70 | ||
62 | template<> bool __cmpHashKeys<const char *>( const char *a, const char *b ) | 71 | template<> bool __cmpHashKeys<char *>( char * const &a, char * const &b ) |
63 | { | 72 | { |
64 | if( a == b ) | 73 | if( a == b ) |
65 | return true; | 74 | return true; |
66 | 75 | ||
67 | for(; *a == *b; a++, b++ ) | 76 | for(int j=0; a[j] == b[j]; j++ ) |
68 | if( *a == '\0' ) | 77 | if( *a == '\0' ) |
69 | return true; | 78 | return true; |
70 | 79 | ||
71 | return false; | 80 | return false; |
72 | } | 81 | } |
73 | 82 | ||
74 | template<> | 83 | template<> uint32_t __calcHashCode<std::string>( const std::string &k ) |
75 | uint32_t __calcHashCode<char *>( char *k ) | ||
76 | { | ||
77 | return __calcHashCode<const char *>((const char *)k ); | ||
78 | } | ||
79 | |||
80 | template<> bool __cmpHashKeys<char *>( char *a, char *b ) | ||
81 | { | ||
82 | return __cmpHashKeys<const char *>((const char *)a, (const char *)b ); | ||
83 | } | ||
84 | |||
85 | template<> uint32_t __calcHashCode<const std::string>( const std::string k ) | ||
86 | { | 84 | { |
87 | std::string::size_type j, sz = k.size(); | 85 | std::string::size_type j, sz = k.size(); |
88 | const char *s = k.c_str(); | 86 | const char *s = k.c_str(); |
@@ -96,28 +94,20 @@ template<> uint32_t __calcHashCode<const std::string>( const std::string k ) | |||
96 | return nPos; | 94 | return nPos; |
97 | } | 95 | } |
98 | 96 | ||
99 | template<> bool __cmpHashKeys<const std::string>( const std::string a, const std::string b ) | 97 | template<> bool __cmpHashKeys<std::string>( const std::string &a, const std::string &b ) |
100 | { | 98 | { |
101 | return a == b; | 99 | return a == b; |
102 | } | 100 | } |
103 | 101 | ||
104 | template<> uint32_t __calcHashCode<std::string>( std::string k ) | 102 | template<> uint32_t __calcHashCode<Hashable>( const Hashable &k ) |
105 | { | ||
106 | return __calcHashCode<const std::string>( k ); | ||
107 | } | ||
108 | |||
109 | template<> bool __cmpHashKeys<std::string>( std::string a, std::string b ) | ||
110 | { | 103 | { |
111 | return __cmpHashKeys<const std::string>( a, b ); | 104 | return 0; |
105 | //return k.getHashCode(); | ||
112 | } | 106 | } |
113 | 107 | ||
114 | template<> uint32_t __calcHashCode<Hashable &>( Hashable &k ) | 108 | template<> bool __cmpHashKeys<Hashable>( const Hashable &a, const Hashable &b ) |
115 | { | 109 | { |
116 | return k.getHashCode(); | 110 | return false; |
117 | } | 111 | //return a.compareForHash( b ); |
118 | |||
119 | template<> bool __cmpHashKeys<Hashable &>( Hashable &a, Hashable &b ) | ||
120 | { | ||
121 | return a.compareForHash( b ); | ||
122 | } | 112 | } |
123 | 113 | ||