From 7090b8b7f7dcbf471b6d2fc3e51e184312a3840c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 3 May 2020 13:13:00 +0200 Subject: [PATCH] list-c++: Add get_first, get_last, set_first, set_last operations. * lib/gl_list.hh (class gl_List): Add methods get_first, get_last, set_first, set_last. * lib/gl_list.h: Tweak comments. --- ChangeLog | 8 +++++++- lib/gl_list.h | 12 ++++++------ lib/gl_list.hh | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1ac3d104f2..899e444873 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2020-05-03 Bruno Haible + + list-c++: Add get_first, get_last, set_first, set_last operations. + * lib/gl_list.hh (class gl_List): Add methods get_first, get_last, + set_first, set_last. + * lib/gl_list.h: Tweak comments. + 2020-05-03 Akim Demaille bison: rely on bison's %require to check a version requirement @@ -9,7 +16,6 @@ input file name. * m4/parse-datetime.m4: Simplify gl_PROG_BISON invocation. - 2020-05-02 Bruno Haible list: Add get_first, get_last, set_first, set_last operations. diff --git a/lib/gl_list.h b/lib/gl_list.h index 7786092232..dfec6d0b5e 100644 --- a/lib/gl_list.h +++ b/lib/gl_list.h @@ -215,11 +215,11 @@ extern gl_list_node_t gl_list_previous_node (gl_list_t list, gl_list_node_t node extern const void * gl_list_get_at (gl_list_t list, size_t position); /* Returns the element at the first position in the list. - LIST must be non-empty. */ + The list must be non-empty. */ extern const void * gl_list_get_first (gl_list_t list); /* Returns the element at the last position in the list. - LIST must be non-empty. */ + The list must be non-empty. */ extern const void * gl_list_get_last (gl_list_t list); /* Replaces the element at a given position in the list. @@ -237,8 +237,8 @@ extern gl_list_node_t gl_list_nx_set_at (gl_list_t list, size_t position, ; /* Replaces the element at the first position in the list. - LIST must be non-empty. - Returns its node. */ + Returns its node. + The list must be non-empty. */ /* declared in gl_xlist.h */ extern gl_list_node_t gl_list_set_first (gl_list_t list, const void *elt); /* Likewise. Returns NULL upon out-of-memory. */ @@ -249,8 +249,8 @@ extern gl_list_node_t gl_list_nx_set_first (gl_list_t list, const void *elt) ; /* Replaces the element at the last position in the list. - LIST must be non-empty. - Returns its node. */ + Returns its node. + The list must be non-empty. */ /* declared in gl_xlist.h */ extern gl_list_node_t gl_list_set_last (gl_list_t list, const void *elt); /* Likewise. Returns NULL upon out-of-memory. */ diff --git a/lib/gl_list.hh b/lib/gl_list.hh index 2cc83be3a2..b19bda71b1 100644 --- a/lib/gl_list.hh +++ b/lib/gl_list.hh @@ -137,6 +137,16 @@ public: ELTYPE * get_at (size_t position) const { return static_cast(gl_list_get_at (_ptr, position)); } + /* Returns the element at the first position in the list. + The list must be non-empty. */ + ELTYPE * get_first () const + { return static_cast(gl_list_get_first (_ptr)); } + + /* Returns the element at the last position in the list. + The list must be non-empty. */ + ELTYPE * get_last () const + { return static_cast(gl_list_get_last (_ptr)); } + /* Searches whether an element is already in the list. Returns its node if found, or NULL if not present in the list. */ gl_list_node_t search (ELTYPE * elt) const @@ -183,6 +193,18 @@ public: gl_list_node_t set_at (size_t position, ELTYPE * elt) { return gl_list_set_at (_ptr, position, elt); } + /* Replaces the element at the first position in the list. + Returns its node. + The list must be non-empty. */ + gl_list_node_t set_first (ELTYPE * elt) + { return gl_list_set_first (_ptr, elt); } + + /* Replaces the element at the last position in the list. + Returns its node. + The list must be non-empty. */ + gl_list_node_t set_last (ELTYPE * elt) + { return gl_list_set_last (_ptr, elt); } + /* Adds an element as the first element of the list. Returns its node. */ gl_list_node_t add_first (ELTYPE * elt) -- 2.39.5