Author: rooneg Date: Tue Sep 12 07:09:28 2006 New Revision: 442588 URL: http://svn.apache.org/viewvc?view=rev&rev=442588 Log: Add an apr_hash_clear() API for clearing the contents of existing hash tables (enabling their re-use without re-allocation). Submitted by: Daniel L. Rall * include/apr_hash.h (apr_hash_clear): Declare the new function. * tables/apr_hash.c (apr_hash_clear): Implement function. * test/testhash.c (hash_clear): Test the new apr_hash_clear() API. (testhash): Run the hash_clear() test function as part of the suite. * CHANGES: Note change. Modified: apr/apr/trunk/CHANGES apr/apr/trunk/include/apr_hash.h apr/apr/trunk/tables/apr_hash.c apr/apr/trunk/test/testhash.c Modified: apr/apr/trunk/CHANGES URL: http://svn.apache.org/viewvc/apr/apr/trunk/CHANGES?view=diff&rev=442588&r1=442587&r2=442588 ============================================================================== --- apr/apr/trunk/CHANGES (original) +++ apr/apr/trunk/CHANGES Tue Sep 12 07:09:28 2006 @@ -1,4 +1,5 @@ Changes for APR 1.3.0 + *) Add apr_hash_clear. [Daniel L. Rall ] *) Don't try to build apr_app.c on MinGW. [Matthias Miller ] Modified: apr/apr/trunk/include/apr_hash.h URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr_hash.h?view=diff&rev=442588&r1=442587&r2=442588 ============================================================================== --- apr/apr/trunk/include/apr_hash.h (original) +++ apr/apr/trunk/include/apr_hash.h Tue Sep 12 07:09:28 2006 @@ -175,6 +175,12 @@ APR_DECLARE(unsigned int) apr_hash_count(apr_hash_t *ht); /** + * Clear any key/value pairs in the hash table. + * @param ht The hash table + */ +APR_DECLARE(void) apr_hash_clear(apr_hash_t *ht); + +/** * Merge two hash tables into one new hash table. The values of the overlay * hash override the values of the base if both have the same key. Both * hash tables must use the same hash function. Modified: apr/apr/trunk/tables/apr_hash.c URL: http://svn.apache.org/viewvc/apr/apr/trunk/tables/apr_hash.c?view=diff&rev=442588&r1=442587&r2=442588 ============================================================================== --- apr/apr/trunk/tables/apr_hash.c (original) +++ apr/apr/trunk/tables/apr_hash.c Tue Sep 12 07:09:28 2006 @@ -367,6 +367,13 @@ return ht->count; } +APR_DECLARE(void) apr_hash_clear(apr_hash_t *ht) +{ + apr_hash_index_t *hi; + for (hi = apr_hash_first(NULL, ht); hi; hi = apr_hash_next(hi)) + apr_hash_set(ht, hi->this->key, hi->this->klen, NULL); +} + APR_DECLARE(apr_hash_t*) apr_hash_overlay(apr_pool_t *p, const apr_hash_t *overlay, const apr_hash_t *base) Modified: apr/apr/trunk/test/testhash.c URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/testhash.c?view=diff&rev=442588&r1=442587&r2=442588 ============================================================================== --- apr/apr/trunk/test/testhash.c (original) +++ apr/apr/trunk/test/testhash.c Tue Sep 12 07:09:28 2006 @@ -152,6 +152,24 @@ ABTS_STR_EQUAL(tc, "value", result); } +static void hash_clear(abts_case *tc, void *data) +{ + apr_hash_t *h; + int i, *e; + + h = apr_hash_make(p); + ABTS_PTR_NOTNULL(tc, h); + + for (i = 1; i <= 10; i++) { + e = apr_palloc(p, sizeof(int)); + *e = i; + apr_hash_set(h, e, sizeof(*e), e); + } + apr_hash_clear(h); + i = apr_hash_count(h); + ABTS_INT_EQUAL(tc, 0, i); +} + /* This is kind of a hack, but I am just keeping an existing test. This is * really testing apr_hash_first, apr_hash_next, and apr_hash_this which * should be tested in three separate tests, but this will do for now. @@ -419,6 +437,7 @@ abts_run_test(suite, hash_count_1, NULL); abts_run_test(suite, hash_count_5, NULL); + abts_run_test(suite, hash_clear, NULL); abts_run_test(suite, hash_traverse, NULL); abts_run_test(suite, summation_test, NULL);