# HG changeset patch # User Tuomo Valkonen # Date 1109459352 -3600 # Node ID f48ffd2f22245294885e26a0395dca4e43df62b4 # Parent 5f72042eb91c1667452e511fe5cc1fef47696721 Added routinesn for generic iterables. diff -r 5f72042eb91c -r f48ffd2f2224 Makefile --- a/Makefile Sat Feb 26 23:10:51 2005 +0100 +++ b/Makefile Sun Feb 27 00:09:12 2005 +0100 @@ -12,7 +12,7 @@ SOURCES=misc.c output.c util.c optparser.c parser.c tokenizer.c \ map.c obj.c objlist.c errorlog.c ptrlist.c rb.c \ - stringstore.c + stringstore.c iterable.c ifdef LIBTU_NO_ERRMSG DEFINES += -DLIBTU_NO_ERRMSG diff -r 5f72042eb91c -r f48ffd2f2224 iterable.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/iterable.c Sun Feb 27 00:09:12 2005 +0100 @@ -0,0 +1,51 @@ +/* + * libtu/iterable.c + * + * Copyright (c) Tuomo Valkonen 2005. + * + * You may distribute and modify this library under the terms of either + * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. + */ + +#include "iterable.h" + + +void *iterable_nth(uint n, VoidIterator *iter, void *st) +{ + void *p; + + while(1){ + p=iter(st); + if(p==NULL || n==0) + break; + n--; + } + + return p; +} + + +bool iterable_is_on(void *p, VoidIterator *iter, void *st) +{ + while(1){ + void *p2=iter(st); + if(p2==NULL) + return FALSE; + if(p==p2) + return TRUE; + } +} + + +void *iterable_find(BoolFilter *f, void *fparam, + VoidIterator *iter, void *st) +{ + while(1){ + void *p=iter(st); + if(p==NULL) + return NULL; + if(f(p, fparam)) + return p; + } +} + diff -r 5f72042eb91c -r f48ffd2f2224 iterable.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/iterable.h Sun Feb 27 00:09:12 2005 +0100 @@ -0,0 +1,29 @@ +/* + * libtu/iterable.h + * + * Copyright (c) Tuomo Valkonen 2005. + * + * You may distribute and modify this library under the terms of either + * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. + */ + +#ifndef LIBTU_ITERABLE_H +#define LIBTU_ITERABLE_H + +#include "types.h" +#include "obj.h" + +typedef void *VoidIterator(void *); +typedef Obj *ObjIterator(void *); + +typedef bool BoolFilter(void *p, void *param); + +#define FOR_ALL_ITER(INIT, ITER, VAR, LL, TMP) \ + for(INIT(TMP, LL), (VAR)=ITER(TMP); (VAR)!=NULL; VAR=ITER(TMP)) + +extern void *iterable_nth(uint n, VoidIterator *iter, void *st); +extern bool iterable_is_on(void *p, VoidIterator *iter, void *st); +extern void *iterable_find(BoolFilter *f, void *fparam, + VoidIterator *iter, void *st); + +#endif /* LIBTU_ITERABLE_H */ diff -r 5f72042eb91c -r f48ffd2f2224 objlist.h --- a/objlist.h Sat Feb 26 23:10:51 2005 +0100 +++ b/objlist.h Sun Feb 27 00:09:12 2005 +0100 @@ -11,8 +11,8 @@ #define LIBTU_OBJLIST_H #include "types.h" +#include "iterable.h" #include "obj.h" -#include "ptrlist.h" INTRSTRUCT(ObjList); diff -r 5f72042eb91c -r f48ffd2f2224 ptrlist.h --- a/ptrlist.h Sat Feb 26 23:10:51 2005 +0100 +++ b/ptrlist.h Sun Feb 27 00:09:12 2005 +0100 @@ -11,7 +11,7 @@ #define LIBTU_PTRLIST_H #include "types.h" -#include "obj.h" +#include "iterable.h" INTRSTRUCT(PtrList); @@ -29,9 +29,6 @@ #define PTRLIST_LAST(TYPE, LL) ((LL)==NULL ? NULL : (TYPE)(LL)->prev->ptr) #define PTRLIST_EMPTY(LIST) ((LIST)==NULL) -#define FOR_ALL_ITER(INIT, ITER, VAR, LL, TMP) \ - for(INIT(TMP, LL), (VAR)=ITER(TMP); (VAR)!=NULL; VAR=ITER(TMP)) - #define FOR_ALL_ON_PTRLIST(TYPE, VAR, LL, TMP) \ FOR_ALL_ITER(ptrlist_iter_init, (TYPE)ptrlist_iter, VAR, LL, &(TMP))