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

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

#include <gfsmIndexed.h>
#include <gfsmArcIter.h>

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

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

//----------------------------------------
gfsmIndexedAutomaton *gfsm_indexed_automaton_copy(gfsmIndexedAutomaton *dst, gfsmIndexedAutomaton *src)
{
  if (!dst) {
    dst = gfsm_indexed_automaton_new_full(src->flags,
					  src->sr->type,
					  gfsm_indexed_automaton_n_states(src),
					  gfsm_indexed_automaton_n_arcs(src));
  }
  else {
    gfsm_indexed_automaton_clear(dst);
    gfsm_indexed_automaton_reserve_states(dst, gfsm_indexed_automaton_n_states(src));
    gfsm_indexed_automaton_reserve_arcs  (dst, gfsm_indexed_automaton_n_arcs(src)  );
  }

  //-- copy: flags, semiring, root
  dst->flags = src->flags;
  gfsm_indexed_automaton_set_semiring(dst, src->sr);
  dst->root_id = src->root_id;

  //-- copy: tables
  gfsm_weight_vector_copy  (dst->state_final_weight, src->state_final_weight);
  gfsm_arc_table_index_copy(dst->arcs, src->arcs);

  return dst;
}

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

//----------------------------------------
gfsmIndexedAutomaton *gfsm_automaton_to_indexed(gfsmAutomaton *fsm, gfsmIndexedAutomaton *xfsm)
{
  //-- maybe allocate new indexed automaton
  if (xfsm==NULL) {
    xfsm = gfsm_indexed_automaton_new_full(fsm->flags,
					   fsm->sr->type,
					   gfsm_automaton_n_states(fsm),
					   gfsm_automaton_n_arcs(fsm)
					   );
  } else {
    gfsm_indexed_automaton_clear(xfsm);
    xfsm->flags = fsm->flags;
    gfsm_indexed_automaton_reserve_states(xfsm,gfsm_automaton_n_states(fsm));
    gfsm_indexed_automaton_reserve_arcs(xfsm,gfsm_automaton_n_arcs(fsm));
  }
  gfsm_indexed_automaton_set_semiring(xfsm,fsm->sr); //-- copy semiring

  //-- set root id
  xfsm->root_id = fsm->root_id;

  //-- index final weights
  gfsm_automaton_to_final_weight_vector(fsm, xfsm->state_final_weight);
  gfsm_automaton_to_arc_table_index(fsm, xfsm->arcs);

  //-- sort arcs (no!)
  //gfsm_indexed_automaton_sort(xfsm, xfsm->flags.sort_mode);


  return xfsm;
}



//----------------------------------------
gfsmAutomaton *gfsm_indexed_to_automaton(gfsmIndexedAutomaton *xfsm, gfsmAutomaton *fsm)
{
  gfsmStateId qid;
  gfsmWeight  srzero;

  //-- maybe allocate new automaton
  if (fsm==NULL) {
    fsm = gfsm_automaton_new_full(xfsm->flags, xfsm->sr->type, gfsm_indexed_automaton_n_states(xfsm));
  } else {
    gfsm_automaton_clear(fsm);
    fsm->flags = xfsm->flags;
    gfsm_automaton_set_semiring(fsm, gfsm_semiring_copy(xfsm->sr));
    gfsm_automaton_reserve(fsm, gfsm_indexed_automaton_n_states(xfsm));
  }

  //-- set root id
  fsm->root_id = xfsm->root_id;

  //-- update state-wise
  srzero = xfsm->sr->zero;
  for (qid=0; qid < xfsm->state_final_weight->len; qid++) {
    gfsmArcRange range;

    //-- state_final_weight
    gfsmWeight fw = g_array_index(xfsm->state_final_weight,gfsmWeight,qid);
    if (fw != srzero) { gfsm_automaton_set_final_state_full(fsm,qid,TRUE,fw); }

    //-- arcs
    for (gfsm_arcrange_open_indexed(&range, xfsm, qid); gfsm_arcrange_ok(&range); gfsm_arcrange_next(&range)) {
      gfsmArc *a = gfsm_arcrange_arc(&range);
      gfsm_automaton_add_arc(fsm,a->source,a->target,a->lower,a->upper,a->weight);
    }
    gfsm_arcrange_close(&range);
  }

  return fsm;
}

/*======================================================================
 * Methods: Accessors: gfsmIndexedAutomaton
 */
//-- inlined

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

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

/*======================================================================
 * I/O
 */