Skip to content

Commit cee9ca6

Browse files
committed
idxmap: convert to use functions instead of macros
1 parent 8f5fe90 commit cee9ca6

File tree

2 files changed

+143
-43
lines changed

2 files changed

+143
-43
lines changed

src/idxmap.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 "idxmap.h"
9+
10+
GIT__USE_IDXMAP
11+
GIT__USE_IDXMAP_ICASE
12+
13+
int git_idxmap_alloc(git_idxmap **map)
14+
{
15+
if ((*map = kh_init(idx)) == NULL) {
16+
giterr_set_oom();
17+
return -1;
18+
}
19+
20+
return 0;
21+
}
22+
23+
int git_idxmap_icase_alloc(git_idxmap_icase **map)
24+
{
25+
if ((*map = kh_init(idxicase)) == NULL) {
26+
giterr_set_oom();
27+
return -1;
28+
}
29+
30+
return 0;
31+
}
32+
33+
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval)
34+
{
35+
khiter_t idx = kh_put(idx, map, key, rval);
36+
37+
if ((*rval) >= 0) {
38+
if ((*rval) == 0)
39+
kh_key(map, idx) = key;
40+
kh_val(map, idx) = value;
41+
}
42+
}
43+
44+
void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval)
45+
{
46+
khiter_t idx = kh_put(idxicase, map, key, rval);
47+
48+
if ((*rval) >= 0) {
49+
if ((*rval) == 0)
50+
kh_key(map, idx) = key;
51+
kh_val(map, idx) = value;
52+
}
53+
}
54+
55+
size_t git_idxmap_lookup_index(git_idxmap *map, const git_index_entry *key)
56+
{
57+
return kh_get(idx, map, key);
58+
}
59+
60+
size_t git_idxmap_icase_lookup_index(git_idxmap_icase *map, const git_index_entry *key)
61+
{
62+
return kh_get(idxicase, map, key);
63+
}
64+
65+
void *git_idxmap_value_at(git_idxmap *map, size_t idx)
66+
{
67+
return kh_val(map, idx);
68+
}
69+
70+
int git_idxmap_valid_index(git_idxmap *map, size_t idx)
71+
{
72+
return idx != kh_end(map);
73+
}
74+
75+
int git_idxmap_has_data(git_idxmap *map, size_t idx)
76+
{
77+
return kh_exist(map, idx);
78+
}
79+
80+
void git_idxmap_resize(git_idxmap *map, size_t size)
81+
{
82+
kh_resize(idx, map, size);
83+
}
84+
85+
void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size)
86+
{
87+
kh_resize(idxicase, map, size);
88+
}
89+
90+
void git_idxmap__free(git_idxmap *map)
91+
{
92+
kh_destroy(idx, map);
93+
}
94+
95+
void git_idxmap_clear(git_idxmap *map)
96+
{
97+
kh_clear(idx, map);
98+
}
99+
100+
void git_idxmap_delete_at(git_idxmap *map, size_t idx)
101+
{
102+
kh_del(idx, map, idx);
103+
}
104+
105+
void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx)
106+
{
107+
kh_del(idxicase, map, idx);
108+
}
109+
110+
void git_idxmap_delete(git_idxmap *map, const git_index_entry *key)
111+
{
112+
khiter_t idx = git_idxmap_lookup_index(map, key);
113+
if (git_idxmap_valid_index(map, idx))
114+
git_idxmap_delete_at(map, idx);
115+
}
116+
void git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key)
117+
{
118+
khiter_t idx = git_idxmap_icase_lookup_index(map, key);
119+
if (git_idxmap_valid_index((git_idxmap *)map, idx))
120+
git_idxmap_icase_delete_at(map, idx);
121+
}

src/idxmap.h

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,49 +44,28 @@ static kh_inline khint_t idxentry_hash(const git_index_entry *e)
4444
#define GIT__USE_IDXMAP_ICASE \
4545
__KHASH_IMPL(idxicase, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_icase_equal)
4646

