> Done through the two attached patches. And another patch, for the C++ interface.
2020-05-03 Bruno Haible <br...@clisp.org> 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. diff --git a/lib/gl_list.h b/lib/gl_list.h index 7786092..dfec6d0 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 2cc83be..b19bda7 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<ELTYPE *>(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<ELTYPE *>(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<ELTYPE *>(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)