csomh / source-git / rpm

Forked from source-git/rpm 4 years ago
Clone
2ff057
/* An expandable hash tables datatype.  
2ff057
   Copyright (C) 1999, 2000 Free Software Foundation, Inc.
2ff057
   Contributed by Vladimir Makarov (vmakarov@cygnus.com).
2ff057
2ff057
This program is free software; you can redistribute it and/or modify
2ff057
it under the terms of the GNU General Public License as published by
2ff057
the Free Software Foundation; either version 2 of the License, or
2ff057
(at your option) any later version.
2ff057
2ff057
This program is distributed in the hope that it will be useful,
2ff057
but WITHOUT ANY WARRANTY; without even the implied warranty of
2ff057
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2ff057
GNU General Public License for more details.
2ff057
2ff057
You should have received a copy of the GNU General Public License
2ff057
along with this program; if not, write to the Free Software
2ff057
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2ff057
2ff057
/* This package implements basic hash table functionality.  It is possible
2ff057
   to search for an entry, create an entry and destroy an entry.
2ff057
2ff057
   Elements in the table are generic pointers.
2ff057
2ff057
   The size of the table is not fixed; if the occupancy of the table
2ff057
   grows too high the hash table will be expanded.
2ff057
2ff057
   The abstract data implementation is based on generalized Algorithm D
2ff057
   from Knuth's book "The art of computer programming".  Hash table is
2ff057
   expanded by creation of new hash table and transferring elements from
2ff057
   the old table to the new table.  */
2ff057
2ff057
#ifndef __HASHTAB_H__
2ff057
#define __HASHTAB_H__
2ff057
2ff057
#ifdef __cplusplus
2ff057
extern "C" {
2ff057
#endif /* __cplusplus */
2ff057
2ff057
/* The type for a hash code.  */
2ff057
typedef unsigned int hashval_t;
2ff057
2ff057
/* Callback function pointer types.  */
2ff057
2ff057
/* Calculate hash of a table entry.  */
2ff057
typedef hashval_t (*htab_hash) (const void *);
2ff057
2ff057
/* Compare a table entry with a possible entry.  The entry already in
2ff057
   the table always comes first, so the second element can be of a
2ff057
   different type (but in this case htab_find and htab_find_slot
2ff057
   cannot be used; instead the variants that accept a hash value
2ff057
   must be used).  */
2ff057
typedef int (*htab_eq) (const void *, const void *);
2ff057
2ff057
/* Cleanup function called whenever a live element is removed from
2ff057
   the hash table.  */
2ff057
typedef void (*htab_del) (void *);
2ff057
  
2ff057
/* Function called by htab_traverse for each live element.  The first
2ff057
   arg is the slot of the element (which can be passed to htab_clear_slot
2ff057
   if desired), the second arg is the auxiliary pointer handed to
2ff057
   htab_traverse.  Return 1 to continue scan, 0 to stop.  */
2ff057
typedef int (*htab_trav) (void **, void *);
2ff057
2ff057
/* Hash tables are of the following type.  The structure
2ff057
   (implementation) of this type is not needed for using the hash
2ff057
   tables.  All work with hash table should be executed only through
2ff057
   functions mentioned below. */
2ff057
2ff057
struct htab
2ff057
{
2ff057
  /* Pointer to hash function.  */
2ff057
  htab_hash hash_f;
2ff057
2ff057
  /* Pointer to comparison function.  */
2ff057
  htab_eq eq_f;
2ff057
2ff057
  /* Pointer to cleanup function.  */
2ff057
  htab_del del_f;
2ff057
2ff057
  /* Table itself.  */
2ff057
  void **entries;
2ff057
2ff057
  /* Current size (in entries) of the hash table */
2ff057
  size_t size;
2ff057
2ff057
  /* Current number of elements including also deleted elements */
2ff057
  size_t n_elements;
2ff057
2ff057
  /* Current number of deleted elements in the table */
2ff057
  size_t n_deleted;
2ff057
2ff057
  /* The following member is used for debugging. Its value is number
2ff057
     of all calls of `htab_find_slot' for the hash table. */
2ff057
  unsigned int searches;
2ff057
2ff057
  /* The following member is used for debugging.  Its value is number
2ff057
     of collisions fixed for time of work with the hash table. */
2ff057
  unsigned int collisions;
2ff057
2ff057
  /* This is non-zero if we are allowed to return NULL for function calls
2ff057
     that allocate memory.  */
2ff057
  int return_allocation_failure;
2ff057
};
2ff057
2ff057
typedef struct htab *htab_t;
2ff057
2ff057
/* An enum saying whether we insert into the hash table or not.  */
2ff057
enum insert_option {NO_INSERT, INSERT};
2ff057
2ff057
/* The prototypes of the package functions. */
2ff057
2ff057
/* This function is like htab_create, but may return NULL if memory
2ff057
   allocation fails, and also signals that htab_find_slot_with_hash and
2ff057
   htab_find_slot are allowed to return NULL when inserting.  */
2ff057
extern htab_t	htab_try_create	(size_t, htab_hash, htab_eq, htab_del);
2ff057
extern void	htab_delete	(htab_t);
2ff057
extern void	htab_empty	(htab_t);
2ff057
2ff057
extern void    *htab_find	(htab_t, const void *);
2ff057
extern void    **htab_find_slot	(htab_t, const void *, enum insert_option);
2ff057
extern void    *htab_find_with_hash (htab_t, const void *, hashval_t);
2ff057
extern void    **htab_find_slot_with_hash (htab_t, const void *, hashval_t,
2ff057
					  enum insert_option);
2ff057
extern void	htab_clear_slot	(htab_t, void **);
2ff057
extern void	htab_remove_elt	(htab_t, void *);
2ff057
2ff057
extern void	htab_traverse	(htab_t, htab_trav, void *);
2ff057
2ff057
extern size_t	htab_size	(htab_t);
2ff057
extern size_t	htab_elements	(htab_t);
2ff057
extern double	htab_collisions	(htab_t);
2ff057
2ff057
/* A hash function for pointers.  */
2ff057
extern htab_hash htab_hash_pointer;
2ff057
2ff057
/* An equality function for pointers.  */
2ff057
extern htab_eq htab_eq_pointer;
2ff057
2ff057
#ifdef __cplusplus
2ff057
}
2ff057
#endif /* __cplusplus */
2ff057
2ff057
#endif /* __HASHTAB_H */