libhmap-c

C hashmap library
git clone https://git.sinitax.com/sinitax/libhmap-c
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

test.c (821B)


      1#include "hmap.h"
      2
      3#include <err.h>
      4#include <stdio.h>
      5#include <string.h>
      6#include <stdlib.h>
      7
      8#define LIBHMAP_ERR(rc) errx(1, "libhmap: %s", \
      9	rc < 0 ? strerror(-rc) : hmap_err[rc])
     10
     11static const char *hmap_err[] = {
     12	HMAP_STRERR_INIT
     13};
     14
     15int
     16main(int argc, const char **argv)
     17{
     18	struct hmap hmap;
     19	struct hmap_iter iter;
     20	char *arg;
     21	int i, rc;
     22
     23	rc = hmap_init(&hmap, 10, hmap_str_hash,
     24		hmap_str_keycmp, &stdlib_heap_allocator);
     25	if (rc) LIBHMAP_ERR(rc);
     26
     27	for (i = 1; i < argc; i++) {
     28		arg = strdup(argv[i]);
     29		rc = hmap_add(&hmap, (struct hmap_key) {.p = arg},
     30			(struct hmap_val) {.i = i});
     31		if (rc) LIBHMAP_ERR(rc);
     32	}
     33
     34	for (HMAP_ITER(&hmap, iter)) {
     35		printf("%s: %i\n", (char *)iter.link->key.p,
     36			iter.link->value.i);
     37	}
     38
     39	for (HMAP_ITER(&hmap, iter))
     40		free(iter.link->key._p);
     41	hmap_deinit(&hmap);
     42}