Sun, 27 Feb 2005 00:09:12 +0100
Added routinesn for generic iterables.
Makefile | file | annotate | diff | comparison | revisions | |
iterable.c | file | annotate | diff | comparison | revisions | |
iterable.h | file | annotate | diff | comparison | revisions | |
objlist.h | file | annotate | diff | comparison | revisions | |
ptrlist.h | file | annotate | diff | comparison | revisions |
--- 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
--- /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; + } +} +
--- /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 */
--- 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);
--- 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))