]>
git.proxmox.com Git - libgit2.git/blob - src/strmap.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #define kmalloc git__malloc
11 #define kcalloc git__calloc
12 #define krealloc git__realloc
13 #define kreallocarray git__reallocarray
14 #define kfree git__free
17 __KHASH_TYPE(str
, const char *, void *)
19 __KHASH_IMPL(str
, static kh_inline
, const char *, void *, 1, kh_str_hash_func
, kh_str_hash_equal
)
21 int git_strmap_new(git_strmap
**out
)
24 GIT_ERROR_CHECK_ALLOC(*out
);
29 void git_strmap_free(git_strmap
*map
)
34 void git_strmap_clear(git_strmap
*map
)
39 size_t git_strmap_size(git_strmap
*map
)
44 void *git_strmap_get(git_strmap
*map
, const char *key
)
46 size_t idx
= kh_get(str
, map
, key
);
47 if (idx
== kh_end(map
) || !kh_exist(map
, idx
))
49 return kh_val(map
, idx
);
52 int git_strmap_set(git_strmap
*map
, const char *key
, void *value
)
57 idx
= kh_put(str
, map
, key
, &rval
);
62 kh_key(map
, idx
) = key
;
64 kh_val(map
, idx
) = value
;
69 int git_strmap_delete(git_strmap
*map
, const char *key
)
71 khiter_t idx
= kh_get(str
, map
, key
);
72 if (idx
== kh_end(map
))
74 kh_del(str
, map
, idx
);
78 int git_strmap_exists(git_strmap
*map
, const char *key
)
80 return kh_get(str
, map
, key
) != kh_end(map
);
83 int git_strmap_iterate(void **value
, git_strmap
*map
, size_t *iter
, const char **key
)
87 while (i
< map
->n_buckets
&& !kh_exist(map
, i
))
90 if (i
>= map
->n_buckets
)
94 *key
= kh_key(map
, i
);
96 *value
= kh_val(map
, i
);