aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmArcIndex.c
blob: 3df0760e4ecf0816cf33d959ba34ad858e269522 (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

/*=============================================================================*\
 * File: gfsmArcIndex.c
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library: arc indices
 *
 * Copyright (c) 2006-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
 *=============================================================================*/

#include <gfsmArcIndex.h>
#include <gfsmArcIter.h>

//-- no-inline definitions
#ifndef GFSM_INLINE_ENABLED
# include <gfsmArcIndex.hi>
#endif

/*======================================================================
 * gfsmReverseArcIndex
 */

/*--------------------------------------------------------------
 * automaton_reverse_arc_index()
 */
gfsmReverseArcIndex *gfsm_automaton_to_reverse_arc_index(gfsmAutomaton *fsm, gfsmReverseArcIndex *rarcs)
{
  gfsmStateId idfrom;
  gfsmArcIter ai;
  gfsmArc *arc;

  if (!rarcs) {
    rarcs = gfsm_reverse_arc_index_sized_new(fsm->states->len);
  }
  g_ptr_array_set_size(rarcs,fsm->states->len);

  for (idfrom=0; idfrom < fsm->states->len; idfrom++) {
    for (gfsm_arciter_open(&ai,fsm,idfrom); gfsm_arciter_ok(&ai); gfsm_arciter_next(&ai)) {
      arc  = gfsm_arciter_arc(&ai);
      g_ptr_array_index(rarcs,arc->target)
	//= gfsm_arclist_prepend(g_ptr_array_index(rarcs,arc->target), arc);
	= g_slist_prepend(g_ptr_array_index(rarcs,arc->target),arc);
    }
    gfsm_arciter_close(&ai);
  }

  return rarcs;
}

/*--------------------------------------------------------------
 * reverse_arc_index_free()
 */
void gfsm_reverse_arc_index_free(gfsmReverseArcIndex *rarcs, gboolean free_lists)
{
  guint i;
  if (free_lists) {
    //-- +free_lists, -free_arcs
    for (i=0; i < rarcs->len; i++) { g_slist_free(g_ptr_array_index(rarcs,i)); }
  }

  //-- free index array
  g_ptr_array_free(rarcs,TRUE);
}



/*======================================================================
 * gfsmWeightVector
 */

/*--------------------------------------------------------------
 * automaton_to_weight_vector()
 */
gfsmWeightVector *gfsm_automaton_to_final_weight_vector(gfsmAutomaton *fsm, gfsmWeightVector *wv)
{
  gfsmStateId qid;
  guint n_states = gfsm_automaton_n_states(fsm);
  gfsmWeight  *wp;

  if (wv==NULL) {
    wv = gfsm_weight_vector_sized_new(n_states);
  } else {
    gfsm_weight_vector_resize(wv,n_states);
  }
  wv->len = n_states;

  for (qid=0,wp=(gfsmWeight*)wv->data; qid < n_states; qid++,wp++) {
    gfsm_automaton_lookup_final(fsm,qid,wp);
  }

  return wv;
}

/*--------------------------------------------------------------
 * weight_vector_write_bin_handle()
 */
gboolean gfsm_weight_vector_write_bin_handle(gfsmWeightVector *wv, gfsmIOHandle *ioh, gfsmError **errp)
{
  guint32 len = wv->len;
  if (!gfsmio_write(ioh,&len,sizeof(guint32))) {
    g_set_error(errp, g_quark_from_static_string("gfsm"),                              //-- domain
		g_quark_from_static_string("weight_vector_write_bin_handle:len"), //-- code
		"could not store weight vector length");
    return FALSE;
  }
  if (!gfsmio_write(ioh,wv->data,wv->len*sizeof(gfsmWeight))) {
    g_set_error(errp, g_quark_from_static_string("gfsm"),                                  //-- domain
		g_quark_from_static_string("weight_vector_write_bin_handle:weights"), //-- code
		"could not store weight vector data");
    return FALSE;
  }
  return TRUE;
}

/*--------------------------------------------------------------
 * weight_vector_read_bin_handle()
 */
gboolean gfsm_weight_vector_read_bin_handle(gfsmWeightVector *wv, gfsmIOHandle *ioh, gfsmError **errp)
{
  guint32 len;
  if (!gfsmio_read(ioh, &len, sizeof(guint32))) {
    g_set_error(errp,
		g_quark_from_static_string("gfsm"),                                    //-- domain
		g_quark_from_static_string("weight_vector_read_bin_handle:len"),  //-- code
		"could not read weight vector length");
    return FALSE;
  }
  gfsm_weight_vector_resize(wv,len);
  if (!gfsmio_read(ioh, wv->data, len*sizeof(gfsmWeight))) {
    g_set_error(errp,
		g_quark_from_static_string("gfsm"),                                     //-- domain
		g_quark_from_static_string("weight_vector_read_bin_handle:data"),  //-- code
		"could not read weight vector data");
    return FALSE;
  }
  return TRUE;
}

/*======================================================================
 * gfsmArcTable
 */

/*--------------------------------------------------------------
 * automaton_to_arc_table()
 */
gfsmArcTable *gfsm_automaton_to_arc_table(gfsmAutomaton *fsm, gfsmArcTable *tab)
{
  gfsmStateId qid, n_states=gfsm_automaton_n_states(fsm);
  guint              n_arcs=gfsm_automaton_n_arcs(fsm);
  gfsmArcIter ai;
  gfsmArc  *arcp;

  //-- maybe allocate
  if (!tab) {
    tab = gfsm_arc_table_sized_new(n_arcs);
  } else {
    gfsm_arc_table_resize(tab, n_arcs);
  }
  tab->len = n_arcs;

  //-- populate arcs
  for (qid=0,arcp=(gfsmArc*)tab->data; qid < n_states; qid++) {
    if (!gfsm_automaton_has_state(fsm,qid)) continue;
    for (gfsm_arciter_open(&ai,fsm,qid); gfsm_arciter_ok(&ai); gfsm_arciter_next(&ai)) {
      gfsmArc *a = gfsm_arciter_arc(&ai);
      *(arcp++) = *a;
    }
    gfsm_arciter_close(&ai);
  }

  //-- return
  return tab;
}

/*--------------------------------------------------------------
 * arc_table_write_bin_handle()
 */
gboolean gfsm_arc_table_write_bin_handle(gfsmArcTable *tab, gfsmIOHandle *ioh, gfsmError **errp)
{
  guint32 len = tab->len;
  if (!gfsmio_write(ioh, &len, sizeof(guint32))) {
    g_set_error(errp,
		g_quark_from_static_string("gfsm"),
		g_quark_from_static_string("arc_table_write_bin_handle:len"),
		"could not write arc table length");
    return FALSE;
  }
  if (!gfsmio_write(ioh, tab->data, len*sizeof(gfsmArc))) {
    g_set_error(errp,
		g_quark_from_static_string("gfsm"),
		g_quark_from_static_string("arc_table_write_bin_handle:data"),
		"could not write arc table data");
    return FALSE;
  }
  return TRUE;
}

/*--------------------------------------------------------------
 * arc_table_read_bin_handle()
 */
gboolean gfsm_arc_table_read_bin_handle(gfsmArcTable *tab, gfsmIOHandle *ioh, gfsmError **errp)
{
  guint32 len;
  if (!gfsmio_read(ioh, &len, sizeof(guint32))) {
    g_set_error(errp,
		g_quark_from_static_string("gfsm"),
		g_quark_from_static_string("arc_table_read_bin_handle:len"),
		"could not read arc table length");
    return FALSE;
  }
  gfsm_arc_table_resize(tab,len);
  if (!gfsmio_read(ioh, tab->data, len*sizeof(gfsmArc))) {
    g_set_error(errp,
		g_quark_from_static_string("gfsm"),
		g_quark_from_static_string("arc_table_read_bin_handle:data"),
		"could not read arc table data");
    return FALSE;
  }
  return TRUE;
}



/*======================================================================
 * gfsmArcTableIndex
 */

/*--------------------------------------------------------------
 * arc_table_index_copy()
 */
gfsmArcTableIndex *gfsm_arc_table_index_copy(gfsmArcTableIndex *dst, gfsmArcTableIndex *src)
{
  gfsmStateId i;
  gfsm_arc_table_copy (dst->tab, src->tab);
  g_ptr_array_set_size(dst->first, src->first->len);

  for (i=0; i < src->first->len; i++) {
    gint offset = (gfsmArc*)g_ptr_array_index(src->first,i) - (gfsmArc*)src->tab->data;
    g_ptr_array_index(dst->first,i) = (gfsmArc*)dst->tab->data + offset;
  }

  return dst;
}


/*--------------------------------------------------------------
 * automaton_to_arc_table_index()
 */
gfsmArcTableIndex *gfsm_automaton_to_arc_table_index(gfsmAutomaton *fsm, gfsmArcTableIndex *tabx)
{
  gfsmStateId qid, n_states=gfsm_automaton_n_states(fsm);
  guint              n_arcs=gfsm_automaton_n_arcs(fsm);
  gfsmArc  *arcp, *arcp_max;
  gfsmArc **firstp;

  //-- maybe allocate
  if (!tabx) {
    tabx = gfsm_arc_table_index_sized_new(n_states, n_arcs);
  } else {
    gfsm_arc_table_index_resize(tabx, n_states, n_arcs);
  }

  //-- populate tabx->arcs
  gfsm_automaton_to_arc_table(fsm,tabx->tab);

  //-- populate tabx->first
  arcp     = (gfsmArc*)tabx->tab->data;
  arcp_max = arcp + n_arcs;
  for (qid=0,firstp=(gfsmArc**)tabx->first->pdata; qid<n_states; qid++,firstp++) {
    *firstp = arcp;
    for ( ; arcp<arcp_max && arcp->source==qid; arcp++) { ; }
  }
  *firstp = arcp_max;

  //-- return
  return tabx;
}

/*--------------------------------------------------------------
 * arc_table_index_sort_with_data()
 */
void gfsm_arc_table_index_sort_with_data(gfsmArcTableIndex *tabx, GCompareDataFunc compare_func, gpointer data)
{
  gfsmArc **firstp     = (gfsmArc**)tabx->first->pdata;
  gfsmArc **firstp_max = firstp + tabx->first->len - 1;
  for ( ; firstp < firstp_max; firstp++) {
    gfsmArc *min = *firstp;
    gfsmArc *max = *(firstp+1);
    g_qsort_with_data(min, max-min, sizeof(gfsmArc), compare_func, data);
  }
}

/*--------------------------------------------------------------
 * arc_table_index_write_bin_handle()
 */
gboolean gfsm_arc_table_index_write_bin_handle(gfsmArcTableIndex *tabx, gfsmIOHandle *ioh, gfsmError **errp)
{
  gfsmStateId first_len=tabx->first->len, qid;
  if (!gfsm_arc_table_write_bin_handle(tabx->tab, ioh, errp)) return FALSE;

  if (!gfsmio_write(ioh, &first_len, sizeof(gfsmStateId))) {
    g_set_error(errp,
		g_quark_from_static_string("gfsm"),
		g_quark_from_static_string("arc_table_index_write_bin_handle:len"),
		"could not write arc table index 'first' length");
    return FALSE;
  }
  for (qid=0; qid < first_len; qid++) {
    gfsmArc     *a = (gfsmArc*)g_ptr_array_index(tabx->first,qid);
    guint32 offset = a - ((gfsmArc*)tabx->tab->data);
    if (!gfsmio_write(ioh, &offset, sizeof(guint32))) {
      g_set_error(errp,
		  g_quark_from_static_string("gfsm"),
		  g_quark_from_static_string("arc_table_index_write_bin_handle:data"),
		  "could not write state arc offset for state '%u'", qid);
      return FALSE;
    }
  }
  return TRUE;
}

/*--------------------------------------------------------------
 * arc_table_index_read_bin_handle()
 */
gboolean gfsm_arc_table_index_read_bin_handle(gfsmArcTableIndex *tabx, gfsmIOHandle *ioh, gfsmError **errp)
{
  gfsmStateId first_len, qid;
  if (!gfsm_arc_table_read_bin_handle(tabx->tab, ioh, errp)) return FALSE;

  if (!gfsmio_read(ioh, &first_len, sizeof(gfsmStateId))) {
    g_set_error(errp,
		g_quark_from_static_string("gfsm"),
		g_quark_from_static_string("arc_table_index_read_bin_handle:len"),
		"could not read arc table index 'first' length");
    return FALSE;
  }
  g_ptr_array_set_size(tabx->first,first_len);
  for (qid=0; qid < first_len; qid++) {
    guint32 offset;
    if (!gfsmio_read(ioh, &offset, sizeof(guint32))) {
      g_set_error(errp,
		  g_quark_from_static_string("gfsm"),
		  g_quark_from_static_string("arc_table_index_write_bin_handle:data"),
		  "could not read state arc offset for state '%u'", qid);
      return FALSE;
    }
    g_ptr_array_index(tabx->first,qid) = &g_array_index(tabx->tab,gfsmArc,offset);
  }
  return TRUE;
}


/*======================================================================
 * gfsmArcLabelIndex [GONE]
 */
//--------------------------------------------------------------
// arc_label_index_compare_arcs()
/*
gint gfsm_arc_label_index_compare_arcs(gfsmArc *a1, gfsmArc *a2, gfsmArcLabelIndexSortData *sdata)
{ return gfsm_arc_label_index_compare_arcs_inline(a1,a2,sdata); }
*/


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

#undef GFSM_ARCRANGE_ENABLE_BSEARCH
#undef GFSM_ARCRANGE_ENABLE_SEEK

#ifdef GFSM_ARCRANGE_ENABLE_BSEARCH
/*--------------------------------------------------------------
 * arc_range_bsearch_*()
 *  + NOT WORTH IT (tested for out_degree {1,2,4,8,16,32,64,128,256,512})
 */
void gfsm_arcrange_bsearch_source(gfsmArcRange *range, gfsmStateId find)
{
  gfsmArc *min=range->min, *max=range->max;
  while (min < max) {
    gfsmArc *mid = min + (max-min)/2;
    if (mid->source < find) { min = mid+1; }
    else                    { max = mid; }
  }
  range->min = min;
}

//--------------------------------------------------------------
void gfsm_arcrange_bsearch_target(gfsmArcRange *range, gfsmStateId find)
{
  gfsmArc *min=range->min, *max=range->max;
  while (min < max) {
    gfsmArc *mid = min + (max-min)/2;
    if (mid->target < find) { min = mid+1; }
    else                    { max = mid; }
  }
  range->min = min;
}

//--------------------------------------------------------------
void gfsm_arcrange_bsearch_lower(gfsmArcRange *range, gfsmLabelId find)
{
  gfsmArc *min=range->min, *max=range->max;
  while (min < max) {
    gfsmArc *mid = min + (max-min)/2;
    if (mid->lower < find) { min = mid+1; }
    else                   { max = mid; }
  }
  range->min = min;
}

//--------------------------------------------------------------
void gfsm_arcrange_bsearch_upper(gfsmArcRange *range, gfsmLabelId find)
{
  gfsmArc *min=range->min, *max=range->max;
  while (min < max) {
    gfsmArc *mid = min + (max-min)/2;
    if (mid->upper < find) { min = mid+1; }
    else                    { max = mid; }
  }
  range->min = min;
}

//--------------------------------------------------------------
void gfsm_arcrange_bsearch_weight(gfsmArcRange *range, gfsmWeight find, gfsmSemiring *sr)
{
  gfsmArc *min=range->min, *max=range->max;
  while (min < max) {
    gfsmArc *mid = min + (max-min)/2;
    if (gfsm_sr_compare(sr,mid->weight,find) < 0) { range->min = mid+1; }
    else                                          { range->max = mid; }
  }
  range->min = min;
}
#endif /* GFSM_ARCRANGE_ENABLE_BSEARCH */

#ifdef GFSM_ARCRANGE_ENABLE_SEEK
//--------------------------------------------------------------
// arcrange_seek_X()
//  -- also not worth it

//----------------------------------------------
GFSM_INLINE
void gfsm_arcrange_seek_source(gfsmArcRange *range, gfsmStateId find)
{
  gfsm_assert(range != NULL);
  while (gfsm_arcrange_ok(range) && gfsm_arcrange_arc(range)->source < find)
    gfsm_arcrange_next(range);
}

//----------------------------------------------
GFSM_INLINE
void gfsm_arcrange_seek_target(gfsmArcRange *range, gfsmStateId find)
{
  gfsm_assert(range != NULL);
  while (gfsm_arcrange_ok(range) && gfsm_arcrange_arc(range)->target < find)
    gfsm_arcrange_next(range);
}

//----------------------------------------------
GFSM_INLINE
void gfsm_arcrange_seek_lower(gfsmArcRange *range, gfsmLabelId find)
{
  gfsm_assert(range != NULL);
  while (gfsm_arcrange_ok(range) && gfsm_arcrange_arc(range)->lower < find)
    gfsm_arcrange_next(range);
}

//----------------------------------------------
GFSM_INLINE
void gfsm_arcrange_seek_upper(gfsmArcRange *range, gfsmLabelId find)
{
  gfsm_assert(range != NULL);
  while (gfsm_arcrange_ok(range) && gfsm_arcrange_arc(range)->upper < find)
    gfsm_arcrange_next(range);
}

//----------------------------------------------
GFSM_INLINE
void gfsm_arcrange_seek_weight(gfsmArcRange *range, gfsmWeight find, gfsmSemiring *sr)
{
  gfsm_assert(range != NULL);
  while (gfsm_arcrange_ok(range) && gfsm_sr_compare(sr,gfsm_arcrange_arc(range)->weight,find) < 0)
    gfsm_arcrange_next(range);
}
#endif /* GFSM_ARCRANGE_ENABLE_SEEK */