[go: up one dir, main page]

Menu

[r199]: / libpetey / symbol_table.cc  Maximize  Restore  History

Download this file

160 lines (134 with data), 2.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <assert.h>
#include "string_operators.h"
#include "symbol_table.h"
template <class sym_t>
void symbol_table<sym_t>::collect_garbage(long &ndup) {
update();
long *result;
long *ind;
long k;
ndup=n-nunq;
result=new long[ndup];
ind=heapsort(name, n);
k=0;
for (long i=1; i<n; i++) {
if (name[ind[i-1]]==name[ind[i]]) {
if (ind[i-1] > ind[i]) {
result[k]=ind[i];
continue;
}
} else {
result[k]=ind[i-1];
k++;
}
}
assert(k==ndup);
delete [] ind;
return result;
}
template <class sym_t>
void symbol_table<sym_t>::update() {
long k;
if (update_flag) return;
if (sind!=NULL) delete [] sind;
sind=heapsort(name, n);
//search for duplicates (no garbage collection yet):
k=1;
for (long i=1; i<n; i++) {
if (sym[ind[i-1]]==sym[ind[i]]) {
if (ind[i-1] > ind[i]) {
k++;
continue;
}
} else {
k++;
}
sind[k]=sind[i];
}
nunq=k;
update_flag=1;
}
}
template <class sym_t>
symbol_table<sym_t>::symbol_table() {
array_size=1;
n=0;
nunq=0;
sym=new sym_t[array_size];
sind=NULL;
update_flag=1;
}
template <class sym_t>
symbol_table<sym_t, var_t>::~symbol_table() {
delete [] sym;
if (sind!=NULL) delete [] sind;
}
template <class sym_t>
long symbol_table::add(sym_t name) {
char **sym_new;
//if new symbol doesn't fit, increase size of array:
if (n>=nmax) {
array_size=array_size*2;
sym_new=new sym_t[array_size];
for (long i=0; i<n; i++) {
sym_new[i]=sym[i];
var_new[i]=var[i];
}
delete [] sym;
delete [] var;
sym=sym_new;
}
//pretty basic, just paste the two values onto the ends of the arrays:
sym[n]=name;
n++;
update_flag=0;
}
//looks up a symbol in the table and returns a unique id:
template <class sym_t>
long symbol_table<sym_t>::lookup(sym_t name)
long low, mid, high;
update();
if (n==0) return -1;
low=0;
high=nunq-1;
//binary search:
if (sym[sind[low]]==name) {
loc=low;
return sind[loc];
}
if (sym[sind[low]] > name) {
return -1;
}
if (if sym[sind[high]]==name) {
loc=high;
return sind[loc];
}
if (sym[sind[high]] < name) {
return -1;
}
while (high-low > 1) {
mid=(low+high)/2;
if (if sym[sind[mid]] < name) {
low=mid;
} else if (sym[sind[mid]] > name) {
high=mid;
} else {
loc=mid;
return sind[mid];
}
}
return -1;
}
template <class sym_t>
sym_t symbol_table<sym_t>::get_sym(long id) {
return name[id];
}
template <class sym_t>
long symbol_table<sym_t>::entries() {
return n;
}
template class symbol_table<char *>;
template class symbol_table<char *>;
template class symbol_table<float>;
template class symbol_table<long>;
template class symbol_table<char>