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

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

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

/*======================================================================
 * Methods: Arc lists
 */

/*--------------------------------------------------------------
 * arclist_insert_node_sorted()
 */
gfsmArcList *gfsm_arclist_insert_node_sorted(gfsmArcList *al, gfsmArcList *nod, gfsmArcCompData *acdata)
{
  gfsmArcList *al_first=al;
  gfsmArcList *al_prev=NULL;

  for (; al != NULL; al_prev=al, al=al->next) {
    if (gfsm_arc_compare_bymask_inline(&(nod->arc), &(al->arc), acdata) <= 0) break;
  }
  if (al_prev == NULL) return gfsm_arclist_prepend_node(al,nod);
  al_prev->next = gfsm_arclist_prepend_node(al,nod);

  return al_first;
}

/*--------------------------------------------------------------
 * arclist_clone()
 */
gfsmArcList *gfsm_arclist_clone(gfsmArcList *src)
{
  gfsmArcList *dst = NULL, *prev=NULL;
  while (src != NULL) {
    gfsmArcList *nod = gfsm_arclist_new_full(src->arc.source,
					     src->arc.target,
					     src->arc.lower,
					     src->arc.upper,
					     src->arc.weight,
					     NULL);
    if (prev==NULL) {
      dst = nod;
    } else {
      prev->next = nod;
    }
    prev = nod;
    src  = src->next;
  }
  return dst;
}

/*--------------------------------------------------------------
 * arclist_concat()
 */
gfsmArcList *gfsm_arclist_concat(gfsmArcList *al1, gfsmArcList *al2)
{
  if (al1==NULL) { return al2; }
  else {
    gfsmArcList *nod=al1;
    while (nod->next != NULL) { nod=nod->next; }
    nod->next = al2;
  }
  return al1;
}

/*--------------------------------------------------------------
 * arclist_length()
 */
guint gfsm_arclist_length(gfsmArcList *al)
{
  guint len=0;
  while (al != NULL) { ++len; al=al->next; }
  return len;
}

/*--------------------------------------------------------------
 * arclist_free()
 */
void gfsm_arclist_free(gfsmArcList *al)
{
  while (al != NULL) {
    gfsmArcList *nxt = al->next;
    g_free(al);
    al = nxt;
  }
}

/*--------------------------------------------------------------
 * arclist_sort_with_data() & friends
 *  + adapted from code in GLib glib/gslist.c:
 *    Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald,
 *    Modified by the GLib Team and others 1997-2000.
 */
static
gfsmArcList *gfsm_arclist_sort_merge (gfsmArcList   *l1, 
				      gfsmArcList   *l2,
				      GFunc     compare_func,
				      gpointer  user_data)
{
  gfsmArcList list, *l;
  gint cmp;

  l=&list;

  while (l1 && l2)
    {
      cmp = ((GCompareDataFunc) compare_func) (&(l1->arc), (&l2->arc), user_data);

      if (cmp <= 0)
        {
	  l=l->next=l1;
	  l1=l1->next;
        } 
      else 
	{
	  l=l->next=l2;
	  l2=l2->next;
        }
    }
  l->next= l1 ? l1 : l2;
  
  return list.next;
}


gfsmArcList *gfsm_arclist_sort_real (gfsmArcList   *list,
				     GFunc     compare_func,
				     gpointer  user_data)
{
  gfsmArcList *l1, *l2;

  if (!list) 
    return NULL;
  if (!list->next) 
    return list;

  l1 = list; 
  l2 = list->next;

  while ((l2 = l2->next) != NULL)
    {
      if ((l2 = l2->next) == NULL) 
	break;
      l1=l1->next;
    }
  l2 = l1->next; 
  l1->next = NULL;

  return gfsm_arclist_sort_merge (gfsm_arclist_sort_real (list, compare_func, user_data),
				  gfsm_arclist_sort_real (l2, compare_func, user_data),
				  compare_func,
				  user_data);
}


/*--------------------------------------------------------------
 * arclist_remove_node()
 *  + adapted from _g_slist_remove_link()
 */
gfsmArcList *gfsm_arclist_remove_node(gfsmArcList *al, gfsmArcList *nod)
{
  gfsmArcList *tmp;
  gfsmArcList *prev;

  prev = NULL;
  tmp = al;

  while (tmp)
    {
      if (tmp == nod)
	{
	  if (prev)
	    prev->next = tmp->next;
	  if (al == tmp)
	    al = al->next;

	  tmp->next = NULL;
	  break;
	}

      prev = tmp;
      tmp = tmp->next;
    }

  return al;
}


/*--------------------------------------------------------------
 * arclist_reverse()
 */
gfsmArcList* gfsm_arclist_reverse(gfsmArcList *al)
{
  gfsmArcList *prev=NULL;
  while (al) {
    gfsmArcList *next = al->next;
    al->next = prev;
    prev     = al;
    al       = next;
  }
  return prev;
}