From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Aleix Conchillo =?UTF-8?Q?Flaqu=C3=A9?= Newsgroups: gmane.lisp.guile.bugs Subject: bug#54172: [PATCH] vectors: add (vector-last) support Date: Sat, 26 Feb 2022 10:14:30 -0800 Message-ID: <20220226181430.70137-1-aconchillo@gmail.com> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4578"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Aleix Conchillo =?UTF-8?Q?Flaqu=C3=A9?= To: 54172@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Sat Feb 26 19:17:05 2022 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nO1cu-000116-P7 for guile-bugs@m.gmane-mx.org; Sat, 26 Feb 2022 19:17:04 +0100 Original-Received: from localhost ([::1]:52038 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nO1ct-0004sJ-Kh for guile-bugs@m.gmane-mx.org; Sat, 26 Feb 2022 13:17:03 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:44482) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nO1aw-0002ne-88 for bug-guile@gnu.org; Sat, 26 Feb 2022 13:15:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:34195) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nO1av-0005Si-UA for bug-guile@gnu.org; Sat, 26 Feb 2022 13:15:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nO1av-0004S6-OL for bug-guile@gnu.org; Sat, 26 Feb 2022 13:15:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Aleix Conchillo =?UTF-8?Q?Flaqu=C3=A9?= Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Sat, 26 Feb 2022 18:15:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 54172 X-GNU-PR-Package: guile X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-guile@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.164589928117065 (code B ref -1); Sat, 26 Feb 2022 18:15:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 26 Feb 2022 18:14:41 +0000 Original-Received: from localhost ([127.0.0.1]:56325 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nO1aa-0004RB-Gz for submit@debbugs.gnu.org; Sat, 26 Feb 2022 13:14:40 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:38662) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nO1aY-0004R3-D7 for submit@debbugs.gnu.org; Sat, 26 Feb 2022 13:14:38 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:44438) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nO1aY-0002BU-AB for bug-guile@gnu.org; Sat, 26 Feb 2022 13:14:38 -0500 Original-Received: from [2607:f8b0:4864:20::634] (port=39902 helo=mail-pl1-x634.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nO1aW-0005PG-6y for bug-guile@gnu.org; Sat, 26 Feb 2022 13:14:37 -0500 Original-Received: by mail-pl1-x634.google.com with SMTP id l9so6976511pls.6 for ; Sat, 26 Feb 2022 10:14:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=sS476rhJBZkPxu1lawvH+fyFXZyPBDh5MrEUlorRYNk=; b=M/o6kW5qylpzbVrTmeYc4St0rzDXgsdGF4fsRvd2XZ30L7QfVQDd597RTnEQihXj/S 3atqYOl2GWW5ibVMHkj3HS4Zx4m5OomVtZjU0yfH2gcMjKh1OKQZbolDJ3I/mUvw/tgd 6DK5XrDbqBfhV1Ck+JK8ST6MSj1u50pHBVlyG8R7Htz+2vjVnqcEr21E5f/MiDh9Da6f k8B/dOjfpjSEoxlz8NFzM9xtiLzomcHVX7SHBUo6I+GcW7C/Ezf4ZDiOogi7Jd9KENP4 nSjP5qFi172GXQEASAavKkVm81MSd9dcVX7waLpAPW8enak7XcguC1oWxlMS5w7Pttxn 0Kwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=sS476rhJBZkPxu1lawvH+fyFXZyPBDh5MrEUlorRYNk=; b=uPrDbgNGwSXGcVf0+HX8XsNVMONm8nlrQdTsvmw9E5Qp1xz7Qyri9yrL2+emNh9hEM 0mh7lsI1/t24g1H9i/X9uTEzxaxodRD2Ny/IxmG0ucLNUkaQgnKJg4CQyEAxtoVuhLw3 fXWQoKs5qV2qfnnZMldpVduYngcjr5fbpkTde8avNYe7LgxehI3hB20fZlkwkwant9h1 4PvSeFJS7i/9Ty4ZV156MiLAPvZawjl0xy2fpIWVP70o1z2sf6DWoXh/VJvqzuuFzyZD 7W4YDid9zvXjB2x0aVJq2pYj18Qyjtpcz1GuBsbWK+MOZ7tPxldGG4of0Y26XOurI8Vj K9uQ== X-Gm-Message-State: AOAM533m3R2nkrsc7JBwm5NUqVSCJqX+IXtIMDkzG1ltvnVlmg1SH1+v N13mBLS9Zaf0OZ8uQDoKZUzRd3pGmpE= X-Google-Smtp-Source: ABdhPJzfIynsNMTAPxhmH+OoCvv3DsXAE7loM+Ks0YHgf207xDsOtPwPqCTnvclvE5xBdw8teB024A== X-Received: by 2002:a17:902:d2ce:b0:150:19a9:d171 with SMTP id n14-20020a170902d2ce00b0015019a9d171mr11885298plc.155.1645899274037; Sat, 26 Feb 2022 10:14:34 -0800 (PST) Original-Received: from localhost (cpe-198-72-134-0.socal.res.rr.com. [198.72.134.0]) by smtp.gmail.com with UTF8SMTPSA id t29-20020a63225d000000b0035e366803dasm5955335pgm.45.2022.02.26.10.14.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 26 Feb 2022 10:14:33 -0800 (PST) X-Mailer: git-send-email 2.35.1 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::634 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=aconchillo@gmail.com; helo=mail-pl1-x634.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:10256 Archived-At: * 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