diff options
| author | Vicent Martà <tanoku@gmail.com> | 2012-05-05 14:22:53 -0700 |
|---|---|---|
| committer | Vicent Martà <tanoku@gmail.com> | 2012-05-05 14:22:53 -0700 |
| commit | 48ecd122ea6fb8cf12fb4029974c314e5d9efb62 (patch) | |
| tree | 88f90fa8c9d903f072a2b1c647c51a9899e520c7 /src/strmap.h | |
| parent | 2218fd57a50ceb851cb131939bf0747e072e40f6 (diff) | |
| parent | 4ef14af93517b3842bc0dfa24147cf10dd029582 (diff) | |
| download | libgit2-48ecd122ea6fb8cf12fb4029974c314e5d9efb62.tar.gz | |
Merge pull request #659 from libgit2/development-merge
New-error-handling
Diffstat (limited to 'src/strmap.h')
| -rw-r--r-- | src/strmap.h | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/strmap.h b/src/strmap.h new file mode 100644 index 000000000..da5ca0dba --- /dev/null +++ b/src/strmap.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_strmap_h__ +#define INCLUDE_strmap_h__ + +#include "common.h" + +#define kmalloc git__malloc +#define kcalloc git__calloc +#define krealloc git__realloc +#define kfree git__free +#include "khash.h" + +__KHASH_TYPE(str, const char *, void *); +typedef khash_t(str) git_strmap; + +#define GIT__USE_STRMAP \ + __KHASH_IMPL(str, static inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal) + +#define git_strmap_alloc() kh_init(str) +#define git_strmap_free(h) kh_destroy(str, h), h = NULL +#define git_strmap_clear(h) kh_clear(str, h) + +#define git_strmap_num_entries(h) kh_size(h) + +#define git_strmap_lookup_index(h, k) kh_get(str, h, k) +#define git_strmap_valid_index(h, idx) (idx != kh_end(h)) + +#define git_strmap_exists(h, k) (kh_get(str, h, k) != kh_end(h)) + +#define git_strmap_value_at(h, idx) kh_val(h, idx) +#define git_strmap_set_value_at(h, idx, v) kh_val(h, idx) = v +#define git_strmap_delete_at(h, idx) kh_del(str, h, idx) + +#define git_strmap_insert(h, key, val, rval) do { \ + khiter_t __pos = kh_put(str, h, key, &rval); \ + if (rval >= 0) { \ + if (rval == 0) kh_key(h, __pos) = key; \ + kh_val(h, __pos) = val; \ + } } while (0) + +#define git_strmap_insert2(h, key, val, oldv, rval) do { \ + khiter_t __pos = kh_put(str, h, key, &rval); \ + if (rval >= 0) { \ + if (rval == 0) { \ + oldv = kh_val(h, __pos); \ + kh_key(h, __pos) = key; \ + } else { oldv = NULL; } \ + kh_val(h, __pos) = val; \ + } } while (0) + +#define git_strmap_delete(h, key) do { \ + khiter_t __pos = git_strmap_lookup_index(h, key); \ + if (git_strmap_valid_index(h, __pos)) \ + git_strmap_delete_at(h, __pos); } while (0) + +#define git_strmap_foreach kh_foreach +#define git_strmap_foreach_value kh_foreach_value + +#endif |
