From 76193d7cb0457807b2f0b95f909ab5de19480cd7 Mon Sep 17 00:00:00 2001 From: rsc Date: Tue, 30 Sep 2003 17:47:42 +0000 Subject: Initial revision --- src/cmd/sam/list.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/cmd/sam/list.c (limited to 'src/cmd/sam/list.c') diff --git a/src/cmd/sam/list.c b/src/cmd/sam/list.c new file mode 100644 index 00000000..a8105425 --- /dev/null +++ b/src/cmd/sam/list.c @@ -0,0 +1,47 @@ +#include "sam.h" + +/* + * Check that list has room for one more element. + */ +void +growlist(List *l) +{ + if(l->listptr==0 || l->nalloc==0){ + l->nalloc = INCR; + l->listptr = emalloc(INCR*sizeof(long)); + l->nused = 0; + }else if(l->nused == l->nalloc){ + l->listptr = erealloc(l->listptr, (l->nalloc+INCR)*sizeof(long)); + memset((void*)(l->longptr+l->nalloc), 0, INCR*sizeof(long)); + l->nalloc += INCR; + } +} + +/* + * Remove the ith element from the list + */ +void +dellist(List *l, int i) +{ + memmove(&l->longptr[i], &l->longptr[i+1], (l->nused-(i+1))*sizeof(long)); + l->nused--; +} + +/* + * Add a new element, whose position is i, to the list + */ +void +inslist(List *l, int i, long val) +{ + growlist(l); + memmove(&l->longptr[i+1], &l->longptr[i], (l->nused-i)*sizeof(long)); + l->longptr[i] = val; + l->nused++; +} + +void +listfree(List *l) +{ + free(l->listptr); + free(l); +} -- cgit v1.2.3