tabular

Flexible input tabulator
git clone https://git.sinitax.com/sinitax/tabular
Log | Files | Refs | Submodules | sfeed.txt

hmap.h (2979B)


      1#pragma once
      2
      3#include "allocator.h"
      4
      5#include <stdint.h>
      6#include <stdbool.h>
      7#include <sys/types.h>
      8
      9#define HMAP_ITER(map, iter) \
     10	hmap_iter_init(&(iter)); hmap_iter_next(map, &(iter));
     11
     12#define HMAP_STRERR_INIT \
     13	[HMAP_OK] = "Success", \
     14	[HMAP_KEY_EXISTS] = "Key exists", \
     15	[HMAP_KEY_MISSING] = "Key missing"
     16
     17#ifdef LIBHMAP_ASSERT_ARGS
     18#define LIBHMAP_ABORT_ON_ARGS(cond) do { if (cond) abort(); } while (0)
     19#else
     20#define LIBHMAP_ABORT_ON_ARGS(cond)
     21#endif
     22
     23#ifdef LIBHMAP_ASSERT_ALLOC
     24#define LIBHMAP_ABORT_ON_ALLOC(cond) do { if (cond) abort(); } while (0)
     25#else
     26#define LIBHMAP_ABORT_ON_ALLOC(cond)
     27#endif
     28
     29struct hmap_key;
     30
     31typedef uint32_t (*hmap_hash_func)(struct hmap_key key);
     32typedef bool (*hmap_keycmp_func)(struct hmap_key k1, struct hmap_key k2);
     33
     34enum {
     35	HMAP_OK = 0,
     36	HMAP_KEY_EXISTS,
     37	HMAP_KEY_MISSING,
     38};
     39
     40struct hmap_key {
     41	union {
     42		bool b;
     43		char c;
     44		float f;
     45		double d;
     46		int i;
     47		unsigned int u;
     48		size_t s;
     49		ssize_t ss;
     50		const void *p;
     51		void *_p;
     52	};
     53};
     54
     55struct hmap_val {
     56	union {
     57		bool b;
     58		char c;
     59		float f;
     60		double d;
     61		int i;
     62		unsigned int u;
     63		size_t s;
     64		ssize_t ss;
     65		const void *p;
     66		void *_p;
     67	};
     68};
     69
     70struct hmap_link {
     71	struct hmap_key key;
     72	struct hmap_val value;
     73	struct hmap_link *next;
     74};
     75
     76struct hmap_iter {
     77	struct hmap_link *link;
     78	size_t bucket;
     79};
     80
     81struct hmap {
     82	hmap_hash_func hash;
     83	hmap_keycmp_func keycmp;
     84	struct hmap_link **buckets;
     85	size_t bucketcnt;
     86	const struct allocator *allocator;
     87};
     88
     89int hmap_init(struct hmap *map, size_t buckets, hmap_hash_func hasher,
     90	hmap_keycmp_func keycmp, const struct allocator *allocator);
     91int hmap_deinit(struct hmap *map);
     92
     93struct hmap *hmap_alloc(size_t buckets, hmap_hash_func hasher,
     94	hmap_keycmp_func keycmp, const struct allocator *allocator, int *rc);
     95int hmap_free(struct hmap *map);
     96
     97int hmap_copy(const struct hmap *dst, const struct hmap *src);
     98void hmap_swap(struct hmap *m1, struct hmap *m2);
     99void hmap_clear(const struct hmap *map);
    100
    101struct hmap_link **hmap_link_get(const struct hmap *map, struct hmap_key key);
    102struct hmap_link **hmap_link_pos(const struct hmap *map, struct hmap_key key);
    103struct hmap_link *hmap_link_pop(const struct hmap *map, struct hmap_link **linkp);
    104struct hmap_link *hmap_link_alloc(const struct hmap *map,
    105	struct hmap_key key, struct hmap_val value, int *rc);
    106
    107struct hmap_link *hmap_get(const struct hmap *map, struct hmap_key key);
    108int hmap_set(const struct hmap *map, struct hmap_key key, struct hmap_val value);
    109int hmap_rm(const struct hmap *map, struct hmap_key key);
    110int hmap_add(const struct hmap *map, struct hmap_key key, struct hmap_val value);
    111
    112void hmap_iter_init(struct hmap_iter *iter);
    113bool hmap_iter_next(const struct hmap *map, struct hmap_iter *iter);
    114static inline bool hmap_iter_done(const struct hmap_iter *iter);
    115
    116uint32_t hmap_str_hash(struct hmap_key key);
    117bool hmap_str_keycmp(struct hmap_key k1, struct hmap_key k2);
    118
    119static inline bool
    120hmap_iter_done(const struct hmap_iter *iter)
    121{
    122	return !iter->link;
    123}