aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmArc.h
blob: 3ea6e225ce92ff0ea555a0e81744617f314b0401 (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
/*=============================================================================*\
 * File: gfsmArc.h
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library: arcs
 *
 * 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
 *=============================================================================*/

/** \file gfsmArc.h
 *  \brief Arc (transition) definitions & utilities
 */

#ifndef _GFSM_ARC_H
#define _GFSM_ARC_H

#include <gfsmSemiring.h>
#include <stdarg.h>

/// "Heavy" arc structure
typedef struct {
  gfsmStateId       source;  /**< ID of source node */
  gfsmStateId       target;  /**< ID of target node */
  gfsmLabelId       lower;   /**< Lower label */
  gfsmLabelId       upper;   /**< Upper label */
  gfsmWeight        weight;  /**< arc weight */
} gfsmArc;

/// Type for identifying arc-label "sides" in a transducer (lower vs. upper)
typedef enum {
  gfsmLSBoth  = 0, ///< Both sides (lower and upper)
  gfsmLSLower = 1, ///< Lower side only
  gfsmLSUpper = 2  ///< Upper side only
} gfsmLabelSide;

//----------------------------------------------------------------------
// arc sorting (new)

/// Enum type for elementary builtin comparisons on ::gfsmArc
/** \since v0.0.10 */
typedef enum {
  gfsmACNone    = 0x0,  /**< '_': no comparison at all */
  //
  //-- Forward (ascending)
  gfsmACLower   = 0x1,  /**< 'l': compare lower labels (ascending order) */
  gfsmACUpper   = 0x2,  /**< 'u': compare upper labels (ascending order) */
  gfsmACWeight  = 0x3,  /**< 'w': compare semiring weights (ascending order) */
  gfsmACSource  = 0x4,  /**< 's': compare source states (if supported and meaningful, ascending order) */
  gfsmACTarget  = 0x5,  /**< 't': compare target states (if supported and meaningful, ascending order) */
  gfsmACUser    = 0x6,  /**< 'x': pseudo-field indicating a user-defined comparison */
  gfsmACUnused1 = 0x7,  /**< unused */
  //
  //-- Reverse (descending)
  gfsmACReverse = 0x8,  /**< not really a comparison: the bit 0x8 is set for all "reverse" comparisons */
  gfsmACLowerR  = 0x9,  /**< 'L': compare lower labels (descending order) */
  gfsmACUpperR  = 0xa,  /**< 'U': compare upper labels (descending order) */
  gfsmACWeightR = 0xb,  /**< 'W': compare semiring weights (descending order) */
  gfsmACSourceR = 0xc,  /**< 'S': compare source states (if supported and meaningful, descending order) */
  gfsmACTargetR = 0xd,  /**< 'T': compare target states (if supported and meaningful, descending order) */
  gfsmACUserR   = 0xe,  /**< 'X': pseudo-field for reversed user-defined comparisons, for symmetry */
  //
  //-- Pseudo-comparisons
  gfsmACAll     = 0xf   /**< '*': pseudo-field for mask of all known elementary comparisons */
} gfsmArcComp;

/** \brief Prioritized list of up to 5 elementary arc comparisons packed as a guint32
 *  \detail
 *   The primary comparison is encoded as a ::gfsmArcComp in the least significant
 *   4-bit nybble of the integer, the secondary comparison is left-shifted by 4 bits,
 *   and so on.
 */
typedef guint32 gfsmArcCompMask;

/** Number of bits to left-shift ::gfsmArcCompMask for each arc sub-comparison using ::gfsmArcComp encoding */
#define gfsmACShift 4

/** Maximum number of ::gfsmArcComp fields supported by ::gfsmArcCompMask (pragmatic definition) */
#define gfsmACMaxN 6

/** Useful aliases for builtin arcsort modes, including backwards-compatible ::gfsmASMLower etc. */
typedef enum {
  gfsmASMNone   = 0x0,                                                                       /**< no sort */
  gfsmASMLower  = (gfsmACLower|(gfsmACUpper<<gfsmACShift)|(gfsmACTarget<<(2*gfsmACShift))),  /**< (lower,upper,target) */
  gfsmASMUpper  = (gfsmACUpper|(gfsmACLower<<gfsmACShift)|(gfsmACTarget<<(2*gfsmACShift))),  /**< (upper,lower,target) */
  gfsmASMWeight = gfsmACWeight,                                                              /**< (weight) */
  gfsmASMLowerWeight = (gfsmACLower|(gfsmACWeight<<gfsmACShift)),                            /**< (lower,weight) */
  gfsmASMUpperWeight = (gfsmACUpper|(gfsmACWeight<<gfsmACShift)),                            /**< (upper,weight) */
  gfsmASMUser        = gfsmACUser,                                                           /**< user-defined sort */
} gfsmArcSortModeE;

/// Semi-compatible typedef for arc sort mode
typedef gfsmArcCompMask gfsmArcSortMode;

/// Data for new-style generic arc comparison
typedef struct {
  gfsmArcCompMask       mask;               /**< Comparison precedence */
  gfsmSemiring         *sr;                 /**< Semiring to use for weight comparisons (if any) */
  GCompareDataFunc    user_compare_func;    /**< User comparison function (if any) */
  gpointer            user_data;            /**< User data (for \c user_compare_func) */
} gfsmArcCompData;

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

/// \name Arcs: Constructors etc.
//@{
/** Create and return a new (empty) ::gfsmArc */
GFSM_INLINE
gfsmArc *gfsm_arc_new(void);

/** Initialize a ::gfsmArc
 * \param a arc to initialize
 * \param src ID of source state
 * \param dst ID of target state
 * \param lo  ID of lower label
 * \param hi  ID of upper label
 * \param w   arc weight
 * \returns initialized arc \a a
 */
GFSM_INLINE
gfsmArc *gfsm_arc_init(gfsmArc *a,
		       gfsmStateId src,
		       gfsmStateId dst,
		       gfsmLabelId lo,
		       gfsmLabelId hi,
		       gfsmWeight wt);

/** Convenience macro to simultaneously create and initialize a ::gfsmArc
 * \param src ID of source state
 * \param dst ID of target state
 * \param lo  ID of lower label
 * \param hi  ID of upper label
 * \param w   arc weight
 * \returns newly allocated and initalized ::gfsmArc
 */
GFSM_INLINE
gfsmArc *gfsm_arc_new_full(gfsmStateId src, gfsmStateId dst, gfsmLabelVal lo, gfsmLabelVal hi, gfsmWeight wt);

/** Create an exact copy of the ::gfsmArc \a src */
GFSM_INLINE
gfsmArc *gfsm_arc_clone(gfsmArc *src);

/** Backwards-compatible convenience alias for gfsm_arc_clone() */
#define gfsm_arc_copy(src) gfsm_arc_clone(src)

/** Destroy a ::gfsmArc \a a */
GFSM_INLINE
void gfsm_arc_free(gfsmArc *a);
//@}

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

/** Get source node of an arc -- may be gfsmNoState */
#define gfsm_arc_source(arcptr) ((arcptr)->source)

/** Get target node of an arc -- may be gfsmNoState */
#define gfsm_arc_target(arcptr) ((arcptr)->target)

/** Get lower label of an arc -- may be gfsmNoLabel */
#define gfsm_arc_lower(arcptr) ((arcptr)->lower)

/** Get upper label of an arc -- may be gfsmNoLabel */
#define gfsm_arc_upper(arcptr) ((arcptr)->upper)

/** Get weight of an arc -- may be gfsmNoWeight */
#define gfsm_arc_weight(arcptr) ((arcptr)->weight)

//@}


/*======================================================================
 * Arc Comparison Utilities
 */
///\name Arc Comparison Utilities
//@{

/** Generic 3-way comparison on arcs (inline version)
 *  \param a1 first arc to compare
 *  \param a2 second arc to compare
 *  \param acdata specifies comparison priorities
 *  \returns
 *    negative, zero, or positive integer depending on whether
 *    \a a1 is less-than, equal-to, or greater-than \a a2 according to \a acdata.
 */
GFSM_INLINE
gint gfsm_arc_compare_bymask_inline(gfsmArc *a1, gfsmArc *a2, gfsmArcCompData *acdata);

/** Generic 3-way comparison on arcs (extern version)
 *  Really just a wrapper for gfsm_arc_compare_mask_inline()
 */
gint gfsm_arc_compare_bymask(gfsmArc *a1, gfsmArc *a2, gfsmArcCompData *acdata);

/** Guts for gfsm_arc_compare_bymask_inline(): compare arcs w.r.t a single attribute \a cmp.
 *  \note gcc deems these calls "unlikely" and refuses to inline...
 */
GFSM_INLINE
gint gfsm_arc_compare_bymask_1_(gfsmArc *a1, gfsmArc *a2, gfsmArcComp cmp, gfsmArcCompData *acdata);

/** Parse a NUL-terminated string into a ::gfsmArcCompMask
 *  \param maskchars
 *    A NUL-terminated string representing the precedence among elementary comparisons.
 *    Each character represents a single elementary comparison.
 *    The primary comparison is the first character of the string.
 *    Correspondence of characters to comparisons is:
 *    \li 'l' ::gfsmACLower
 *    \li 'u' ::gfsmACUpper
 *    \li 'w' ::gfsmACWeight
 *    \li 's' ::gfsmACSource
 *    \li 't' ::gfsmACTarget
 *    \li 'x' ::gfsmACUser
 *    \li 'L' ::gfsmACLowerR
 *    \li 'U' ::gfsmACUpperR
 *    \li 'W' ::gfsmACWeightR
 *    \li 'S' ::gfsmACSourceR
 *    \li 'T' ::gfsmACTargetR
 *    \li 'X' ::gfsmACUserR
 *  \returns a ::gfsmArcCompMask for \a maskchars
 */
gfsmArcCompMask gfsm_acmask_from_chars(const char *maskchars);

/** Create and return a ::gfsmArcCompMask from a variable argument list of ::gfsmArcComp
 *  The argument list must be terminated with a zero (e.g. ::gfsmACNone)
 *  \param cmp0 primary comparison
 *  \param ...  secondary, tertiary, ... comparisons
 *  \returns ::gfsmArcCompMask for specified comparisons
 */
gfsmArcCompMask gfsm_acmask_from_args(gfsmArcComp cmp0, ...);

/** Create a partial ::gfsmArcCompMask for \a cmp as the \a nth (sub-)comparison */
GFSM_INLINE
gfsmArcCompMask gfsm_acmask_new(gfsmArcComp cmp, gint nth);

/** Get \a nth (sub-)comparison from a ::gfsmArcCompMask */
GFSM_INLINE
gfsmArcComp gfsm_acmask_nth(gfsmArcCompMask m, gint nth);

/** Get basic \a nth basic (sub-)comparison from a ::gfsmArcCompMask, disregarding sort order */
GFSM_INLINE
gfsmArcComp gfsm_acmask_nth_comp(gfsmArcCompMask m, gint nth);

/** Get sort order for \a nth (sub-)comparison of a ::gfsmArcCompMask.
 *  \returns a true value if \a nth sub-comparison is reversed (descending order), otherwise FALSE.
 */
GFSM_INLINE
gboolean gfsm_acmask_nth_reverse(gfsmArcCompMask m, gint nth);

/** Get single character representing the \a nth field of arc comparison mask \a m, as for gfsm_acmask_from_chars() */
GFSM_INLINE
gchar gfsm_acmask_nth_char(gfsmArcCompMask m, gint nth);

/** Get a static human-readable string representing the \a nth field of arc comparison mask \a m */
const gchar *gfsm_acmask_nth_string(gfsmArcCompMask m, gint nth);

/** Populate a character string representing a ::gfsmArcCompMask.
 *  \param mask[in]   mask to convert to a string
 *  \param chars[out] character string representing \a mask, in the format acepted by gfsm_acmask_from_chars(),
 *                    or NULL to allocate a new string.  If specified and non-NULL, \a chars should be
 *                    long enough to hold ::gfsmACMaxN+1 characters, since a terminating NUL
 *                    is implicitly added as the final character.
 *  \returns \a chars if specified, otherwise a newly allocated string.
 *  \note User is responsible for freeing the returned string with g_free() when it is no longer needed.
 */
gchar *gfsm_acmask_to_chars(gfsmArcCompMask m, gchar *chars);

/** Populate a GString* with a human-readable representation of a ::gfsmArcCompMask.
 *  \param mask[in]   mask to convert to a string
 *  \param gstr[out]  GString representing \a mask, in human-readable format,
 *                    or NULL to allocate a new GString*.
*  \returns \a gstr if specified, otherwise a newly allocated GString*.
 *  \note User is responsible for freeing the returned GString* with g_string_free() when it is no longer needed.
 */
GString *gfsm_acmask_to_gstring(gfsmArcCompMask m, GString *gstr);

/** Backwards-compatible arc sort mode name resolution function
 *  \deprecated in favor of gfsm_acmask_nth_string(), gfsm_acmask_to_gstring(), gfsm_acmask_to_chars()
 */
GFSM_INLINE
const gchar *gfsm_arc_sortmode_to_name(gfsmArcCompMask m);

//@}

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

#endif /* _GFSM_ARC_H */