hash.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* hash.c -- gas hash table code
  2. Copyright (C) 1987-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "as.h"
  17. /* Insert ELEMENT into HTAB. If REPLACE is non-zero existing elements
  18. are overwritten. If ELEMENT already exists, a pointer to the slot
  19. is returned. Otherwise NULL is returned. */
  20. void **
  21. htab_insert (htab_t htab, void *element, int replace)
  22. {
  23. void **slot = htab_find_slot (htab, element, INSERT);
  24. if (*slot != NULL)
  25. {
  26. if (replace)
  27. {
  28. if (htab->del_f)
  29. (*htab->del_f) (*slot);
  30. *slot = element;
  31. }
  32. return slot;
  33. }
  34. *slot = element;
  35. return NULL;
  36. }
  37. /* Print statistics about a hash table. */
  38. void
  39. htab_print_statistics (FILE *f, const char *name, htab_t table)
  40. {
  41. fprintf (f, "%s hash statistics:\n", name);
  42. fprintf (f, "\t%u searches\n", table->searches);
  43. fprintf (f, "\t%u collisions\n", table->collisions);
  44. fprintf (f, "\t%lu elements\n", (unsigned long) htab_elements (table));
  45. fprintf (f, "\t%lu table size\n", (unsigned long) htab_size (table));
  46. }