47-
#define git_idxmap_alloc(hp) \
48-
((*(hp) = kh_init(idx)) == NULL) ? giterr_set_oom(), -1 : 0
49-
50-
#define git_idxmap_icase_alloc(hp) \
51-
((*(hp) = kh_init(idxicase)) == NULL) ? giterr_set_oom(), -1 : 0
52-
53-
#define git_idxmap_insert(h, key, val, rval) do { \
54-
khiter_t __pos = kh_put(idx, h, key, rval); \
55-
if ((*rval) >= 0) { \
56-
if ((*rval) == 0) kh_key(h, __pos) = key; \
57-
kh_val(h, __pos) = val; \
58-
} } while (0)
59-
60-
#define git_idxmap_icase_insert(h, key, val, rval) do { \
61-
khiter_t __pos = kh_put(idxicase, h, key, rval); \
62-
if ((*rval) >= 0) { \
63-
if ((*rval) == 0) kh_key(h, __pos) = key; \
64-
kh_val(h, __pos) = val; \
65-
} } while (0)
66-
67-
#define git_idxmap_lookup_index(h, k) kh_get(idx, h, k)
68-
#define git_idxmap_icase_lookup_index(h, k) kh_get(idxicase, h, k)
69-
#define git_idxmap_value_at(h, idx) kh_val(h, idx)
70-
#define git_idxmap_valid_index(h, idx) (idx != kh_end(h))
71-
#define git_idxmap_has_data(h, idx) kh_exist(h, idx)
72-
73-
#define git_idxmap_resize(h,s) kh_resize(idx, h, s)
74-
#define git_idxmap_icase_resize(h,s) kh_resize(idxicase, h, s)
75-
#define git_idxmap_free(h) kh_destroy(idx, h), h = NULL
76-
#define git_idxmap_clear(h) kh_clear(idx, h)
77-
78-
#define git_idxmap_delete_at(h, id) kh_del(idx, h, id)
79-
#define git_idxmap_icase_delete_at(h, id) kh_del(idxicase, h, id)
80-
81-
#define git_idxmap_delete(h, key) do { \
82-
khiter_t __pos = git_idxmap_lookup_index(h, key); \
83-
if (git_idxmap_valid_index(h, __pos)) \
84-
git_idxmap_delete_at(h, __pos); } while (0)
85-
86-
#define git_idxmap_icase_delete(h, key) do { \
87-
khiter_t __pos = git_idxmap_icase_lookup_index(h, key); \
88-
if (git_idxmap_valid_index(h, __pos)) \
89-
git_idxmap_icase_delete_at(h, __pos); } while (0)
47+
int git_idxmap_alloc(git_idxmap **map);
48+
int git_idxmap_icase_alloc(git_idxmap_icase **map);
49+
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval);
50+
void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval);
51+
52+
size_t git_idxmap_lookup_index(git_idxmap *map, const git_index_entry *key);
53+
size_t git_idxmap_icase_lookup_index(git_idxmap_icase *map, const git_index_entry *key);
54+
void *git_idxmap_value_at(git_idxmap *map, size_t idx);
55+
int git_idxmap_valid_index(git_idxmap *map, size_t idx);
56+
int git_idxmap_has_data(git_idxmap *map, size_t idx);
57+
58+
void git_idxmap_resize(git_idxmap *map, size_t size);
59+
void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size);
60+
#define git_idxmap_free(h) git_idxmap__free(h); (h) = NULL
61+
void git_idxmap__free(git_idxmap *map);
62+
void git_idxmap_clear(git_idxmap *map);
63+
64+
void git_idxmap_delete_at(git_idxmap *map, size_t idx);
65+
void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx);
66+
67+
void git_idxmap_delete(git_idxmap *map, const git_index_entry *key);
68+
void git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key);
9069

9170
#define git_idxmap_begin kh_begin
9271
#define git_idxmap_end kh_end

0 commit comments

Comments
 (0)