Back to index

tetex-bin  3.0
Defines | Functions
hash.c File Reference
#include <kpathsea/config.h>
#include <kpathsea/c-ctype.h>
#include <kpathsea/hash.h>
#include <kpathsea/str-list.h>

Go to the source code of this file.

Defines

#define TRANSFORM(x)   (x)

Functions

static unsigned hash P2C (hash_table_type, table, const_string, key)
hash_table_type hash_create P1C (unsigned, size)
void hash_insert P3C (hash_table_type *, table, const_string, key, const_string, value)
void hash_print P2C (hash_table_type, table, boolean, summary_only)

Define Documentation

#define TRANSFORM (   x)    (x)

Definition at line 37 of file hash.c.


Function Documentation

hash_table_type hash_create P1C ( unsigned  ,
size   
)

Definition at line 68 of file hash.c.

{
  /* hash_table_type ret; changed into "static ..." to work around gcc
     optimizer bug for Alpha.  */
  static hash_table_type ret;
  unsigned b;
  ret.buckets = XTALLOC (size, hash_element_type *);
  ret.size = size;
  
  /* calloc's zeroes aren't necessarily NULL, so be safe.  */
  for (b = 0; b <ret.size; b++)
    ret.buckets[b] = NULL;
    
  return ret;
}
string *hash_lookup P2C ( hash_table_type  ,
table  ,
const_string  ,
key   
) [static]

Definition at line 41 of file hash.c.

{
  unsigned n = 0;
  
  /* Our keys aren't often anagrams of each other, so no point in
     weighting the characters.  */
  while (*key != 0)
    n = (n + n + TRANSFORM (*key++)) % table.size;
  
  return n;
}
void hash_print P2C ( hash_table_type  ,
table  ,
boolean  ,
summary_only   
)

Definition at line 215 of file hash.c.

{
  unsigned b;
  unsigned total_elements = 0, total_buckets = 0;
  
  for (b = 0; b < table.size; b++) {
    hash_element_type *bucket = table.buckets[b];

    if (bucket) {
      unsigned len = 1;
      hash_element_type *tb;

      total_buckets++;
      if (!summary_only) fprintf (stderr, "%4d ", b);

      for (tb = bucket->next; tb != NULL; tb = tb->next)
        len++;
      if (!summary_only) fprintf (stderr, ":%-5d", len);
      total_elements += len;

      if (!summary_only) {
        for (tb = bucket; tb != NULL; tb = tb->next)
          fprintf (stderr, " %s=>%s", tb->key, tb->value);
        putc ('\n', stderr);
      }
    }
  }
  
  fprintf (stderr,
          "%u buckets, %u nonempty (%u%%); %u entries, average chain %.1f.\n",
          table.size,
          total_buckets,
          100 * total_buckets / table.size,
          total_elements,
          total_buckets ? total_elements / (double) total_buckets : 0.0);
}

Here is the call graph for this function:

void hash_remove P3C ( hash_table_type ,
table  ,
const_string  ,
key  ,
const_string  ,
value   
)

Definition at line 88 of file hash.c.

{
  unsigned n = hash (*table, key);
  hash_element_type *new_elt = XTALLOC1 (hash_element_type);

  new_elt->key = key;
  new_elt->value = value;
  new_elt->next = NULL;
  
  /* Insert the new element at the end of the list.  */
  if (!table->buckets[n])
    /* first element in bucket is a special case.  */
    table->buckets[n] = new_elt;
  else
    {
      hash_element_type *loc = table->buckets[n];
      while (loc->next)            /* Find the last element.  */
        loc = loc->next;
      loc->next = new_elt;  /* Insert the new one after.  */
    }
}