Azinix

st.h File Reference

Hash data structures. More...

Go to the source code of this file.

Data Structures

struct  Hash_Entry_t
 Hash table entry. More...
struct  Hash_Generator_t
 Used for traversing entries in a table. More...
struct  Hash_t
 Hash table. More...

Defines

#define HASH_DEFAULT_MAX_DENSITY   5
#define HASH_DEFAULT_INIT_TABLE_SIZE   11
#define HASH_DEFAULT_GROW_FACTOR   2.0
#define HASH_DEFAULT_REORDER_FLAG   0
#define ST_DEFAULT_MAX_DENSITY   HASH_DEFAULT_MAX_DENSITY
#define ST_DEFAULT_INIT_TABLE_SIZE   HASH_DEFAULT_INIT_TABLE_SIZE
#define ST_DEFAULT_GROW_FACTOR   HASH_DEFAULT_GROW_FACTOR
#define ST_DEFAULT_REORDER_FLAG   HASH_DEFAULT_REORDER_FLAG
#define Hash_ForEachItem(table, gen, key, value)   for(gen=st_init_gen(table); st_gen(gen,key,value) || (st_free_gen(gen),0);)
 An iteration macro which loops over all the entries in `table'.
#define st_foreach_item(table, gen, key, value)   Hash_ForEachItem(table, gen, key, value)
#define Hash_ForEachItemNew(table, key, value)   for(table->gen=st_init_gen(table); st_gen(table->gen,key,value) || (st_free_gen(table->gen),0);)
#define st_foreach_item_new(table, key, value)   Hash_ForEachItemNew(table, key, value)
#define st_foreach_item_int(table, gen, key, value)   for(gen=st_init_gen(table); st_gen_int(gen,key,value) || (st_free_gen(gen),0);)
 An iteration macro which loops over all the entries in `table'.
#define HASH_OUT_OF_MEM   -10000
#define ST_OUT_OF_MEM   -10000

Typedefs

typedef Hash_Entry_t Hash_Entry_t
typedef Hash_Entry_t st_table_entry
typedef Hash_t Hash_t
typedef Hash_Generator_t Hash_Generator_t
typedef Hash_Generator_t st_generator
typedef int(*) Hash_GenericFunctionPointer_t ()
typedef Hash_t st_table
typedef int(*) ST_PFI ()

Enumerations

enum  st_retval { ST_CONTINUE, ST_STOP, ST_DELETE }

Functions

void Hash_Test ()
 Simple test of the hash package.
Hash_tHash_InitTableWithParams (ST_PFI, ST_PFI, int, int, double, int)
Hash_tHash_InitTable (ST_PFI a, ST_PFI b)
void Hash_FreeTable (Hash_t *)
 Free any internal storage associated with table.
int Hash_Lookup (Hash_t *, char *, char **)
 Lookup up `key' in `table'.
int Hash_LookupInt (Hash_t *, char *, int *)
 Lookup up int `key' in `table'.
int Hash_Insert (Hash_t *, char *, char *)
 Insert value in table under the key 'key'.
int Hash_AddDirect (Hash_t *, char *, char *)
 Place 'value' in 'table' under the key 'key'. This is done without checking if 'key' is in 'table' already.
int Hash_FindOrAdd (Hash_t *, char *, char ***)
 Lookup `key' in `table'. If not found, create an entry.In either case set slot to point to the field in the entry where the value is stored. The value associated with `key' may then be changed by accessing directly through slot. Returns 1 if an entry already existed, 0 otherwise.
int Hash_Find (Hash_t *a, char *b, char ***c)
 Like Hash_FindOrAdd, but does not create an entry if one is not found.
Hash_tHash_Copy (Hash_t *)
 Return a copy of old_table and all its members.
int Hash_Delete (Hash_t *, char **, char **)
 Delete the entry with the key pointed to by `key_ptr'.
int Hash_DeleteInt (Hash_t *, int *, char **)
 Delete the entry with the key pointed to by `key_ptr'.
int Hash_ForEach (Hash_t *, ST_PFSR, char *)
int Hash_StrHash (char *, int)
 Hash function for strings.
