comparison help2html/support.c @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children 60d3ca9d3f6b
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include <err.h>
5
6 #include "must.h"
7 #include "stringdict.h"
8 #include "support.h"
9
10 ////////////////////////////////////////////////////////////
11
12 void intstack_init(struct intstack *is) {
13 is->pos = 0;
14 }
15
16 void intstack_push(struct intstack *is, int val) {
17 if (is->pos >= INTSTACKSIZE) {
18 errx(1, "Paragraph stack overflow - increase INTSTACKSIZE and recompile");
19 }
20 is->vals[is->pos++] = val;
21 }
22
23 int intstack_pop(struct intstack *is) {
24 assert(is->pos > 0);
25 return is->vals[--is->pos];
26 }
27
28 int intstack_top(const struct intstack *is) {
29 assert(is->pos > 0);
30 return is->vals[is->pos-1];
31 }
32
33 ////////////////////////////////////////////////////////////
34
35 static const struct stringdict *sortdict;
36 static int sorter(const void *av, const void *bv) {
37 const char *as = stringdict_getbynum(sortdict, *(const unsigned *)av);
38 const char *bs = stringdict_getbynum(sortdict, *(const unsigned *)bv);
39 return strcmp(as, bs);
40 }
41
42 struct permutation *mySort(const struct stringdict *sd) {
43 sortdict = sd;
44
45 /* establish permutation vector */
46 int i, n = stringdict_count(sortdict);
47 int *pv = must_malloc(n*sizeof(int));
48 for (i=0; i<n; i++) {
49 pv[i] = i;
50 }
51
52 /* sort it */
53 qsort(pv, n, sizeof(int *), sorter);
54
55 sortdict = NULL;
56
57 /* return it */
58 struct permutation *p = must_malloc(sizeof(*p));
59 p->v = pv;
60 p->num = n;
61 return p;
62 }
63
64 void permutation_destroy(struct permutation *p) {
65 free(p->v);
66 free(p);
67 }