-
Notifications
You must be signed in to change notification settings - Fork 26
/
vec.c
60 lines (48 loc) · 1.29 KB
/
vec.c
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
/* vec.c -- argv[] and envp[] vectors ($Revision: 1.1.1.1 $) */
#include "es.h"
#include "gc.h"
DefineTag(Vector, static);
extern Vector *mkvector(int n) {
int i;
Vector *v = gcalloc(offsetof(Vector, vector[n + 1]), &VectorTag);
v->alloclen = n;
v->count = 0;
for (i = 0; i <= n; i++)
v->vector[i] = NULL;
return v;
}
static void *VectorCopy(void *ov) {
size_t n = offsetof(Vector, vector[((Vector *) ov)->alloclen + 1]);
void *nv = gcalloc(n, &VectorTag);
memcpy(nv, ov, n);
return nv;
}
static size_t VectorScan(void *p) {
Vector *v = p;
int i, n = v->count;
for (i = 0; i <= n; i++)
v->vector[i] = forward(v->vector[i]);
return offsetof(Vector, vector[v->alloclen + 1]);
}
extern Vector *vectorize(List *list) {
int i, n = length(list);
Ref(Vector *, v, NULL);
Ref(List *, lp, list);
v = mkvector(n);
v->count = n;
for (i = 0; lp != NULL; lp = lp->next, i++) {
char *s = getstr(lp->term); /* must evaluate before v->vector[i] */
v->vector[i] = s;
}
RefEnd(lp);
RefReturn(v);
}
/* qstrcmp -- a strcmp wrapper for qsort */
extern int qstrcmp(const void *s1, const void *s2) {
return strcmp(*(const char **)s1, *(const char **)s2);
}
/* sortvector */
extern void sortvector(Vector *v) {
assert(v->vector[v->count] == NULL);
qsort(v->vector, v->count, sizeof (char *), qstrcmp);
}