aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmIndexed.hi
blob: 670f03d76a9af112e6948b88fb63b6240a4f5f2c (plain)
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

/*=============================================================================*\
 * File: gfsmIndexed.hi
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library: indexed automaton: inline definitions
 *
 * Copyright (c) 2007 Bryan Jurish.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *=============================================================================*/

/*======================================================================
 * Constructors etc.
 */

//----------------------------------------
gfsmIndexedAutomaton *gfsm_indexed_automaton_new_full(gfsmAutomatonFlags flags,
						      gfsmSRType         srtype,
						      gfsmStateId        n_states,
						      guint              n_arcs)
						      
{
  gfsmIndexedAutomaton *xfsm = g_new0(gfsmIndexedAutomaton,1);
#if 0
  gfsmStateId qid;
  gfsmWeight  srzero;
#endif

  xfsm->flags   = flags;
  xfsm->sr      = gfsm_semiring_new(srtype);
  xfsm->root_id = gfsmNoState;

  //xfsm->state_is_valid     = gfsm_bitvector_sized_new(n_states);
  xfsm->state_final_weight = gfsm_weight_vector_sized_new(n_states);
  xfsm->arcs               = gfsm_arc_table_index_sized_new(n_states, n_arcs);

#if 0
  //-- initialize: states
  for (qid=0; qid < n_states; qid++) {
    g_array_index(xfsm->state_final_weight,gfsmWeight,qid) = xfsm->sr->zero;
  }
#endif

  return xfsm;
}

//----------------------------------------
GFSM_INLINE
gfsmIndexedAutomaton *gfsm_indexed_automaton_new(void)
{
  return gfsm_indexed_automaton_new_full(gfsmAutomatonDefaultFlags,
					 gfsmAutomatonDefaultSRType,
					 gfsmAutomatonDefaultSize,
					 gfsmAutomatonDefaultSize
					 );
}


//----------------------------------------
GFSM_INLINE
gfsmIndexedAutomaton *gfsm_indexed_automaton_clone(gfsmIndexedAutomaton *src)
{
  return gfsm_indexed_automaton_copy(gfsm_indexed_automaton_new_full(src->flags,
								     src->sr->type,
								     gfsm_indexed_automaton_n_states(src),
								     gfsm_indexed_automaton_n_arcs(src)),
				     src);
}

//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_clear(gfsmIndexedAutomaton *xfsm)
{
  //gfsm_bitvector_clear(xfsm->state_is_valid);
  gfsm_weight_vector_resize(xfsm->state_final_weight,0);
  gfsm_arc_table_index_resize(xfsm->arcs,0,0);
  xfsm->root_id = gfsmNoState;
  return;
 }

//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_free(gfsmIndexedAutomaton *xfsm)
{
  if (!xfsm) return;
  if (xfsm->sr)                 gfsm_semiring_free(xfsm->sr);
  //if (xfsm->state_is_valid)     gfsm_bitvector_free(xfsm->state_is_valid);
  if (xfsm->state_final_weight) gfsm_weight_vector_free(xfsm->state_final_weight);
  if (xfsm->arcs)               gfsm_arc_table_index_free(xfsm->arcs);
  g_free(xfsm);
}

/*======================================================================
 * Methods: Import & Export
 */
//-- EXTERN


/*======================================================================
 * Methods: Accessors: gfsmIndexedAutomaton
 */

//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_reserve_states(gfsmIndexedAutomaton *xfsm, gfsmStateId n_states)
{
#if 0
  gfsmStateId n_states_old = gfsm_indexed_automaton_n_states(xfsm);
  gfsmStateId qid;
  gfsmWeight  srzero;
#endif

  //-- resize state-indexed arrays
  //gfsm_bitvector_resize(xfsm->state_is_valid, n_states);
  gfsm_weight_vector_resize(xfsm->state_final_weight, n_states);
  gfsm_arc_table_index_resize(xfsm->arcs, n_states, xfsm->arcs->tab->len);

#if 0
  //-- ... adjust final weights
  srzero = xfsm->sr->zero;
  for (qid=n_states_old; qid < n_states; qid++) {
    g_array_index(xfsm->state_final_weight,gfsmWeight,qid) = srzero;
  }
#endif
}

//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_reserve_arcs(gfsmIndexedAutomaton *xfsm, guint n_arcs)
{
  gfsm_arc_table_index_resize(xfsm->arcs, xfsm->arcs->first->len-1, n_arcs);
}

//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_sort(gfsmIndexedAutomaton *xfsm, gfsmArcCompMask sort_mask)
{
  if (xfsm->flags.sort_mode != sort_mask && sort_mask != gfsmASMNone) {
    gfsm_arc_table_index_sort_bymask(xfsm->arcs, sort_mask, xfsm->sr);
  }
  xfsm->flags.sort_mode = sort_mask;
}


/*======================================================================
 * Methods: Accessors: gfsmAutomaton API: Automaton
 */

