Few more things
[apertium.git] / lttoolbox / lttoolbox / TransExe.C
blobeb33731f65be243eada8257b1bc3185d0094f8d1
1 /*
2  * Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
20 #include <lttoolbox/TransExe.H>
21 #include <lttoolbox/Compression.H>
22 #include <lttoolbox/MyStdio.H>
24 TransExe::TransExe()
28 TransExe::~TransExe()
30   destroy();
33 TransExe::TransExe(TransExe const &te)
35   copy(te);
38 TransExe &
39 TransExe::operator =(TransExe const &te)
41   if(this != &te)
42   {
43     destroy();
44     copy(te);
45   }
46   return *this;
49 void
50 TransExe::copy(TransExe const &te)
52   initial_id = te.initial_id;
53   node_list = te.node_list;
54   finals = te.finals;
57 void
58 TransExe::destroy()
62 void
63 TransExe::read(FILE *input, int encoding_base)
65   TransExe &new_t = *this;
66   new_t.destroy();
67   new_t.initial_id = Compression::multibyte_read(input);
68   int finals_size = Compression::multibyte_read(input);
70   int base = 0;
72   set<int> myfinals;
74   while(finals_size > 0)
75   {
76     finals_size--;
78     base += Compression::multibyte_read(input);
79     myfinals.insert(base);
80   }
82   base = Compression::multibyte_read(input);
84   int number_of_states = base;
85   int current_state = 0;   
86   new_t.node_list.resize(number_of_states);
88   for(set<int>::iterator it = myfinals.begin(), limit = myfinals.end(); 
89       it != limit; it++)
90   {
91     new_t.finals.insert(&new_t.node_list[*it]);
92   }
94   while(number_of_states > 0)
95   {
96     int number_of_local_transitions = Compression::multibyte_read(input);
97     int tagbase = 0;
98     
99     Node &mynode = new_t.node_list[current_state];
101     while(number_of_local_transitions > 0)
102     {
103       number_of_local_transitions--;
104       tagbase += Compression::multibyte_read(input);
105       int state = (current_state + Compression::multibyte_read(input)) % base;
107       unsigned short i_symbol = static_cast<unsigned short>(tagbase / encoding_base);
108       unsigned short o_symbol = static_cast<unsigned short>(tagbase % encoding_base);
110       mynode.addTransition(i_symbol, o_symbol, &new_t.node_list[state]);
111     }   
112     number_of_states--;
113     current_state++;
114   }
117 void
118 TransExe::unifyFinals()
120   node_list.resize(node_list.size()+1);
122   Node *newfinal = &node_list[node_list.size()-1];
124   for(set<Node *>::iterator it = finals.begin(), limit = finals.end(); 
125       it != limit; it++)
126   {
127     (*it)->addTransition(0, 0, newfinal);
128   }
129   
130   finals.clear();
131   finals.insert(newfinal);
134 Node *
135 TransExe::getInitial()
137   return &node_list[initial_id];
140 set<Node *> &
141 TransExe::getFinals()
143   return finals;