Back to index

php5  5.3.10
Classes | Defines | Typedefs | Functions | Variables
st.c File Reference
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "st.h"

Go to the source code of this file.

Classes

struct  st_table_entry

Defines

#define ST_DEFAULT_MAX_DENSITY   5
#define ST_DEFAULT_INIT_TABLE_SIZE   11
#define alloc(type)   (type*)xmalloc((unsigned)sizeof(type))
#define Calloc(n, s)   (char*)xcalloc((n),(s))
#define EQUAL(table, x, y)   ((x)==(y) || (*table->type->compare)((x),(y)) == 0)
#define do_hash(key, table)   (unsigned int)(*(table)->type->hash)((key))
#define do_hash_bin(key, table)   (do_hash(key, table)%(table)->num_bins)
#define MINSIZE   8
#define PTR_NOT_EQUAL(table, ptr, hash_val, key)   ((ptr) != 0 && (ptr->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key)))
#define COLLISION
#define FIND_ENTRY(table, ptr, hash_val, bin_pos)
#define ADD_DIRECT(table, key, value, hash_val, bin_pos)

Typedefs

typedef struct st_table_entry

Functions

static int numcmp (long, long)
static int numhash (long)
static int strhash (const char *)
static void rehash (st_table *)
static int new_size (int size)
st_tablest_init_table_with_size (struct st_hash_type *type, int size)
st_tablest_init_table (struct st_hash_type *type)
st_tablest_init_numtable (void)
st_tablest_init_numtable_with_size (int size)
st_tablest_init_strtable (void)
st_tablest_init_strtable_with_size (int size)
void st_free_table (st_table *table)
int st_lookup (st_table *table, st_data_t key, st_data_t *value)
int st_insert (st_table *table, st_data_t key, st_data_t value)
void st_add_direct (st_table *table, st_data_t key, st_data_t value)
st_tablest_copy (st_table *old_table)
int st_delete (st_table *table, st_data_t *key, st_data_t *value)
int st_delete_safe (st_table *table, st_data_t *key, st_data_t *value, st_data_t never)
static int delete_never (st_data_t key, st_data_t value, st_data_t never)
void st_cleanup_safe (st_table *table, st_data_t never)
int st_foreach (st_table *table, int(*func)(), st_data_t arg)

Variables

static struct st_hash_type
static const long primes []

Class Documentation

struct st_table_entry

Definition at line 34 of file st.c.

Collaboration diagram for st_table_entry:
Class Members
unsigned int hash
st_data_t key
st_table_entry * next
st_data_t record

Define Documentation

#define ADD_DIRECT (   table,
  key,
  value,
  hash_val,
  bin_pos 
)
Value:
do {\
    st_table_entry *entry;\
    if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY) {\
       rehash(table);\
        bin_pos = hash_val % table->num_bins;\
    }\
    \
    entry = alloc(st_table_entry);\
    \
    entry->hash = hash_val;\
    entry->key = key;\
    entry->record = value;\
    entry->next = table->bins[bin_pos];\
    table->bins[bin_pos] = entry;\
    table->num_entries++;\
} while (0)

Definition at line 276 of file st.c.

#define alloc (   type)    (type*)xmalloc((unsigned)sizeof(type))

Definition at line 70 of file st.c.

#define Calloc (   n,
 
)    (char*)xcalloc((n),(s))

Definition at line 71 of file st.c.

#define COLLISION

Definition at line 240 of file st.c.

#define do_hash (   key,
  table 
)    (unsigned int)(*(table)->type->hash)((key))

Definition at line 75 of file st.c.

#define do_hash_bin (   key,
  table 
)    (do_hash(key, table)%(table)->num_bins)

Definition at line 76 of file st.c.

#define EQUAL (   table,
  x,
 
)    ((x)==(y) || (*table->type->compare)((x),(y)) == 0)

Definition at line 73 of file st.c.

#define FIND_ENTRY (   table,
  ptr,
  hash_val,
  bin_pos 
)
Value:
do {\
    bin_pos = hash_val%(table)->num_bins;\
    ptr = (table)->bins[bin_pos];\
    if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {\
       COLLISION;\
       while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {\
	    ptr = ptr->next;\
       }\
	ptr = ptr->next;\
    }\
} while (0)

Definition at line 243 of file st.c.

#define MINSIZE   8

Definition at line 82 of file st.c.

#define PTR_NOT_EQUAL (   table,
  ptr,
  hash_val,
  key 
)    ((ptr) != 0 && (ptr->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key)))

Definition at line 234 of file st.c.

#define ST_DEFAULT_INIT_TABLE_SIZE   11

Definition at line 42 of file st.c.

#define ST_DEFAULT_MAX_DENSITY   5

Definition at line 41 of file st.c.


Typedef Documentation

typedef struct st_table_entry

Definition at line 32 of file st.c.


Function Documentation

static int delete_never ( st_data_t  key,
st_data_t  value,
st_data_t  never 
) [static]

