1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include "sam.h"
/*
* Check that list has room for one more element.
*/
static void
growlist(List *l, int esize)
{
uchar *p;
if(l->listptr == nil || l->nalloc == 0){
l->nalloc = INCR;
l->listptr = emalloc(INCR*esize);
l->nused = 0;
}
else if(l->nused == l->nalloc){
p = erealloc(l->listptr, (l->nalloc+INCR)*esize);
l->listptr = p;
memset(p+l->nalloc*esize, 0, INCR*esize);
l->nalloc += INCR;
}
}
/*
* Remove the ith element from the list
*/
void
dellist(List *l, int i)
{
Posn *pp;
void **vpp;
l->nused--;
switch(l->type){
case 'P':
pp = l->posnptr+i;
memmove(pp, pp+1, (l->nused-i)*sizeof(*pp));
break;
case 'p':
vpp = l->voidpptr+i;
memmove(vpp, vpp+1, (l->nused-i)*sizeof(*vpp));
break;
}
}
/*
* Add a new element, whose position is i, to the list
*/
void
inslist(List *l, int i, ...)
{
Posn *pp;
void **vpp;
va_list list;
va_start(list, i);
switch(l->type){
case 'P':
growlist(l, sizeof(*pp));
pp = l->posnptr+i;
memmove(pp+1, pp, (l->nused-i)*sizeof(*pp));
*pp = va_arg(list, Posn);
break;
case 'p':
growlist(l, sizeof(*vpp));
vpp = l->voidpptr+i;
memmove(vpp+1, vpp, (l->nused-i)*sizeof(*vpp));
*vpp = va_arg(list, void*);
break;
}
va_end(list);
l->nused++;
}
void
listfree(List *l)
{
free(l->listptr);
free(l);
}
List*
listalloc(int type)
{
List *l;
l = emalloc(sizeof(List));
l->type = type;
l->nalloc = 0;
l->nused = 0;
return l;
}
|