aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmAutomaton.hi
blob: 33a16744d86ed4f7e430340392f08ea4eae0c01d (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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*=============================================================================*\
 * File: gfsmAutomaton.hi
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library: automata: inline definitions
 *
 * Copyright (c) 2004-2008 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
 *=============================================================================*/

#include <gfsmUtils.h>
#include <gfsmBitVector.h>
#include <stdlib.h>

/*======================================================================
 * Methods: Constructors etc.
 */

/*--------------------------------------------------------------
 * new_full()
 */
GFSM_INLINE
gfsmAutomaton *gfsm_automaton_new_full(gfsmAutomatonFlags flags, gfsmSRType srtype, gfsmStateId size)
{
  gfsmAutomaton *fsm = (gfsmAutomaton*)g_new0(gfsmAutomaton,1);
  fsm->flags         = flags;
  fsm->sr            = gfsm_semiring_new(srtype);
  fsm->states        = g_array_sized_new(FALSE, TRUE, sizeof(gfsmState), size);
  fsm->finals        = gfsm_set_new(gfsm_uint_compare);
  fsm->root_id       = gfsmNoState;
  return fsm;
}

/*--------------------------------------------------------------
 * new()
 */
GFSM_INLINE
gfsmAutomaton *gfsm_automaton_new(void)
{
  return gfsm_automaton_new_full(gfsmAutomatonDefaultFlags,
				 gfsmAutomatonDefaultSRType,
				 gfsmAutomatonDefaultSize);
}


/*--------------------------------------------------------------
 * copy_shallow()
 */
//#define GFSM_SHALLOW_ROOT 1
#undef  GFSM_SHALLOW_ROOT  //-- for local testing
GFSM_INLINE
gfsmAutomaton *gfsm_automaton_copy_shallow(gfsmAutomaton *dst, gfsmAutomaton *src)
{
  dst->flags   = src->flags;
#ifdef GFSM_SHALLOW_ROOT
  dst->root_id = src->root_id; //-- pre v0.0.9: DANGEROUS! (assumed by clone() ?!)
#endif
  //
  //-- copy semiring
  if (dst->sr && dst->sr->type != src->sr->type) {
    gfsm_semiring_free(dst->sr);
    dst->sr = gfsm_semiring_copy(src->sr);
  }
  return dst;
}

/*--------------------------------------------------------------
 * copy()
 */
//--extern

/*--------------------------------------------------------------
 * clone()
 */
GFSM_INLINE
gfsmAutomaton *gfsm_automaton_clone(gfsmAutomaton *fsm)
{
  return gfsm_automaton_copy(gfsm_automaton_new_full(gfsmAutomatonDefaultFlags,fsm->sr->type,0),fsm);
}

/*--------------------------------------------------------------
 * shadow()
 */
GFSM_INLINE
gfsmAutomaton *gfsm_automaton_shadow(gfsmAutomaton *fsm)
{
  return gfsm_automaton_copy_shallow(gfsm_automaton_new(), fsm);
}

/*--------------------------------------------------------------
 * swap()
 */
GFSM_INLINE
void gfsm_automaton_swap(gfsmAutomaton *fsm1, gfsmAutomaton *fsm2)
{
  if (fsm1 != fsm2) {
    gfsmAutomaton *tmp = g_new0(gfsmAutomaton,1);
    *tmp = *fsm2;
    *fsm2 = *fsm1;
    *fsm1 = *tmp;
    g_free(tmp);
  }
}

/*--------------------------------------------------------------
 * clear()
 */
//--extern

/*--------------------------------------------------------------
 * free()
 */
GFSM_INLINE
void gfsm_automaton_free(gfsmAutomaton *fsm)
{
  if (!fsm) return;
  gfsm_automaton_clear(fsm); //-- implicitly frees indices
  if (fsm->sr)     gfsm_semiring_free(fsm->sr);
  if (fsm->states) g_array_free(fsm->states,TRUE);
  if (fsm->finals) gfsm_weightmap_free(fsm->finals);
  g_free(fsm);
}

/*======================================================================
 * Methods: Accessors: Semiring
 */

/*--------------------------------------------------------------
 * get_semiring()
 */
GFSM_INLINE
gfsmSemiring *gfsm_automaton_get_semiring(gfsmAutomaton *fsm) { return fsm->sr; }

/*--------------------------------------------------------------
 * set_semiring()
 */
GFSM_INLINE
gfsmSemiring *gfsm_automaton_set_semiring(gfsmAutomaton *fsm, gfsmSemiring *sr)
{
  if (fsm->sr) gfsm_semiring_free(fsm->sr);
  fsm->sr = gfsm_semiring_copy(sr);
  return fsm->sr; //-- WARNING: in gfsm < v0.0.9, returned literal 'sr' parameter!
}

/*--------------------------------------------------------------
 * set_semiring_type()
 */
GFSM_INLINE
void gfsm_automaton_set_semiring_type(gfsmAutomaton *fsm, gfsmSRType srtype)
{
  if (!fsm->sr) fsm->sr = gfsm_semiring_new(srtype);
  else if (fsm->sr->type != srtype) {
    gfsm_semiring_free(fsm->sr);
    fsm->sr = gfsm_semiring_new(srtype);  
  }
}

/*======================================================================*/
///\name API: Automaton Structure

/*--------------------------------------------------------------
 * reserve_states()
 */
GFSM_INLINE
void gfsm_automaton_reserve_states(gfsmAutomaton *fsm, gfsmStateId n_states)
{
  if (n_states != gfsmNoState && n_states > fsm->states->len)
    g_array_set_size(fsm->states, n_states);
}

/*--------------------------------------------------------------
 * reserve_arcs()
 */
GFSM_INLINE
void gfsm_automaton_reserve_arcs(GFSM_UNUSED gfsmAutomaton *fsm, GFSM_UNUSED guint n_arcs)
{
  return;
}

/*--------------------------------------------------------------
 * n_states()
 */
GFSM_INLINE
gfsmStateId gfsm_automaton_n_states(gfsmAutomaton *fsm)
{
  return fsm->states->len;
} 

/*--------------------------------------------------------------
 * n_arcs()
 */
GFSM_INLINE
guint gfsm_automaton_n_arcs(gfsmAutomaton *fsm)
{
  return gfsm_automaton_n_arcs_full(fsm,NULL,NULL,NULL);
}

/*--------------------------------------------------------------
 * n_final_states()
 */
GFSM_INLINE
gfsmStateId gfsm_automaton_n_final_states(gfsmAutomaton *fsm)
{
  return gfsm_weightmap_size(fsm->finals);
}

/*--------------------------------------------------------------
 * finals_foreach
 */
GFSM_INLINE
void gfsm_automaton_finals_foreach(gfsmAutomaton *fsm, GTraverseFunc func, gpointer data)
{
  gfsm_weightmap_foreach(fsm->finals,func,data);
  return;
}

/*--------------------------------------------------------------
 * finals_to_array
 */
GFSM_INLINE
gfsmStateWeightPairArray* gfsm_automaton_finals_to_array(gfsmAutomaton *fsm, gfsmStateWeightPairArray *array)
{
  return gfsm_weightmap_to_array(fsm->finals,array);
}

/*--------------------------------------------------------------
 * get_root()
 */
GFSM_INLINE
gfsmStateId gfsm_automaton_get_root(gfsmAutomaton *fsm)
{
  return fsm->root_id;
}

/*--------------------------------------------------------------
 * set_root()
 */
GFSM_INLINE
void gfsm_automaton_set_root(gfsmAutomaton *fsm, gfsmStateId qid)
{
  if (qid!=gfsmNoState) qid = gfsm_automaton_ensure_state(fsm,qid);
  fsm->root_id = qid;
}


/*======================================================================
 * API: Automaton Properties
 */

/*--------------------------------------------------------------
 * is_cyclic_state()
 */
//--EXTERN

/*--------------------------------------------------------------
 * is_cyclic()
 */
//--extern

/*--------------------------------------------------------------
 * get_alphabet()
 */
//--EXTERN

/*======================================================================
 * API: Automaton States: gfsmState*
 */

/*--------------------------------------------------------------
 * open_state() [aka find_state()]
 */
GFSM_INLINE
gfsmState *gfsm_automaton_open_state(gfsmAutomaton *fsm, gfsmStateId qid)
{
  return (qid < fsm->states->len
	  ? (((gfsmState*)fsm->states->data)+qid)
	  : NULL);
}

/*--------------------------------------------------------------
 * add_state_full()
 */
GFSM_INLINE
gfsmStateId gfsm_automaton_add_state_full(gfsmAutomaton *fsm, gfsmStateId qid)
{
  gfsmState *st;
  if (qid == gfsmNoState)      qid = fsm->states->len;
  if (qid >= fsm->states->len) gfsm_automaton_reserve(fsm,qid+1);
  st           = gfsm_automaton_open_state(fsm,qid);
  st->is_valid = TRUE;
  return qid;
}

/*--------------------------------------------------------------
 * ensure_state()
 */
GFSM_INLINE
gfsmStateId gfsm_automaton_ensure_state(gfsmAutomaton *fsm, gfsmStateId qid)
{ return gfsm_automaton_add_state_full(fsm,qid); }

/*--------------------------------------------------------------
 * open_state_force() [aka get_state()]
 */
GFSM_INLINE
gfsmState *gfsm_automaton_open_state_force(gfsmAutomaton *fsm, gfsmStateId qid)
{
  return ((gfsmState*)fsm->states->data) + gfsm_automaton_ensure_state(fsm,qid);
}

/*--------------------------------------------------------------
 * close_state()
 */
GFSM_INLINE
void gfsm_automaton_close_state(GFSM_UNUSED gfsmAutomaton *fsm, GFSM_UNUSED gfsmState *qp)
{
  //gfsm_state_close(qp);
  return;
}

/*======================================================================
 * API: Automaton States
 */

/*--------------------------------------------------------------
 * has_state()
 */
GFSM_INLINE
gboolean gfsm_automaton_has_state(gfsmAutomaton *fsm, gfsmStateId qid)
{
  return qid < fsm->states->len && ((gfsmState*)fsm->states->data)[qid].is_valid;
}

/*--------------------------------------------------------------
 * state_is_final()
 */
GFSM_INLINE
gboolean gfsm_automaton_state_is_final(gfsmAutomaton *fsm, gfsmStateId qid)
{
  gfsmState *qp = gfsm_automaton_find_state(fsm,qid);
  return qp!=NULL && qp->is_valid && qp->is_final;
}

/*--------------------------------------------------------------
 * add_state()
 */
GFSM_INLINE
gfsmStateId gfsm_automaton_add_state(gfsmAutomaton *fsm)
{
  return gfsm_automaton_add_state_full(fsm,gfsmNoState);
}


/*--------------------------------------------------------------
 * remove_state()
 */
GFSM_INLINE
void gfsm_automaton_remove_state(gfsmAutomaton *fsm, gfsmStateId qid)
{
  gfsmState *s = gfsm_automaton_find_state(fsm,qid);
  if (!s || !s->is_valid) return;
  //
  if (s->is_final) gfsm_weightmap_remove(fsm->finals,GUINT_TO_POINTER(qid));
  if (qid==fsm->root_id) fsm->root_id = gfsmNoState;
  //
  gfsm_arclist_free(s->arcs);
  s->arcs     = NULL;
  s->is_valid = FALSE;
}

/*--------------------------------------------------------------
 * lookup_final()
 */
GFSM_INLINE
gboolean gfsm_automaton_lookup_final(gfsmAutomaton *fsm, gfsmStateId qid, gfsmWeight *wp)
{
  gfsmState *qp = gfsm_automaton_find_state(fsm,qid);
  if (!qp || !qp->is_valid || !qp->is_final) {
    *wp = fsm->sr->zero;
    return FALSE;
  }
  return gfsm_weightmap_lookup(fsm->finals, GUINT_TO_POINTER(qid), wp);
}

/*--------------------------------------------------------------
 * get_final_weight
 */
GFSM_INLINE
gfsmWeight gfsm_automaton_get_final_weight(gfsmAutomaton *fsm, gfsmStateId qid)
{
  gfsmWeight w =0; //-- convince gcc not to complain about uninitialized 'w'
#if 0
  //-- old implementation: direct weightmap lookup
  if (gfsm_weightmap_lookup(fsm->finals, GUINT_TO_POINTER(id), &w)) return w;
  return fsm->sr->zero;
#else
  gfsm_automaton_lookup_final(fsm,qid,&w);
  return w;
#endif
}

/*--------------------------------------------------------------
 * set_final_state_full
 */
GFSM_INLINE
void gfsm_automaton_set_final_state_full(gfsmAutomaton *fsm,
					 gfsmStateId    qid,
					 gboolean       is_final,
					 gfsmWeight     final_weight)
{
  gfsm_state_set_final(gfsm_automaton_get_state(fsm,qid),is_final);
  if (is_final) {
    gfsm_weightmap_insert(fsm->finals, GUINT_TO_POINTER(qid), final_weight);
  } else {
    gfsm_weightmap_remove(fsm->finals, GUINT_TO_POINTER(qid));
  }
}

/*--------------------------------------------------------------
 * set_final_state()
 */
GFSM_INLINE
void gfsm_automaton_set_final_state(gfsmAutomaton *fsm, gfsmStateId qid, gboolean is_final)
{
  gfsm_automaton_set_final_state_full(fsm,qid,is_final,fsm->sr->one);
}


/*--------------------------------------------------------------
 * out_degree()
 */
GFSM_INLINE
guint gfsm_automaton_out_degree(gfsmAutomaton *fsm, gfsmStateId qid)
{
  gfsmState *qp = gfsm_automaton_open_state(fsm,qid);
  if (!qp || !qp->is_valid) return 0;
  return gfsm_state_out_degree(qp);
}

/*--------------------------------------------------------------
 * renumber_states()
 */
//--EXTERN

/*--------------------------------------------------------------
 * renumber_states_full()
 */
//--EXTERN


/*======================================================================
 * Methods: Accessors: Automaton Arcs
 */

/*--------------------------------------------------------------
 * add_arc_link()
 */
GFSM_INLINE
void gfsm_automaton_add_arc_node(gfsmAutomaton *fsm,
				 gfsmState      *sp,
				 gfsmArcList    *node)
{
  //-- possibly sorted
  gfsmArcCompData acdata = { fsm->flags.sort_mode, fsm->sr, NULL, NULL };
  sp->arcs = gfsm_arclist_insert_node(sp->arcs, node, &acdata);

  //-- always unmark 'deterministic' flag -- better: check
  fsm->flags.is_deterministic = FALSE;

  //-- always mark arcs "dirty"
  /*fsm->flags.arcs_dirty=1;
   */

  //- always mark 'unsorted'
  /*
  alp->next = sp->arcs;
  sp->arcs = alp;
  fsm->flags.sort_mode = gfsmASMNone;
  */
}

/*--------------------------------------------------------------
 * add_arc()
 */
GFSM_INLINE
void gfsm_automaton_add_arc(gfsmAutomaton *fsm,
			    gfsmStateId qid1,
			    gfsmStateId qid2,
			    gfsmLabelId lo,
			    gfsmLabelId hi,
			    gfsmWeight  w)
{
  gfsmState *qp1;
  gfsm_automaton_ensure_state(fsm,qid2);
  qp1 = gfsm_automaton_get_state(fsm,qid1);
  gfsm_automaton_add_arc_node(fsm,
			      qp1,
			      gfsm_arclist_new_full(qid1,qid2,lo,hi,w,NULL));
}


/*--------------------------------------------------------------
 * arcsort_full()
 */
//-- EXTERN

/*--------------------------------------------------------------
 * arcsort()
 */
GFSM_INLINE
gfsmAutomaton *gfsm_automaton_arcsort(gfsmAutomaton *fsm, gfsmArcCompMask mode)
{
  if (mode != fsm->flags.sort_mode && mode != gfsmASMNone) {
    gfsmArcCompData acdata = { mode, fsm->sr, NULL, NULL };    
    gfsm_automaton_arcsort_full(fsm, (GCompareDataFunc)gfsm_arc_compare_bymask, &acdata);
  }
  fsm->flags.sort_mode = mode;
  return fsm;
}