Skip to content

Commit 659f5d0

Browse files
committed
oidmap: convert macros to functions
1 parent 13c3bc9 commit 659f5d0

File tree

2 files changed

+115
-22
lines changed

2 files changed

+115
-22
lines changed

src/oidmap.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 "oidmap.h"
9+
10+
__KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, git_oidmap_hash, git_oid_equal)
11+
12+
git_oidmap *git_oidmap_alloc()
13+
{
14+
return kh_init(oid);
15+
}
16+
17+
void git_oidmap__free(git_oidmap *map)
18+
{
19+
kh_destroy(oid, map);
20+
}
21+
22+
void git_oidmap_clear(git_oidmap *map)
23+
{
24+
kh_clear(oid, map);
25+
}
26+
27+
size_t git_oidmap_size(git_oidmap *map)
28+
{
29+
return kh_size(map);
30+
}
31+
32+
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
33+
{
34+
return kh_get(oid, map, key);
35+
}
36+
37+
int git_oidmap_valid_index(git_oidmap *map, size_t idx)
38+
{
39+
return idx != kh_end(map);
40+
}
41+
42+
int git_oidmap_exists(git_oidmap *map, const git_oid *key)
43+
{
44+
return kh_get(oid, map, key) != kh_end(map);
45+
}
46+
47+
int git_oidmap_has_data(git_oidmap *map, size_t idx)
48+
{
49+
return kh_exist(map, idx);
50+
}
51+
52+
const git_oid *git_oidmap_key(git_oidmap *map, size_t idx)
53+
{
54+
return kh_key(map, idx);
55+
}
56+
57+
void git_oidmap_set_key_at(git_oidmap *map, size_t idx, git_oid *key)
58+
{
59+
kh_key(map, idx) = key;
60+
}
61+
62+
void *git_oidmap_value_at(git_oidmap *map, size_t idx)
63+
{
64+
return kh_val(map, idx);
65+
}
66+
67+
void git_oidmap_set_value_at(git_oidmap *map, size_t idx, void *value)
68+
{
69+
kh_val(map, idx) = value;
70+
}
71+
72+
void git_oidmap_delete_at(git_oidmap *map, size_t idx)
73+
{
74+
kh_del(oid, map, idx);
75+
}
76+
77+
int git_oidmap_put(git_oidmap *map, const git_oid *key, int *err)
78+
{
79+
return kh_put(oid, map, key, err);
80+
}
81+
82+
void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rval)
83+
{
84+
khiter_t idx = kh_put(oid, map, key, rval);
85+
86+
if ((*rval) >= 0) {
87+
if ((*rval) == 0)
88+
kh_key(map, idx) = key;
89+
kh_val(map, idx) = value;
90+
}
91+
}
92+
93+
void git_oidmap_delete(git_oidmap *map, const git_oid *key)
94+
{
95+
khiter_t idx = git_oidmap_lookup_index(map, key);
96+
if (git_oidmap_valid_index(map, idx))
97+
git_oidmap_delete_at(map, idx);
98+
}

src/oidmap.h

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,32 @@ GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid)
3030
#define GIT__USE_OIDMAP \
3131
__KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, git_oidmap_hash, git_oid_equal)
3232

33-
#define git_oidmap_alloc() kh_init(oid)
34-
#define git_oidmap_free(h) kh_destroy(oid,h), h = NULL
33+
git_oidmap *git_oidmap_alloc(void);
34+
#define git_oidmap_free(h) git_oidmap__free(h); (h) = NULL
35+
void git_oidmap__free(git_oidmap *map);
36+
void git_oidmap_clear(git_oidmap *map);
3537

36-
#define git_oidmap_lookup_index(h, k) kh_get(oid, h, k)
37-
#define git_oidmap_valid_index(h, idx) (idx != kh_end(h))
38+
size_t git_oidmap_size(git_oidmap *map);
3839

39-
#define git_oidmap_exists(h, k) (kh_get(oid, h, k) != kh_end(h))
40-
#define git_oidmap_has_data(h, idx) kh_exist(h, idx)
40+
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key);
41+
int git_oidmap_valid_index(git_oidmap *map, size_t idx);
4142

42-
#define git_oidmap_key(h, idx) kh_key(h, idx)
43-
#define git_oidmap_set_key_at(h, idx, k) kh_key(h, idx) = k
44-
#define git_oidmap_value_at(h, idx) kh_val(h, idx)
45-
#define git_oidmap_set_value_at(h, idx, v) kh_val(h, idx) = v
46-
#define git_oidmap_delete_at(h, idx) kh_del(oid, h, idx)
43+
int git_oidmap_exists(git_oidmap *map, const git_oid *key);
44+
int git_oidmap_has_data(git_oidmap *map, size_t idx);
4745

48-
#define git_oidmap_put(h, k, err) kh_put(oid, h, k, err)
46+
const git_oid *git_oidmap_key(git_oidmap *map, size_t idx);
47+
void git_oidmap_set_key_at(git_oidmap *map, size_t idx, git_oid *key);
48+
void *git_oidmap_value_at(git_oidmap *map, size_t idx);
49+
void git_oidmap_set_value_at(git_oidmap *map, size_t idx, void *value);
50+
void git_oidmap_delete_at(git_oidmap *map, size_t idx);
4951

50-
#define git_oidmap_insert(h, key, val, rval) do { \
51-
khiter_t __pos = kh_put(oid, h, key, rval); \
52-
if ((*rval) >= 0) { \
53-
if ((*rval) == 0) kh_key(h, __pos) = key; \
54-
kh_val(h, __pos) = val; \
55-
} } while (0)
52+
int git_oidmap_put(git_oidmap *map, const git_oid *key, int *err);
53+
void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rval);
54+
void git_oidmap_delete(git_oidmap *map, const git_oid *key);
5655

5756
#define git_oidmap_foreach_value kh_foreach_value
5857

5958
#define git_oidmap_begin kh_begin
6059
#define git_oidmap_end kh_end
6160

62-
#define git_oidmap_size(h) kh_size(h)
63-
64-
#define git_oidmap_clear(h) kh_clear(oid, h)
65-
6661
#endif

0 commit comments

Comments
 (0)