| [25ae32e] | 1 | #include "symtab.h" |
|---|
| 2 | #include <values.h> |
|---|
| [1e121f4] | 3 | #include <cstdio> |
|---|
| 4 | //#include <alloc.h> |
|---|
| 5 | #include <cstdlib> |
|---|
| 6 | using namespace std; |
|---|
| [25ae32e] | 7 | //--------------------------------------------------------------------------- |
|---|
| 8 | |
|---|
| 9 | SymbolTable::SymbolTable(int n, int (*h)(const char*,int), const char* filename) |
|---|
| 10 | : _mx(n), _cnt(0), hash(h) |
|---|
| 11 | { |
|---|
| 12 | _sz=first(n); |
|---|
| 13 | _key=new char*[_sz]; |
|---|
| 14 | _defind=new int[_sz]; |
|---|
| 15 | _hashind=new int[_sz]; |
|---|
| 16 | _def=new char*[_mx]; |
|---|
| 17 | for(int i=0; i<_sz; i++) _key[i]=NULL; |
|---|
| 18 | if(filename) |
|---|
| 19 | add_from_file(filename); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | //--------------------------------------------------------------------------- |
|---|
| 23 | |
|---|
| 24 | SymbolTable::SymbolTable(int n, const char* filename) |
|---|
| 25 | : _mx(n), _cnt(0), hash(hash1) |
|---|
| 26 | { |
|---|
| 27 | _sz=first(n); |
|---|
| 28 | _key=new char*[_sz]; |
|---|
| 29 | _defind=new int[_sz]; |
|---|
| 30 | _hashind=new int[_sz]; |
|---|
| 31 | _def=new char*[_mx]; |
|---|
| 32 | for(int i=0; i<_sz; ++i) _key[i]=NULL; |
|---|
| 33 | if(filename) |
|---|
| 34 | add_from_file(filename); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | //--------------------------------------------------------------------------- |
|---|
| 38 | |
|---|
| 39 | SymbolTable::~SymbolTable() |
|---|
| 40 | { |
|---|
| 41 | clear(); |
|---|
| 42 | delete[] _key; |
|---|
| 43 | delete[] _defind; |
|---|
| 44 | delete[] _hashind; |
|---|
| 45 | delete[] _def; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | //--------------------------------------------------------------------------- |
|---|
| 49 | |
|---|
| 50 | void SymbolTable::clear() |
|---|
| 51 | { |
|---|
| 52 | for(int i=0; i<_sz; ++i) |
|---|
| 53 | if(_key[i]) |
|---|
| 54 | free(_key[i]); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | //--------------------------------------------------------------------------- |
|---|
| 58 | |
|---|
| 59 | bool SymbolTable::add_from_file(const char* filename) |
|---|
| 60 | { |
|---|
| 61 | FILE* in=fopen(filename,"r"); |
|---|
| 62 | char buf[MAXKEYLEN+1]; |
|---|
| 63 | |
|---|
| 64 | if(in) |
|---|
| 65 | while(fscanf(in,"%s",buf)==1) |
|---|
| 66 | { |
|---|
| 67 | if(strlen(buf)==MAXKEYLEN || add(buf)<0) |
|---|
| 68 | return false; |
|---|
| 69 | } |
|---|
| 70 | return true; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | //--------------------------------------------------------------------------- |
|---|
| 74 | |
|---|
| 75 | int SymbolTable::add(const char* s) |
|---|
| 76 | { |
|---|
| 77 | if(_cnt<_mx) |
|---|
| 78 | { |
|---|
| 79 | int ind=hash(s,_sz); |
|---|
| 80 | while(_key[ind]) |
|---|
| 81 | if(strcmp(_key[ind],s)) |
|---|
| 82 | ind=++ind%_sz; |
|---|
| 83 | else |
|---|
| 84 | return _defind[ind]; |
|---|
| 85 | _key[ind]=strdup(s); |
|---|
| 86 | _defind[ind]=_cnt; |
|---|
| 87 | _hashind[_cnt]=ind; |
|---|
| 88 | _def[_cnt]=_key[ind]; |
|---|
| 89 | _cnt++; |
|---|
| 90 | return _cnt-1; |
|---|
| 91 | } |
|---|
| 92 | else |
|---|
| 93 | return -1; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | //--------------------------------------------------------------------------- |
|---|
| 97 | |
|---|
| 98 | int SymbolTable::operator[](const char* s) |
|---|
| 99 | { |
|---|
| 100 | int ind=hash(s,_sz); |
|---|
| 101 | while(_key[ind]) |
|---|
| 102 | if(strcmp(_key[ind],s)==0) |
|---|
| 103 | return _defind[ind]; |
|---|
| 104 | else |
|---|
| 105 | ind=++ind % _sz; |
|---|
| 106 | return -1; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | //--------------------------------------------------------------------------- |
|---|
| 110 | |
|---|
| 111 | int SymbolTable::first(unsigned int n) |
|---|
| 112 | { |
|---|
| 113 | int fi=n; |
|---|
| 114 | int bound=(n/2 < MAXKEYLEN)? n/2 : MAXKEYLEN; |
|---|
| 115 | bool found; |
|---|
| 116 | do |
|---|
| 117 | { |
|---|
| 118 | found=true; |
|---|
| 119 | if(fi++ == MAXINT) return -1; |
|---|
| 120 | for(int i=2; i<bound; i++) |
|---|
| 121 | if(fi%i==0) { found=false; break; } |
|---|
| 122 | } while(!found); |
|---|
| 123 | return fi; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | float SymbolTable::search_rate() |
|---|
| 127 | { |
|---|
| 128 | long s=0; |
|---|
| 129 | for(int i=0; i<_sz; i++) |
|---|
| 130 | if(_key[i]) |
|---|
| 131 | s+=(i+_sz-hash(_key[i],_sz))%_sz+1; |
|---|
| 132 | return _cnt ? (float)s/(float)_cnt : 0; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | //--------------------------------------------------------------------------- |
|---|
| 136 | |
|---|
| 137 | int hash1(const char* s, int _sz) |
|---|
| 138 | { |
|---|
| 139 | int l=strlen(s); |
|---|
| 140 | if(l>=4) |
|---|
| 141 | return abs((*((int*)(s+(l/2-2)))+(int)(*s * s[l-1])) % _sz); |
|---|
| 142 | else |
|---|
| 143 | { |
|---|
| 144 | int i=0; |
|---|
| 145 | strcpy((char*)&i,s); |
|---|
| 146 | return abs((i+(int)(*s * s[l-1])) % _sz); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | //--------------------------------------------------------------------------- |
|---|
| 151 | |
|---|
| 152 | int hash2(const char* s, int _sz) |
|---|
| 153 | { |
|---|
| 154 | int l=strlen(s); |
|---|
| 155 | if(l>=6) |
|---|
| 156 | { |
|---|
| 157 | unsigned int i1,i2,i3; |
|---|
| 158 | strncpy((char*)&i1,s,sizeof(int)); |
|---|
| 159 | strncpy((char*)&i2,s+(l/2-2),sizeof(int)); |
|---|
| 160 | strncpy((char*)&i3,s+(l-4),sizeof(int)); |
|---|
| [1e121f4] | 161 | //return abs((i1+i2+i3) % _sz); |
|---|
| 162 | return (i1+i2+i3) % _sz; |
|---|
| [25ae32e] | 163 | } |
|---|
| 164 | else |
|---|
| 165 | { |
|---|
| 166 | int i=0; |
|---|
| 167 | strncpy((char*)&i,s,sizeof(int)); |
|---|
| 168 | return abs((i+(int)(*s * s[l-1])) % _sz); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | //--------------------------------------------------------------------------- |
|---|
| 173 | |
|---|