libutils.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006  SRI International
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  * SRI International: 333 Ravenswood Ave, Menlo Park, CA 94025
00019  */
00020 
00021 #include "libicl.h"
00022 #include "dictionary.h"
00023 
00024 /* Make sure only loaded once... */
00025 #ifndef _LIBUTILS_H_INCLUDED
00026 #define _LIBUTILS_H_INCLUDED
00027 
00028 #ifdef __cplusplus
00029 extern "C" {
00030 #endif
00031 
00032 #define DEBUG_LEVEL 1
00033 
00034   EXTERN int oaa_ResolveVariable(char* inVarName, ICLTerm **resolvedVar);
00035   EXTERN void printDebug(int level, char *str, ...);
00036   EXTERN void printWarning(int level, char *str, ...);
00037   void print_dictionary(DICTIONARY *d);
00038 
00039   EXTERN char* get64BitFormatWrapped(char* pre, char* post);
00040   EXTERN char* get64BitFormat();
00041 
00042 #include <stddef.h>           /* For size_t     */
00043 
00044   /*
00045    * A hash table consists of an array of these buckets.  Each bucket
00046    * holds a copy of the key, a pointer to the data associated with the
00047    * key, and a pointer to the next bucket that collided with this one,
00048    * if there was one.
00049    */
00050 
00051   typedef struct htbucket_struct {
00052     char *key;
00053     void *data;
00054     struct htbucket_struct *next;
00055   } htbucket
00056   ;
00057 
00058   /*
00059    * This is what you actually declare an instance of to create a table.
00060    * You then call 'construct_table' with the address of this structure,
00061    * and a guess at the size of the table.  Note that more nodes than this
00062    * can be inserted in the table, but performance degrades as this
00063    * happens.  Performance should still be quite adequate until 2 or 3
00064    * times as many nodes have been inserted as the table was created with.
00065    */
00066 
00067   typedef struct hthash_table_struct {
00068     size_t size;
00069     htbucket **table;
00070   } hthash_table;
00071 
00072   typedef struct htfreeNodeData_struct
00073   {
00074     void (*function)(void *);
00075     hthash_table *the_table;  
00076   } htfreeNodeData
00077   ;
00078 
00079   /*
00080    * This is used to construct the table.  If it doesn't succeed, it sets
00081    * the table's size to 0, and the pointer to the table to NULL.
00082    */
00083 
00084   hthash_table *htconstruct_table(hthash_table *table,size_t size);
00085 
00086   /*
00087    * Inserts a pointer to 'data' in the table, with a copy of 'key' as its
00088    * key.  Note that this makes a copy of the key, but NOT of the
00089    * associated data.
00090    */
00091 
00092   void *htinsert(char *key,void *data,hthash_table *table);
00093 
00094   /*
00095    * Returns a pointer to the data associated with a key.  If the key has
00096    * not been inserted in the table, returns NULL.
00097    */
00098 
00099   void *htlookup(char *key,hthash_table *table);
00100 
00101   /*
00102    * Deletes an entry from the table.  Returns a pointer to the data that
00103    * was associated with the key so the calling code can dispose of it
00104    * properly.
00105    */
00106 
00107   void *htdel(char *key,hthash_table *table);
00108 
00109   /*
00110    * Goes through a hash table and calls the function passed to it
00111    * for each node that has been inserted.  The function is passed
00112    * a pointer to the key, and a pointer to the data associated
00113    * with it.
00114    */
00115 
00116   void htenumerate(hthash_table *table,void (*func)(char *,void *, void *),void *otherData);
00117 
00118   /*
00119    * Frees a hash table.  For each node that was inserted in the table,
00120    * it calls the function whose address it was passed, with a pointer
00121    * to the data that was in the table.  The function is expected to
00122    * free the data.  Typical usage would be:
00123    * free_table(&table, free);
00124    * if the data placed in the table was dynamically allocated, or:
00125    * free_table(&table, NULL);
00126    * if not.  ( If the parameter passed is NULL, it knows not to call
00127    * any function with the data. )
00128    */
00129 
00130   void htfree_table(hthash_table *table, void (*func)(void *));
00131 
00132   void htprint(hthash_table *table);
00133 
00134 #ifdef __cplusplus
00135 }
00136 #endif
00137 
00138 #endif
00139 

Generated on Wed May 23 17:20:12 2007 using doxygen 1.5.2