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

/*=============================================================================*\
 * File: gfsmIndexed.h
 * 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
 *=============================================================================*/

/** \file gfsmIndexed.h
 *  \brief First stab at indexed automata
 */

#ifndef _GFSM_INDEXED_H
#define _GFSM_INDEXED_H

#include <gfsmArcIndex.h>

/*======================================================================
 * Types
 */

/// Type for an indexed automaton.
typedef struct {
  //-- gfsmAutomaton compatibility
  gfsmAutomatonFlags  flags;              /**< automaton flags, for ::gfsmAutomaton compatibility */
  gfsmSemiring       *sr;                 /**< semiring used for arc weight computations */
  gfsmStateId         root_id;            /**< id of root state, or gfsmNoState if not defined */
  //
  //-- Basic data
  //gfsmBitVector      *state_is_valid;     /* per-state validity flags */
  gfsmWeightVector   *state_final_weight; /**< State final weight, or sr->zero */
  gfsmArcTableIndex  *arcs;               /**< Arc storage (sorted primarily by source state) */
} gfsmIndexedAutomaton;

/*======================================================================
 * Methods: gfsmIndexedAutomaton: constructors, etc.
 */
/// \name Constructors etc.
//@{

/** Create a new ::gfsmIndexedAutomaton, specifying some basic automaton & index structure */
GFSM_INLINE
gfsmIndexedAutomaton *gfsm_indexed_automaton_new_full(gfsmAutomatonFlags flags,
						      gfsmSRType         srtype,
						      gfsmStateId        n_states,
						      guint              n_arcs);

/** Create a new indexed automaton, using some default values */
GFSM_INLINE
gfsmIndexedAutomaton *gfsm_indexed_automaton_new(void);

/** Copy a ::gfsmIndexedAutomaton \a src to \a dst.  \returns \a dst */
gfsmIndexedAutomaton *gfsm_indexed_automaton_copy(gfsmIndexedAutomaton *dst, gfsmIndexedAutomaton *src);

/** Create and return an exact clone of a ::gfsmIndexedAutomaton */
GFSM_INLINE
gfsmIndexedAutomaton *gfsm_indexed_automaton_clone(gfsmIndexedAutomaton *xfsm);

/** Clear a ::gfsmIndexedAutomaton */
GFSM_INLINE
void gfsm_indexed_automaton_clear(gfsmIndexedAutomaton *xfsm);

/** Free a ::gfsmIndexedAutomaton */
GFSM_INLINE
void gfsm_indexed_automaton_free(gfsmIndexedAutomaton *xfsm);

//@}

/*======================================================================
 * Methods: Import & Export
 */
/// \name Import & Export
//@{

/** Populate a ::gfsmIndexedAutomaton from a ::gfsmAutomaton
 *  \param fsm source automaton
 *  \param xfsm destination indexed automaton,
 *         may be passed as NULL to create a new ::gfsmIndexedAutomaton
 *  \returns (new) indexed automaton \a xfsm
 *  \note implicitly clears \a xfsm
 */
gfsmIndexedAutomaton *gfsm_automaton_to_indexed(gfsmAutomaton *fsm, gfsmIndexedAutomaton *xfsm);

/** Export a ::gfsmIndexedAutomaton to a ::gfsmAutomaton
 *  \param xfsm source indexed automaton
 *  \param fsm destination :.gfsmAutomaton
 *         may be passed as NULL to create a new ::gfsmAutomaton
 *  \returns (new) automaton \a fsm
 *  \note implicitly clears \a fsm
 */
gfsmAutomaton *gfsm_indexed_to_automaton(gfsmIndexedAutomaton *xfsm, gfsmAutomaton *fsm);

//@}

/*======================================================================
 * Methods: Accessors: gfsmIndexedAutomaton
 */
/// \name Accessors: gfsmIndexedAutomaton
//@{

/** Reserve space for at least \a n_states states */
GFSM_INLINE
void gfsm_indexed_automaton_reserve_states(gfsmIndexedAutomaton *xfsm, gfsmStateId n_states);

/** Reserve space for at least \a n_arcs arcs */
GFSM_INLINE
void gfsm_indexed_automaton_reserve_arcs(gfsmIndexedAutomaton *xfsm, guint n_arcs);

/** (re-)sort arcs in a ::gfsmIndexedAutomaton */
GFSM_INLINE
void gfsm_indexed_automaton_sort(gfsmIndexedAutomaton *xfsm, gfsmArcCompMask sort_mask);