Definition at line 470 of file st.c.

{
    if (value == never) return ST_DELETE;
    return ST_CONTINUE;
}

Here is the caller graph for this function:

static int new_size ( int  size) [static]

Definition at line 120 of file st.c.

{
    int i;

#if 0
    for (i=3; i<31; i++) {
       if ((1<<i) > size) return 1<<i;
    }
    return -1;
#else
    int newsize;

    for (i = 0, newsize = MINSIZE;
        i < (int )(sizeof(primes)/sizeof(primes[0]));
        i++, newsize <<= 1)
    {
       if (newsize > size) return primes[i];
    }
    /* Ran out of polynomials */
    return -1;                     /* should raise exception */
#endif
}

Here is the caller graph for this function:

static int numcmp ( long  x,
long  y 
) [static]

Definition at line 578 of file st.c.

{
    return x != y;
}
static int numhash ( long  n) [static]

Definition at line 585 of file st.c.

{
    return n;
}
static void rehash ( st_table table) [static]

Definition at line 330 of file st.c.

{
    register st_table_entry *ptr, *next, **new_bins;
    int i, old_num_bins = table->num_bins, new_num_bins;
    unsigned int hash_val;

    new_num_bins = new_size(old_num_bins+1);
    new_bins = (st_table_entry**)Calloc(new_num_bins, sizeof(st_table_entry*));

    for(i = 0; i < old_num_bins; i++) {
       ptr = table->bins[i];
       while (ptr != 0) {
           next = ptr->next;
           hash_val = ptr->hash % new_num_bins;
           ptr->next = new_bins[hash_val];
           new_bins[hash_val] = ptr;
           ptr = next;
       }
    }
    free(table->bins);
    table->num_bins = new_num_bins;
    table->bins = new_bins;
}

Here is the call graph for this function:

void st_add_direct ( st_table table,
st_data_t  key,
st_data_t  value 
)

Definition at line 317 of file st.c.

{
    unsigned int hash_val, bin_pos;

    hash_val = do_hash(key, table);
    bin_pos = hash_val % table->num_bins;
    ADD_DIRECT(table, key, value, hash_val, bin_pos);
}
void st_cleanup_safe ( st_table table,
st_data_t  never 
)

Definition at line 478 of file st.c.

{
    int num_entries = table->num_entries;

    st_foreach(table, delete_never, never);
    table->num_entries = num_entries;
}

Here is the call graph for this function:

st_table* st_copy ( st_table old_table)

Definition at line 356 of file st.c.

{
    st_table *new_table;
    st_table_entry *ptr, *entry;
    int i, num_bins = old_table->num_bins;

    new_table = alloc(st_table);
    if (new_table == 0) {
       return 0;
    }

    *new_table = *old_table;
    new_table->bins = (st_table_entry**)
       Calloc((unsigned)num_bins, sizeof(st_table_entry*));

    if (new_table->bins == 0) {
       free(new_table);
       return 0;
    }

    for(i = 0; i < num_bins; i++) {
       new_table->bins[i] = 0;
       ptr = old_table->bins[i];
       while (ptr != 0) {
           entry = alloc(st_table_entry);
           if (entry == 0) {
              free(new_table->bins);
              free(new_table);
              return 0;
           }
           *entry = *ptr;
           entry->next = new_table->bins[i];
           new_table->bins[i] = entry;
           ptr = ptr->next;
       }
    }
    return new_table;
}
int st_delete ( st_table table,
st_data_t key,
st_data_t value 
)

Definition at line 397 of file st.c.

{
    unsigned int hash_val;
    st_table_entry *tmp;
    register st_table_entry *ptr;

    hash_val = do_hash_bin(*key, table);
    ptr = table->bins[hash_val];

    if (ptr == 0) {
       if (value != 0) *value = 0;
       return 0;
    }

    if (EQUAL(table, *key, ptr->key)) {
       table->bins[hash_val] = ptr->next;
       table->num_entries--;
       if (value != 0) *value = ptr->record;
       *key = ptr->key;
       free(ptr);
       return 1;
    }

    for(; ptr->next != 0; ptr = ptr->next) {
       if (EQUAL(table, ptr->next->key, *key)) {
           tmp = ptr->next;
           ptr->next = ptr->next->next;
           table->num_entries--;
           if (value != 0) *value = tmp->record;
           *key = tmp->key;
           free(tmp);
           return 1;
       }
    }

    return 0;
}
int st_delete_safe ( st_table table,
st_data_t key,
st_data_t value,
st_data_t  never 
)

Definition at line 439 of file st.c.

{
    unsigned int hash_val;
    register st_table_entry *ptr;

    hash_val = do_hash_bin(*key, table);
    ptr = table->bins[hash_val];

    if (ptr == 0) {
       if (value != 0) *value = 0;
       return 0;
    }

    for(; ptr != 0; ptr = ptr->next) {
       if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
           table->num_entries--;
           *key = ptr->key;
           if (value != 0) *value = ptr->record;
           ptr->key = ptr->record = never;
           return 1;
       }
    }

    return 0;
}
int st_foreach ( st_table table,
int (*)()  func,
st_data_t  arg 
)

