unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#54172: [PATCH] vectors: add (vector-last) support
@ 2022-02-26 18:14 Aleix Conchillo Flaqué
  0 siblings, 0 replies; only message in thread
From: Aleix Conchillo Flaqué @ 2022-02-26 18:14 UTC (permalink / raw)
  To: 54172; +Cc: Aleix Conchillo Flaqué

* libguile/vectors.c: add (vector-last) support.

* libguile/vectors.h: define scm_vector_last and scm_c_vector_last.

* doc/ref/api-data.texi (Vector Accessors): add documentation for
(vector-last).
---
 doc/ref/api-data.texi         | 10 ++++++++++
 libguile/vectors.c            | 30 +++++++++++++++++++++++++++++-
 libguile/vectors.h            |  6 ++++--
 test-suite/tests/srfi-43.test | 12 +++++++++++-
 4 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/doc/ref/api-data.texi b/doc/ref/api-data.texi
index 8658b9785..cf7253b31 100644
--- a/doc/ref/api-data.texi
+++ b/doc/ref/api-data.texi
@@ -6277,6 +6277,16 @@ Return the contents of position @var{k} (a @code{size_t}) of
 @var{vec}.
 @end deftypefn
 
+@rnindex vector-last
+@deffn {Scheme Procedure} vector-last vec
+@deffnx {C Function} scm_vector_last (vec)
+Return the contents of the last element of @var{vec}.
+@end deffn
+
+@deftypefn {C Function} SCM scm_c_vector_last (SCM vec)
+Return the contents of the last element of @var{vec}.
+@end deftypefn
+
 A vector created by one of the dynamic vector constructor procedures
 (@pxref{Vector Creation}) can be modified using the following
 procedures.
diff --git a/libguile/vectors.c b/libguile/vectors.c
index 18c7dc54d..e5aa297df 100644
--- a/libguile/vectors.c
+++ b/libguile/vectors.c
@@ -188,7 +188,35 @@ scm_c_vector_ref (SCM v, size_t k)
 }
 #undef FUNC_NAME
 
-SCM_DEFINE (scm_vector_set_x, "vector-set!", 3, 0, 0, 
+SCM_DEFINE (scm_vector_last, "vector-last", 1, 0, 0,
+	    (SCM vector),
+            "@samp{Vector-ref} returns the contents of the last element of\n"
+            "@var{vector}.\n\n"
+            "@lisp\n"
+            "(vector-ref '#(3 1 27 5)) @result{} 5\n"
+            "@end lisp")
+#define FUNC_NAME s_scm_vector_last
+{
+  return scm_c_vector_last (vector);
+}
+#undef FUNC_NAME
+
+SCM
+scm_c_vector_last (SCM v)
+#define FUNC_NAME s_scm_vector_last
+{
+  SCM_VALIDATE_VECTOR (1, v);
+
+  size_t len = SCM_I_VECTOR_LENGTH (v);
+
+  if (len == 0)
+    scm_out_of_range (NULL, 0);
+
+  return scm_c_vector_ref (v, len - 1);
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_vector_set_x, "vector-set!", 3, 0, 0,
 	    (SCM vector, SCM k, SCM obj),
             "@var{k} must be a valid index of @var{vector}.\n"
             "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
diff --git a/libguile/vectors.h b/libguile/vectors.h
index 005999a2b..b729e6105 100644
--- a/libguile/vectors.h
+++ b/libguile/vectors.h
@@ -1,7 +1,7 @@
 #ifndef SCM_VECTORS_H
 #define SCM_VECTORS_H
 
-/* Copyright 1995-1996,1998,2000-2002,2004-2006,2008-2009,2011,2014,2018
+/* Copyright 1995-1996,1998,2000-2002,2004-2006,2008-2009,2011,2014,2018,2020
      Free Software Foundation, Inc.
 
    This file is part of Guile.
@@ -32,13 +32,14 @@ SCM_API SCM scm_vector_p (SCM x);
 SCM_API SCM scm_vector_length (SCM v);
 SCM_API SCM scm_vector (SCM l);
 SCM_API SCM scm_vector_ref (SCM v, SCM k);
+SCM_API SCM scm_vector_last (SCM v);
 SCM_API SCM scm_vector_set_x (SCM v, SCM k, SCM obj);
 SCM_API SCM scm_make_vector (SCM k, SCM fill);
 SCM_API SCM scm_vector_to_list (SCM v);
 SCM_API SCM scm_vector_fill_x (SCM v, SCM fill_x);
 SCM_API SCM scm_vector_move_left_x (SCM vec1, SCM start1, SCM end1,
 				    SCM vec2, SCM start2);
-SCM_API SCM scm_vector_move_right_x (SCM vec1, SCM start1, SCM end1, 
+SCM_API SCM scm_vector_move_right_x (SCM vec1, SCM start1, SCM end1,
 				     SCM vec2, SCM start2);
 SCM_API SCM scm_vector_copy (SCM vec);
 SCM_API SCM scm_vector_copy_partial (SCM vec, SCM start, SCM end);
@@ -48,6 +49,7 @@ SCM_API int scm_is_vector (SCM obj);
 SCM_API SCM scm_c_make_vector (size_t len, SCM fill);
 SCM_API size_t scm_c_vector_length (SCM vec);
 SCM_API SCM scm_c_vector_ref (SCM vec, size_t k);
+SCM_API SCM scm_c_vector_last (SCM vec);
 SCM_API void scm_c_vector_set_x (SCM vec, size_t k, SCM obj);
 SCM_API const SCM *scm_vector_elements (SCM array,
 					scm_t_array_handle *h,
diff --git a/test-suite/tests/srfi-43.test b/test-suite/tests/srfi-43.test
index 554843e75..4d0093974 100644
--- a/test-suite/tests/srfi-43.test
+++ b/test-suite/tests/srfi-43.test
@@ -1,6 +1,6 @@
 ;;;; srfi-43.test --- test suite for SRFI-43 Vector library -*- scheme -*-
 ;;;;
-;;;; Copyright (C) 2014 Free Software Foundation, Inc.
+;;;; Copyright (C) 2014, 2020 Free Software Foundation, Inc.
 ;;;;
 ;;;; This library is free software; you can redistribute it and/or
 ;;;; modify it under the terms of the GNU Lesser General Public
@@ -390,6 +390,16 @@
   (pass-if-error "non-vector" (vector-ref '(a b c) 0))
   (pass-if-error "inexact index" (vector-ref '#(a b c) 1.0)))
 
+;;
+;; vector-last
+;;
+
+(with-test-prefix "vector-last"
+  (pass-if-equal "single element" 'a (vector-last '#(a)))
+  (pass-if-equal "multiple elements" 'c (vector-last '#(a b c)))
+  (pass-if-error "empty vector" (vector-last '#()))
+  (pass-if-error "non-vector" (vector-last '(a b c))))
+
 ;;
 ;; vector-length
 ;;
-- 
2.35.1






^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-02-26 18:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-26 18:14 bug#54172: [PATCH] vectors: add (vector-last) support Aleix Conchillo Flaqué

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).