From 138ac70130b55a0a97dce3ad742896f675bb1ddc Mon Sep 17 00:00:00 2001 From: koba-e964 Date: Sun, 16 Mar 2014 01:38:12 +0900 Subject: [PATCH] list? for circular lists pic_list_p() detects circular lists using Floyd's cycle-finding algorithm. --- src/pair.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/pair.c b/src/pair.c index 1b993722..2a24583e 100644 --- a/src/pair.c +++ b/src/pair.c @@ -49,12 +49,24 @@ bool pic_list_p(pic_state *pic, pic_value obj) { UNUSED(pic); + pic_value twice=obj; - while (pic_pair_p(obj)) { + while (1) { + int i; + /* variable twice are scanned twice as fast as obj */ + for(i=0;i<2;i++){ + if(pic_pair_p(twice)){ + twice=pic_pair_ptr(twice)->cdr; + } + else{ + return pic_nil_p(twice); + } + } obj = pic_pair_ptr(obj)->cdr; + if(pic_eq_p(obj,twice)){ /* circular list */ + return false; + } } - - return pic_nil_p(obj); } pic_value