mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-12 23:00:22 +02:00
Optimize scm_ilength and 'length+'.
* libguile/list.c (scm_ilength): Test for SCM_NULL_OR_NIL_P only after testing scm_is_pair. Conform to GNU coding standards. * libguile/srfi-1.c (scm_srfi1_length_plus): Ditto.
This commit is contained in:
parent
4afca1a066
commit
fc8a90043b
2 changed files with 31 additions and 22 deletions
|
@ -1,5 +1,5 @@
|
||||||
/* Copyright (C) 1995,1996,1997,2000,2001,2003,2004,2008,2009,2010,2011
|
/* Copyright (C) 1995-1997, 2000, 2001, 2003, 2004, 2008-2011,
|
||||||
* Free Software Foundation, Inc.
|
* 2014 Free Software Foundation, Inc.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public License
|
* modify it under the terms of the GNU Lesser General Public License
|
||||||
|
@ -179,24 +179,25 @@ SCM_DEFINE (scm_list_p, "list?", 1, 0, 0,
|
||||||
long" lists (i.e. lists with cycles in their cdrs), and returns -1
|
long" lists (i.e. lists with cycles in their cdrs), and returns -1
|
||||||
if it does find one. */
|
if it does find one. */
|
||||||
long
|
long
|
||||||
scm_ilength(SCM sx)
|
scm_ilength (SCM sx)
|
||||||
{
|
{
|
||||||
long i = 0;
|
long i = 0;
|
||||||
SCM tortoise = sx;
|
SCM tortoise = sx;
|
||||||
SCM hare = sx;
|
SCM hare = sx;
|
||||||
|
|
||||||
do {
|
do
|
||||||
if (SCM_NULL_OR_NIL_P(hare)) return i;
|
{
|
||||||
if (!scm_is_pair (hare)) return -1;
|
if (!scm_is_pair (hare))
|
||||||
hare = SCM_CDR(hare);
|
return SCM_NULL_OR_NIL_P (hare) ? i : -1;
|
||||||
i++;
|
hare = SCM_CDR (hare);
|
||||||
if (SCM_NULL_OR_NIL_P(hare)) return i;
|
i++;
|
||||||
if (!scm_is_pair (hare)) return -1;
|
if (!scm_is_pair (hare))
|
||||||
hare = SCM_CDR(hare);
|
return SCM_NULL_OR_NIL_P (hare) ? i : -1;
|
||||||
i++;
|
hare = SCM_CDR (hare);
|
||||||
/* For every two steps the hare takes, the tortoise takes one. */
|
i++;
|
||||||
tortoise = SCM_CDR(tortoise);
|
/* For every two steps the hare takes, the tortoise takes one. */
|
||||||
}
|
tortoise = SCM_CDR (tortoise);
|
||||||
|
}
|
||||||
while (!scm_is_eq (hare, tortoise));
|
while (!scm_is_eq (hare, tortoise));
|
||||||
|
|
||||||
/* If the tortoise ever catches the hare, then the list must contain
|
/* If the tortoise ever catches the hare, then the list must contain
|
||||||
|
|
|
@ -620,20 +620,28 @@ SCM_DEFINE (scm_srfi1_length_plus, "length+", 1, 0, 0,
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (SCM_NULL_OR_NIL_P (hare))
|
|
||||||
return scm_from_size_t (i);
|
|
||||||
if (!scm_is_pair (hare))
|
if (!scm_is_pair (hare))
|
||||||
scm_wrong_type_arg_msg (FUNC_NAME, 1, lst, "proper or circular list");
|
{
|
||||||
|
if (SCM_NULL_OR_NIL_P (hare))
|
||||||
|
return scm_from_size_t (i);
|
||||||
|
else
|
||||||
|
scm_wrong_type_arg_msg (FUNC_NAME, 1, lst,
|
||||||
|
"proper or circular list");
|
||||||
|
}
|
||||||
hare = SCM_CDR (hare);
|
hare = SCM_CDR (hare);
|
||||||
i++;
|
i++;
|
||||||
if (SCM_NULL_OR_NIL_P (hare))
|
|
||||||
return scm_from_size_t (i);
|
|
||||||
if (!scm_is_pair (hare))
|
if (!scm_is_pair (hare))
|
||||||
scm_wrong_type_arg_msg (FUNC_NAME, 1, lst, "proper or circular list");
|
{
|
||||||
|
if (SCM_NULL_OR_NIL_P (hare))
|
||||||
|
return scm_from_size_t (i);
|
||||||
|
else
|
||||||
|
scm_wrong_type_arg_msg (FUNC_NAME, 1, lst,
|
||||||
|
"proper or circular list");
|
||||||
|
}
|
||||||
hare = SCM_CDR (hare);
|
hare = SCM_CDR (hare);
|
||||||
i++;
|
i++;
|
||||||
/* For every two steps the hare takes, the tortoise takes one. */
|
/* For every two steps the hare takes, the tortoise takes one. */
|
||||||
tortoise = SCM_CDR(tortoise);
|
tortoise = SCM_CDR (tortoise);
|
||||||
}
|
}
|
||||||
while (!scm_is_eq (hare, tortoise));
|
while (!scm_is_eq (hare, tortoise));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue