]>
Commit | Line | Data |
---|---|---|
c0f4a011 CMN |
1 | /* |
2 | * Copyright (C) 2012 the libgit2 contributors | |
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 | #ifndef INCLUDE_offmap_h__ | |
8 | #define INCLUDE_offmap_h__ | |
9 | ||
10 | #include "common.h" | |
11 | #include "git2/types.h" | |
12 | ||
13 | #define kmalloc git__malloc | |
14 | #define kcalloc git__calloc | |
15 | #define krealloc git__realloc | |
16 | #define kfree git__free | |
17 | #include "khash.h" | |
18 | ||
19 | __KHASH_TYPE(off, git_off_t, void *); | |
20 | typedef khash_t(off) git_offmap; | |
21 | ||
22 | #define GIT__USE_OFFMAP \ | |
23 | __KHASH_IMPL(off, static kh_inline, git_off_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal); | |
24 | ||
25 | #define git_offmap_alloc() kh_init(off) | |
26 | #define git_offmap_free(h) kh_destroy(off, h), h = NULL | |
27 | #define git_offmap_clear(h) kh_clear(off, h) | |
28 | ||
29 | #define git_offmap_num_entries(h) kh_size(h) | |
30 | ||
31 | #define git_offmap_lookup_index(h, k) kh_get(off, h, k) | |
32 | #define git_offmap_valid_index(h, idx) (idx != kh_end(h)) | |
33 | ||
34 | #define git_offmap_exists(h, k) (kh_get(off, h, k) != kh_end(h)) | |
35 | ||
36 | #define git_offmap_value_at(h, idx) kh_val(h, idx) | |
37 | #define git_offmap_set_value_at(h, idx, v) kh_val(h, idx) = v | |
38 | #define git_offmap_delete_at(h, idx) kh_del(off, h, idx) | |
39 | ||
40 | #define git_offmap_insert(h, key, val, rval) do { \ | |
41 | khiter_t __pos = kh_put(off, h, key, &rval); \ | |
42 | if (rval >= 0) { \ | |
43 | if (rval == 0) kh_key(h, __pos) = key; \ | |
44 | kh_val(h, __pos) = val; \ | |
45 | } } while (0) | |
46 | ||
47 | #define git_offmap_insert2(h, key, val, oldv, rval) do { \ | |
48 | khiter_t __pos = kh_put(off, h, key, &rval); \ | |
49 | if (rval >= 0) { \ | |
50 | if (rval == 0) { \ | |
51 | oldv = kh_val(h, __pos); \ | |
52 | kh_key(h, __pos) = key; \ | |
53 | } else { oldv = NULL; } \ | |
54 | kh_val(h, __pos) = val; \ | |
55 | } } while (0) | |
56 | ||
57 | #define git_offmap_delete(h, key) do { \ | |
58 | khiter_t __pos = git_offmap_lookup_index(h, key); \ | |
59 | if (git_offmap_valid_index(h, __pos)) \ | |
60 | git_offmap_delete_at(h, __pos); } while (0) | |
61 | ||
62 | #define git_offmap_foreach kh_foreach | |
63 | #define git_offmap_foreach_value kh_foreach_value | |
64 | ||
65 | #endif |