#include "allocator.h" #include "dvec.h" #include #include #include #include #include #define LIBDVEC_ERR(rc) errx(1, "libdvec: %s", \ rc < 0 ? strerror(-rc) : dvec_err[rc]) static const char *dvec_err[] = { DVEC_STRERR_INIT }; bool str_sort_order(const void *p1, const void *p2, void *user) { const char *s1 = *(const char **)p1; const char *s2 = *(const char **)p2; return strcmp(s1, s2) <= 0; } int str_search(const void *p, void *user) { const char *str = *(const char **)p, *cmp = user; return strcmp(cmp, str); } int main(int argc, const char **argv) { struct dvec dvec, *fused; ssize_t below, above, on; const char **val, *tmp; int i, rc; rc = dvec_init(&dvec, sizeof(const char *), 16, &stdlib_heap_allocator); if (rc) LIBDVEC_ERR(rc); for (i = 1; i < argc; i++) { rc = dvec_add_back(&dvec, 1); if (rc) LIBDVEC_ERR(rc); val = dvec_back(&dvec); *val = argv[i]; } rc = dvec_quick_sort_ex(&dvec, &stdlib_heap_allocator, &tmp, false, str_sort_order, NULL); if (rc) LIBDVEC_ERR(rc); on = dvec_binary_search(&dvec, str_search, "abc", &below, &above); for (DVEC_ITER(&dvec, val)) printf("%s\n", *val); printf("dvec len: %lu\n", dvec.len); printf("\n"); printf("below: %li\n", below); printf("on: %li\n", on); printf("above: %li\n", above); printf("\n"); fused = dvec_alloc_fused(sizeof(const char *), 0, &stdlib_heap_allocator, &rc); if (!fused) LIBDVEC_ERR(rc); printf("fused: %p %p (+%li)\n", (void *) fused, fused->data, fused->data - (uint8_t *) fused); rc = dvec_free(fused); if (rc) LIBDVEC_ERR(rc); dvec_deinit(&dvec); }