]> git.proxmox.com Git - libgit2.git/blobdiff - src/strmap.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / src / strmap.c
index de6826d036c709e7a73fc80198fc7d4e8a9ec504..c6e5b6dc70b3aed2e7ed5fa684175c861024dbea 100644 (file)
@@ -7,14 +7,21 @@
 
 #include "strmap.h"
 
+#define kmalloc git__malloc
+#define kcalloc git__calloc
+#define krealloc git__realloc
+#define kreallocarray git__reallocarray
+#define kfree git__free
+#include "khash.h"
+
+__KHASH_TYPE(str, const char *, void *)
+
 __KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
 
-int git_strmap_alloc(git_strmap **map)
+int git_strmap_new(git_strmap **out)
 {
-       if ((*map = kh_init(str)) == NULL) {
-               giterr_set_oom();
-               return -1;
-       }
+       *out = kh_init(str);
+       GIT_ERROR_CHECK_ALLOC(*out);
 
        return 0;
 }
@@ -29,99 +36,65 @@ void git_strmap_clear(git_strmap *map)
        kh_clear(str, map);
 }
 
-size_t git_strmap_num_entries(git_strmap *map)
+size_t git_strmap_size(git_strmap *map)
 {
        return kh_size(map);
 }
 
-size_t git_strmap_lookup_index(git_strmap *map, const char *key)
-{
-       return kh_get(str, map, key);
-}
-
-int git_strmap_valid_index(git_strmap *map, size_t idx)
-{
-       return idx != kh_end(map);
-}
-
-int git_strmap_exists(git_strmap *map, const char *key)
+void *git_strmap_get(git_strmap *map, const char *key)
 {
-       return kh_get(str, map, key) != kh_end(map);
+       size_t idx = kh_get(str, map, key);
+       if (idx == kh_end(map) || !kh_exist(map, idx))
+               return NULL;
+       return kh_val(map, idx);
 }
 
-int git_strmap_has_data(git_strmap *map, size_t idx)
+int git_strmap_set(git_strmap *map, const char *key, void *value)
 {
-       return kh_exist(map, idx);
-}
+       size_t idx;
+       int rval;
 
-const char *git_strmap_key(git_strmap *map, size_t idx)
-{
-       return kh_key(map, idx);
-}
+       idx = kh_put(str, map, key, &rval);
+       if (rval < 0)
+               return -1;
 
-void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key)
-{
-       kh_val(map, idx) = key;
-}
+       if (rval == 0)
+               kh_key(map, idx) = key;
 
-void *git_strmap_value_at(git_strmap *map, size_t idx)
-{
-       return kh_val(map, idx);
-}
-
-void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value)
-{
        kh_val(map, idx) = value;
-}
-
-void git_strmap_delete_at(git_strmap *map, size_t idx)
-{
-       kh_del(str, map, idx);
-}
 
-int git_strmap_put(git_strmap *map, const char *key, int *err)
-{
-       return kh_put(str, map, key, err);
+       return 0;
 }
 
-void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
+int git_strmap_delete(git_strmap *map, const char *key)
 {
-       khiter_t idx = kh_put(str, map, key, rval);
-
-       if ((*rval) >= 0) {
-               if ((*rval) == 0)
-                       kh_key(map, idx) = key;
-               kh_val(map, idx) = value;
-       }
+       khiter_t idx = kh_get(str, map, key);
+       if (idx == kh_end(map))
+               return GIT_ENOTFOUND;
+       kh_del(str, map, idx);
+       return 0;
 }
 
-void git_strmap_delete(git_strmap *map, const char *key)
+int git_strmap_exists(git_strmap *map, const char *key)
 {
-       khiter_t idx = git_strmap_lookup_index(map, key);
-       if (git_strmap_valid_index(map, idx))
-               git_strmap_delete_at(map, idx);
+       return kh_get(str, map, key) != kh_end(map);
 }
 
-int git_strmap_next(
-       void **data,
-       git_strmap_iter* iter,
-       git_strmap *map)
+int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key)
 {
-       if (!map)
-               return GIT_ERROR;
+       size_t i = *iter;
 
-       while (*iter != git_strmap_end(map)) {
-               if (!(git_strmap_has_data(map, *iter))) {
-                       ++(*iter);
-                       continue;
-               }
+       while (i < map->n_buckets && !kh_exist(map, i))
+               i++;
 
-               *data = git_strmap_value_at(map, *iter);
+       if (i >= map->n_buckets)
+               return GIT_ITEROVER;
 
-               ++(*iter);
+       if (key)
+               *key = kh_key(map, i);
+       if (value)
+               *value = kh_val(map, i);
+       *iter = ++i;
 
-               return GIT_OK;
-       }
-
-       return GIT_ITEROVER;
+       return 0;
 }