From: Ben Pfaff Date: Sun, 4 Apr 2010 19:51:08 +0000 (-0700) Subject: hmapx: New function hmapx_clear(). X-Git-Tag: sav-api~317 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pspp;a=commitdiff_plain;h=07f535c5ce76a82df05dafe831e44fb15855ae27 hmapx: New function hmapx_clear(). --- diff --git a/src/libpspp/hmapx.c b/src/libpspp/hmapx.c index 437b2bdba5..25dabf6eda 100644 --- a/src/libpspp/hmapx.c +++ b/src/libpspp/hmapx.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 @@ -18,9 +18,9 @@ #include #endif -#include +#include "libpspp/hmapx.h" #include -#include "xalloc.h" +#include "gl/xalloc.h" /* Frees the memory, if any, allocated by hash map MAP, including all hmapx_nodes that it contains. The user-defined data items @@ -45,6 +45,18 @@ hmapx_destroy (struct hmapx *map) } } +/* Removes all hmapx_nodes from MAP and frees them. The user-defined data + items that the hmapx_nodes point to are not affected. */ +void +hmapx_clear (struct hmapx *map) +{ + struct hmapx_node *node, *next; + void *data; + + HMAPX_FOR_EACH_SAFE (data, node, next, map) + hmapx_delete (map, node); +} + /* Allocates and returns a new hmapx_node with DATA as its data item. */ static struct hmapx_node * diff --git a/src/libpspp/hmapx.h b/src/libpspp/hmapx.h index d463504a52..2fe7f9f770 100644 --- a/src/libpspp/hmapx.h +++ b/src/libpspp/hmapx.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 @@ -143,6 +143,7 @@ struct hmapx /* Creation and destruction. */ static inline void hmapx_init (struct hmapx *); static inline void hmapx_swap (struct hmapx *, struct hmapx *); +void hmapx_clear (struct hmapx *); void hmapx_destroy (struct hmapx *); /* Storage management. */ diff --git a/tests/libpspp/hmapx-test.c b/tests/libpspp/hmapx-test.c index 5ad5d5723f..18b7aad9d6 100644 --- a/tests/libpspp/hmapx-test.c +++ b/tests/libpspp/hmapx-test.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. + Copyright (C) 2007, 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 @@ -935,6 +935,44 @@ test_swap_random_hash (void) test_swap (128, random_hash); } +/* Inserts elements into an HMAPX in ascending order, then clears the hash + table using hmapx_clear(). */ +static void +test_clear (void) +{ + const int max_elems = 128; + struct element *elements; + struct hmapx_node **nodes; + int *values; + struct hmapx hmapx; + int cnt; + + elements = xnmalloc (max_elems, sizeof *elements); + nodes = xnmalloc (max_elems, sizeof *nodes); + values = xnmalloc (max_elems, sizeof *values); + + hmapx_init (&hmapx); + for (cnt = 0; cnt <= max_elems; cnt++) + { + int i; + + for (i = 0; i < cnt; i++) + { + values[i] = elements[i].data = i; + nodes[i] = hmapx_insert (&hmapx, &elements[i], + random_hash (elements[i].data)); + check_hmapx (&hmapx, values, i + 1, random_hash); + } + hmapx_clear (&hmapx); + check_hmapx (&hmapx, NULL, 0, random_hash); + } + hmapx_destroy (&hmapx); + + free (elements); + free (nodes); + free (values); +} + static void test_destroy_null (void) { @@ -1026,6 +1064,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");