]>
Commit | Line | Data |
---|---|---|
6385fc5f NG |
1 | /* |
2 | * Copyright (C) the libgit2 contributors. All rights reserved. | |
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 | ||
8 | #include "strmap.h" | |
9 | ||
ac3d33df JK |
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 | |
15 | #include "khash.h" | |
16 | ||
17 | __KHASH_TYPE(str, const char *, void *) | |
18 | ||
13c3bc9a | 19 | __KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal) |
a13cfd2a | 20 | |
22a2d3d5 | 21 | int git_strmap_new(git_strmap **out) |
a13cfd2a | 22 | { |
22a2d3d5 UG |
23 | *out = kh_init(str); |
24 | GIT_ERROR_CHECK_ALLOC(*out); | |
a13cfd2a PS |
25 | |
26 | return 0; | |
27 | } | |
28 | ||
94af9155 | 29 | void git_strmap_free(git_strmap *map) |
a13cfd2a PS |
30 | { |
31 | kh_destroy(str, map); | |
32 | } | |
33 | ||
34 | void git_strmap_clear(git_strmap *map) | |
35 | { | |
36 | kh_clear(str, map); | |
37 | } | |
38 | ||
22a2d3d5 | 39 | size_t git_strmap_size(git_strmap *map) |
a13cfd2a PS |
40 | { |
41 | return kh_size(map); | |
42 | } | |
43 | ||
22a2d3d5 | 44 | void *git_strmap_get(git_strmap *map, const char *key) |
a13cfd2a | 45 | { |
22a2d3d5 UG |
46 | size_t idx = kh_get(str, map, key); |
47 | if (idx == kh_end(map) || !kh_exist(map, idx)) | |
48 | return NULL; | |
a13cfd2a PS |
49 | return kh_val(map, idx); |
50 | } | |
51 | ||
22a2d3d5 | 52 | int git_strmap_set(git_strmap *map, const char *key, void *value) |
a13cfd2a | 53 | { |
22a2d3d5 UG |
54 | size_t idx; |
55 | int rval; | |
a13cfd2a | 56 | |
22a2d3d5 UG |
57 | idx = kh_put(str, map, key, &rval); |
58 | if (rval < 0) | |
59 | return -1; | |
a13cfd2a | 60 | |
22a2d3d5 UG |
61 | if (rval == 0) |
62 | kh_key(map, idx) = key; | |
a13cfd2a | 63 | |
22a2d3d5 | 64 | kh_val(map, idx) = value; |
a13cfd2a | 65 | |
22a2d3d5 | 66 | return 0; |
a13cfd2a PS |
67 | } |
68 | ||
22a2d3d5 | 69 | int git_strmap_delete(git_strmap *map, const char *key) |
ac3d33df | 70 | { |
22a2d3d5 UG |
71 | khiter_t idx = kh_get(str, map, key); |
72 | if (idx == kh_end(map)) | |
73 | return GIT_ENOTFOUND; | |
74 | kh_del(str, map, idx); | |
ac3d33df JK |
75 | return 0; |
76 | } | |
77 | ||
22a2d3d5 | 78 | int git_strmap_exists(git_strmap *map, const char *key) |
ac3d33df | 79 | { |
22a2d3d5 | 80 | return kh_get(str, map, key) != kh_end(map); |
ac3d33df JK |
81 | } |
82 | ||
22a2d3d5 | 83 | int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key) |
6385fc5f | 84 | { |
22a2d3d5 | 85 | size_t i = *iter; |
6385fc5f | 86 | |
22a2d3d5 UG |
87 | while (i < map->n_buckets && !kh_exist(map, i)) |
88 | i++; | |
6385fc5f | 89 | |
22a2d3d5 UG |
90 | if (i >= map->n_buckets) |
91 | return GIT_ITEROVER; | |
6385fc5f | 92 | |
22a2d3d5 UG |
93 | if (key) |
94 | *key = kh_key(map, i); | |
95 | if (value) | |
96 | *value = kh_val(map, i); | |
97 | *iter = ++i; | |
6385fc5f | 98 | |
22a2d3d5 | 99 | return 0; |
6385fc5f | 100 | } |