Mercurial > hg > octave-lojdl > gnulib-hg
changeset 8436:36bbb949160c
Add an element disposal function.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 15 Mar 2007 23:56:13 +0000 |
parents | 9ffcb6d5f355 |
children | 166e8d07e046 |
files | ChangeLog NEWS lib/gl_anytree_oset.h lib/gl_anytreehash_list1.h lib/gl_array_oset.c lib/gl_avltree_oset.c lib/gl_oset.c lib/gl_oset.h lib/gl_rbtree_oset.c tests/test-array_oset.c tests/test-avltree_oset.c tests/test-rbtree_oset.c |
diffstat | 12 files changed, 87 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,25 @@ +2007-03-15 Bruno Haible <bruno@clisp.org> + + * lib/gl_oset.h (gl_setelement_dispose_fn): New type. + (gl_oset_create_empty): Add dispose_fn argument. + (struct gl_oset_implementation): Add dispose_fn argument to + 'create_empty' method. + (struct gl_oset_impl_base): Add dispose_fn field. + * lib/gl_oset.c (gl_oset_create_empty): Add dispose_fn argument. + * lib/gl_array_oset.c (gl_array_create_empty): Add dispose_fn argument. + (gl_array_remove_at, gl_array_free): Call dispose_fn on the dropped + values. + * lib/gl_anytree_oset.h (gl_tree_create_empty): Add dispose_fn argument. + (gl_tree_oset_free): Call dispose_fn on the dropped values. + * lib/gl_avltree_oset.c (gl_tree_remove_node): Call dispose_fn on the + dropped value. + * lib/gl_rbtree_oset.c (gl_tree_remove_node): Call dispose_fn on the + dropped value. + * tests/test-array_oset.c (main): Update. + * tests/test-avltree_oset.c (main): Update. + * tests/test-rbtree_oset.c (main): Update. + * lib/gl_anytreehash_list1.h (add_to_bucket): Update. + 2007-03-12 Bruno Haible <bruno@clisp.org> * lib/quotearg.c: Include <wctype.h> early, before the definition of
--- a/NEWS +++ b/NEWS @@ -6,6 +6,11 @@ Date Modules Changes +2007-03-15 oset The function gl_oset_create_empty now takes a + array-oset third argument. You can pass NULL. + avltree-oset + rbtree-oset + 2007-03-12 des The types and functions in lib/des.h have been gc-des renamed:
--- a/lib/gl_anytree_oset.h +++ b/lib/gl_anytree_oset.h @@ -1,5 +1,5 @@ /* Ordered set data type implemented by a binary tree. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006-2007 Free Software Foundation, Inc. Written by Bruno Haible <bruno@clisp.org>, 2006. This program is free software; you can redistribute it and/or modify @@ -30,12 +30,14 @@ static gl_oset_t gl_tree_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn) + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { struct gl_oset_impl *set = XMALLOC (struct gl_oset_impl); set->base.vtable = implementation; set->base.compar_fn = compar_fn; + set->base.dispose_fn = dispose_fn; set->root = NULL; set->count = 0; @@ -216,6 +218,8 @@ if (!stack_ptr->rightp) break; /* Free the current node. */ + if (set->base.dispose_fn != NULL) + set->base.dispose_fn (node->value); free (node); } /* Descend on right branch. */
--- a/lib/gl_anytreehash_list1.h +++ b/lib/gl_anytreehash_list1.h @@ -1,5 +1,5 @@ /* Sequential list data type implemented by a hash table with a binary tree. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006-2007 Free Software Foundation, Inc. Written by Bruno Haible <bruno@clisp.org>, 2006. This program is free software; you can redistribute it and/or modify @@ -152,7 +152,7 @@ nodes = gl_oset_create_empty (OSET_TREE_FLAVOR, - compare_by_position); + compare_by_position, NULL); gl_oset_add (nodes, node); gl_oset_add (nodes, new_node);
--- a/lib/gl_array_oset.c +++ b/lib/gl_array_oset.c @@ -43,12 +43,14 @@ static gl_oset_t gl_array_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn) + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { struct gl_oset_impl *set = XMALLOC (struct gl_oset_impl); set->base.vtable = implementation; set->base.compar_fn = compar_fn; + set->base.dispose_fn = dispose_fn; set->elements = NULL; set->count = 0; set->allocated = 0; @@ -204,6 +206,8 @@ size_t i; elements = set->elements; + if (set->base.dispose_fn != NULL) + set->base.dispose_fn (elements[position]); for (i = position + 1; i < count; i++) elements[i - 1] = elements[i]; set->count = count - 1; @@ -262,7 +266,23 @@ gl_array_free (gl_oset_t set) { if (set->elements != NULL) - free (set->elements); + { + if (set->base.dispose_fn != NULL) + { + size_t count = set->count; + + if (count > 0) + { + gl_setelement_dispose_fn dispose = set->base.dispose_fn; + const void **elements = set->elements; + + do + dispose (*elements++); + while (--count > 0); + } + } + free (set->elements); + } free (set); }
--- a/lib/gl_avltree_oset.c +++ b/lib/gl_avltree_oset.c @@ -1,5 +1,5 @@ /* Ordered set data type implemented by a binary tree. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006-2007 Free Software Foundation, Inc. Written by Bruno Haible <bruno@clisp.org>, 2006. This program is free software; you can redistribute it and/or modify @@ -519,6 +519,8 @@ } set->count--; + if (set->base.dispose_fn != NULL) + set->base.dispose_fn (node->value); free (node); return true; }
--- a/lib/gl_oset.c +++ b/lib/gl_oset.c @@ -1,5 +1,5 @@ /* Abstract ordered set data type. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006-2007 Free Software Foundation, Inc. Written by Bruno Haible <bruno@clisp.org>, 2006. This program is free software; you can redistribute it and/or modify @@ -29,9 +29,10 @@ gl_oset_t gl_oset_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn) + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { - return implementation->create_empty (implementation, compar_fn); + return implementation->create_empty (implementation, compar_fn, dispose_fn); } size_t
--- a/lib/gl_oset.h +++ b/lib/gl_oset.h @@ -1,5 +1,5 @@ /* Abstract ordered set data type. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006-2007 Free Software Foundation, Inc. Written by Bruno Haible <bruno@clisp.org>, 2006. This program is free software; you can redistribute it and/or modify @@ -70,6 +70,10 @@ NULL denotes pointer comparison. */ typedef int (*gl_setelement_compar_fn) (const void *elt1, const void *elt2); +/* Type of function used to dispose an element once it's removed from a set. + NULL denotes a no-op. */ +typedef void (*gl_setelement_dispose_fn) (const void *elt); + /* Type of function used to compare an element with a threshold. Return true if the element is greater or equal than the threshold. */ typedef bool (*gl_setelement_threshold_fn) (const void *elt, const void *threshold); @@ -84,9 +88,11 @@ /* Create an empty set. IMPLEMENTATION is one of GL_ARRAY_OSET, GL_AVLTREE_OSET, GL_RBTREE_OSET. - COMPAR_FN is an element comparison function or NULL. */ + COMPAR_FN is an element comparison function or NULL. + DISPOSE_FN is an element disposal function or NULL. */ extern gl_oset_t gl_oset_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn); + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn); /* Return the current number of elements in an ordered set. */ extern size_t gl_oset_size (gl_oset_t set); @@ -155,7 +161,8 @@ { /* gl_oset_t functions. */ gl_oset_t (*create_empty) (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn); + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn); size_t (*size) (gl_oset_t set); bool (*search) (gl_oset_t set, const void *elt); bool (*search_atleast) (gl_oset_t set, @@ -174,6 +181,7 @@ { const struct gl_oset_implementation *vtable; gl_setelement_compar_fn compar_fn; + gl_setelement_dispose_fn dispose_fn; }; #if HAVE_INLINE @@ -185,9 +193,10 @@ # define gl_oset_create_empty gl_oset_create_empty_inline static inline gl_oset_t gl_oset_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn) + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { - return implementation->create_empty (implementation, compar_fn); + return implementation->create_empty (implementation, compar_fn, dispose_fn); } # define gl_oset_size gl_oset_size_inline
--- a/lib/gl_rbtree_oset.c +++ b/lib/gl_rbtree_oset.c @@ -1,5 +1,5 @@ /* Ordered set data type implemented by a binary tree. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006-2007 Free Software Foundation, Inc. Written by Bruno Haible <bruno@clisp.org>, 2006. This program is free software; you can redistribute it and/or modify @@ -749,6 +749,8 @@ } set->count--; + if (set->base.dispose_fn != NULL) + set->base.dispose_fn (node->value); free (node); return true; }
--- a/tests/test-array_oset.c +++ b/tests/test-array_oset.c @@ -88,7 +88,7 @@ unsigned int repeat; /* Create set1. */ - set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp); + set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); /* Create set2. */ set2 = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, false);
--- a/tests/test-avltree_oset.c +++ b/tests/test-avltree_oset.c @@ -86,10 +86,10 @@ unsigned int repeat; /* Create set1. */ - set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp); + set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); /* Create set2. */ - set2 = gl_oset_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp); + set2 = gl_oset_create_empty (GL_AVLTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL); check_all (set1, set2);
--- a/tests/test-rbtree_oset.c +++ b/tests/test-rbtree_oset.c @@ -88,10 +88,10 @@ unsigned int repeat; /* Create set1. */ - set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp); + set1 = gl_oset_create_empty (GL_ARRAY_OSET, (gl_setelement_compar_fn) strcmp, NULL); /* Create set2. */ - set2 = gl_oset_create_empty (GL_RBTREE_OSET, (gl_setelement_compar_fn) strcmp); + set2 = gl_oset_create_empty (GL_RBTREE_OSET, (gl_setelement_compar_fn) strcmp, NULL); check_all (set1, set2);