list? for circular lists
pic_list_p() detects circular lists using Floyd's cycle-finding algorithm.
This commit is contained in:
parent
e35820da54
commit
138ac70130
20
src/pair.c
20
src/pair.c
|
@ -49,12 +49,24 @@ bool
|
||||||
pic_list_p(pic_state *pic, pic_value obj)
|
pic_list_p(pic_state *pic, pic_value obj)
|
||||||
{
|
{
|
||||||
UNUSED(pic);
|
UNUSED(pic);
|
||||||
|
pic_value twice=obj;
|
||||||
|
|
||||||
while (pic_pair_p(obj)) {
|
while (1) {
|
||||||
obj = pic_pair_ptr(obj)->cdr;
|
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
|
pic_value
|
||||||
|
|
Loading…
Reference in New Issue