diff options
Diffstat (limited to 'src/hash.cpp')
| -rw-r--r-- | src/hash.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/hash.cpp b/src/hash.cpp new file mode 100644 index 0000000..a207c29 --- /dev/null +++ b/src/hash.cpp | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | #include "hash.h" | ||
| 2 | |||
| 3 | namespace Bu { subExceptionDef( HashException ) } | ||
| 4 | |||
| 5 | template<> uint32_t Bu::__calcHashCode<int>( const int &k ) | ||
| 6 | { | ||
| 7 | return k; | ||
| 8 | } | ||
| 9 | |||
| 10 | template<> bool Bu::__cmpHashKeys<int>( const int &a, const int &b ) | ||
| 11 | { | ||
| 12 | return a == b; | ||
| 13 | } | ||
| 14 | |||
| 15 | template<> uint32_t Bu::__calcHashCode<unsigned int>( const unsigned int &k ) | ||
| 16 | { | ||
| 17 | return k; | ||
| 18 | } | ||
| 19 | |||
| 20 | template<> bool Bu::__cmpHashKeys<unsigned int>( const unsigned int &a, const unsigned int &b ) | ||
| 21 | { | ||
| 22 | return a == b; | ||
| 23 | } | ||
| 24 | |||
| 25 | template<> | ||
| 26 | uint32_t Bu::__calcHashCode<const char *>( const char * const &k ) | ||
| 27 | { | ||
| 28 | if (k == NULL) | ||
| 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 | } | ||
| 38 | |||
| 39 | return nPos; | ||
| 40 | } | ||
| 41 | |||
| 42 | template<> bool Bu::__cmpHashKeys<const char *>( const char * const &a, const char * const &b ) | ||
| 43 | { | ||
| 44 | if( a == b ) | ||
| 45 | return true; | ||
| 46 | |||
| 47 | for(int j=0; a[j] == b[j]; j++ ) | ||
| 48 | if( a[j] == '\0' ) | ||
| 49 | return true; | ||
| 50 | |||
| 51 | return false; | ||
| 52 | } | ||
| 53 | |||
| 54 | template<> | ||
| 55 | uint32_t Bu::__calcHashCode<char *>( char * const &k ) | ||
| 56 | { | ||
| 57 | if (k == NULL) | ||
| 58 | { | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | unsigned long int nPos = 0; | ||
| 63 | for( const char *s = k; *s; s++ ) | ||
| 64 | { | ||
| 65 | nPos = *s + (nPos << 6) + (nPos << 16) - nPos; | ||
| 66 | } | ||
| 67 | |||
| 68 | return nPos; | ||
| 69 | } | ||
| 70 | |||
| 71 | template<> bool Bu::__cmpHashKeys<char *>( char * const &a, char * const &b ) | ||
| 72 | { | ||
| 73 | if( a == b ) | ||
| 74 | return true; | ||
| 75 | |||
| 76 | for(int j=0; a[j] == b[j]; j++ ) | ||
| 77 | if( a[j] == '\0' ) | ||
| 78 | return true; | ||
| 79 | |||
| 80 | return false; | ||
| 81 | } | ||
| 82 | |||
| 83 | template<> uint32_t Bu::__calcHashCode<std::string>( const std::string &k ) | ||
| 84 | { | ||
| 85 | std::string::size_type j, sz = k.size(); | ||
| 86 | const char *s = k.c_str(); | ||
| 87 | |||
| 88 | unsigned long int nPos = 0; | ||
| 89 | for( j = 0; j < sz; j++, s++ ) | ||
| 90 | { | ||
| 91 | nPos = *s + (nPos << 6) + (nPos << 16) - nPos; | ||
| 92 | } | ||
| 93 | |||
| 94 | return nPos; | ||
| 95 | } | ||
| 96 | |||
| 97 | template<> bool Bu::__cmpHashKeys<std::string>( const std::string &a, const std::string &b ) | ||
| 98 | { | ||
| 99 | return a == b; | ||
| 100 | } | ||
| 101 | |||
