source: app/src/lib/symtab.cc @ 25ae32e

help
Last change on this file since 25ae32e was 25ae32e, checked in by obrebski <obrebski@…>, 16 years ago

git-svn-id: svn://atos.wmid.amu.edu.pl/utt@4 e293616e-ec6a-49c2-aa92-f4a8b91c5d16

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