//----------------------------------------
GFSM_INLINE
gfsmSemiring *gfsm_indexed_automaton_set_semiring(gfsmIndexedAutomaton *xfsm, gfsmSemiring *sr)
{
  if (xfsm->sr) gfsm_semiring_free(xfsm->sr);
  xfsm->sr = gfsm_semiring_copy(sr);
  return sr;
}

//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_set_semiring_type(gfsmIndexedAutomaton *xfsm, gfsmSRType srtype)
{
  if (!xfsm->sr) xfsm->sr = gfsm_semiring_new(srtype);
  else if (xfsm->sr->type != srtype) {
    gfsm_semiring_free(xfsm->sr);
    xfsm->sr = gfsm_semiring_new(srtype);  
  }
}

//----------------------------------------
GFSM_INLINE
gfsmStateId gfsm_indexed_automaton_n_states(gfsmIndexedAutomaton *xfsm)
{ return xfsm->state_final_weight->len; }

//----------------------------------------
GFSM_INLINE
guint gfsm_indexed_automaton_n_arcs(gfsmIndexedAutomaton *xfsm)
{ return xfsm->arcs->tab->len; }

//----------------------------------------
GFSM_INLINE
guint gfsm_indexed_automaton_get_root(gfsmIndexedAutomaton *xfsm)
{ return xfsm->root_id; }

//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_set_root(gfsmIndexedAutomaton *xfsm, gfsmStateId qid)
{
  if (qid >= gfsm_indexed_automaton_n_states(xfsm) && qid != gfsmNoState) {
    gfsm_indexed_automaton_reserve_states(xfsm,qid+1);
  }
  xfsm->root_id = qid;
}

/*======================================================================
 * Methods: Accessors: gfsmAutomaton API: States
 */

//----------------------------------------
GFSM_INLINE
gboolean gfsm_indexed_automaton_has_state(gfsmIndexedAutomaton *xfsm, gfsmStateId qid)
{ return qid < gfsm_indexed_automaton_n_states(xfsm); }

//----------------------------------------
GFSM_INLINE
gfsmStateId gfsm_indexed_automaton_ensure_state(gfsmIndexedAutomaton *xfsm, gfsmStateId qid)
{
  if (qid >= gfsm_indexed_automaton_n_states(xfsm) && qid != gfsmNoState) {
    gfsm_indexed_automaton_reserve_states(xfsm,qid+1);
  }
  return qid;
}

//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_remove_state(gfsmIndexedAutomaton *fsm, gfsmStateId qid)
{ return; }


//----------------------------------------
GFSM_INLINE
void gfsm_indexed_automaton_set_final_state_full(gfsmIndexedAutomaton *xfsm,
						 gfsmStateId    qid,
						 gboolean       is_final,
						 gfsmWeight     final_weight)
{
  gfsm_indexed_automaton_ensure_state(xfsm,qid);
  if (!is_final) final_weight = xfsm->sr->zero;
  g_array_index(xfsm->state_final_weight,gfsmWeight,qid) = final_weight;
}

//----------------------------------------
GFSM_INLINE
gboolean gfsm_indexed_automaton_lookup_final(gfsmIndexedAutomaton *xfsm, gfsmStateId qid, gfsmWeight *wp)
{
  if (!gfsm_indexed_automaton_has_state(xfsm,qid)) {
    *wp = xfsm->sr->zero;
    return FALSE;
  }
  *wp = g_array_index(xfsm->state_final_weight,gfsmWeight,qid);
  return ((*wp)!=xfsm->sr->zero);
}

//----------------------------------------
GFSM_INLINE
gboolean gfsm_indexed_automaton_state_is_final(gfsmIndexedAutomaton *xfsm, gfsmStateId qid)
{
  gfsmWeight fw;
  return gfsm_indexed_automaton_lookup_final(xfsm,qid,&fw);
}

//----------------------------------------
GFSM_INLINE
gfsmWeight gfsm_indexed_automaton_get_final_weight(gfsmIndexedAutomaton *xfsm, gfsmStateId qid)
{
  if (!gfsm_indexed_automaton_has_state(xfsm,qid)) return xfsm->sr->zero;
  return g_array_index(xfsm->state_final_weight,gfsmWeight,qid);
}


//----------------------------------------
GFSM_INLINE
guint gfsm_indexed_automaton_out_degree(gfsmIndexedAutomaton *fsm, gfsmStateId qid)
{
  if (!gfsm_indexed_automaton_has_state(fsm,qid)) return 0;
  return gfsm_arc_table_index_out_degree(fsm->arcs,qid);
}


/*======================================================================
 * gfsmArcRange
 */

//----------------------------------------
GFSM_INLINE
void gfsm_arcrange_open_indexed(gfsmArcRange *range, gfsmIndexedAutomaton *xfsm, gfsmStateId qid)
{
  if (gfsm_indexed_automaton_has_state(xfsm,qid)) {
    gfsm_arcrange_open_table_index(range,xfsm->arcs,qid);
  } else {
    gfsm_arcrange_close(range);
  }
}