aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmEnum.hi
blob: f7ae20e5710752fdef4df8c05e57d80b9dc0aae5 (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
/*=============================================================================*\
 * File: gfsmEnum.def
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library: enumerations: inline definitions
 *
 * Copyright (c) 2004 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 <glib.h>
#include <gfsmUtils.h>

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

/*--------------------------------------------------------------
 * new_full()
 */
GFSM_INLINE
gfsmEnum *gfsm_enum_new_full(gfsmDupFunc key_dup_func,
			     GHashFunc   key_hash_func,
			     GEqualFunc  key_equal_func,
			     GDestroyNotify key_destroy_func)
{
  gfsmEnum *en = g_new(gfsmEnum,1);
  en->table    = g_hash_table_new_full(key_hash_func,
				       key_equal_func,
				       key_destroy_func,
				       NULL);
  en->nxtval   = 0;
  en->key_dup  = key_dup_func;
  return en;
}

/*--------------------------------------------------------------
 * clear()
 */
GFSM_INLINE
void gfsm_enum_clear(gfsmEnum *en)
{
  g_hash_table_foreach_remove(en->table, gfsm_hash_clear_func, NULL);
  en->nxtval = 0;
}

/*--------------------------------------------------------------
 * free()
 */
GFSM_INLINE
void gfsm_enum_free(gfsmEnum *en)
{
  g_hash_table_destroy(en->table);
  g_free(en);
}


/*======================================================================
 * Methods: Accessors
 */

/*--------------------------------------------------------------
 * lookup_extended()
 */
GFSM_INLINE
gboolean gfsm_enum_lookup_extended(gfsmEnum      *en,
				   gconstpointer  lookup_key,
				   gpointer      *stored_key,
				   gpointer      *stored_val)
{
  return g_hash_table_lookup_extended(en->table, lookup_key, stored_key, stored_val);
}

/*--------------------------------------------------------------
 * lookup()
 */
GFSM_INLINE
guint gfsm_enum_lookup(gfsmEnum *en, gconstpointer key)
{
  gpointer s_val;
  if (g_hash_table_lookup_extended(en->table, key, NULL, &s_val)) return GPOINTER_TO_UINT(s_val);
  return gfsmEnumNone;
}


/*--------------------------------------------------------------
 * insert_full()
 */
GFSM_INLINE
guint gfsm_enum_insert_full(gfsmEnum *en, gpointer key, guint val)
{
  gpointer s_key, s_val;
  if (g_hash_table_lookup_extended(en->table, key, &s_key, &s_val)) {
    //-- already present: steal it & replace
    if (val == gfsmEnumNone) {
      val = GPOINTER_TO_UINT(s_val);
    } else {
      g_hash_table_steal(en->table, s_key);
      g_hash_table_insert(en->table, s_key, GUINT_TO_POINTER(val));
    }
  }
  else {
    //-- no key already present: insert a new one
    if (en->key_dup) s_key = (*(en->key_dup))(key);
    else             s_key = key;
    if (val == gfsmEnumNone) val = en->nxtval;
    g_hash_table_insert(en->table, s_key, GUINT_TO_POINTER(val));
  }

  //-- keep track of available values
  if (val >= en->nxtval) en->nxtval = val+1;

  return val;
}