[PATCH] libibery/hashtab: add new functions

Martin Liška mliska@suse.cz
Mon Aug 17 14:03:32 GMT 2020


Hey.

I'm working on bintuils where I would like to port a hash table
implementation in gas/hash.[ch] to libiberty one.

But it would be handy for me to add 2 new functions.

Thoughts?
Thanks,
Martin

include/ChangeLog:

	* hashtab.h (htab_insert): New function.
	(htab_print_statistics): Likewise.

libiberty/ChangeLog:

	* hashtab.c (htab_insert): New function.
	(htab_print_statistics): Likewise.
---
  include/hashtab.h   |  6 ++++++
  libiberty/hashtab.c | 23 +++++++++++++++++++++++
  2 files changed, 29 insertions(+)

diff --git a/include/hashtab.h b/include/hashtab.h
index 6cca342b989..bcaee909bcf 100644
--- a/include/hashtab.h
+++ b/include/hashtab.h
@@ -37,6 +37,7 @@ extern "C" {
  #endif /* __cplusplus */
  
  #include "ansidecl.h"
+#include <stdio.h>
  
  /* The type for a hash code.  */
  typedef unsigned int hashval_t;
@@ -172,6 +173,7 @@ extern void **	htab_find_slot (htab_t, const void *, enum insert_option);
  extern void *	htab_find_with_hash (htab_t, const void *, hashval_t);
  extern void **	htab_find_slot_with_hash (htab_t, const void *,
  					  hashval_t, enum insert_option);
+extern void	htab_insert (htab_t, void *);
  extern void	htab_clear_slot	(htab_t, void **);
  extern void	htab_remove_elt	(htab_t, const void *);
  extern void	htab_remove_elt_with_hash (htab_t, const void *, hashval_t);
@@ -183,6 +185,10 @@ extern size_t	htab_size (htab_t);
  extern size_t	htab_elements (htab_t);
  extern double	htab_collisions	(htab_t);
  
+extern void	htab_print_statistics (FILE *f, htab_t table,
+				       const char *name,
+				       const char *prefix);
+
  /* A hash function for pointers.  */
  extern htab_hash htab_hash_pointer;
  
diff --git a/libiberty/hashtab.c b/libiberty/hashtab.c
index 225e9e540a7..fb3152ec9c6 100644
--- a/libiberty/hashtab.c
+++ b/libiberty/hashtab.c
@@ -704,6 +704,15 @@ htab_find_slot (htab_t htab, const PTR element, enum insert_option insert)
  				   insert);
  }
  
+/* Insert ELEMENT into HTAB.  If the element exists, it is overwritten.  */
+
+void
+htab_insert (htab_t htab, PTR element)
+{
+  void **slot = htab_find_slot (htab, element, INSERT);
+  *slot = element;
+}
+
  /* This function deletes an element with the given value from hash
     table (the hash is computed from the element).  If there is no matching
     element in the hash table, this function does nothing.  */
@@ -803,6 +812,20 @@ htab_collisions (htab_t htab)
    return (double) htab->collisions / (double) htab->searches;
  }
  
+/* Print statistics about a hash table.  */
+
+void
+htab_print_statistics (FILE *f, htab_t table, const char *name,
+		       const char *prefix)
+{
+  fprintf (f, "%s hash statistics:\n", name);
+  fprintf (f, "%s%u searches\n", prefix, table->searches);
+  fprintf (f, "%s%lu elements\n", prefix, htab_elements (table));
+  fprintf (f, "%s%lu table size\n", prefix, htab_size (table));
+  fprintf (f, "%s%.2f collisions per search\n",
+	   prefix, htab_collisions (table));
+}
+
  /* Hash P as a null-terminated string.
  
     Copied from gcc/hashtable.c.  Zack had the following to say with respect
-- 
2.28.0



More information about the Gcc-patches mailing list