aoc-2019-c

Advent of Code 2019 Solutions in C
git clone https://git.sinitax.com/sinitax/aoc-2019-c
Log | Files | Refs | README | sfeed.txt

dvec_s.h (958B)


      1#include "dvec.h"
      2#include "util.h"
      3
      4#include <assert.h>
      5#include <unistd.h>
      6
      7static inline void *
      8dvec_add_slots(struct dvec *dvec, size_t count)
      9{
     10	assert(!dvec_add(dvec, dvec->len, count));
     11
     12	return dvec->data + (dvec->len - count) * dvec->dsize;
     13}
     14
     15static inline void *
     16dvec_add_slot(struct dvec *dvec)
     17{
     18	return dvec_add_slots(dvec, 1);
     19}
     20
     21static inline void
     22dvec_rm_slots(struct dvec *dvec, void *slot, size_t count)
     23{
     24	dvec_rm(dvec, ((size_t) (slot - dvec->data)) / dvec->dsize, count);
     25}
     26
     27static inline void
     28dvec_rm_slot(struct dvec *dvec, void *slot)
     29{
     30	dvec_rm_slots(dvec, slot, 1);
     31}
     32
     33static inline ssize_t
     34dvec_sprintf(struct dvec *dvec, const char *fmtstr, ...)
     35{
     36	va_list ap, cpy;
     37	ssize_t n;
     38
     39	va_copy(cpy, ap);
     40
     41	va_start(cpy, fmtstr);
     42	n = vsnprintf(NULL, 0, fmtstr, cpy);
     43	assert(n >= 0);
     44	va_end(cpy);
     45
     46	va_start(ap, fmtstr);
     47	dvec_add_slots(dvec, (size_t) n + 1);
     48	vsnprintf(dvec->data, dvec->len, fmtstr, ap);
     49	va_end(ap);
     50
     51	return n;
     52}