00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <iostream>
00027 #include <cassert>
00028 #include "mempool.h"
00029 #include "htable.h"
00030
00031 using namespace std;
00032
00033 template <>
00034 void htable<int*>::set_keylen(int kl)
00035 {
00036 keylen=kl/sizeof(int);
00037 return;
00038 }
00039
00040 template <>
00041 void htable<char*>::set_keylen(int kl)
00042 {
00043 keylen=kl;
00044 return;
00045 }
00046
00047 template <>
00048 address htable<int *>::Hash(int* key)
00049 {
00050 address h;
00051 register int i;
00052
00053
00054 for (i=0,h=0; i<keylen; i++) {
00055 h+=key[i];
00056 h += ~(h << 15);
00057 h ^= (h >> 10);
00058 h += (h << 3);
00059 h ^= (h >> 6);
00060 h += ~(h << 11);
00061 h ^= (h >> 16);
00062 };
00063
00064 return h;
00065 }
00066
00067 template <>
00068 address htable<char *>::Hash(char* key)
00069 {
00070
00071 char *Key = *(char**)key;
00072 int length=strlen(Key);
00073
00074 register address h=0;
00075 register int i;
00076
00077 for (i=0,h=0; i<length; i++)
00078 h = h * Prime1 ^ (Key[i] - ' ');
00079 h %= Prime2;
00080
00081 return h;
00082 }
00083
00084 template <>
00085 int htable<int*>::Comp(int *key1, int *key2)
00086 {
00087 assert(key1 && key2);
00088
00089 register int i;
00090
00091 for (i=0; i<keylen; i++)
00092 if (key1[i]!=key2[i]) return 1;
00093 return 0;
00094 }
00095
00096 template <>
00097 int htable<char*>::Comp(char *key1, char *key2)
00098 {
00099 assert(key1 && key2);
00100
00101 char *Key1 = *(char**)key1;
00102 char *Key2 = *(char**)key2;
00103
00104 assert(Key1 && Key2);
00105
00106 return (strcmp(Key1,Key2));
00107 }