aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmWeightMap.hi
blob: 1361bb8ba7fb2bc6d9f2757735fdfd0aa7436d64 (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
/*=============================================================================*\
 * File: gfsmWeightmap.hi
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library: weight maps: inlined definitions
 *
 * 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 <gfsmUtils.h>

/*======================================================================
 * gfsmWeight <-> gpointer conversions
 */
GFSM_INLINE
gfsmWeight gfsm_ptr2weight(const gpointer p)
{
  gfsmWeightOrPointer wp;
  wp.p=p;
  return wp.w;
}

GFSM_INLINE
gpointer gfsm_weight2ptr(const gfsmWeight w)
{
  gfsmWeightOrPointer wp;
  wp.p=0;
  wp.w=w;
  return wp.p;
}


/*======================================================================
 * gfsmWeightMap: Constructors etc.
 */

//--------------------------------------------------------------
GFSM_INLINE
gfsmWeightMap *gfsm_weightmap_new_full(GCompareDataFunc key_cmp_func,
				       gpointer         key_cmp_data,
				       GDestroyNotify   key_free_func)
{
  return g_tree_new_full(key_cmp_func,key_cmp_data,key_free_func,NULL);
}

//--------------------------------------------------------------
GFSM_INLINE
gfsmWeightMap *gfsm_weightmap_new(GCompareFunc key_cmp_func)
{
  return g_tree_new(key_cmp_func);
}

//--------------------------------------------------------------
GFSM_INLINE
gfsmWeightMap *gfsm_weightmap_copy(gfsmWeightMap *dst, gfsmWeightMap *src)
{
  return gfsm_set_copy(dst,src);
}

//--------------------------------------------------------------
GFSM_INLINE
void gfsm_weightmap_clear(gfsmWeightMap *wm)
{ gfsm_set_clear(wm); }

//--------------------------------------------------------------
GFSM_INLINE
void gfsm_weightmap_free(gfsmWeightMap *wm)
{
  gfsm_set_free(wm);
}

/*======================================================================
 * gfsmWeightMap: Accessors
 */


//--------------------------------------------------------------
GFSM_INLINE
gboolean gfsm_weightmap_contains(gfsmWeightMap *weightmap, gconstpointer key)
{
  gfsmWeight w;
  return gfsm_weightmap_lookup(weightmap, key, &w);
}

//--------------------------------------------------------------
GFSM_INLINE
gboolean gfsm_weightmap_lookup(gfsmWeightMap *weightmap, gconstpointer key, gfsmWeight *wp)
{
  gpointer orig_key, orig_value;
  if (g_tree_lookup_extended(weightmap, key, &orig_key, &orig_value)) {
    *wp = gfsm_ptr2weight(orig_value);
    return TRUE;
  }
  return FALSE;
}

//--------------------------------------------------------------
GFSM_INLINE
void gfsm_weightmap_insert(gfsmWeightMap *weightmap, gconstpointer key, gfsmWeight w)
{
   g_tree_insert(weightmap, (gpointer)key, gfsm_weight2ptr(w));
}

/*======================================================================
 * gfsmWeighHash: Constructors etc.
 */
//--------------------------------------------------------------
GFSM_INLINE
gfsmWeightHash *gfsm_weighthash_new_full(gfsmDupFunc key_dup_func,
					 GHashFunc   key_hash_func,
					 GEqualFunc  key_equal_func,
					 GDestroyNotify key_destroy_func)
{
  gfsmWeightHash *wh = g_new(gfsmWeightHash,1);
  wh->table   = g_hash_table_new_full(key_hash_func, key_equal_func, key_destroy_func, NULL);
  wh->key_dup = key_dup_func;
  return wh;
}


//--------------------------------------------------------------
GFSM_INLINE
void gfsm_weighthash_clear(gfsmWeightHash *wh)
{
  g_hash_table_foreach_remove(wh->table, gfsm_hash_clear_func,NULL);
}

//--------------------------------------------------------------
GFSM_INLINE
void gfsm_weighthash_free(gfsmWeightHash *wh)
{
  g_hash_table_destroy(wh->table);
  g_free(wh);
}


/*======================================================================
 * gfsmWeightHash: Accessors
 */

//--------------------------------------------------------------
GFSM_INLINE
gboolean gfsm_weighthash_lookup(gfsmWeightHash *wh, gconstpointer key, gfsmWeight *wp)
{
  gpointer s_key, s_val;
  if (g_hash_table_lookup_extended(wh->table, key, &s_key, &s_val)) {
    *wp = gfsm_ptr2weight(s_val);
    return TRUE;
  }
  return FALSE;
}

//--------------------------------------------------------------
GFSM_INLINE
void gfsm_weighthash_insert(gfsmWeightHash *wh, gconstpointer key, gfsmWeight w)
{
  gpointer s_key, s_val;
  if (wh->key_dup && g_hash_table_lookup_extended(wh->table, key, &s_key, &s_val)) {
    //-- already present: steal & replace
    g_hash_table_steal(wh->table, s_key);
    g_hash_table_insert(wh->table, s_key, gfsm_weight2ptr(w));
  }
  else {
    //-- not yet present: insert new mapping
    if (wh->key_dup) s_key = (*(wh->key_dup))(key);
    else             s_key = (gpointer)key;
    g_hash_table_insert(wh->table, s_key, gfsm_weight2ptr(w));
  }
}

//--------------------------------------------------------------
GFSM_INLINE
gboolean gfsm_weighthash_insert_if_less(gfsmWeightHash *wh, gconstpointer key, gfsmWeight w, gfsmSemiring *sr)
{
  gpointer s_key, s_val;
  if (wh->key_dup && g_hash_table_lookup_extended(wh->table, key, &s_key, &s_val)) {
    //-- already present 
    gfsmWeight s_w = gfsm_ptr2weight(s_val);
    if (!gfsm_sr_less(sr, w, s_w)) return FALSE; //-- (s_w) <= (w) : no update required

    //-- steal & update
    g_hash_table_steal(wh->table, s_key);
    g_hash_table_insert(wh->table, s_key, gfsm_weight2ptr(w));
  }
  else {
    //-- not yet present: insert new mapping
    if (wh->key_dup) s_key = (*(wh->key_dup))(key);
    else             s_key = (gpointer)key;
    g_hash_table_insert(wh->table, s_key, gfsm_weight2ptr(w));
  }

  return TRUE; //-- update performed
}

//--------------------------------------------------------------
GFSM_INLINE
gboolean gfsm_weighthash_insert_sum_if_less(gfsmWeightHash *wh, gconstpointer key, gfsmWeight w, gfsmSemiring *sr)
{
  gpointer s_key, s_val;
  if (wh->key_dup && g_hash_table_lookup_extended(wh->table, key, &s_key, &s_val)) {
    //-- already present
    gfsmWeight s_w = gfsm_ptr2weight(s_val);
    w = gfsm_sr_plus(sr,w,s_w);
    if (!gfsm_sr_less(sr,w,s_w)) return FALSE; //-- (s_w) <= (w+s_w) : no update required

    //-- steal & update
    g_hash_table_steal(wh->table, s_key);
    g_hash_table_insert(wh->table, s_key, gfsm_weight2ptr(w));
  }
  else {
    //-- not yet present: insert new mapping
    if (wh->key_dup) s_key = (*(wh->key_dup))(key);
    else             s_key = (gpointer)key;
    g_hash_table_insert(wh->table, s_key, gfsm_weight2ptr(w));
  }

  return TRUE; //-- update performed
}