]> git.proxmox.com Git - libgit2.git/blob - src/strmap.h
Merge pull request #968 from arrbee/diff-support-typechange
[libgit2.git] / src / strmap.h
1 /*
2 * Copyright (C) 2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_strmap_h__
8 #define INCLUDE_strmap_h__
9
10 #include "common.h"
11
12 #define kmalloc git__malloc
13 #define kcalloc git__calloc
14 #define krealloc git__realloc
15 #define kfree git__free
16 #include "khash.h"
17
18 __KHASH_TYPE(str, const char *, void *);
19 typedef khash_t(str) git_strmap;
20
21 #define GIT__USE_STRMAP \
22 __KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
23
24 #define git_strmap_alloc() kh_init(str)
25 #define git_strmap_free(h) kh_destroy(str, h), h = NULL
26 #define git_strmap_clear(h) kh_clear(str, h)
27
28 #define git_strmap_num_entries(h) kh_size(h)
29
30 #define git_strmap_lookup_index(h, k) kh_get(str, h, k)
31 #define git_strmap_valid_index(h, idx) (idx != kh_end(h))
32
33 #define git_strmap_exists(h, k) (kh_get(str, h, k) != kh_end(h))
34
35 #define git_strmap_value_at(h, idx) kh_val(h, idx)
36 #define git_strmap_set_value_at(h, idx, v) kh_val(h, idx) = v
37 #define git_strmap_delete_at(h, idx) kh_del(str, h, idx)
38
39 #define git_strmap_insert(h, key, val, rval) do { \
40 khiter_t __pos = kh_put(str, h, key, &rval); \
41 if (rval >= 0) { \
42 if (rval == 0) kh_key(h, __pos) = key; \
43 kh_val(h, __pos) = val; \
44 } } while (0)
45
46 #define git_strmap_insert2(h, key, val, oldv, rval) do { \
47 khiter_t __pos = kh_put(str, h, key, &rval); \
48 if (rval >= 0) { \
49 if (rval == 0) { \
50 oldv = kh_val(h, __pos); \
51 kh_key(h, __pos) = key; \
52 } else { oldv = NULL; } \
53 kh_val(h, __pos) = val; \
54 } } while (0)
55
56 #define git_strmap_delete(h, key) do { \
57 khiter_t __pos = git_strmap_lookup_index(h, key); \
58 if (git_strmap_valid_index(h, __pos)) \
59 git_strmap_delete_at(h, __pos); } while (0)
60
61 #define git_strmap_foreach kh_foreach
62 #define git_strmap_foreach_value kh_foreach_value
63
64 #endif