Mercurial > ~dholland > hg > ag > index.cgi
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/help2html/support.c Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,67 @@ +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <err.h> + +#include "must.h" +#include "stringdict.h" +#include "support.h" + +//////////////////////////////////////////////////////////// + +void intstack_init(struct intstack *is) { + is->pos = 0; +} + +void intstack_push(struct intstack *is, int val) { + if (is->pos >= INTSTACKSIZE) { + errx(1, "Paragraph stack overflow - increase INTSTACKSIZE and recompile"); + } + is->vals[is->pos++] = val; +} + +int intstack_pop(struct intstack *is) { + assert(is->pos > 0); + return is->vals[--is->pos]; +} + +int intstack_top(const struct intstack *is) { + assert(is->pos > 0); + return is->vals[is->pos-1]; +} + +//////////////////////////////////////////////////////////// + +static const struct stringdict *sortdict; +static int sorter(const void *av, const void *bv) { + const char *as = stringdict_getbynum(sortdict, *(const unsigned *)av); + const char *bs = stringdict_getbynum(sortdict, *(const unsigned *)bv); + return strcmp(as, bs); +} + +struct permutation *mySort(const struct stringdict *sd) { + sortdict = sd; + + /* establish permutation vector */ + int i, n = stringdict_count(sortdict); + int *pv = must_malloc(n*sizeof(int)); + for (i=0; i<n; i++) { + pv[i] = i; + } + + /* sort it */ + qsort(pv, n, sizeof(int *), sorter); + + sortdict = NULL; + + /* return it */ + struct permutation *p = must_malloc(sizeof(*p)); + p->v = pv; + p->num = n; + return p; +} + +void permutation_destroy(struct permutation *p) { + free(p->v); + free(p); +}