1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-04 22:40:25 +02:00

Require vector argument to scm_vector_elements, scm_vector_writable_elements

* libguile/vectors.c (scm_vector_elements, scm_vector_writable_elements): As
  stated.
* libguile/sort.c: Fix usage of scm_vector_elements on possibly non-vector
  array.
* doc/ref/api-data.texi (scm_vector_elements): Remove mention of non-vector
  arrays.
This commit is contained in:
Daniel Llorens 2020-02-03 13:04:13 +01:00
parent fa19f702f6
commit 996bbb47f2
3 changed files with 32 additions and 26 deletions

View file

@ -6463,10 +6463,7 @@ handle must eventually be released with
The variables pointed to by @var{lenp} and @var{incp} are filled with The variables pointed to by @var{lenp} and @var{incp} are filled with
the number of elements of the vector and the increment (number of the number of elements of the vector and the increment (number of
elements) between successive elements, respectively. Successive elements) between successive elements, respectively.
elements of @var{vec} need not be contiguous in their underlying
``root vector'' returned here; hence the increment is not necessarily
equal to 1 and may well be negative too (@pxref{Shared Arrays}).
The following example shows the typical way to use this function. It The following example shows the typical way to use this function. It
creates a list of all elements of @var{vec} (in reverse order). creates a list of all elements of @var{vec} (in reverse order).

View file

@ -562,22 +562,23 @@ SCM_DEFINE (scm_stable_sort_x, "stable-sort!", 2, 0, 0,
} }
else if (scm_is_array (items) && 1 == scm_c_array_rank (items)) else if (scm_is_array (items) && 1 == scm_c_array_rank (items))
{ {
scm_t_array_handle temp_handle, vec_handle; scm_t_array_handle vec_handle;
SCM temp, *temp_elts, *vec_elts; scm_array_get_handle (items, &vec_handle);
size_t len;
ssize_t inc; SCM *vec_elts = scm_array_handle_writable_elements (&vec_handle);
scm_t_array_dim *vec_dim = scm_array_handle_dims (&vec_handle);
size_t len = vec_dim->ubnd + 1 - vec_dim->lbnd;
ssize_t inc = vec_dim->inc;
vec_elts = scm_vector_writable_elements (items, &vec_handle,
&len, &inc);
if (len == 0) if (len == 0)
{ {
scm_array_handle_release (&vec_handle); scm_array_handle_release (&vec_handle);
return items; return items;
} }
temp = scm_c_make_vector (len, SCM_UNDEFINED); SCM temp = scm_c_make_vector (len, SCM_UNDEFINED);
temp_elts = scm_vector_writable_elements (temp, &temp_handle, scm_t_array_handle temp_handle;
NULL, NULL); SCM *temp_elts = scm_vector_writable_elements (temp, &temp_handle, NULL, NULL);
scm_merge_vector_step (vec_elts, temp_elts, less, 0, len-1, inc); scm_merge_vector_step (vec_elts, temp_elts, less, 0, len-1, inc);

View file

@ -64,34 +64,42 @@ scm_is_simple_vector (SCM obj)
const SCM * const SCM *
scm_vector_elements (SCM vec, scm_t_array_handle *h, scm_vector_elements (SCM vec, scm_t_array_handle *h,
size_t *lenp, ssize_t *incp) size_t *lenp, ssize_t *incp)
#define FUNC_NAME "scm_vector_elements"
{ {
SCM_VALIDATE_VECTOR (1, vec);
scm_array_get_handle (vec, h); scm_array_get_handle (vec, h);
if (1 != scm_array_handle_rank (h))
{
scm_array_handle_release (h);
scm_wrong_type_arg_msg (NULL, 0, vec, "rank 1 array of Scheme values");
}
if (lenp) if (lenp)
{ {
scm_t_array_dim *dim = scm_array_handle_dims (h); scm_t_array_dim *dim = scm_array_handle_dims (h);
*lenp = dim->ubnd - dim->lbnd + 1; *lenp = dim->ubnd - dim->lbnd + 1;
*incp = dim->inc;
} }
if (incp)
*incp = 1;
return scm_array_handle_elements (h); return scm_array_handle_elements (h);
} }
#undef FUNC_NAME
SCM * SCM *
scm_vector_writable_elements (SCM vec, scm_t_array_handle *h, scm_vector_writable_elements (SCM vec, scm_t_array_handle *h,
size_t *lenp, ssize_t *incp) size_t *lenp, ssize_t *incp)
#define FUNC_NAME "scm_vector_writable_elements"
{ {
const SCM *ret = scm_vector_elements (vec, h, lenp, incp); SCM_VALIDATE_MUTABLE_VECTOR (1, vec);
scm_array_get_handle (vec, h);
if (h->writable_elements != h->elements) if (lenp)
scm_wrong_type_arg_msg (NULL, 0, vec, "mutable vector"); {
scm_t_array_dim *dim = scm_array_handle_dims (h);
*lenp = dim->ubnd - dim->lbnd + 1;
}
if (incp)
*incp = 1;
return (SCM *) ret; return scm_array_handle_writable_elements (h);
} }
#undef FUNC_NAME
SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0, SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
(SCM obj), (SCM obj),