home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.ee.lbl.gov
/
2014.05.ftp.ee.lbl.gov.tar
/
ftp.ee.lbl.gov
/
mrdebug.tar.Z
/
mrdebug.tar
/
mrhash.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-25
|
6KB
|
271 lines
/*
**++
** ABSTRACT:
**
** This module contains hashing functions specific to the gateway
** implementation.
**
** FUNCTIONS and PROCEDURES:
**
** create_htable Create hash table and initialize it
** delete_htable Remove entry from hash table
** insert_htable Insert entry into hash table
** lookup_htable Search for specific entry in table
** update_htable Modify entry in table
**
** AUTHOR:
**
** Deb Agarwal (original by: Paul W. Ciarfella)
**
** CREATION DATE: 14June93 (17Aug92)
**
** MODIFICATION HISTORY:
**--
**/
/*
**
** INCLUDE FILES
**
*/
#include <stddef.h>
#include <stdio.h>
#include <sysexits.h>
#include "types.h"
#include "mrhash.h"
#include "data_struct_proto.h"
void Sys_exit( int error )
{
printf( "PROGRAM EXITTING WITH ERROR %d\n", error );
exit( error );
}
/*
** The hash table uses a simple division scheme with chaining on collision.
** The table itself is an array of pointers to HENTRY. The data in the table
** is stored as a linked list of HENTRY structures anchored from a slot in
** the table.
**
** Slots Data entries
** -------- --------------------------------------------
** [ 0 ] -> [ HENTRY ] <-> [ HENTRY ] <-> ... [ HENTRY ]
** :
** [ n ] -> [ HENTRY ] <-> [ HENTRY ] <-> ... [ HENTRY ]
**
**
** Posn in slot Prev_p Next_p
** ------------ -------- ------------------------------
** First Null Non-null if next entry exists
** Null if no entry exists
** Middle Non-null Non-null
** End Non-null Null
*/
void create_htable( HTABLE_t *table_p, int slots )
{
HENTRY_t *newtbl_p;
/*
** Use calloc to allocate memory, explicit initialization to 0 is not needed
*/
if ( ( newtbl_p = (HENTRY_t *)calloc(slots, sizeof(HENTRY_t)) ) == NULL )
{
printf("create_htable: Malloc returned NULL\n");
Sys_exit( EX_OSERR );
}
table_p->htbl_p = (HENTRY_t **) newtbl_p;
table_p->slots = slots;
table_p->entries = 0;
}
/*
** Hashing function: very generic. Specific functions could be defined
** by adding a hash_function field to the HTABLE structure. Different
** hash tables could use different hashing functions.
*/
long hash_index( char *key_p, long tbl_length )
{
long len,
pos,
index;
index = 0;
len = strlen( key_p );
for( pos = 0; pos < len; pos++ )
index += (int)key_p[pos];
index %= tbl_length;
return index;
}
/*
** Look-up an entry in the hash table and return
** a pointer to it if it is found.
*/
HENTRY_t *lookup_htable( HTABLE_t *table_p, char *key_p )
{
long hidx;
HENTRY_t *match_p;
/*
** Calculate index
*/
hidx = hash_index( key_p, table_p->slots );
/*
** Search for match
*/
for ( match_p = table_p->htbl_p[ hidx ];
match_p != NULL; match_p = match_p->next_p )
if ( !strcmp( match_p->key_p, key_p ))
/* the entry is already in the table */
break;
/*
** Return a pointer to the entry in the table.
*/
return( match_p );
}
void delete_htable( HTABLE_t *table_p, char *key_p )
{
HENTRY_t *entry_p;
/*
** Find entry in the table
*/
if ( ( entry_p = lookup_htable( table_p, key_p ) ) == NULL )
{
printf("ERROR: delete_htable: Did not find entry\n");
Sys_exit( EX_SOFTWARE );
}
/*
** Remove entry from the table.
*/
if ( entry_p->prev_p == NULL )
table_p->htbl_p[ entry_p->hidx ] = entry_p->next_p;
else
{
entry_p->prev_p->next_p = entry_p->next_p;
if ( entry_p->next_p != NULL )
entry_p->next_p->prev_p = entry_p->prev_p;
}
free( entry_p );
if ( --table_p->entries == 0 )
printf("delete_htable: WARNING: table is empty.\n" );
else
if ( table_p->entries < 0 )
{
printf("delete_htable: ERROR!! hash table entries < 0 \n");
Sys_exit( EX_SOFTWARE );
}
}
/*
** Assumes that entry does not already exist for this data
*/
void insert_htable( HTABLE_t *table_p, char *key_p, long index, long *subnet, char *name )
{
long hidx;
HENTRY_t *nentry_p;
/*
** Calculate index
*/
hidx = hash_index( key_p, table_p->slots );
/*printf( "name %s has hash index %ld\n", key_p, hidx );*/
/*
** Create new entry
*/
if ( ( nentry_p = (HENTRY_t *) malloc( sizeof( HENTRY_t ) ) ) == NULL )
{
printf("insert_htable: malloc returned NULL\n");
Sys_exit( EX_OSERR );
}
nentry_p->hidx = hidx; /* Slot number in table */
strcpy( nentry_p->key_p, key_p ); /* IP name */
nentry_p->index = index; /* adjacency list index of name */
nentry_p->subnet = subnet; /* pointer to the subnet interface */
if( name != NULL )
strcpy( nentry_p->name, name );
/*
** Insert entry:
** if slot in table is empty then put it there; otherwise slip it in
** front of the existing entries on that slot.
*/
nentry_p->prev_p = NULL;
if ( table_p->htbl_p[ hidx ] == NULL )
nentry_p->next_p = NULL;
else
{
nentry_p->next_p = table_p->htbl_p[ hidx ];
nentry_p->next_p->prev_p = nentry_p;
}
table_p->htbl_p[ hidx ] = nentry_p;
++table_p->entries;
}
void update_htable( HTABLE_t *table_p, char *key_p, long index )
{
HENTRY_t *entry_p;
/*
** Find entry in the table
*/
if ( ( entry_p = lookup_htable( table_p, key_p ) ) == NULL )
{
printf("ERROR: update_htable: Did not find entry\n");
Sys_exit( EX_SOFTWARE );
}
/*
** Update entry
*/
entry_p->index = index;
}
void print_htable( HTABLE_t *table_p )
{
long i, count;
HENTRY_t *cur_p;
count = 0;
printf( "there should be %d entries in the table\n", table_p->entries);
for( i = 0; i < table_p->slots; i++ )
{
printf( "\nSLOT %ld: ", i);
for( cur_p = table_p->htbl_p[i]; cur_p != NULL;
cur_p = cur_p->next_p )
{
printf( "%s,%ld; ", cur_p->key_p, cur_p->index );
count++;
}
}
printf( "The actual count came to %ld\n", count );
}
/*
**
** END: hash.c
**
*/