/*=============================================================================*\ * File: gfsmEnum.def * Author: Bryan Jurish * 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 #include /*====================================================================== * 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; }