From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] vectors: add (vector-last) support Date: Fri, 12 Feb 2021 12:03:48 -0800 Message-ID: <20210212200348.34042-1-aconchillo@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40469"; mail-complaints-to="usenet@ciao.gmane.io" Cc: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Fri Feb 12 22:25:20 2021 Return-path: Envelope-to: guile-devel@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 1lAfwF-000APE-KP for guile-devel@m.gmane-mx.org; Fri, 12 Feb 2021 22:25:19 +0100 Original-Received: from localhost ([::1]:51776 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lAfwE-0001vX-FM for guile-devel@m.gmane-mx.org; Fri, 12 Feb 2021 16:25:18 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55724) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lAefX-00078V-3G for guile-devel@gnu.org; Fri, 12 Feb 2021 15:03:59 -0500 Original-Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]:41440) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lAefV-00070G-0L for guile-devel@gnu.org; Fri, 12 Feb 2021 15:03:58 -0500 Original-Received: by mail-pg1-x535.google.com with SMTP id t11so366528pgu.8 for ; Fri, 12 Feb 2021 12:03:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3XhnE8KOMGYIruLTENCzJMA4jJwNWBKKAWEREIgBOSM=; b=IERkJYPDWTm3837z+6vRRkvQ3IopMzHwYWeKw+XTA/SZa/jB823crIoxkX+gneH51U NK6Hvdo6hIOawzjZ6gV5vVREK+CK8ARi55QEc61MjdpmF+EaK9WLroghUZgfQJDONAGZ /a2C/ouEKNwrNQFHCcrXmaUxdyVpEI2/78TZ7zZki/vDz16vOfdcC8RUlF8kvP5WYmKA gDrInCGYlV+962qij7zbnJNkAeoNcfv5Zyrw728Y+lo8CXEyrfrnjaGvbyu+Dkep5ftW 7BwSwsFIgR9HlphtwHBYBU6T1HcRP8U1bpT5CVOSfu8n8dOirXXeldY5WPfM23bAwdwl zHzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3XhnE8KOMGYIruLTENCzJMA4jJwNWBKKAWEREIgBOSM=; b=LsFObdidfbA0Eo3zBxj2+DLpsR2zatsx/OpRQchGqw2rT4xxe2+pHl0Mvyo+Tl2t5S q/GBKwDKDLB6DxMDPeOEQ/nAA5nGF2KpLO29117rqCnrYt4PlH9Jfflkj6wqkjlAXsVZ S6LudTQUF4asEDPo263vAO9y31mjhr3yVFf6nxU9J3bxURBPPFg+8ifr5wSJx8OhIxXb ZgDYCotgfBpZICbjPlHsz/H3IXLQGqi2VH8Z+CrI0HGTUXFSShlkSI0XcRdRKNyCEZSj Ye2uRLUsQ3KPmhwp7XYp7Y4+qF2sVqcd8xGdNowZtpLeB0o+hRA1/SQbfAkA+Y0L/E8k S4zQ== X-Gm-Message-State: AOAM532VwRqJGaB0T79JR55JrXURw6SQUYupe7Xdw1n+/js0rYCVDd5X p9spph8T9jK55Gyd3l/HA8arYirT+enbcg== X-Google-Smtp-Source: ABdhPJyx4QRryek08Cq+VO1Q+Lml27YoGnXbuSsL03waia2mEANsF2Nfd7WrQrMxliI3brZLDkWZug== X-Received: by 2002:a63:5142:: with SMTP id r2mr4866878pgl.248.1613160235075; Fri, 12 Feb 2021 12:03:55 -0800 (PST) Original-Received: from localhost (cpe-75-82-162-230.socal.res.rr.com. [75.82.162.230]) by smtp.gmail.com with ESMTPSA id s18sm8503730pjr.14.2021.02.12.12.03.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 12 Feb 2021 12:03:54 -0800 (PST) X-Mailer: git-send-email 2.30.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::535; envelope-from=aconchillo@gmail.com; helo=mail-pg1-x535.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 12 Feb 2021 16:24:37 -0500 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.io gmane.lisp.guile.devel:20663 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). Signed-off-by: Aleix Conchillo FlaquƩ --- 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 2ad13f5a5..0878c1173 100644 --- a/doc/ref/api-data.texi +++ b/doc/ref/api-data.texi @@ -6354,6 +6354,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 0f1e6085e..f079f1f53 100644 --- a/libguile/vectors.c +++ b/libguile/vectors.c @@ -193,7 +193,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 41e2c8909..08fe19f49 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); @@ -47,6 +48,7 @@ SCM_API int scm_is_simple_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 vec, 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.30.0