X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fgl_oset.h;h=c5f38dc423ca8f62988b1e89e52a349f1e29d0fc;hb=83142d08d8fa02a5e133f4b5c0f989584c0427bb;hp=0ac678f59273b393c8fe0901231a6eca0bb19ed1;hpb=a7e6fe2b890c07cb428be7ff82d1267847a1efdf;p=gnulib.git diff --git a/lib/gl_oset.h b/lib/gl_oset.h index 0ac678f59..c5f38dc42 100644 --- a/lib/gl_oset.h +++ b/lib/gl_oset.h @@ -1,11 +1,11 @@ /* Abstract ordered set data type. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2009-2012 Free Software Foundation, Inc. Written by Bruno Haible , 2006. - This program is free software; you can redistribute it and/or modify + 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 - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -13,8 +13,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see . */ #ifndef _GL_OSET_H #define _GL_OSET_H @@ -56,9 +55,10 @@ extern "C" { Operation ARRAY TREE gl_oset_size O(1) O(1) - gl_oset_add - gl_oset_remove - gl_oset_search + gl_oset_add O(n) O(log n) + gl_oset_remove O(n) O(log n) + gl_oset_search O(log n) O(log n) + gl_oset_search_atleast O(log n) O(log n) gl_oset_iterator O(1) O(log n) gl_oset_iterator_next O(1) O(log n) */ @@ -69,6 +69,14 @@ extern "C" { 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); + struct gl_oset_impl; /* Type representing an entire ordered set. */ typedef struct gl_oset_impl * gl_oset_t; @@ -79,9 +87,17 @@ typedef const struct gl_oset_implementation * gl_oset_implementation_t; /* 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. */ +#if 0 /* declared in gl_xoset.h */ 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); +#endif +/* Likewise. Return NULL upon out-of-memory. */ +extern gl_oset_t gl_oset_nx_create_empty (gl_oset_implementation_t implementation, + 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); @@ -90,9 +106,27 @@ extern size_t gl_oset_size (gl_oset_t set); Return true if found, or false if not present in the set. */ extern bool gl_oset_search (gl_oset_t set, const void *elt); +/* Search the least element in the ordered set that compares greater or equal + to the given THRESHOLD. The representation of the THRESHOLD is defined + by the THRESHOLD_FN. + Return true and store the found element in *ELTP if found, otherwise return + false. */ +extern bool gl_oset_search_atleast (gl_oset_t set, + gl_setelement_threshold_fn threshold_fn, + const void *threshold, + const void **eltp); + /* Add an element to an ordered set. - Return true if it was not already in the set and added. */ + Return true if it was not already in the set and added, false otherwise. */ +#if 0 /* declared in gl_xoset.h */ extern bool gl_oset_add (gl_oset_t set, const void *elt); +#endif +/* Likewise. Return -1 upon out-of-memory. */ +extern int gl_oset_nx_add (gl_oset_t set, const void *elt) +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) + __attribute__ ((__warn_unused_result__)) +#endif + ; /* Remove an element from an ordered set. Return true if it was found and removed. */ @@ -129,7 +163,7 @@ extern gl_oset_iterator_t gl_oset_iterator (gl_oset_t set); /* If there is a next element, store the next element in *ELTP, advance the iterator and return true. Otherwise, return false. */ extern bool gl_oset_iterator_next (gl_oset_iterator_t *iterator, - const void **eltp); + const void **eltp); /* Free an iterator. */ extern void gl_oset_iterator_free (gl_oset_iterator_t *iterator); @@ -138,15 +172,19 @@ extern void gl_oset_iterator_free (gl_oset_iterator_t *iterator); struct gl_oset_implementation { - // gl_oset_t functions. - gl_oset_t (*create_empty) (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn); + /* gl_oset_t functions. */ + gl_oset_t (*nx_create_empty) (gl_oset_implementation_t implementation, + 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 (*add) (gl_oset_t set, const void *elt); - bool (*remove) (gl_oset_t set, const void *elt); + bool (*search_atleast) (gl_oset_t set, + gl_setelement_threshold_fn threshold_fn, + const void *threshold, const void **eltp); + int (*nx_add) (gl_oset_t set, const void *elt); + bool (*remove_elt) (gl_oset_t set, const void *elt); void (*oset_free) (gl_oset_t set); - // gl_oset_iterator_t functions. + /* gl_oset_iterator_t functions. */ gl_oset_iterator_t (*iterator) (gl_oset_t set); bool (*iterator_next) (gl_oset_iterator_t *iterator, const void **eltp); void (*iterator_free) (gl_oset_iterator_t *iterator); @@ -156,6 +194,7 @@ struct gl_oset_impl_base { const struct gl_oset_implementation *vtable; gl_setelement_compar_fn compar_fn; + gl_setelement_dispose_fn dispose_fn; }; #if HAVE_INLINE @@ -164,12 +203,14 @@ struct gl_oset_impl_base struct gl_oset_implementation. Use #define to avoid a warning because of extern vs. static. */ -# define gl_oset_create_empty gl_oset_create_empty_inline +# define gl_oset_nx_create_empty gl_oset_nx_create_empty_inline static inline gl_oset_t -gl_oset_create_empty (gl_oset_implementation_t implementation, - gl_setelement_compar_fn compar_fn) +gl_oset_nx_create_empty (gl_oset_implementation_t implementation, + gl_setelement_compar_fn compar_fn, + gl_setelement_dispose_fn dispose_fn) { - return implementation->create_empty (implementation, compar_fn); + return implementation->nx_create_empty (implementation, compar_fn, + dispose_fn); } # define gl_oset_size gl_oset_size_inline @@ -186,18 +227,29 @@ gl_oset_search (gl_oset_t set, const void *elt) return ((const struct gl_oset_impl_base *) set)->vtable->search (set, elt); } -# define gl_oset_add gl_oset_add_inline +# define gl_oset_search_atleast gl_oset_search_atleast_inline static inline bool -gl_oset_add (gl_oset_t set, const void *elt) +gl_oset_search_atleast (gl_oset_t set, + gl_setelement_threshold_fn threshold_fn, + const void *threshold, const void **eltp) +{ + return ((const struct gl_oset_impl_base *) set)->vtable + ->search_atleast (set, threshold_fn, threshold, eltp); +} + +# define gl_oset_nx_add gl_oset_nx_add_inline +static inline int +gl_oset_nx_add (gl_oset_t set, const void *elt) { - return ((const struct gl_oset_impl_base *) set)->vtable->add (set, elt); + return ((const struct gl_oset_impl_base *) set)->vtable->nx_add (set, elt); } # define gl_oset_remove gl_oset_remove_inline static inline bool gl_oset_remove (gl_oset_t set, const void *elt) { - return ((const struct gl_oset_impl_base *) set)->vtable->remove (set, elt); + return ((const struct gl_oset_impl_base *) set)->vtable + ->remove_elt (set, elt); } # define gl_oset_free gl_oset_free_inline