int st_strhash (char *, int)
int Hash_NumHash (char *, int)
 Hash function for numbers.
int st_numhash (char *, int)
int Hash_PtrHash (char *, int)
 Hash function for pointers.
int st_ptrhash (char *, int)
int Hash_NumCmp (int, int)
 Compare function for numbers.
int st_numcmp (int, int)
int Hash_PtrCmp (char *, char *)
 Compare function for pointers.
int st_ptrcmp (char *, char *)
Hash_Generator_tHash_InitGen (Hash_t *)
 Returns a generator handle which when used with Hash_Gen() will progressively return each (key, value) record in `table'.
Hash_Generator_tst_init_gen (Hash_t *a)
int Hash_Gen (Hash_Generator_t *, char **, char **)
 Given a generator returned by Hash_InitGen(), this routine returns the next (key, value) pair in the generation sequence.
int st_gen (Hash_Generator_t *a, char **b, char **c)
int Hash_GenInt (Hash_Generator_t *, char **, int *)
 Given a generator returned by Hash_InitGen(), this routine returns the next (key, value) pair in the generation sequence.
int st_gen_int (Hash_Generator_t *a, char **b, int *c)
void Hash_FreeGen (Hash_Generator_t *)
 After generating all items in a generation sequence, this routine must be called to reclaim the resources associated with `gen'.
void st_free_gen (Hash_Generator_t *a)

Variables

enum st_retval(*) ST_PFSR ()


Detailed Description

Hash data structures.

Definition in file st.h.


Define Documentation

#define HASH_DEFAULT_GROW_FACTOR   2.0

Definition at line 201 of file st.h.

#define HASH_DEFAULT_INIT_TABLE_SIZE   11

Definition at line 200 of file st.h.

#define HASH_DEFAULT_MAX_DENSITY   5

Definition at line 199 of file st.h.

#define HASH_DEFAULT_REORDER_FLAG   0

Definition at line 202 of file st.h.

#define Hash_ForEachItem ( table,
gen,
key,
value   )     for(gen=st_init_gen(table); st_gen(gen,key,value) || (st_free_gen(gen),0);)

An iteration macro which loops over all the entries in `table'.

It sets `key_p' to point to the key and `value_p' to the associated value (if it is not nil). `gen' is a generator variable used internally.

Sample usage:

                                                                                                      	char *key, *value;
      st_generator *gen;

      st_foreach_item(table, gen, &key, &value) {
           process_item(value);
      }

Definition at line 225 of file st.h.

#define Hash_ForEachItemNew ( table,
key,
value   )     for(table->gen=st_init_gen(table); st_gen(table->gen,key,value) || (st_free_gen(table->gen),0);)

Definition at line 231 of file st.h.

#define HASH_OUT_OF_MEM   -10000

Definition at line 260 of file st.h.

#define ST_DEFAULT_GROW_FACTOR   HASH_DEFAULT_GROW_FACTOR

Definition at line 206 of file st.h.

#define ST_DEFAULT_INIT_TABLE_SIZE   HASH_DEFAULT_INIT_TABLE_SIZE

Definition at line 205 of file st.h.

#define ST_DEFAULT_MAX_DENSITY   HASH_DEFAULT_MAX_DENSITY

Definition at line 204 of file st.h.

#define ST_DEFAULT_REORDER_FLAG   HASH_DEFAULT_REORDER_FLAG

Definition at line 207 of file st.h.

#define st_foreach_item ( table,
gen,
key,
value   )     Hash_ForEachItem(table, gen, key, value)

Definition at line 228 of file st.h.

#define st_foreach_item_int ( table,
gen,
key,
value   )     for(gen=st_init_gen(table); st_gen_int(gen,key,value) || (st_free_gen(gen),0);)

An iteration macro which loops over all the entries in `table'.