//@}

/*======================================================================
 * gfsmAutomaton API: Automaton properties
 */
/// \name gfsmAutomaton API: automaton properties
//@{

/** Get pointer to the semiring associated with this automaton */
#define gfsm_indexed_automaton_get_semiring(xfsm) (xfsm->sr)

/** Set the semiring associated with this automaton */
GFSM_INLINE
gfsmSemiring *gfsm_indexed_automaton_set_semiring(gfsmIndexedAutomaton *xfsm, gfsmSemiring *sr);

/** Set the semiring associated with this automaton by semiring-type */
GFSM_INLINE
void gfsm_indexed_automaton_set_semiring_type(gfsmIndexedAutomaton *xfsm, gfsmSRType srtype);

/** Get number of states (constant time) */
GFSM_INLINE
gfsmStateId gfsm_indexed_automaton_n_states(gfsmIndexedAutomaton *xfsm);

/** Get total number of arcs (constant time) */
GFSM_INLINE
guint gfsm_indexed_automaton_n_arcs(gfsmIndexedAutomaton *xfsm);

/** Get Id of root node, or gfsmNoState if undefined */
GFSM_INLINE
gfsmStateId gfsm_indexed_automaton_get_root(gfsmIndexedAutomaton *xfsm);

/** Set Id of root node, creating state if necessary */
GFSM_INLINE
void gfsm_indexed_automaton_set_root(gfsmIndexedAutomaton *xfsm, gfsmStateId qid);

//@}

/*======================================================================
 * Methods: Accessors: gfsmAutomaton API: States
 */
/// \name gfsmAutomaton API: States
//@{

/** Check whether automaton has a state with ID \a qid. */
GFSM_INLINE
gboolean gfsm_indexed_automaton_has_state(gfsmIndexedAutomaton *xfsm, gfsmStateId qid);

/** Ensures that state \a id exists \returns \a qid */
GFSM_INLINE
gfsmStateId gfsm_indexed_automaton_ensure_state(gfsmIndexedAutomaton *xfsm, gfsmStateId qid);

/* Remove the state with id \a qid, if any.
 * Currently does nothing.
 */
GFSM_INLINE
void gfsm_indexed_automaton_remove_state(gfsmIndexedAutomaton *fsm, gfsmStateId qid);

/** Set boolean final-state flag. \returns (void) */
#define gfsm_indexed_automaton_set_final_state(xfsm,qid,is_final) \
  gfsm_indexed_automaton_set_final_state_full((xfsm),(qid),(is_final),(xfsm)->sr->one)

/** Set final weight. \returns (void) */
GFSM_INLINE
void gfsm_indexed_automaton_set_final_state_full(gfsmIndexedAutomaton *fsm,
						 gfsmStateId    qid,
						 gboolean       is_final,
						 gfsmWeight     final_weight);

/** Lookup final weight. \returns TRUE iff state \a id is final, and sets \a *wp to its final weight. */
GFSM_INLINE
gboolean gfsm_indexed_automaton_lookup_final(gfsmIndexedAutomaton *fsm, gfsmStateId id, gfsmWeight *wp);

/** Is \a qid final in \a xfsm?  Really just wraps gfsm_indexed_automaton_lookup_final() */
GFSM_INLINE
gboolean gfsm_indexed_automaton_state_is_final(gfsmIndexedAutomaton *xfsm, gfsmStateId qid);

/** Get final weight for \a qid final in \a xfsm?  Really just wraps gfsm_indexed_automaton_lookup_final() */
GFSM_INLINE
gfsmWeight gfsm_indexed_automaton_get_final_weight(gfsmIndexedAutomaton *xfsm, gfsmStateId qid);

/** Get number of outgoing arcs. \returns guint */
GFSM_INLINE
guint gfsm_indexed_automaton_out_degree(gfsmIndexedAutomaton *fsm, gfsmStateId qid);

//@}

/*======================================================================
 * ArcRange
 */
///\name gfsmArcRange interface
//@{

/** Open a ::gfsmArcRange for outgoing arcs from state \a qid in \a xfsm */
GFSM_INLINE
void gfsm_arcrange_open_indexed(gfsmArcRange *range, gfsmIndexedAutomaton *xfsm, gfsmStateId qid);

//@}

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

#endif /* _GFSM_INDEXED_H */