Commit 5bfb3b58 by Patrick Steinhardt

khash: implement map-specific foreach macros

The current foreach map macros simply redirect to the type-indifferent
`kh_foreach` macro. As this type-indifferent macro directly accesses the
structures, the current implementation makes it impossible to make the
stuctures private to the implementation only. And making them private is
required to move out the khash include into the implementations to
decrease the namespace leak.
parent 382b668b
...@@ -44,6 +44,16 @@ int git_offmap_exists(git_offmap *map, const git_off_t key) ...@@ -44,6 +44,16 @@ int git_offmap_exists(git_offmap *map, const git_off_t key)
return kh_get(off, map, key) != kh_end(map); return kh_get(off, map, key) != kh_end(map);
} }
int git_offmap_has_data(git_offmap *map, size_t idx)
{
return kh_exist(map, idx);
}
git_off_t git_offmap_key_at(git_offmap *map, size_t idx)
{
return kh_key(map, idx);
}
void *git_offmap_value_at(git_offmap *map, size_t idx) void *git_offmap_value_at(git_offmap *map, size_t idx)
{ {
return kh_val(map, idx); return kh_val(map, idx);
......
...@@ -31,7 +31,9 @@ size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key); ...@@ -31,7 +31,9 @@ size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
int git_offmap_valid_index(git_offmap *map, size_t idx); int git_offmap_valid_index(git_offmap *map, size_t idx);
int git_offmap_exists(git_offmap *map, const git_off_t key); int git_offmap_exists(git_offmap *map, const git_off_t key);
int git_offmap_has_data(git_offmap *map, size_t idx);
git_off_t git_offmap_key_at(git_offmap *map, size_t idx);
void *git_offmap_value_at(git_offmap *map, size_t idx); void *git_offmap_value_at(git_offmap *map, size_t idx);
void git_offmap_set_value_at(git_offmap *map, size_t idx, void *value); void git_offmap_set_value_at(git_offmap *map, size_t idx, void *value);
void git_offmap_delete_at(git_offmap *map, size_t idx); void git_offmap_delete_at(git_offmap *map, size_t idx);
...@@ -43,7 +45,19 @@ void git_offmap_delete(git_offmap *map, const git_off_t key); ...@@ -43,7 +45,19 @@ void git_offmap_delete(git_offmap *map, const git_off_t key);
size_t git_offmap_begin(git_offmap *map); size_t git_offmap_begin(git_offmap *map);
size_t git_offmap_end(git_offmap *map); size_t git_offmap_end(git_offmap *map);
#define git_offmap_foreach kh_foreach #define git_offmap_foreach(h, kvar, vvar, code) { size_t __i; \
#define git_offmap_foreach_value kh_foreach_value for (__i = git_offmap_begin(h); __i != git_offmap_end(h); ++__i) { \
if (!git_offmap_has_data(h,__i)) continue; \
(kvar) = git_offmap_key_at(h,__i); \
(vvar) = git_offmap_value_at(h,__i); \
code; \
} }
#define git_offmap_foreach_value(h, vvar, code) { size_t __i; \
for (__i = git_offmap_begin(h); __i != git_offmap_end(h); ++__i) { \
if (!git_offmap_has_data(h,__i)) continue; \
(vvar) = git_offmap_value_at(h,__i); \
code; \
} }
#endif #endif
...@@ -46,6 +46,11 @@ void git_oidmap_delete(git_oidmap *map, const git_oid *key); ...@@ -46,6 +46,11 @@ void git_oidmap_delete(git_oidmap *map, const git_oid *key);
size_t git_oidmap_begin(git_oidmap *map); size_t git_oidmap_begin(git_oidmap *map);
size_t git_oidmap_end(git_oidmap *map); size_t git_oidmap_end(git_oidmap *map);
#define git_oidmap_foreach_value kh_foreach_value #define git_oidmap_foreach_value(h, vvar, code) { size_t __i; \
for (__i = git_oidmap_begin(h); __i != git_oidmap_end(h); ++__i) { \
if (!git_oidmap_has_data(h,__i)) continue; \
(vvar) = git_oidmap_value_at(h,__i); \
code; \
} }
#endif #endif
...@@ -42,8 +42,20 @@ int git_strmap_put(git_strmap *map, const char *key, int *err); ...@@ -42,8 +42,20 @@ int git_strmap_put(git_strmap *map, const char *key, int *err);
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval); void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval);
void git_strmap_delete(git_strmap *map, const char *key); void git_strmap_delete(git_strmap *map, const char *key);
#define git_strmap_foreach kh_foreach #define git_strmap_foreach(h, kvar, vvar, code) { size_t __i; \
#define git_strmap_foreach_value kh_foreach_value for (__i = git_strmap_begin(h); __i != git_strmap_end(h); ++__i) { \
if (!git_strmap_has_data(h,__i)) continue; \
(kvar) = git_strmap_key(h,__i); \
(vvar) = git_strmap_value_at(h,__i); \
code; \
} }
#define git_strmap_foreach_value(h, vvar, code) { size_t __i; \
for (__i = git_strmap_begin(h); __i != git_strmap_end(h); ++__i) { \
if (!git_strmap_has_data(h,__i)) continue; \
(vvar) = git_strmap_value_at(h,__i); \
code; \
} }
size_t git_strmap_begin(git_strmap *map); size_t git_strmap_begin(git_strmap *map);
size_t git_strmap_end(git_strmap *map); size_t git_strmap_end(git_strmap *map);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment