From: Bruno Haible Date: Sat, 2 May 2020 19:14:29 +0000 (+0200) Subject: list: Remove redundant code for remove_first and remove_last operations. X-Git-Tag: v1.0~4140 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=da5396455733d383a4ac4d0cf7eb4cd191072352;p=gnulib.git list: Remove redundant code for remove_first and remove_last operations. * lib/gl_list.h (struct gl_list_implementation): Remove fields remove_first, remove_last. (gl_list_remove_first, gl_list_remove_last): Implement in a generic way. * lib/gl_array_list.c: Revert last change. * lib/gl_carray_list.c: Likewise. * lib/gl_anylinked_list2.h: Likewise. * lib/gl_linked_list.c: Likewise. * lib/gl_linkedhash_list.c: Likewise. * lib/gl_anytree_list2.h: Likewise. * lib/gl_avltree_list.c: Likewise. * lib/gl_avltreehash_list.c: Likewise. * lib/gl_rbtree_list.c: Likewise. * lib/gl_rbtreehash_list.c: Likewise. * lib/gl_sublist.c: Likewise. --- diff --git a/ChangeLog b/ChangeLog index 6c0f620785..ac235441d4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2020-05-02 Bruno Haible + + list: Remove redundant code for remove_first and remove_last operations. + * lib/gl_list.h (struct gl_list_implementation): Remove fields + remove_first, remove_last. + (gl_list_remove_first, gl_list_remove_last): Implement in a generic way. + * lib/gl_array_list.c: Revert last change. + * lib/gl_carray_list.c: Likewise. + * lib/gl_anylinked_list2.h: Likewise. + * lib/gl_linked_list.c: Likewise. + * lib/gl_linkedhash_list.c: Likewise. + * lib/gl_anytree_list2.h: Likewise. + * lib/gl_avltree_list.c: Likewise. + * lib/gl_avltreehash_list.c: Likewise. + * lib/gl_rbtree_list.c: Likewise. + * lib/gl_rbtreehash_list.c: Likewise. + * lib/gl_sublist.c: Likewise. + 2020-05-02 Bruno Haible bison-i18n: Add support for cross-compilation. diff --git a/lib/gl_anylinked_list2.h b/lib/gl_anylinked_list2.h index 0032dc8826..114106c7dc 100644 --- a/lib/gl_anylinked_list2.h +++ b/lib/gl_anylinked_list2.h @@ -881,68 +881,6 @@ gl_linked_remove_at (gl_list_t list, size_t position) return true; } -static bool -gl_linked_remove_first (gl_list_t list) -{ - size_t count = list->count; - gl_list_node_t removed_node; - - if (count == 0) - return false; - /* Here we know count > 0. */ - /* Like gl_linked_remove_at (list, 0). */ - { - gl_list_node_t node; - gl_list_node_t after_removed; - - node = &list->root; - removed_node = node->next; - after_removed = node->next->next; - ASYNCSAFE(gl_list_node_t) node->next = after_removed; - after_removed->prev = node; - } -#if WITH_HASHTABLE - remove_from_bucket (list, removed_node); -#endif - list->count--; - - if (list->base.dispose_fn != NULL) - list->base.dispose_fn (removed_node->value); - free (removed_node); - return true; -} - -static bool -gl_linked_remove_last (gl_list_t list) -{ - size_t count = list->count; - gl_list_node_t removed_node; - - if (count == 0) - return false; - /* Here we know count > 0. */ - /* Like gl_linked_remove_at (list, count - 1). */ - { - gl_list_node_t node; - gl_list_node_t before_removed; - - node = &list->root; - removed_node = node->prev; - before_removed = node->prev->prev; - node->prev = before_removed; - ASYNCSAFE(gl_list_node_t) before_removed->next = node; - } -#if WITH_HASHTABLE - remove_from_bucket (list, removed_node); -#endif - list->count--; - - if (list->base.dispose_fn != NULL) - list->base.dispose_fn (removed_node->value); - free (removed_node); - return true; -} - static bool gl_linked_remove (gl_list_t list, const void *elt) { diff --git a/lib/gl_anytree_list2.h b/lib/gl_anytree_list2.h index 41e41dd81b..c5a67dbd0c 100644 --- a/lib/gl_anytree_list2.h +++ b/lib/gl_anytree_list2.h @@ -485,34 +485,6 @@ gl_tree_remove_at (gl_list_t list, size_t position) return gl_tree_remove_node (list, node); } -static bool -gl_tree_remove_first (gl_list_t list) -{ - gl_list_node_t node = list->root; - - if (node != NULL) - { - node = node_at (node, 0); - return gl_tree_remove_node (list, node); - } - else - return false; -} - -static bool -gl_tree_remove_last (gl_list_t list) -{ - gl_list_node_t node = list->root; - - if (node != NULL) - { - node = node_at (node, node->branch_size - 1); - return gl_tree_remove_node (list, node); - } - else - return false; -} - static bool gl_tree_remove (gl_list_t list, const void *elt) { diff --git a/lib/gl_array_list.c b/lib/gl_array_list.c index e00255c724..49509620f7 100644 --- a/lib/gl_array_list.c +++ b/lib/gl_array_list.c @@ -405,41 +405,6 @@ gl_array_remove_at (gl_list_t list, size_t position) return true; } -static bool -gl_array_remove_first (gl_list_t list) -{ - size_t count = list->count; - const void **elements; - size_t i; - - if (count == 0) - return false; - /* Like gl_array_remove_at (list, 0). */ - elements = list->elements; - if (list->base.dispose_fn != NULL) - list->base.dispose_fn (elements[0]); - for (i = 1; i < count; i++) - elements[i - 1] = elements[i]; - list->count = count - 1; - return true; -} - -static bool -gl_array_remove_last (gl_list_t list) -{ - size_t count = list->count; - const void **elements; - - if (count == 0) - return false; - /* Like gl_array_remove_at (list, count - 1). */ - elements = list->elements; - if (list->base.dispose_fn != NULL) - list->base.dispose_fn (elements[count - 1]); - list->count = count - 1; - return true; -} - static bool gl_array_remove (gl_list_t list, const void *elt) { @@ -697,8 +662,6 @@ const struct gl_list_implementation gl_array_list_implementation = gl_array_nx_add_at, gl_array_remove_node, gl_array_remove_at, - gl_array_remove_first, - gl_array_remove_last, gl_array_remove, gl_array_list_free, gl_array_iterator, diff --git a/lib/gl_avltree_list.c b/lib/gl_avltree_list.c index 2906f5a8ca..35ffec6f3c 100644 --- a/lib/gl_avltree_list.c +++ b/lib/gl_avltree_list.c @@ -89,8 +89,6 @@ const struct gl_list_implementation gl_avltree_list_implementation = gl_tree_nx_add_at, gl_tree_remove_node, gl_tree_remove_at, - gl_tree_remove_first, - gl_tree_remove_last, gl_tree_remove, gl_tree_list_free, gl_tree_iterator, diff --git a/lib/gl_avltreehash_list.c b/lib/gl_avltreehash_list.c index 576f5336a4..9f795ff864 100644 --- a/lib/gl_avltreehash_list.c +++ b/lib/gl_avltreehash_list.c @@ -111,8 +111,6 @@ const struct gl_list_implementation gl_avltreehash_list_implementation = gl_tree_nx_add_at, gl_tree_remove_node, gl_tree_remove_at, - gl_tree_remove_first, - gl_tree_remove_last, gl_tree_remove, gl_tree_list_free, gl_tree_iterator, diff --git a/lib/gl_carray_list.c b/lib/gl_carray_list.c index fa17d48e6b..36a8773c35 100644 --- a/lib/gl_carray_list.c +++ b/lib/gl_carray_list.c @@ -544,44 +544,6 @@ gl_carray_remove_at (gl_list_t list, size_t position) return true; } -static bool -gl_carray_remove_first (gl_list_t list) -{ - size_t count = list->count; - size_t i0; - - if (count == 0) - return false; - /* Here we know count > 0. */ - /* Like gl_carray_remove_at (list, 0). */ - i0 = list->offset; - if (list->base.dispose_fn != NULL) - list->base.dispose_fn (list->elements[i0]); - i0++; - list->offset = (i0 == list->allocated ? 0 : i0); - list->count = count - 1; - return true; -} - -static bool -gl_carray_remove_last (gl_list_t list) -{ - size_t count = list->count; - size_t i1; - - if (count == 0) - return false; - /* Here we know count > 0. */ - /* Like gl_carray_remove_at (list, count - 1). */ - i1 = list->offset + count - 1; - if (i1 >= list->allocated) - i1 -= list->allocated; - if (list->base.dispose_fn != NULL) - list->base.dispose_fn (list->elements[i1]); - list->count = count - 1; - return true; -} - static bool gl_carray_remove_node (gl_list_t list, gl_list_node_t node) { @@ -893,8 +855,6 @@ const struct gl_list_implementation gl_carray_list_implementation = gl_carray_nx_add_at, gl_carray_remove_node, gl_carray_remove_at, - gl_carray_remove_first, - gl_carray_remove_last, gl_carray_remove, gl_carray_list_free, gl_carray_iterator, diff --git a/lib/gl_linked_list.c b/lib/gl_linked_list.c index e2bf526d1a..b1391cef9a 100644 --- a/lib/gl_linked_list.c +++ b/lib/gl_linked_list.c @@ -49,8 +49,6 @@ const struct gl_list_implementation gl_linked_list_implementation = gl_linked_nx_add_at, gl_linked_remove_node, gl_linked_remove_at, - gl_linked_remove_first, - gl_linked_remove_last, gl_linked_remove, gl_linked_list_free, gl_linked_iterator, diff --git a/lib/gl_linkedhash_list.c b/lib/gl_linkedhash_list.c index 5ffc0cef2f..7c7f999f87 100644 --- a/lib/gl_linkedhash_list.c +++ b/lib/gl_linkedhash_list.c @@ -97,8 +97,6 @@ const struct gl_list_implementation gl_linkedhash_list_implementation = gl_linked_nx_add_at, gl_linked_remove_node, gl_linked_remove_at, - gl_linked_remove_first, - gl_linked_remove_last, gl_linked_remove, gl_linked_list_free, gl_linked_iterator, diff --git a/lib/gl_list.h b/lib/gl_list.h index ea5018d34e..8094006dd3 100644 --- a/lib/gl_list.h +++ b/lib/gl_list.h @@ -518,8 +518,6 @@ struct gl_list_implementation const void *elt); bool (*remove_node) (gl_list_t list, gl_list_node_t node); bool (*remove_at) (gl_list_t list, size_t position); - bool (*remove_first) (gl_list_t list); - bool (*remove_last) (gl_list_t list); bool (*remove_elt) (gl_list_t list, const void *elt); void (*list_free) (gl_list_t list); /* gl_list_iterator_t functions. */ @@ -762,15 +760,21 @@ gl_list_remove_at (gl_list_t list, size_t position) GL_LIST_INLINE bool gl_list_remove_first (gl_list_t list) { - return ((const struct gl_list_impl_base *) list)->vtable - ->remove_first (list); + size_t size = gl_list_size (list); + if (size > 0) + return gl_list_remove_at (list, 0); + else + return false; } GL_LIST_INLINE bool gl_list_remove_last (gl_list_t list) { - return ((const struct gl_list_impl_base *) list)->vtable - ->remove_last (list); + size_t size = gl_list_size (list); + if (size > 0) + return gl_list_remove_at (list, size - 1); + else + return false; } GL_LIST_INLINE bool diff --git a/lib/gl_rbtree_list.c b/lib/gl_rbtree_list.c index 6ae78c7fd8..d79becf4aa 100644 --- a/lib/gl_rbtree_list.c +++ b/lib/gl_rbtree_list.c @@ -87,8 +87,6 @@ const struct gl_list_implementation gl_rbtree_list_implementation = gl_tree_nx_add_at, gl_tree_remove_node, gl_tree_remove_at, - gl_tree_remove_first, - gl_tree_remove_last, gl_tree_remove, gl_tree_list_free, gl_tree_iterator, diff --git a/lib/gl_rbtreehash_list.c b/lib/gl_rbtreehash_list.c index b59b5f4877..be2ee6f4e9 100644 --- a/lib/gl_rbtreehash_list.c +++ b/lib/gl_rbtreehash_list.c @@ -112,8 +112,6 @@ const struct gl_list_implementation gl_rbtreehash_list_implementation = gl_tree_nx_add_at, gl_tree_remove_node, gl_tree_remove_at, - gl_tree_remove_first, - gl_tree_remove_last, gl_tree_remove, gl_tree_list_free, gl_tree_iterator, diff --git a/lib/gl_sublist.c b/lib/gl_sublist.c index 6e95c3b3ef..e529a6b897 100644 --- a/lib/gl_sublist.c +++ b/lib/gl_sublist.c @@ -258,22 +258,6 @@ gl_sublist_remove_at (gl_list_t list, size_t position) return gl_list_remove_at (list->whole, list->start + position); } -static bool -gl_sublist_remove_first (gl_list_t list) -{ - if (list->end == list->start) - return false; - return gl_list_remove_at (list->whole, list->start); -} - -static bool -gl_sublist_remove_last (gl_list_t list) -{ - if (list->end == list->start) - return false; - return gl_list_remove_at (list->whole, list->end - 1); -} - static bool gl_sublist_remove (gl_list_t list, const void *elt) { @@ -440,8 +424,6 @@ static const struct gl_list_implementation gl_sublist_list_implementation = gl_sublist_nx_add_at, gl_sublist_remove_node, gl_sublist_remove_at, - gl_sublist_remove_first, - gl_sublist_remove_last, gl_sublist_remove, gl_sublist_list_free, gl_sublist_iterator,