hash_table.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* hash.h - hash tables for opkg
  2. Steven M. Ayer, Jamey Hicks
  3. Copyright (C) 2002 Compaq Computer Corporation
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program 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. */
  13. #ifndef _HASH_TABLE_H_
  14. #define _HASH_TABLE_H_
  15. typedef struct hash_entry hash_entry_t;
  16. typedef struct hash_table hash_table_t;
  17. struct hash_entry {
  18. char *key;
  19. void *data;
  20. struct hash_entry *next;
  21. };
  22. struct hash_table {
  23. const char *name;
  24. hash_entry_t *entries;
  25. unsigned int n_buckets;
  26. unsigned int n_elements;
  27. /* useful stats */
  28. unsigned int n_used_buckets;
  29. unsigned int n_collisions;
  30. unsigned int max_bucket_len;
  31. unsigned int n_hits, n_misses;
  32. };
  33. void hash_table_init(const char *name, hash_table_t * hash, int len);
  34. void hash_table_deinit(hash_table_t * hash);
  35. void hash_print_stats(hash_table_t * hash);
  36. void *hash_table_get(hash_table_t * hash, const char *key);
  37. int hash_table_insert(hash_table_t * hash, const char *key, void *value);
  38. int hash_table_remove(hash_table_t * has, const char *key);
  39. void hash_table_foreach(hash_table_t * hash,
  40. void (*f) (const char *key, void *entry, void *data),
  41. void *data);
  42. #endif /* _HASH_TABLE_H_ */