view help2html/uintarray.c @ 8:ec2b657edf13

Add explicit lint-comment-style fallthrough annotations. GCC now assumes that if you don't have these you're making a mistake, which is annoying. XXX: This changeset updates the AG output files only (by hand) and is XXX: abusive - rebuilding them will erase the change. However, I need XXX: to get things to build before I can try to get AG to issue the XXX: annotations itself, so this seems like a reasonable expedient.
author David A. Holland
date Mon, 30 May 2022 23:51:43 -0400
parents 13d2b8934445
children 60b08b68c750
line wrap: on
line source

#include <stdlib.h>
#include "must.h"
#include "uintarray.h"

////////////////////////////////////////////////////////////

void uintarray_init(struct uintarray *a) {
   a->v = NULL;
   a->num = a->max = 0;
}

struct uintarray *uintarray_create(void) {
   struct uintarray *a;
   a = must_malloc(sizeof(*a));
   uintarray_init(a);
   return a;
}

void uintarray_cleanup(struct uintarray *a) {
   free(a->v);
   a->v = NULL;
   a->num = a->max = 0;
}

void uintarray_destroy(struct uintarray *a) {
   uintarray_cleanup(a);
   free(a);
}

////////////////////////////////////////////////////////////

/*
 * I wish there were a way to check that these were the same as the
 * inline versions, but apparently allowing such things to diverge is
 * a feature of C99. Bleh.
 */

unsigned uintarray_num(const struct uintarray *a) {
   return a->num;
}

unsigned uintarray_get(const struct uintarray *a, unsigned ix) {
   assert(ix < a->num);
   return a->v[ix];
}

void uintarray_set(struct uintarray *a, unsigned ix, unsigned val) {
   assert(ix < a->num);
   a->v[ix] = val;
}

////////////////////////////////////////////////////////////

void uintarray_add(struct uintarray *a, unsigned val) {
   unsigned ix;

   ix = a->num;
   uintarray_setsize(a, a->num+1);
   a->v[ix] = val;
}

void uintarray_setsize(struct uintarray *a, unsigned newnum) {
   unsigned newmax;
   unsigned *newptr;

   if (newnum > a->max) {
      newmax = a->max;
      if (newmax == 0) {
	 newmax = 4;
      }
      while (newmax < newnum) {
	 newmax *= 2;
      }
      newptr = must_realloc(a->v, newmax*sizeof(unsigned));
      a->v = newptr;
      a->max = newmax;
   }

   a->num = newnum;

   /* Note: it's the user's responsibility to initialize the new space */
}

static int (*uintarray_usersort)(unsigned a, unsigned b);

static int uintarray_sortthunk(const void *av, const void *bv) {
   /* av and bv point to elements of the uintarray */
   unsigned a = *(const unsigned *)av;
   unsigned b = *(const unsigned *)bv;
   return uintarray_usersort(a, b);
}

void uintarray_sort(struct uintarray *a, int (*f)(unsigned a, unsigned b)) {
   uintarray_usersort = f;
   qsort(a->v, a->num, sizeof(unsigned), uintarray_sortthunk);
}