diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-11-21 12:23:13 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-11-21 12:23:13 +0000 |
commit | 4b8bad3d711f39db84f094edf83c5496a3f02cd6 (patch) | |
tree | bbaf654af3e82e67544ae17f07b7fbe7d02ce0ec /src/hash.cpp | |
parent | 737b1aee54da9ff45a4fb6eb7e636eff9019128e (diff) | |
download | libbu++-4b8bad3d711f39db84f094edf83c5496a3f02cd6.tar.gz libbu++-4b8bad3d711f39db84f094edf83c5496a3f02cd6.tar.bz2 libbu++-4b8bad3d711f39db84f094edf83c5496a3f02cd6.tar.xz libbu++-4b8bad3d711f39db84f094edf83c5496a3f02cd6.zip |
Wow, craziness. Part way through working on the confpair system I got some
sudden insperation and completely redid Hash. Now everything but delete is
implemented, including typesafe iterators and more. It's really cool, and
everyone should check it out and start using it right away!
Diffstat (limited to 'src/hash.cpp')
-rw-r--r-- | src/hash.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/hash.cpp b/src/hash.cpp index b4aac77..14be208 100644 --- a/src/hash.cpp +++ b/src/hash.cpp | |||
@@ -1 +1,81 @@ | |||
1 | #include "hash.h" | 1 | #include "hash.h" |
2 | |||
3 | template<> | ||
4 | uint32_t __calcHashCode<const char *>( const char * k ) | ||
5 | { | ||
6 | if (k == NULL) | ||
7 | { | ||
8 | return 0; | ||
9 | } | ||
10 | |||
11 | unsigned long int nPos = 0; | ||
12 | for( const char *s = k; *s; s++ ) | ||
13 | { | ||
14 | nPos = *s + (nPos << 6) + (nPos << 16) - nPos; | ||
15 | } | ||
16 | |||
17 | return nPos; | ||
18 | } | ||
19 | |||
20 | template<> bool __cmpHashKeys<const char *>( const char *a, const char *b ) | ||
21 | { | ||
22 | if( a == b ) | ||
23 | return true; | ||
24 | |||
25 | for(; *a != *b; a++, b++ ) | ||
26 | if( *a == '\0' && *b == '\0' ) | ||
27 | return true; | ||
28 | |||
29 | return false; | ||
30 | } | ||
31 | |||
32 | template<> | ||
33 | uint32_t __calcHashCode<char *>( char *k ) | ||
34 | { | ||
35 | return __calcHashCode<const char *>((const char *)k ); | ||
36 | } | ||
37 | |||
38 | template<> bool __cmpHashKeys<char *>( char *a, char *b ) | ||
39 | { | ||
40 | return __cmpHashKeys<const char *>((const char *)a, (const char *)b ); | ||
41 | } | ||
42 | |||
43 | template<> uint32_t __calcHashCode<const std::string>( const std::string k ) | ||
44 | { | ||
45 | std::string::size_type j, sz = k.size(); | ||
46 | const char *s = k.c_str(); | ||
47 | |||
48 | unsigned long int nPos = 0; | ||
49 | for( j = 0; j < sz; j++, s++ ) | ||
50 | { | ||
51 | nPos = *s + (nPos << 6) + (nPos << 16) - nPos; | ||
52 | } | ||
53 | |||
54 | return nPos; | ||
55 | } | ||
56 | |||
57 | template<> bool __cmpHashKeys<const std::string>( const std::string a, const std::string b ) | ||
58 | { | ||
59 | return a == b; | ||
60 | } | ||
61 | |||
62 | template<> uint32_t __calcHashCode<std::string>( std::string k ) | ||
63 | { | ||
64 | return __calcHashCode<const std::string>( k ); | ||
65 | } | ||
66 | |||
67 | template<> bool __cmpHashKeys<std::string>( std::string a, std::string b ) | ||
68 | { | ||
69 | return __cmpHashKeys<const std::string>( a, b ); | ||
70 | } | ||
71 | |||
72 | template<> uint32_t __calcHashCode<Hashable &>( Hashable &k ) | ||
73 | { | ||
74 | return k.getHashCode(); | ||
75 | } | ||
76 | |||
77 | template<> bool __cmpHashKeys<Hashable &>( Hashable &a, Hashable &b ) | ||
78 | { | ||
79 | return a.compareForHash( b ); | ||
80 | } | ||
81 | |||