+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
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:
/* 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
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;
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. */
/* 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
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);
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;
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;
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);
}
/* 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
}
set->count--;
+ if (set->base.dispose_fn != NULL)
+ set->base.dispose_fn (node->value);
free (node);
return true;
}
/* 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
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
/* 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
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);
/* 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);
{
/* 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,
{
const struct gl_oset_implementation *vtable;
gl_setelement_compar_fn compar_fn;
+ gl_setelement_dispose_fn dispose_fn;
};
#if HAVE_INLINE
# 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
/* 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
}
set->count--;
+ if (set->base.dispose_fn != NULL)
+ set->base.dispose_fn (node->value);
free (node);
return true;
}
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);
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);
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);