00001 00002 #ifndef STRTABLE_H 00003 #define STRTABLE_H 00004 00005 00006 /*===============================<o>===================================== 00007 00008 Copyright 1996, 1997, 2004 Ian Kaplan, Bear Products International, 00009 www.bearcave.com. 00010 00011 All Rights Reserved 00012 00013 You may use this software in software components for which you do 00014 not collect money (e.g., non-commercial software). All commercial 00015 use is reserved. 00016 00017 ===============================<o>=====================================*/ 00018 00019 00020 /* 00021 include dependencies: 00022 00023 <string.h> -- strlen(), strcmp(), etc... 00024 <stdio.h> -- for printf and FILE (needed by str.h) 00025 stdtypes.h 00026 blockpool.h 00027 pools.h 00028 str.h 00029 list.h 00030 hash_serv.h 00031 sparse.h 00032 00033 */ 00034 00035 #include <stdio.h> 00036 00037 #include "stdtypes.h" 00038 #include "blockpool.h" 00039 #include "pools.h" 00040 #include "str.h" 00041 #include "list.h" 00042 #include "hash_serv.h" 00043 #include "sparse.h" 00044 00056 class strtable : public hash_services 00057 { 00058 private: 00059 00060 // class for the hash collision chains 00061 class chain_elem { 00062 public: 00063 LIST<STRING *> list; 00064 private: 00065 chain_elem(const chain_elem &) {} 00066 public: 00067 chain_elem(void) { list = LIST<STRING *>(); } 00068 ~chain_elem(void) { list.dealloc(); } 00069 00070 STRING *search_list( STRING item ); 00071 00072 unsigned int list_len(void) 00073 { 00074 LIST<STRING *>::handle h; 00075 int len = 0;; 00076 00077 for (h = list.first(); h != NULL; h = list.next(h)) { 00078 len++; 00079 } // for 00080 return len; 00081 } // list_len 00082 00083 }; // class chain_elem 00084 00085 unsigned int table_size; 00086 unsigned int hash_slot; // used in iterating through the hash table 00087 LIST<STRING *>::handle list_handle; 00088 00089 sparse_array<chain_elem> *hash; 00090 pool *alloc_pool; 00091 private: 00092 00093 STRING *new_item( STRING str ); 00094 00095 public: 00096 strtable( unsigned int size, pool *p ) 00097 { 00098 00099 assert( p != NULL ); 00100 alloc_pool = p; 00101 hash = new sparse_array<chain_elem>( (const unsigned int)size ); 00102 table_size = hash->get_total_size(); 00103 } 00104 void dealloc() 00105 { 00106 hash->dealloc(); 00107 00108 delete hash; 00109 } 00110 ~strtable() 00111 { 00112 dealloc(); 00113 } 00114 00115 STRING find_string( STRING item, Boolean insert = TRUE); 00116 00117 STRING find_string( const char *str, Boolean insert = TRUE ) 00118 { 00119 STRING local_str; 00120 00121 local_str.SetText( str ); 00122 return find_string( local_str, insert ); 00123 } // find_item with a char * argument 00124 00125 // note that hash can never be NULL, since it is initialized in the constructor 00126 unsigned int get_percent_alloced(void) { return hash->get_percent_alloced(); } 00127 00128 unsigned int get_max_list(void) 00129 { 00130 uint i; 00131 uint max, len; 00132 00133 max = 0; 00134 for (i = 0; i < table_size; i++) { 00135 if (hash->probe( i )) { 00136 len = (*hash)[i].list_len(); 00137 if (len > max) { 00138 max = len; 00139 } 00140 } 00141 } 00142 return max; 00143 } // get_max_list 00144 00145 void pr(FILE *fp = stdout); 00146 00159 STRING first(void) 00160 { 00161 hash_slot = 0; 00162 list_handle = NULL; 00163 return get_str(); 00164 } 00165 00166 STRING get_str(void); 00167 00168 }; // class strtable 00169 00170 00171 // 00172 // square root of the string table size 00173 // 00174 const uint SQRT_STRTAB = 1024; 00175 00176 extern strtable strtab; 00177 00178 #endif