Commit 5dc9cffd by Zack Weinberg Committed by Zack Weinberg

hashtab.h (struct htab): Add del_f.

	* hashtab.h (struct htab): Add del_f.
	(htab_del): New type.
	(htab_create): Add fourth argument.

	* hashtab.c (htab_create): Set del_f.
	(htab_delete, htab_empty, htab_remove_elt, htab_clear_slot):
	Use it.

From-SVN: r32459
parent aa77ac5f
2000-03-09 Zack Weinberg <zack@wolery.cumb.org>
* hashtab.h (struct htab): Add del_f.
(htab_del): New type.
(htab_create): Add fourth argument.
2000-03-08 Zack Weinberg <zack@wolery.cumb.org> 2000-03-08 Zack Weinberg <zack@wolery.cumb.org>
* hashtab.h (hash_table_t): Rename to htab_t. * hashtab.h (hash_table_t): Rename to htab_t.
......
...@@ -47,6 +47,10 @@ typedef unsigned int (*htab_hash) PARAMS ((const void *)); ...@@ -47,6 +47,10 @@ typedef unsigned int (*htab_hash) PARAMS ((const void *));
the table always comes first. */ the table always comes first. */
typedef int (*htab_eq) PARAMS ((const void *, const void *)); typedef int (*htab_eq) PARAMS ((const void *, const void *));
/* Cleanup function called whenever a live element is removed from
the hash table. */
typedef void (*htab_del) PARAMS ((void *));
/* Function called by htab_traverse for each live element. The first /* Function called by htab_traverse for each live element. The first
arg is the element, the second arg is the auxiliary pointer handed arg is the element, the second arg is the auxiliary pointer handed
to htab_traverse. Return 1 to continue scan, 0 to stop. */ to htab_traverse. Return 1 to continue scan, 0 to stop. */
...@@ -62,10 +66,13 @@ struct htab ...@@ -62,10 +66,13 @@ struct htab
/* Pointer to hash function. */ /* Pointer to hash function. */
htab_hash hash_f; htab_hash hash_f;
/* Pointer to comparison function. */ /* Pointer to comparison function. */
htab_eq eq_f; htab_eq eq_f;
/* Table itself */ /* Pointer to cleanup function. */
htab_del del_f;
/* Table itself. */
void **entries; void **entries;
/* Current size (in entries) of the hash table */ /* Current size (in entries) of the hash table */
...@@ -90,7 +97,8 @@ typedef struct htab *htab_t; ...@@ -90,7 +97,8 @@ typedef struct htab *htab_t;
/* The prototypes of the package functions. */ /* The prototypes of the package functions. */
extern htab_t htab_create PARAMS ((size_t, htab_hash, htab_eq)); extern htab_t htab_create PARAMS ((size_t, htab_hash,
htab_eq, htab_del));
extern void htab_delete PARAMS ((htab_t)); extern void htab_delete PARAMS ((htab_t));
extern void htab_empty PARAMS ((htab_t)); extern void htab_empty PARAMS ((htab_t));
......
2000-03-09 Zack Weinberg <zack@wolery.cumb.org>
* hashtab.c (htab_create): Set del_f.
(htab_delete, htab_empty, htab_remove_elt, htab_clear_slot):
Use it.
2000-03-08 Zack Weinberg <zack@wolery.cumb.org> 2000-03-08 Zack Weinberg <zack@wolery.cumb.org>
* hashtab.c: Remove debugging variables (all_searches, * hashtab.c: Remove debugging variables (all_searches,
......
...@@ -88,10 +88,11 @@ higher_prime_number (n) ...@@ -88,10 +88,11 @@ higher_prime_number (n)
created hash table. */ created hash table. */
htab_t htab_t
htab_create (size, hash_f, eq_f) htab_create (size, hash_f, eq_f, del_f)
size_t size; size_t size;
htab_hash hash_f; htab_hash hash_f;
htab_eq eq_f; htab_eq eq_f;
htab_del del_f;
{ {
htab_t result; htab_t result;
...@@ -101,6 +102,7 @@ htab_create (size, hash_f, eq_f) ...@@ -101,6 +102,7 @@ htab_create (size, hash_f, eq_f)
result->size = size; result->size = size;
result->hash_f = hash_f; result->hash_f = hash_f;
result->eq_f = eq_f; result->eq_f = eq_f;
result->del_f = del_f;
return result; return result;
} }
...@@ -111,6 +113,15 @@ void ...@@ -111,6 +113,15 @@ void
htab_delete (htab) htab_delete (htab)
htab_t htab; htab_t htab;
{ {
int i;
if (htab->del_f)
for (i = htab->size - 1; i >= 0; i--)
{
if (htab->entries[i] != EMPTY_ENTRY
&& htab->entries[i] != DELETED_ENTRY)
(*htab->del_f) (htab->entries[i]);
}
free (htab->entries); free (htab->entries);
free (htab); free (htab);
} }
...@@ -121,6 +132,15 @@ void ...@@ -121,6 +132,15 @@ void
htab_empty (htab) htab_empty (htab)
htab_t htab; htab_t htab;
{ {
int i;
if (htab->del_f)
for (i = htab->size - 1; i >= 0; i--)
{
if (htab->entries[i] != EMPTY_ENTRY
&& htab->entries[i] != DELETED_ENTRY)
(*htab->del_f) (htab->entries[i]);
}
memset (htab->entries, 0, htab->size * sizeof (void *)); memset (htab->entries, 0, htab->size * sizeof (void *));
} }
...@@ -273,6 +293,9 @@ htab_remove_elt (htab, element) ...@@ -273,6 +293,9 @@ htab_remove_elt (htab, element)
if (*slot == EMPTY_ENTRY) if (*slot == EMPTY_ENTRY)
return; return;
if (htab->del_f)
(*htab->del_f) (*slot);
*slot = DELETED_ENTRY; *slot = DELETED_ENTRY;
htab->n_deleted++; htab->n_deleted++;
} }
...@@ -289,6 +312,8 @@ htab_clear_slot (htab, slot) ...@@ -289,6 +312,8 @@ htab_clear_slot (htab, slot)
if (slot < htab->entries || slot >= htab->entries + htab->size if (slot < htab->entries || slot >= htab->entries + htab->size
|| *slot == EMPTY_ENTRY || *slot == DELETED_ENTRY) || *slot == EMPTY_ENTRY || *slot == DELETED_ENTRY)
abort (); abort ();
if (htab->del_f)
(*htab->del_f) (*slot);
*slot = DELETED_ENTRY; *slot = DELETED_ENTRY;
htab->n_deleted++; htab->n_deleted++;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment