From 91c50c1bf38fa2377a685343608b88915aa0168a Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 11 Apr 2010 13:04:37 -0700 Subject: [PATCH] hmap: New function hmap_clear(). --- src/libpspp/hmap.c | 14 ++++++++++++- src/libpspp/hmap.h | 3 ++- tests/libpspp/hmap-test.c | 41 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/libpspp/hmap.c b/src/libpspp/hmap.c index 4c97e2359c..9050cc08f5 100644 --- a/src/libpspp/hmap.c +++ b/src/libpspp/hmap.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2008, 2009 Free Software Foundation, Inc. + Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -49,6 +49,18 @@ hmap_swap (struct hmap *a, struct hmap *b) b->buckets = &b->one; } +/* Removes all of the elements from MAP, without destroying MAP itself and + without accessing the existing elements (if any). */ +void +hmap_clear (struct hmap *map) +{ + size_t i; + + for (i = 0; i <= map->mask; i++) + map->buckets[i] = NULL; + map->count = 0; +} + /* Frees the memory, if any, allocated by hash map MAP. This has no effect on the actual data items in MAP, if any, because the client is responsible for allocating and freeing them. It diff --git a/src/libpspp/hmap.h b/src/libpspp/hmap.h index 1592f803be..095edf1046 100644 --- a/src/libpspp/hmap.h +++ b/src/libpspp/hmap.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2008, 2009 Free Software Foundation, Inc. + Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -158,6 +158,7 @@ struct hmap /* Creation and destruction. */ void hmap_init (struct hmap *); void hmap_swap (struct hmap *, struct hmap *); +void hmap_clear (struct hmap *); void hmap_destroy (struct hmap *); /* Storage management. */ diff --git a/tests/libpspp/hmap-test.c b/tests/libpspp/hmap-test.c index f9d963d83b..bbe341388a 100644 --- a/tests/libpspp/hmap-test.c +++ b/tests/libpspp/hmap-test.c @@ -915,6 +915,45 @@ test_swap_random_hash (void) test_swap (128, random_hash); } +/* Inserts elements into an hmap in ascending order, then clears the hash table + using hmap_clear(). */ +static void +test_clear (void) +{ + const int max_elems = 128; + struct element *elements; + int *values; + struct hmap hmap; + int cnt; + +#if __GNUC__ == 4 && __GNUC_MINOR__ == 3 + return; +#endif /* GCC 4.3 */ + + elements = xnmalloc (max_elems, sizeof *elements); + values = xnmalloc (max_elems, sizeof *values); + + for (cnt = 0; cnt <= max_elems; cnt++) + { + int i; + + hmap_init (&hmap); + for (i = 0; i < cnt; i++) + { + values[i] = elements[i].data = i; + hmap_insert (&hmap, &elements[i].node, + random_hash (elements[i].data)); + check_hmap (&hmap, values, i + 1, random_hash); + } + hmap_clear (&hmap); + check_hmap (&hmap, NULL, 0, random_hash); + hmap_destroy (&hmap); + } + + free (elements); + free (values); +} + static void test_destroy_null (void) { @@ -999,6 +1038,8 @@ main (void) run_test (test_swap_random_hash, "test swapping tables"); + run_test (test_clear, "test clearing hash table"); + run_test (test_destroy_null, "test destroying null table"); run_test (test_shrink_empty, "test shrinking an empty table"); -- 2.30.2