00001 #ifndef STRING_HASHER_IMPLEMENTATION_FILE
00002 #define STRING_HASHER_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "static_memory_gremlin.h"
00019 #include "string_hasher.h"
00020
00021 #include <basis/function.h>
00022 #include <basis/istring.h>
00023
00024 #include <stdio.h>
00025
00026 const int MAX_STRING_CHARS_USED = 3;
00027
00028
00029
00031
00032 hashing_algorithm *string_hasher::clone() const
00033 { return new string_hasher; }
00034
00035 u_int string_hasher::hash(const void *key_data, int key_length_in) const
00036 {
00037 if (!key_data) return 0;
00038 if (key_length_in <= 1) return 0;
00039
00040 byte *our_key = (byte *)key_data;
00041 byte hashed[4] = { 0, 0, 0, 0 };
00042
00043 int key_length = minimum(key_length_in - 1, MAX_STRING_CHARS_USED);
00044
00045 int fill_posn = 0;
00046
00047 for (int i = 0; i < key_length; i++) {
00048
00049 hashed[fill_posn] = hashed[fill_posn] + our_key[i];
00050 fill_posn++;
00051 if (fill_posn >= 4) fill_posn = 0;
00052
00053 hashed[fill_posn] = hashed[fill_posn] + (our_key[i] / 4);
00054 }
00055
00056 for (int k = key_length_in - 2;
00057 (k >= 0) && (k >= key_length_in - 1 - key_length); k--) {
00058
00059 hashed[fill_posn] = hashed[fill_posn] + our_key[k];
00060 fill_posn++;
00061 if (fill_posn >= 4) fill_posn = 0;
00062
00063 hashed[fill_posn] = hashed[fill_posn] + (our_key[k] / 4);
00064 }
00065
00066 u_int to_return = 0;
00067 for (int j = 0; j < 4; j++) to_return = (to_return << 8) + hashed[j];
00068 return to_return;
00069 }
00070
00072
00073 hashing_algorithm *istring_hasher::clone() const
00074 { return new istring_hasher; }
00075
00076 u_int istring_hasher::hash(const void *key_data, int key_length_in) const
00077 {
00078 if (!key_data) return 0;
00079 const istring *real_key = (const istring *)key_data;
00080 if (real_key->length() + 1 != key_length_in) {
00081
00082
00083 }
00084 return string_hasher().hash((const void *)real_key->observe(),
00085 real_key->length() + 1);
00086 }
00087
00088
00089 #endif //STRING_HASHER_IMPLEMENTATION_FILE
00090