parse_hash.c
Go to the documentation of this file.
00001 /* parse_hash.c 
00002 
00003   *****
00004   Copyright 1994-1997, Brown University
00005   Copyright 1998, 1999, Anthony R. Cassandra
00006 
00007                            All Rights Reserved
00008                            
00009   Permission to use, copy, modify, and distribute this software and its
00010   documentation for any purpose other than its incorporation into a
00011   commercial product is hereby granted without fee, provided that the
00012   above copyright notice appear in all copies and that both that
00013   copyright notice and this permission notice appear in supporting
00014   documentation.
00015   
00016   ANTHONY CASSANDRA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00017   INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00018   PARTICULAR PURPOSE.  IN NO EVENT SHALL ANTHONY CASSANDRA BE LIABLE FOR
00019   ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00020   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00021   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00022   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00023   *****
00024 */
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include "mdpCassandra.h"
00030 #include "parse_hash.h"
00031 
00032 Node *Hash_Table;
00033 
00034 /**********************************************************************/
00035 void H_create() {
00036 
00037    Hash_Table = (Node *) calloc( HASH_TABLE_SIZE , sizeof( *Hash_Table ));
00038 
00039    /* Need these to start at zero! */
00040    gNumStates = gNumActions = gNumObservations = 0;
00041 }  /* H_init */
00042 /**********************************************************************/
00043 void H_destroy() {
00044    Node temp;
00045    int i;
00046 
00047    for( i = 0; i < HASH_TABLE_SIZE; i++) 
00048       while( Hash_Table[i] != NULL ) {
00049          temp = Hash_Table[i];
00050          Hash_Table[i] = temp->next;
00051          free( temp->str );
00052          free( temp );
00053       }  /* while */
00054   
00055    free( Hash_Table );
00056 }  /* H_destroy */
00057 /**********************************************************************/
00058 int H_string( char *str ) {
00059    int max;
00060 
00061    if(( str == NULL) || (str[0] == '\0' )) {
00062       fprintf( stderr, "**ERR: Bad string in H_string().\n");
00063       exit( -1);
00064    }
00065 
00066    max = strlen( str ) - 1;
00067 
00068    switch( max ) {
00069    case 0:
00070       return( str[0] % HASH_TABLE_SIZE);
00071    case 1:
00072       return( ( str[0] * str[1] ) % HASH_TABLE_SIZE);
00073    case 2:
00074       return( ( str[0] * str[1] + str[2] ) % HASH_TABLE_SIZE);
00075    default:
00076       return( ( str[0] * str[1] * str[max-1] + str[max] ) % HASH_TABLE_SIZE);
00077 
00078    }  /* switch */
00079 
00080 }  /* H_string */
00081 /**********************************************************************/
00082 int H_match( char *str, Mnemonic_Type type, Node node ) {
00083 
00084    if( node == NULL )  {
00085       fprintf( stderr, "**ERR: Null node in H_match().\n");
00086       exit(-1);
00087    }
00088       
00089    if( type != node->type )
00090       return 0;
00091    if( strcmp( str, node->str ) != 0 )
00092       return 0;
00093    
00094    return 1;
00095 }  /* H_match */
00096 /**********************************************************************/
00097 int H_enter( char *str, Mnemonic_Type type ) {
00098    Node trail, temp;
00099    int hash;
00100 
00101    if(( str == NULL) || ( str[0] ==  '\0' )) {
00102       fprintf( stderr, "**ERR: Bad string in H_enter().\n");
00103       exit( -1);
00104    }
00105 
00106    hash = H_string( str );
00107 
00108    /* Find end of linked list */
00109    trail = temp = Hash_Table[hash];
00110    while( temp != NULL ) {
00111       trail = temp;
00112       /* if already in the list then there's a duplicate */
00113       if ( H_match( str, type, temp) == 1 ) 
00114          return 0;
00115       temp = temp->next;
00116    }  /* while */
00117 
00118    /* create node and set fields */
00119    temp = (Node) malloc( sizeof(*temp));
00120    checkAllocatedPointer((void *) temp );
00121 
00122    temp->next = NULL;
00123    temp->type = type;
00124    temp->str = (char *) malloc( (strlen( str )+1) * sizeof (char ));
00125    checkAllocatedPointer((void *) temp->str );
00126 
00127    strcpy( temp->str, str );
00128 
00129    /* Set number and increment appropriate value */
00130    switch( type ) {
00131    case nt_state:
00132       temp->number = gNumStates++;
00133       break;
00134    case nt_action:      
00135       temp->number = gNumActions++;
00136       break;
00137    case nt_observation:
00138       temp->number = gNumObservations++;
00139       break;
00140    default:
00141       fprintf( stderr, "**ERR: Bad type in H_enter()\n");
00142       exit( -1);
00143    }
00144 
00145    /* Add to hash table */
00146    if( trail == NULL ) 
00147       Hash_Table[hash] = temp;
00148    else
00149       trail->next = temp;
00150 
00151    return 1;
00152 }  /* H_enterString */
00153 /**********************************************************************/
00154 int H_lookup( char *str, Mnemonic_Type type ) {
00155    int hash;
00156    Node temp;
00157 
00158    if(( str == NULL) || (str[0] ==  '\0' )) {
00159       fprintf( stderr, "**ERR: Bad string in H_getNum().\n");
00160       exit( -1);
00161    }
00162 
00163    hash = H_string( str );
00164 
00165    /* Find end of linked list */
00166    temp = Hash_Table[hash];
00167    while( temp != NULL ) {
00168       /* if already in the list then there's a duplicate */
00169       if ( H_match( str, type, temp) == 1 ) 
00170          return temp->number;
00171       temp = temp->next;
00172    }  /* while */
00173 
00174    return -1;
00175 
00176 }  /* H_getNum */
00177 /**********************************************************************/


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29