make-compact.C
9.52 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*******************************************************************/
/* */
/* FILE make-compact.C */
/* MODULE make-compact */
/* PROGRAM SFST */
/* AUTHOR Helmut Schmid, IMS, University of Stuttgart */
/* */
/* PURPOSE Code needed for generating compact automata */
/* */
/*******************************************************************/
#include <math.h>
#include "make-compact.h"
using std::equal_range;
using std::sort;
using std::cerr;
class ARC {
public:
int cv;
Label label;
unsigned int target_node;
bool operator< ( const ARC a ) const {
return cv < a.cv;
};
};
typedef hash_map<Label, size_t, Label::label_hash, Label::label_eq> LabelNumber;
/*******************************************************************/
/* */
/* MakeCompactTransducer::sort */
/* */
/*******************************************************************/
void MakeCompactTransducer::sort( Level level )
{
for( unsigned int n=0; n<number_of_nodes; n++) {
unsigned int from=first_arc[n];
unsigned int to=first_arc[n+1];
int l=to-from;
// copy the arcs to a temporary table
ARC *arc=new ARC[l];
for( unsigned int i=from; i<to; i++) {
arc[i-from].cv = (int)label[i].get_char(level);
// make sure that epsilon arcs are stored at the beginning
// even if epsilon is not 0
if (arc[i-from].cv == (int)Label::epsilon)
arc[i-from].cv = -1;
arc[i-from].label = label[i];
arc[i-from].target_node = target_node[i];
}
// sort the table
::sort( arc, arc+l );
// copy the arcs back to the original table
for( unsigned int i=from; i<to; i++) {
label[i] = arc[i-from].label;
target_node[i] = arc[i-from].target_node;
}
delete[] arc;
}
}
/*******************************************************************/
/* */
/* MakeCompactTransducer::count_arcs */
/* */
/*******************************************************************/
void MakeCompactTransducer::count_arcs( Node *node, NodeNumbering &index,
VType vmark )
{
if (!node->was_visited( vmark )) {
unsigned int n = index[node];
finalp[n] = node->is_final();
first_arc[n] = 0;
Arcs *arcs=node->arcs();
for( ArcsIter p(arcs); p; p++ ) {
Arc *arc=p;
first_arc[n]++;
count_arcs(arc->target_node(), index, vmark);
}
}
}
/*******************************************************************/
/* */
/* MakeCompactTransducer::store_arcs */
/* */
/*******************************************************************/
void MakeCompactTransducer::store_arcs( Node *node, NodeNumbering &index,
VType vmark )
{
if (!node->was_visited( vmark )) {
unsigned int n=first_arc[index[node]];
Arcs *arcs=node->arcs();
for( ArcsIter p(arcs); p; p++ ) {
Arc *arc=p;
label[n] = arc->label();
target_node[n++] = index[arc->target_node()];
store_arcs(arc->target_node(), index, vmark);
}
}
}
/*******************************************************************/
/* */
/* MakeCompactTransducer::MakeCompactTransducer */
/* */
/*******************************************************************/
MakeCompactTransducer::MakeCompactTransducer( Transducer &a, Level l )
{
if (a.is_infinitely_ambiguous()) {
cerr << "Error: resulting transducer contains an infinite loop!\n";
exit(1);
}
NodeNumbering index(a);
alphabet.copy(a.alphabet);
// memory allocation
number_of_nodes = (unsigned)index.number_of_nodes();
finalp = new char[number_of_nodes];
first_arc = new unsigned int[number_of_nodes+1];
// count the number of outgoing arcs for each node
// and store them in first_arc[]
a.incr_vmark();
count_arcs( a.root_node(), index, a.vmark );
for( int n=number_of_nodes; n>0; n-- )
first_arc[n] = first_arc[n-1];
first_arc[0] = 0;
for( unsigned int n=0; n<number_of_nodes; n++ )
first_arc[n+1] += first_arc[n];
number_of_arcs = first_arc[number_of_nodes];
// memory allocation
label = new Label[number_of_arcs];
target_node = new unsigned int[number_of_arcs];
// store the arcs
a.incr_vmark();
store_arcs( a.root_node(), index, a.vmark );
// sort the arcs
sort( l );
}
/*******************************************************************/
/* */
/* MakeCompactTransducer::store_finalp */
/* */
/*******************************************************************/
void MakeCompactTransducer::store_finalp( FILE *file )
{
int k=0;
unsigned char n=0;
for( size_t i=0; i<number_of_nodes; i++ ) {
n = (unsigned char)(n << 1);
if (finalp[i])
n |= 1;
if (++k == 8) {
fputc(n, file);
n = 0;
k = 0;
}
}
if (k > 0) {
n = (unsigned char)(n << (8-k));
fputc(n, file);
}
}
/*******************************************************************/
/* */
/* MakeCompactTransducer::store_first_arcs */
/* */
/* The data is encoded with the minimal number of bits needed. */
/* */
/*******************************************************************/
void MakeCompactTransducer::store_first_arcs( FILE *file )
{
int k=0;
unsigned int n=0;
// compute number of bits required for storing each item
int bits=(int)ceil(log(number_of_arcs+1)/log(2));
for( size_t i=0; i<=number_of_nodes; i++ ) {
unsigned int m=first_arc[i];
m <<= (sizeof(n)*8) - bits;
m >>= k;
n = n | m;
k += bits;
if (k >= (int)sizeof(n)*8) {
fwrite(&n, sizeof(n), 1, file);
k -= (int)sizeof(n) * 8;
n = first_arc[i];
if (k == 0)
n = 0;
else
n = first_arc[i] << (sizeof(n) * 8 - k);
}
}
if (k > 0)
fwrite(&n, sizeof(n), 1, file);
}
/*******************************************************************/
/* */
/* MakeCompactTransducer::store_target_nodes */
/* */
/*******************************************************************/
void MakeCompactTransducer::store_target_nodes( FILE *file )
{
int k=0;
unsigned int n=0;
int bits=(int)ceil(log(number_of_nodes)/log(2));
for( size_t i=0; i<number_of_arcs; i++ ) {
unsigned int m=target_node[i];
m <<= (sizeof(n)*8) - bits;
m >>= k;
n = n | m;
k += bits;
if (k >= (int)sizeof(n)*8) {
fwrite(&n, sizeof(n), 1, file);
k -= (int)sizeof(n)*8;
if (k == 0)
n = 0;
else
n = target_node[i] << (sizeof(n) * 8 - k);
}
}
if (k > 0)
fwrite(&n, sizeof(n), 1, file);
}
/*******************************************************************/
/* */
/* MakeCompactTransducer::store_labels */
/* */
/*******************************************************************/
void MakeCompactTransducer::store_labels( FILE *file )
{
size_t N=0;
LabelNumber LNum;
for( Alphabet::const_iterator it=alphabet.begin();
it != alphabet.end(); it++ )
{
Label l=*it;
LNum[l] = N++;
}
int k=0;
unsigned int n=0;
int bits=(int)ceil(log((double)alphabet.size())/log(2));
for( size_t i=0; i<number_of_arcs; i++ ) {
unsigned int l = (unsigned)LNum[label[i]];
unsigned int m=l;
m <<= (sizeof(n)*8) - bits;
m >>= k;
n = n | m;
k += bits;
if (k >= (int)sizeof(n)*8) {
fwrite(&n, sizeof(n), 1, file);
k -= (int)sizeof(n)*8;
if (k == 0)
n = 0;
else
n = l << (sizeof(n) * 8 - k);
}
}
if (k > 0)
fwrite(&n, sizeof(n), 1, file);
}
/*******************************************************************/
/* */
/* MakeCompactTransducer::store */
/* */
/*******************************************************************/
void MakeCompactTransducer::store( FILE *file )
{
fputc('c',file);
alphabet.store(file);
fwrite(&number_of_nodes, sizeof(number_of_nodes), 1, file);
fwrite(&number_of_arcs, sizeof(number_of_arcs), 1, file);
store_finalp(file);
store_first_arcs(file);
store_labels(file);
store_target_nodes(file);
if (ferror(file))
throw "Error encountered while writing transducer to file\n";
}