aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmArc.hi
blob: fcd9d79200a6a81128a8b09aa4182ea86438e754 (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

/*=============================================================================*\
 * File: gfsmArc.hi
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library: arcs: 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 <stdlib.h>

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

/*--------------------------------------------------------------
 * arc_new()
 */
GFSM_INLINE
gfsmArc *gfsm_arc_new(void)
{
  return g_new0(gfsmArc,1);
}

/*--------------------------------------------------------------
 * arc_init()
 */
GFSM_INLINE
gfsmArc *gfsm_arc_init(gfsmArc *a,
		       gfsmStateId src,
		       gfsmStateId dst,
		       gfsmLabelId lo,
		       gfsmLabelId hi,
		       gfsmWeight wt)
{
  if (a) {
    a->source = src;
    a->target = dst;
    a->lower  = lo;
    a->upper  = hi;
    a->weight = wt;
  }
  return a;
}

/*--------------------------------------------------------------
 * arc_new_full()
 */
GFSM_INLINE
gfsmArc *gfsm_arc_new_full(gfsmStateId src, gfsmStateId dst, gfsmLabelVal lo, gfsmLabelVal hi, gfsmWeight wt)
{
  return gfsm_arc_init(g_new(gfsmArc,1),src,dst,lo,hi,wt);
}

/*--------------------------------------------------------------
 * arc_clone()
 */
GFSM_INLINE
gfsmArc *gfsm_arc_clone(gfsmArc *src)
{
  gfsmArc *dst = g_new(gfsmArc,1);
  *dst = *src;
  return dst;
}

/*--------------------------------------------------------------
 * arc_free()
 */
GFSM_INLINE
void gfsm_arc_free(gfsmArc *a)
{ g_free(a); }


/*======================================================================
 * Methods: Arc Comparison (backwards-compatible)
 */
//-- none

/*======================================================================
 * Methods: arc comparison mask utilties
 */

//--------------------------------------------------------------
GFSM_INLINE
gfsmArcCompMask gfsm_acmask_new(gfsmArcComp cmp, gint nth)
{
  return
#ifdef __cplusplus
    //-- this kind of crap is the reason i hate c++ --moo
    static_cast<gfsmArcCompMask>
#endif
    ( (cmp&gfsmACAll)<<(nth*gfsmACShift) );
}

//--------------------------------------------------------------
GFSM_INLINE
gfsmArcComp gfsm_acmask_nth(gfsmArcCompMask m, gint nth)
{
  return
#ifdef __cplusplus
    //-- this kind of crap is the reason i hate c++ --moo
    static_cast<gfsmArcComp>
#endif
    ( (m>>(nth*gfsmACShift))&gfsmACAll );
}

//--------------------------------------------------------------
GFSM_INLINE
gfsmArcComp gfsm_acmask_nth_comp(gfsmArcCompMask m, gint nth)
{
  return
#ifdef __cplusplus
    //-- this kind of crap is the reason i hate c++ --moo
    static_cast<gfsmArcComp>(
			     static_cast<gfsmArcCompMask>(gfsm_acmask_nth(m,nth))
			     &
			     ~static_cast<gfsmArcCompMask>(gfsmACReverse)
			     )
#else
    gfsm_acmask_nth(m,nth) & (~gfsmACReverse)
#endif
    ;
}

//--------------------------------------------------------------
GFSM_INLINE
gboolean gfsm_acmask_nth_reverse(gfsmArcCompMask m, gint nth)
{ return (gfsm_acmask_nth(m,nth)&gfsmACReverse) ? TRUE : FALSE; }


/*======================================================================
 * Methods: Arc comparison: prioritized
 */

//--------------------------------------------------------------
GFSM_INLINE
gint gfsm_arc_compare_bymask_1_(gfsmArc *a1, gfsmArc *a2, gfsmArcComp cmp, gfsmArcCompData *acdata)
{
  switch (cmp) { 
    //
    //-- forward (ascending)
  case gfsmACLower:
    if (a1->lower < a2->lower)   return -1;
    if (a1->lower > a2->lower)   return  1;
    return 0;
  case gfsmACUpper:
    if (a1->upper < a2->upper)   return -1;
    if (a1->upper > a2->upper)   return  1;
    return 0;
  case gfsmACWeight:
    return acdata->sr ? gfsm_sr_compare(acdata->sr, a1->weight, a2->weight) : 0;
  case gfsmACSource:
    if (a1->source < a2->source) return -1;
    if (a1->source > a2->source) return  1;
    return 0;
  case gfsmACTarget:
    if (a1->target < a2->target) return -1;
    if (a1->target > a2->target) return  1;
    return 0;
    //
    //-- reverse (descending)
   case gfsmACLowerR:
    if (a1->lower < a2->lower)   return  1;
    if (a1->lower > a2->lower)   return -1;
    return 0;
  case gfsmACUpperR:
    if (a1->upper < a2->upper)   return  1;
    if (a1->upper > a2->upper)   return -1;
    return 0;
  case gfsmACWeightR:
    return acdata->sr ? gfsm_sr_compare(acdata->sr, a2->weight, a1->weight) : 0;
  case gfsmACSourceR:
    if (a1->source < a2->source) return  1;
    if (a1->source > a2->source) return -1;
    return 0;
  case gfsmACTargetR:
    if (a1->target < a2->target) return  1;
    if (a1->target > a2->target) return -1;
    return 0;
    //
    //-- user
  case gfsmACUser:
    if (acdata->user_compare_func) {
      return (*(acdata->user_compare_func))(a1,a2,acdata->user_data);
    }
    //
    //-- default
  case gfsmACNone:
  default:
    return 0;
  }
  return 0;
}

//--------------------------------------------------------------
GFSM_INLINE
gint gfsm_arc_compare_bymask_inline(gfsmArc *a1, gfsmArc *a2, gfsmArcCompData *acdata)
{
  gint rc=0;
  //-- NULL check
  if (!a1) {
    if (!a2) return 0;
    return 1;
  }
  if (!a2) return -1;
  //
  if ( (rc=gfsm_arc_compare_bymask_1_(a1,a2,gfsm_acmask_nth(acdata->mask,0),acdata)) ) return rc;
  if ( (rc=gfsm_arc_compare_bymask_1_(a1,a2,gfsm_acmask_nth(acdata->mask,1),acdata)) ) return rc;
  if ( (rc=gfsm_arc_compare_bymask_1_(a1,a2,gfsm_acmask_nth(acdata->mask,2),acdata)) ) return rc;
  if ( (rc=gfsm_arc_compare_bymask_1_(a1,a2,gfsm_acmask_nth(acdata->mask,3),acdata)) ) return rc;
  if ( (rc=gfsm_arc_compare_bymask_1_(a1,a2,gfsm_acmask_nth(acdata->mask,4),acdata)) ) return rc;
  if ( (rc=gfsm_arc_compare_bymask_1_(a1,a2,gfsm_acmask_nth(acdata->mask,5),acdata)) ) return rc;
  return 0;
}


/*======================================================================
 * Methods: String utilities
 */

//--------------------------------------------------------------
GFSM_INLINE
gchar gfsm_acmask_nth_char(gfsmArcCompMask m, gint nth)
{
  switch (gfsm_acmask_nth(m,nth)) {
  case gfsmACLower:  return 'l';
  case gfsmACUpper:  return 'u';
  case gfsmACWeight: return 'w';
  case gfsmACSource: return 's';
  case gfsmACTarget: return 't';
    //
  case gfsmACLowerR:  return 'L';
  case gfsmACUpperR:  return 'U';
  case gfsmACWeightR: return 'W';
  case gfsmACSourceR: return 'S';
  case gfsmACTargetR: return 'T';
    //
  case gfsmACUser:    return 'x';
  case gfsmACNone:    return '_';
  default:            return '?';
  }
  return '?';
}

//--------------------------------------------------------------
GFSM_INLINE
const gchar *gfsm_arc_sortmode_to_name(gfsmArcCompMask m)
{ return gfsm_acmask_nth_string(m,0); }