From 4b8bad3d711f39db84f094edf83c5496a3f02cd6 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 21 Nov 2006 12:23:13 +0000 Subject: 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! --- src/hash.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'src/hash.cpp') 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 @@ #include "hash.h" + +template<> +uint32_t __calcHashCode( 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 *a, const char *b ) +{ + if( a == b ) + return true; + + for(; *a != *b; a++, b++ ) + if( *a == '\0' && *b == '\0' ) + return true; + + return false; +} + +template<> +uint32_t __calcHashCode( char *k ) +{ + return __calcHashCode((const char *)k ); +} + +template<> bool __cmpHashKeys( char *a, char *b ) +{ + return __cmpHashKeys((const char *)a, (const char *)b ); +} + +template<> uint32_t __calcHashCode( 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 a, const std::string b ) +{ + return a == b; +} + +template<> uint32_t __calcHashCode( std::string k ) +{ + return __calcHashCode( k ); +} + +template<> bool __cmpHashKeys( std::string a, std::string b ) +{ + return __cmpHashKeys( a, b ); +} + +template<> uint32_t __calcHashCode( Hashable &k ) +{ + return k.getHashCode(); +} + +template<> bool __cmpHashKeys( Hashable &a, Hashable &b ) +{ + return a.compareForHash( b ); +} + -- cgit v1.2.3