view help2html/support.c @ 9:60b08b68c750

Switch to static inline as an expedient build fix. Should probably set this up with working C99 inline but for the moment I don't have the energy.
author David A. Holland
date Mon, 30 May 2022 23:56:45 -0400
parents 60d3ca9d3f6b
children
line wrap: on
line source

#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);
}