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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
diff --git a/src/offmap.c b/src/offmap.c
index 2ab4855..e6e5cfe 100644
--- a/src/offmap.c
+++ b/src/offmap.c
@@ -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);
}
+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)
{
return kh_val(map, idx);
diff --git a/src/offmap.h b/src/offmap.h
index bf2b13d..82ebfb7 100644
--- a/src/offmap.h
+++ b/src/offmap.h
@@ -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_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_set_value_at(git_offmap *map, size_t idx, void *value);
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);
size_t git_offmap_begin(git_offmap *map);
size_t git_offmap_end(git_offmap *map);
-#define git_offmap_foreach kh_foreach
-#define git_offmap_foreach_value kh_foreach_value
+#define git_offmap_foreach(h, kvar, 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; \
+ (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
diff --git a/src/oidmap.h b/src/oidmap.h
index 8f6016a..f34c034 100644
--- a/src/oidmap.h
+++ b/src/oidmap.h
@@ -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_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
diff --git a/src/strmap.h b/src/strmap.h
index 6d999e1..51a720f 100644
--- a/src/strmap.h
+++ b/src/strmap.h
@@ -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_delete(git_strmap *map, const char *key);
-#define git_strmap_foreach kh_foreach
-#define git_strmap_foreach_value kh_foreach_value
+#define git_strmap_foreach(h, kvar, 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; \
+ (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_end(git_strmap *map);