diff options
Diffstat (limited to 'src/stable/hash.cpp')
| -rw-r--r-- | src/stable/hash.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/stable/hash.cpp b/src/stable/hash.cpp new file mode 100644 index 0000000..59572ec --- /dev/null +++ b/src/stable/hash.cpp | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
| 3 | * | ||
| 4 | * This file is part of the libbu++ library and is released under the | ||
| 5 | * terms of the license contained in the file LICENSE. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "bu/hash.h" | ||
| 9 | |||
| 10 | namespace Bu { subExceptionDef( HashException ) } | ||
| 11 | |||
| 12 | template<> | ||
| 13 | uint32_t Bu::__calcHashCode<const char *>( const char * const &k ) | ||
| 14 | { | ||
| 15 | if (k == NULL) | ||
| 16 | { | ||
| 17 | return 0; | ||
| 18 | } | ||
| 19 | |||
| 20 | unsigned long int nPos = 0; | ||
| 21 | for( const char *s = k; *s; s++ ) | ||
| 22 | { | ||
| 23 | nPos = *s + (nPos << 6) + (nPos << 16) - nPos; | ||
| 24 | } | ||
| 25 | |||
| 26 | return nPos; | ||
| 27 | } | ||
| 28 | |||
| 29 | template<> bool Bu::__cmpHashKeys<const char *>( const char * const &a, const char * const &b ) | ||
| 30 | { | ||
| 31 | if( a == b ) | ||
| 32 | return true; | ||
| 33 | |||
| 34 | for(int j=0; a[j] == b[j]; j++ ) | ||
| 35 | if( a[j] == '\0' ) | ||
| 36 | return true; | ||
| 37 | |||
| 38 | return false; | ||
| 39 | } | ||
| 40 | |||
| 41 | template<> | ||
| 42 | uint32_t Bu::__calcHashCode<char *>( char * const &k ) | ||
| 43 | { | ||
| 44 | if (k == NULL) | ||
| 45 | { | ||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | unsigned long int nPos = 0; | ||
| 50 | for( const char *s = k; *s; s++ ) | ||
| 51 | { | ||
| 52 | nPos = *s + (nPos << 6) + (nPos << 16) - nPos; | ||
| 53 | } | ||
| 54 | |||
| 55 | return nPos; | ||
| 56 | } | ||
| 57 | |||
| 58 | template<> bool Bu::__cmpHashKeys<char *>( char * const &a, char * const &b ) | ||
| 59 | { | ||
| 60 | if( a == b ) | ||
| 61 | return true; | ||
| 62 | |||
| 63 | for(int j=0; a[j] == b[j]; j++ ) | ||
| 64 | if( a[j] == '\0' ) | ||
| 65 | return true; | ||
| 66 | |||
| 67 | return false; | ||
| 68 | } | ||
| 69 | |||
