00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef MF_HTABLE_H
00024 #define MF_HTABLE_H
00025
00026 #include <iostream>
00027
00028 #define Prime1 37
00029 #define Prime2 1048583
00030 #define BlockSize 100
00031
00032
00033
00034
00035
00036 typedef struct ENTRY{
00037 char* key;
00038 ENTRY* next;
00039 }entry;
00040
00041 typedef unsigned int address;
00042
00043 typedef enum {HT_FIND,
00044 HT_ENTER,
00045 HT_INIT,
00046 HT_CONT
00047 } HT_ACTION;
00048
00049 typedef enum {
00050 STR,
00051 STRPTR,
00052 INT,
00053 INTPTR
00054 }HTYPE;
00055
00057
00058 class htable {
00059 int size;
00060 int keylen;
00061 HTYPE htype;
00062 entry **table;
00063 int scan_i;
00064 entry *scan_p;
00065
00066 long keys;
00067 long accesses;
00068 long collisions;
00069
00070 mempool *memory;
00071
00072 size_t (*keylenfunc)(const char*);
00073 address (*hashfunc)(const char*);
00074 int (*compfunc)(const char*, const char*);
00075
00076 public:
00077
00079 htable(int n,int kl=0,HTYPE ht=STRPTR,size_t (*klf)(const char* )=NULL);
00080
00082 ~htable();
00083
00085 address Hash(char *key){
00086 switch (htype){
00087 case INT:case INTPTR: return HashInt(key);
00088 break;
00089 case STR:case STRPTR: return HashStr(key);
00090 }
00091 return 0;
00092 }
00093 address HashInt(char *key);
00094 address HashStr(char *key);
00095
00097 int Comp(char *Key1,char *Key2){
00098 switch (htype){
00099 case INT:case INTPTR: return CompInt(Key1,Key2);
00100 break;
00101 case STR:case STRPTR: return CompStr(Key1,Key2);
00102 }
00103 return 0;
00104 }
00105
00106 int CompInt(char *Key1,char *Key2);
00107 int CompStr(char *Key1,char *Key2);
00108
00110 char *search(char *item, HT_ACTION action);
00111
00113 char *scan(HT_ACTION action);
00114
00116 void stat();
00117
00119 void map(std::ostream& co=std::cout, int cols=80);
00120
00122 int used(){
00123 return size * sizeof(entry **) + memory->used();
00124 }
00125 };
00126
00127
00128
00129 #endif
00130
00131
00132