It sets `key_p' to point to the key and `value_p' to the associated value (if it is not nil). `value_p' is assumed to be a pointer to an integer. `gen' is a generator variable used internally.

Sample usage:

                                                                                                           
        char *key;
        int *value;
        st_generator *gen;

        st_foreach_item_int(table, gen, &key, &value) {
            process_item(value);
        }

Definition at line 257 of file st.h.

#define st_foreach_item_new ( table,
key,
value   )     Hash_ForEachItemNew(table, key, value)

Definition at line 234 of file st.h.

#define ST_OUT_OF_MEM   -10000

Definition at line 261 of file st.h.


Typedef Documentation

typedef struct Hash_Entry_t Hash_Entry_t

Definition at line 25 of file st.h.

typedef struct Hash_Generator_t Hash_Generator_t

Definition at line 42 of file st.h.

typedef int(*) Hash_GenericFunctionPointer_t()

Definition at line 46 of file st.h.

typedef struct Hash_t Hash_t

Definition at line 31 of file st.h.

typedef Hash_Generator_t st_generator

Definition at line 44 of file st.h.

typedef int(*) ST_PFI()

Definition at line 76 of file st.h.

typedef Hash_t st_table

Definition at line 70 of file st.h.

typedef Hash_Entry_t st_table_entry

Definition at line 27 of file st.h.


Enumeration Type Documentation

enum st_retval

Enumerator:
ST_CONTINUE 
ST_STOP 
ST_DELETE 

Definition at line 72 of file st.h.


Function Documentation

int Hash_AddDirect ( Hash_t table,
char *  key,
char *  value 
)

Place 'value' in 'table' under the key 'key'. This is done without checking if 'key' is in 'table' already.

This should only be used if you are sure there is not already an entry for 'key', since it is undefined which entry you would later get from Hash_Lookup or Hash_FindOrAdd.

Definition at line 453 of file st.c.

Hash_t* Hash_Copy ( Hash_t old_table  ) 

Return a copy of old_table and all its members.

(Hash_t *) 0 is returned if there was insufficient memory to do the copy.

Definition at line 654 of file st.c.

int Hash_Delete ( Hash_t table,
char **  keyp,
char **  value 
)

Delete the entry with the key pointed to by `key_ptr'.

If the entry is found, 1 is returned and `key_ptr' is set to the actual key and `entry_ptr' is set to the corresponding entry (This allows the freeing of the associated storage). If the entry is not found, then 0 is returned and nothing is changed.

Definition at line 714 of file st.c.

int Hash_DeleteInt ( Hash_t table,
int *  keyp,
char **  value 
)

Delete the entry with the key pointed to by `key_ptr'.

`key_ptr' should be specifically a pointer to an integer. If the entry is found, 1 is returned and `key_ptr' is set to the actual key and `entry_ptr' is set to the corresponding entry (This allows the freeing of the associated storage). If the entry is not found, then 0 is returned and nothing is changed.

Definition at line 752 of file st.c.

int Hash_Find ( Hash_t a,
char *  b,
char ***  c 
)

Like Hash_FindOrAdd, but does not create an entry if one is not found.

Definition at line 563 of file st.c.

int Hash_FindOrAdd ( Hash_t table,
char *  key,
char ***  slot 
)

Lookup `key' in `table'. If not found, create an entry.In either case set slot to point to the field in the entry where the value is stored. The value associated with `key' may then be changed by accessing directly through slot. Returns 1 if an entry already existed, 0 otherwise.

As an example:

                                                                                                      
      char **slot;
      char *key;
      char *value = (char *) item_ptr // ptr to a malloc'd structure

      if (Hash_FindOrAdd(table, key, &slot)) {
         FREE(*slot); // free the old value of the record 
      }
slot = value;  // attach the new value to the record 

This replaces the equivelent code:

                                                                                                      
      if (Hash_Lookup(table, key, &ovalue)) {
         FREE(ovalue);
      }
      Hash_Insert(table, key, value);

Definition at line 513 of file st.c.

int Hash_ForEach ( Hash_t ,
ST_PFSR  ,
char *   
)

void Hash_FreeGen ( Hash_Generator_t  ) 

After generating all items in a generation sequence, this routine must be called to reclaim the resources associated with `gen'.

Definition at line 1020 of file st.c.

void Hash_FreeTable ( Hash_t table  ) 

Free any internal storage associated with table.

It is the user's responsibility to free any storage associated with the pointers he placed in the table (by perhaps using Hash_ForEach).

Definition at line 266 of file st.c.

int Hash_Gen ( Hash_Generator_t gen,
char **  key_p,
char **  value_p 
)

Given a generator returned by Hash_InitGen(), this routine returns the next (key, value) pair in the generation sequence.

The pointer `value_p' can be zero which means no value will be returned. When there are no more items in the generation sequence, the routine returns 0.

While using a generation sequence, deleting any (key, value) pair other than the one just generated may cause a fatal error when Hash_Gen() is called later in the sequence and is therefore not recommended.

Definition at line 937 of file st.c.

int Hash_GenInt ( Hash_Generator_t gen,
char **  key_p,
int *  value_p 
)

Given a generator returned by Hash_InitGen(), this routine returns the next (key, value) pair in the generation sequence.

`value' must be a pointer to an integer. The pointer `value_p' can be zero which means no value will be returned. When there are no more items in the generation sequence, the routine returns 0.

Definition at line 981 of file st.c.

Hash_Generator_t* Hash_InitGen ( Hash_t  ) 

Returns a generator handle which when used with Hash_Gen() will progressively return each (key, value) record in `table'.

Definition at line 907 of file st.c.

Hash_t* Hash_InitTable ( ST_PFI  a,
ST_PFI  b 
)

Hash_t* Hash_InitTableWithParams ( ST_PFI  ,
ST_PFI  ,
int  ,
int  ,
double  ,
int   
)

int Hash_Insert ( Hash_t table,
char *  key,
char *  value 
)

Insert value in table under the key 'key'.

Returns 1 if there was an entry already under the key, 0 otherewise. In either case the new value is added.

Definition at line 400 of file st.c.

int Hash_Lookup ( Hash_t table,
char *  key,
char **  value 
)

Lookup up `key' in `table'.

If an entry is found, 1 is returned and if `value_ptr' is not nil, the variable it points to is set to associated value. If an entry is not found, 0 is return and value_ptr is unchanged.

Definition at line 311 of file st.c.

int Hash_LookupInt ( Hash_t table,
char *  key,
int *  value 
)

Lookup up int `key' in `table'.

If an entry is found, 1 is returned and if `int_ptr' is not nil, the variable it points to is set to associated integer value. If an entry is not found, 0 is return and int_ptr is unchanged.

Definition at line 348 of file st.c.

int Hash_NumCmp ( int  ,
int   
)

Compare function for numbers.

Definition at line 887 of file st.c.

int Hash_NumHash ( char *  ,
int   
)

Hash function for numbers.

Definition at line 870 of file st.c.

int Hash_PtrCmp ( char *  ,
char *   
)

Compare function for pointers.

Definition at line 896 of file st.c.

int Hash_PtrHash ( char *  ,
int   
)

Hash function for pointers.

Definition at line 878 of file st.c.

int Hash_StrHash ( char *  ,
int   
)

Hash function for strings.

Definition at line 854 of file st.c.

void Hash_Test (  ) 

Simple test of the hash package.

Definition at line 1031 of file st.c.

void st_free_gen ( Hash_Generator_t a  ) 

Definition at line 150 of file st.c.

int st_gen ( Hash_Generator_t a,
char **  b,
char **  c 
)

Definition at line 138 of file st.c.

int st_gen_int ( Hash_Generator_t a,
char **  b,
int *  c 
)

Definition at line 142 of file st.c.

Hash_Generator_t* st_init_gen ( Hash_t a  ) 

Definition at line 146 of file st.c.

int st_numcmp ( int  ,
int   
)

Definition at line 130 of file st.c.

int st_numhash ( char *  ,
int   
)

Definition at line 122 of file st.c.

int st_ptrcmp ( char *  ,
char *   
)

Definition at line 134 of file st.c.

int st_ptrhash ( char *  ,
int   
)

Definition at line 126 of file st.c.

int st_strhash ( char *  ,
int   
)

Definition at line 118 of file st.c.


Variable Documentation

enum st_retval(*) ST_PFSR()

Definition at line 75 of file st.h.