hash.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Hash routine.
  2. Copyright (C) 1998 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your
  7. option) any later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #ifndef _ZEBRA_HASH_H
  17. #define _ZEBRA_HASH_H
  18. /* Default hash table size. */
  19. #define HASH_INITIAL_SIZE 256 /* initial number of backets. */
  20. #define HASH_THRESHOLD 10 /* expand when backet. */
  21. struct hash_backet
  22. {
  23. /* Linked list. */
  24. struct hash_backet *next;
  25. /* Hash key. */
  26. unsigned int key;
  27. /* Data. */
  28. void *data;
  29. };
  30. struct hash
  31. {
  32. /* Hash backet. */
  33. struct hash_backet **index;
  34. /* Hash table size. Must be power of 2 */
  35. unsigned int size;
  36. /* If expansion failed. */
  37. int no_expand;
  38. /* Key make function. */
  39. unsigned int (*hash_key) (void *);
  40. /* Data compare function. */
  41. int (*hash_cmp) (const void *, const void *);
  42. /* Backet alloc. */
  43. unsigned long count;
  44. };
  45. extern struct hash *hash_create (unsigned int (*) (void *),
  46. int (*) (const void *, const void *));
  47. extern struct hash *hash_create_size (unsigned int, unsigned int (*) (void *),
  48. int (*) (const void *, const void *));
  49. extern void *hash_get (struct hash *, void *, void * (*) (void *));
  50. extern void *hash_alloc_intern (void *);
  51. extern void *hash_lookup (struct hash *, void *);
  52. extern void *hash_release (struct hash *, void *);
  53. extern void hash_iterate (struct hash *,
  54. void (*) (struct hash_backet *, void *), void *);
  55. extern void hash_clean (struct hash *, void (*) (void *));
  56. extern void hash_free (struct hash *);
  57. extern unsigned int string_hash_make (const char *);
  58. #endif /* _ZEBRA_HASH_H */