Definition at line 489 of file st.c.

{
    st_table_entry *ptr, *last, *tmp;
    enum st_retval retval;
    int i;

    for(i = 0; i < table->num_bins; i++) {
       last = 0;
       for(ptr = table->bins[i]; ptr != 0;) {
           retval = (*func)(ptr->key, ptr->record, arg);
           switch (retval) {
           case ST_CHECK:   /* check if hash is modified during iteration */
               tmp = 0;
              if (i < table->num_bins) {
                  for (tmp = table->bins[i]; tmp; tmp=tmp->next) {
                     if (tmp == ptr) break;
                  }
              }
              if (!tmp) {
                  /* call func with error notice */
                  return 1;
              }
              /* fall through */
           case ST_CONTINUE:
              last = ptr;
              ptr = ptr->next;
              break;
           case ST_STOP:
               return 0;
           case ST_DELETE:
              tmp = ptr;
              if (last == 0) {
                  table->bins[i] = ptr->next;
              }
              else {
                  last->next = ptr->next;
              }
              ptr = ptr->next;
              free(tmp);
              table->num_entries--;
           }
       }
    }
    return 0;
}

Here is the caller graph for this function:

void st_free_table ( st_table table)

Definition at line 216 of file st.c.

{
    register st_table_entry *ptr, *next;
    int i;

    for(i = 0; i < table->num_bins; i++) {
       ptr = table->bins[i];
       while (ptr != 0) {
           next = ptr->next;
           free(ptr);
           ptr = next;
       }
    }
    free(table->bins);
    free(table);
}

Definition at line 190 of file st.c.

{
    return st_init_table(&type_numhash);
}

Here is the call graph for this function:

Definition at line 196 of file st.c.

{
    return st_init_table_with_size(&type_numhash, size);
}

Here is the call graph for this function:

Definition at line 203 of file st.c.

{
    return st_init_table(&type_strhash);
}

Here is the call graph for this function:

Definition at line 209 of file st.c.

{
    return st_init_table_with_size(&type_strhash, size);
}

Here is the call graph for this function:

st_table* st_init_table ( struct st_hash_type type)

Definition at line 183 of file st.c.

{
    return st_init_table_with_size(type, 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

st_table* st_init_table_with_size ( struct st_hash_type type,
int  size 
)

Definition at line 158 of file st.c.

{
    st_table *tbl;

#ifdef HASH_LOG
    if (init_st == 0) {
       init_st = 1;
       atexit(stat_col);
    }
#endif

    size = new_size(size);  /* round up to prime number */

    tbl = alloc(st_table);
    tbl->type = type;
    tbl->num_entries = 0;
    tbl->num_bins = size;
    tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*));

    return tbl;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int st_insert ( st_table table,
st_data_t  key,
st_data_t  value 
)

Definition at line 295 of file st.c.

{
    unsigned int hash_val, bin_pos;
    register st_table_entry *ptr;

    hash_val = do_hash(key, table);
    FIND_ENTRY(table, ptr, hash_val, bin_pos);

    if (ptr == 0) {
       ADD_DIRECT(table, key, value, hash_val, bin_pos);
       return 0;
    }
    else {
       ptr->record = value;
       return 1;
    }
}
int st_lookup ( st_table table,
st_data_t  key,
st_data_t value 
)

Definition at line 256 of file st.c.

{
    unsigned int hash_val, bin_pos;
    register st_table_entry *ptr;

    hash_val = do_hash(key, table);
    FIND_ENTRY(table, ptr, hash_val, bin_pos);

    if (ptr == 0) {
       return 0;
    }
    else {
       if (value != 0)  *value = ptr->record;
       return 1;
    }
}
static int strhash ( const char *  string) [static]

Definition at line 539 of file st.c.

{
    register int c;

#ifdef HASH_ELFHASH
    register unsigned int h = 0, g;

    while ((c = *string++) != '\0') {
       h = ( h << 4 ) + c;
       if ( g = h & 0xF0000000 )
           h ^= g >> 24;
       h &= ~g;
    }
    return h;
#elif HASH_PERL
    register int val = 0;

    while ((c = *string++) != '\0') {
       val += c;
       val += (val << 10);
       val ^= (val >> 6);
    }
    val += (val << 3);
    val ^= (val >> 11);

    return val + (val << 15);
#else
    register int val = 0;

    while ((c = *string++) != '\0') {
       val = val*997 + c;
    }

    return val + (val>>5);
#endif
}

Here is the call graph for this function:


Variable Documentation

const long primes[] [static]

Definition at line 87 of file st.c.

static struct st_hash_type [static]
Initial value:
 {
    numcmp,
    numhash,
}

Definition at line 56 of file st.c.