VPR-6.0

vpr/SRC/util/hash.c File Reference

#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "util.h"
Include dependency graph for hash.c:

Go to the source code of this file.

Defines

#define HASHSIZE   4093

Functions

static int hash_value (char *name)
struct s_hash ** alloc_hash_table (void)
void free_hash_table (struct s_hash **hash_table)
struct s_hash_iterator start_hash_table_iterator (void)
struct s_hashget_next_hash (struct s_hash **hash_table, struct s_hash_iterator *hash_iterator)
struct s_hashinsert_in_hash_table (struct s_hash **hash_table, char *name, int next_free_index)
struct s_hashget_hash_entry (struct s_hash **hash_table, char *name)

Define Documentation

#define HASHSIZE   4093

Definition at line 6 of file hash.c.


Function Documentation

struct s_hash** alloc_hash_table ( void  ) [read]

Creates a hash table with HASHSIZE different locations (hash values).

Definition at line 14 of file hash.c.

{
    struct s_hash **hash_table;

    hash_table = (struct s_hash **)my_calloc(sizeof(struct s_hash *),
                                             HASHSIZE);
    return (hash_table);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void free_hash_table ( struct s_hash **  hash_table)

Frees all the storage associated with a hash table.

Definition at line 26 of file hash.c.

{
    int i;
    struct s_hash *h_ptr, *temp_ptr;

    for(i = 0; i < HASHSIZE; i++)
        {
            h_ptr = hash_table[i];
            while(h_ptr != NULL)
                {
                    free(h_ptr->name);
                    temp_ptr = h_ptr->next;
                    free(h_ptr);
                    h_ptr = temp_ptr;
                }
        }

    free(hash_table);
}

Here is the caller graph for this function:

struct s_hash* get_hash_entry ( struct s_hash **  hash_table,
char *  name 
) [read]

Returns the hash entry with this name, or NULL if there is no corresponding entry.

Definition at line 142 of file hash.c.

{

    int i;
    struct s_hash *h_ptr;

    i = hash_value(name);
    h_ptr = hash_table[i];

    while(h_ptr != NULL)
        {
            if(strcmp(h_ptr->name, name) == 0)
                return (h_ptr);

            h_ptr = h_ptr->next;
        }

    return (NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct s_hash* get_next_hash ( struct s_hash **  hash_table,
struct s_hash_iterator hash_iterator 
) [read]

Returns the next occupied hash entry, and moves the iterator structure forward so the next call gets the next entry.

Definition at line 64 of file hash.c.

{

    int i;
    struct s_hash *h_ptr;

    i = hash_iterator->i;
    h_ptr = hash_iterator->h_ptr;

    while(h_ptr == NULL)
        {
            i++;
            if(i >= HASHSIZE)
                return (NULL);  /* End of table */

            h_ptr = hash_table[i];
        }

    hash_iterator->h_ptr = h_ptr->next;
    hash_iterator->i = i;
    return (h_ptr);
}

Here is the caller graph for this function:

static int hash_value ( char *  name) [static]

Creates a hash key from a character string. Only the first character and the last 8 characters of the string are used -- that may be dumb.

Definition at line 167 of file hash.c.

{
    int i, k;
    int val = 0, mult = 1;

        i = strlen(name);
    k = max(i - 8, 0);
    for(i = strlen(name) - 1; i >= k; i--)
        {
            val += mult * ((int)name[i]);
            mult *= 7;
        }
    val += (int)name[0];
    val %= HASHSIZE;
    return (val);
}

Here is the caller graph for this function:

struct s_hash* insert_in_hash_table ( struct s_hash **  hash_table,
char *  name,
int  next_free_index 
) [read]

Adds the string pointed to by name to the hash table, and returns the hash structure created or updated. If name is already in the hash table the count member of that hash element is incremented. Otherwise a new hash entry with a count of zero and an index of next_free_index is created.

Definition at line 95 of file hash.c.

{

    int i;
    struct s_hash *h_ptr, *prev_ptr;

    i = hash_value(name);
    prev_ptr = NULL;
    h_ptr = hash_table[i];

    while(h_ptr != NULL)
        {
            if(strcmp(h_ptr->name, name) == 0)
                {
                    h_ptr->count++;
                    return (h_ptr);
                }

            prev_ptr = h_ptr;
            h_ptr = h_ptr->next;
        }

/* Name string wasn't in the hash table.  Add it. */

    h_ptr = (struct s_hash *)my_malloc(sizeof(struct s_hash));
    if(prev_ptr == NULL)
        {
            hash_table[i] = h_ptr;
        }
    else
        {
            prev_ptr->next = h_ptr;
        }
    h_ptr->next = NULL;
    h_ptr->index = next_free_index;
    h_ptr->count = 1;
    h_ptr->name = (char *)my_malloc((strlen(name) + 1) * sizeof(char));
    strcpy(h_ptr->name, name);
    return (h_ptr);
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct s_hash_iterator start_hash_table_iterator ( void  ) [read]

Call this routine before you start going through all the elements in a hash table. It sets the internal indices to the start of the table.

Definition at line 51 of file hash.c.

{
    struct s_hash_iterator hash_iterator;

    hash_iterator.i = -1;
    hash_iterator.h_ptr = NULL;
    return (hash_iterator);
}

Here is the caller graph for this function: