Skip to content

Commit a13cfd2

Browse files
committed
strmap: convert macros to functions
1 parent f14f75d commit a13cfd2

File tree

2 files changed

+112
-27
lines changed

2 files changed

+112
-27
lines changed

src/strmap.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,101 @@
77

88
#include "strmap.h"
99

10+
GIT__USE_STRMAP
11+
12+
int git_strmap_alloc(git_strmap **map)
13+
{
14+
if ((*map = kh_init(str)) == NULL) {
15+
giterr_set_oom();
16+
return -1;
17+
}
18+
19+
return 0;
20+
}
21+
22+
void git_strmap__free(git_strmap *map)
23+
{
24+
kh_destroy(str, map);
25+
}
26+
27+
void git_strmap_clear(git_strmap *map)
28+
{
29+
kh_clear(str, map);
30+
}
31+
32+
size_t git_strmap_num_entries(git_strmap *map)
33+
{
34+
return kh_size(map);
35+
}
36+
37+
size_t git_strmap_lookup_index(git_strmap *map, const char *key)
38+
{
39+
return kh_get(str, map, key);
40+
}
41+
42+
int git_strmap_valid_index(git_strmap *map, size_t idx)
43+
{
44+
return idx != kh_end(map);
45+
}
46+
47+
int git_strmap_exists(git_strmap *map, const char *key)
48+
{
49+
return kh_get(str, map, key) != kh_end(map);
50+
}
51+
52+
int git_strmap_has_data(git_strmap *map, size_t idx)
53+
{
54+
return kh_exist(map, idx);
55+
}
56+
57+
const char *git_strmap_key(git_strmap *map, size_t idx)
58+
{
59+
return kh_key(map, idx);
60+
}
61+
62+
void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key)
63+
{
64+
kh_val(map, idx) = key;
65+
}
66+
67+
void *git_strmap_value_at(git_strmap *map, size_t idx)
68+
{
69+
return kh_val(map, idx);
70+
}
71+
72+
void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value)
73+
{
74+
kh_val(map, idx) = value;
75+
}
76+
77+
void git_strmap_delete_at(git_strmap *map, size_t idx)
78+
{
79+
kh_del(str, map, idx);
80+
}
81+
82+
int git_strmap_put(git_strmap *map, const char *key, int *err)
83+
{
84+
return kh_put(str, map, key, err);
85+
}
86+
87+
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
88+
{
89+
khiter_t idx = kh_put(str, map, key, rval);
90+
91+
if ((*rval) >= 0) {
92+
if ((*rval) == 0)
93+
kh_key(map, idx) = key;
94+
kh_val(map, idx) = value;
95+
}
96+
}
97+
98+
void git_strmap_delete(git_strmap *map, const char *key)
99+
{
100+
khiter_t idx = git_strmap_lookup_index(map, key);
101+
if (git_strmap_valid_index(map, idx))
102+
git_strmap_delete_at(map, idx);
103+
}
104+
10105
int git_strmap_next(
11106
void **data,
12107
git_strmap_iter* iter,

src/strmap.h

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,29 @@ typedef khiter_t git_strmap_iter;
2323
#define GIT__USE_STRMAP \
2424
__KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
2525

26-
#define git_strmap_alloc(hp) \
27-
((*(hp) = kh_init(str)) == NULL) ? giterr_set_oom(), -1 : 0
26+
int git_strmap_alloc(git_strmap **map);
2827

29-
#define git_strmap_free(h) kh_destroy(str, h), h = NULL
30-
#define git_strmap_clear(h) kh_clear(str, h)
28+
#define git_strmap_free(h) git_strmap__free(h); (h) = NULL
29+
void git_strmap__free(git_strmap *map);
30+
void git_strmap_clear(git_strmap *map);
3131

32-
#define git_strmap_num_entries(h) kh_size(h)
32+
size_t git_strmap_num_entries(git_strmap *map);
3333

34-
#define git_strmap_lookup_index(h, k) kh_get(str, h, k)
35-
#define git_strmap_valid_index(h, idx) (idx != kh_end(h))
34+
size_t git_strmap_lookup_index(git_strmap *map, const char *key);
35+
int git_strmap_valid_index(git_strmap *map, size_t idx);
3636

37-
#define git_strmap_exists(h, k) (kh_get(str, h, k) != kh_end(h))
38-
#define git_strmap_has_data(h, idx) kh_exist(h, idx)
37+
int git_strmap_exists(git_strmap *map, const char *key);
38+
int git_strmap_has_data(git_strmap *map, size_t idx);
3939

40-
#define git_strmap_key(h, idx) kh_key(h, idx)
41-
#define git_strmap_set_key_at(h, idx, k) kh_val(h, idx) = k
42-
#define git_strmap_value_at(h, idx) kh_val(h, idx)
43-
#define git_strmap_set_value_at(h, idx, v) kh_val(h, idx) = v
44-
#define git_strmap_delete_at(h, idx) kh_del(str, h, idx)
40+
const char *git_strmap_key(git_strmap *map, size_t idx);
41+
void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key);
42+
void *git_strmap_value_at(git_strmap *map, size_t idx);
43+
void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value);
44+
void git_strmap_delete_at(git_strmap *map, size_t idx);
4545

46-
#define git_strmap_put(h, k, err) kh_put(str, h, k, err)
47-
48-
#define git_strmap_insert(h, key, val, rval) do { \
49-
khiter_t __pos = kh_put(str, h, key, rval); \
50-
if ((*rval) >= 0) { \
51-
if ((*rval) == 0) kh_key(h, __pos) = key; \
52-
kh_val(h, __pos) = val; \
53-
} } while (0)
54-
55-
#define git_strmap_delete(h, key) do { \
56-
khiter_t __pos = git_strmap_lookup_index(h, key); \
57-
if (git_strmap_valid_index(h, __pos)) \
58-
git_strmap_delete_at(h, __pos); } while (0)
46+
int git_strmap_put(git_strmap *map, const char *key, int *err);
47+
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval);
48+
void git_strmap_delete(git_strmap *map, const char *key);
5949

6050
#define git_strmap_foreach kh_foreach
6151
#define git_strmap_foreach_value kh_foreach_value

0 commit comments

Comments
 (0)